Skip to content

Commit

Permalink
Merge pull request #19 from ba-st/descendant_combinator
Browse files Browse the repository at this point in the history
Descendant combinator
  • Loading branch information
gcotelli authored Mar 26, 2018
2 parents a9db4f0 + 29519a2 commit 466ca97
Show file tree
Hide file tree
Showing 120 changed files with 61 additions and 1,709 deletions.
72 changes: 43 additions & 29 deletions docs/tutorial/Tutorial - Part II.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ So far our focus was on the *style* part of the rule. Let's focus now on the ava
These selectors match a specific element type in the DOM. The library provides out-of-the-box support for HTML elements. One example is the `div` selector used in the previous chapter. Another is the following:

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector orderedList ]
with: [:style | style listStyleType: CssConstants lowerRoman ];
build
Expand All @@ -28,7 +28,7 @@ To get a list of the supported type selectors inspect `CssSelector selectorsInPr

One of the most common use cases is the **descendant combinator**.
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector div orderedList ]
with: [:style | style listStyleType: CssConstants lowerRoman ];
build
Expand All @@ -40,9 +40,23 @@ div ol
}
```

In case you need to use parenthesis in the right part of the expression, use `/`.
```smalltalk
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector div / (selector class: 'custom') ]
with: [:style | style listStyleType: CssConstants lowerRoman ];
build
```
```css
div .custom
{
list-style-type: lower-roman;
}
```

#### Child combinator
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector div > selector orderedList ]
with: [:style | style listStyleType: CssConstants lowerRoman ];
build
Expand All @@ -56,7 +70,7 @@ div > ol

#### Adjacent & General Siblings combinators
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector div + selector orderedList ]
with: [:style | style listStyleType: CssConstants lowerRoman ];
build
Expand All @@ -68,7 +82,7 @@ div + ol
}
```
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector div ~ selector orderedList ]
with: [:style | style listStyleType: CssConstants lowerRoman ];
build
Expand All @@ -82,7 +96,7 @@ div ~ ol

### Class and Id Selectors
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | (selector div class: 'pastoral') id: #account5 ]
with: [:style | style listStyleType: CssConstants lowerRoman ];
build
Expand All @@ -103,7 +117,7 @@ Attribute selectors are useful to match an element if that element has an attrib

Attribute presence:
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector h1 havingAttribute: 'title' ]
with: [:style | style color: CssSVGColors blue ];
build
Expand All @@ -117,7 +131,7 @@ h1[title]

exact attribute value matching:
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector span withAttribute: 'class' equalTo: 'example' ]
with: [:style | style color: CssSVGColors blue ];
build
Expand All @@ -132,7 +146,7 @@ span[class="example"]
inclusion:

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor attribute: 'rel' includes: 'copyright' ]
with: [:style | style color: CssSVGColors blue ];
build
Expand All @@ -146,7 +160,7 @@ a[rel~="copyright"]
and:

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor firstValueOfAttribute: 'hreflang' beginsWith: 'en' ]
with: [:style | style color: CssSVGColors blue ];
build
Expand All @@ -160,13 +174,13 @@ a[hreflang|="en"]

#### Substring matching attribute selectors

This selectors are provided for matching substrings in the value of an attribute:
- `attribute:beginsWith:`
- `attribute:endsWith:`
This selectors are provided for matching substrings in the value of an attribute:
- `attribute:beginsWith:`
- `attribute:endsWith:`
- `attribute:includesSubstring:`

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor attribute: 'type' beginsWith: 'image/' ]
with: [:style | style color: CssSVGColors blue ];
build
Expand All @@ -179,7 +193,7 @@ a[type^="image/"]
```

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor attribute: 'type' endsWith: '.html' ]
with: [:style | style color: CssSVGColors blue ];
build
Expand All @@ -192,7 +206,7 @@ a[type$=".html"]
```

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector paragraph attribute: 'title' includesSubstring: 'hello' ]
with: [:style | style color: CssSVGColors blue ];
build
Expand All @@ -209,7 +223,7 @@ The pseudo-class concept is introduced to allow selection based on information t

Here is a small example that uses the pseudo-classes:
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor link ]
with: [:style | style color: CssSVGColors blue ];
declareRuleSetFor: [:selector | selector anchor visited active]
Expand Down Expand Up @@ -251,7 +265,7 @@ input:checked

#### Language Pseudo-class:
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | (selector lang: 'es') > selector div ]
with: [:style | style quotes: { '"«"'. '"»"' } ];
build
Expand All @@ -269,7 +283,7 @@ The negation pseudo-class, `:not(X)`, is a functional notation taking a simple s

This selector is supported sending the message `not:`. Lets see an example:
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector button not: (selector havingAttribute: 'DISABLED') ]
with: [:style | style color: CssSVGColors blue ];
build
Expand All @@ -283,12 +297,12 @@ button:not([DISABLED])
#### Structural Pseudo-classes
These selectors allow selection based on extra information that lies in the document tree but cannot be represented by other simpler selectors nor combinators.

Standalone text and other non-element nodes are not counted when calculating the position of an element in the list of children of its parent. When calculating the position of an element in the list of children of its parent, the index numbering starts at 1.
Standalone text and other non-element nodes are not counted when calculating the position of an element in the list of children of its parent. When calculating the position of an element in the list of children of its parent, the index numbering starts at 1.

##### Root Pseudo-class
The :root pseudo-class represents an element that is the root of the document. To build this kind of selector just send the message `root` to another selector:
```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector root ]
with: [:style | style color: CssSVGColors grey ];
build
Expand All @@ -303,7 +317,7 @@ In addition to this, `:nth-child()` can take ‘odd’ and ‘even’ as argumen
Since version 1.1.0 the library supports an abstraction for this kind of formulae (`CssLinealPolynomial`), or just an integer if `n` is not required.

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector childAt: 3 n + 1 ]
with: [:style | style color: CssSVGColors blue ];
declareRuleSetFor: [:selector | selector childAt: 5 ]
Expand Down Expand Up @@ -339,7 +353,7 @@ Some examples:
```

The rest of the selectors in this category are modeled using the following messsages:
- `nth-last-child()` -> `childFromLastAt:`
- `nth-last-child()` -> `childFromLastAt:`
- `nth-of-type()` -> `siblingOfTypeAt:`
- `nth-last-of-type()` -> `siblingOfTypeFromLastAt:`
- `first-child` -> `firstChild`
Expand All @@ -358,7 +372,7 @@ Pseudo-elements create abstractions about the document tree beyond those specifi
This selector describes the contents of the first formatted line of an element.

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector paragraph firstLine ]
with: [:style | style textTransform: CssConstants uppercase ];
build
Expand All @@ -375,7 +389,7 @@ p::first-line
This pseudo-element represents the first letter of an element, if it is not preceded by any other content (such as images or inline tables) on its line.

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector paragraph firstLetter ]
with: [:style | style fontSize: 200 percent ];
build
Expand All @@ -392,7 +406,7 @@ p::first-letter
These pseudo-elements can be used to describe generated content before or after an element's content. The `content` property, in conjunction with these pseudo-elements, specifies what is inserted.

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | (selector paragraph class: 'note') before ]
with: [:style | style content: '"Note: "' ];
declareRuleSetFor: [:selector | (selector paragraph class: 'note') after ]
Expand All @@ -408,15 +422,15 @@ p.note::before
p.note::after
{
content: "[*]";
}
}
```

### Selector Groups

A comma-separated list of selectors represents the union of all elements selected by each of the individual selectors in the list. For example, in CSS when several selectors share the same declarations, they may be grouped into a comma-separated list.
A comma-separated list of selectors represents the union of all elements selected by each of the individual selectors in the list. For example, in CSS when several selectors share the same declarations, they may be grouped into a comma-separated list.

```smalltalk
CascadingStyleSheetBuilder new
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | (selector div class: 'note') after , (selector paragraph class: 'note') before ]
with: [:style | style content: '"Note: "' ];
build
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Tests
testBuildingShortcuts

| ancestor descendant |

ancestor := CssUniversalSelector implicit class: 'custom'.
descendant := CssTypeSelector ofType: 'div'.

self
assert: (ancestor / descendant) printString equals: '.custom div';
assert: ancestor div printString equals: '.custom div'
Loading

0 comments on commit 466ca97

Please sign in to comment.