These all test if something is true about the current node.
- All SizzleJS selectors including:
:has(> .selector .foo)
:contains(text)
:lang()
:not(div > p)
(Note: Sass will silently fail if you write:not(:has(foo))
):not-has(> .selector .foo)
(the same as:not(:has(...))
except that SASS will not silently fail)
:target(href, '.match > .selector, .foo')
this is used for creating links to things like figures.
The way :target(...)
works is best explained by providing an example:
a:target(href, 'figure') { content: 'See Figure'; }
a:target(href, 'table') { content: 'See Table'; }
a:target(href, '.chapter') { content: 'See Chapter'; }
It looks up the target (by checking using the href
attribute on the link) and then only matches
when the target matches the specified selector.
These all add a node(s) in the vicinity of this node.
::after
or::after(1)
: adds a new child element after the existing content::before
or::before(1)
: adds a new child element before the existing content::inside
or::inside(1)
: wraps all children of this element into this new element::outside
or::outside(1)
: wraps the current element with this new element::for-each(1, descendant, '> .selector .of.descendants > .to-match')
: creates a new child element in the current element for each element that is matched by the selector and changes the context to be the matched element- any sub pseudo-class selectors are used to filter the selector that was matched
- any sub pseudo-element selectors are used to continue constructing elements
Selecting attributes with a namespace is supported. For example:
/* Define the `epub`prefix to be 'http://www.idpf.org/2007/ops' */
@namespace epub url('http://www.idpf.org/2007/ops');
/* Match attributes like `<div epub:type="glossary">` */
[epub|type="glossary"] {
content: "kittens";
}
Each declaration can also take the value of none
(like class-add: none;
) to disable the declaration.
content: "strings or functions: " attr(href) move-here('.foo');
: replaces the contents of the current Element with these nodesclass-add: "name1", "name2";
class-remove: "name1", "name2";
(orclass-remove: '*';
to remove all)attrs-add: "name1" "val1" "val2 is concatenated", "name2" attr(href);
attrs-remove: "name1", "name2";
(orattrs-remove: '*';
to remove all)tag-name-set: "tagName";
: changes the element name (div
,a
,strong
)display: none;
ordisplay: default;
: removes the element from the DOMx-log: "message or elements:" move-here('.foo');
: generates a log message for debugging (can specifydefault
to suppress the log message)x-throw: now;
orx-throw: later;
orx-throw: attr(href);
: used by unit tests to intentionally explode
TODO: Consider dropping the x-
prefix because it is cumbersome to type
Each function operates on the current context (usually looking up something in the DOM). Usually the current context is the DOM element that was matched in the selector. Exceptions are:
Changing the current context:
::for-each(...)
(defined earlier)parent-context(fn())
changes the context thatfn()
is evaluated to be the parent element- this is "syntactic-sugar" for
ancestor-context('*', fn())
- this is "syntactic-sugar" for
ancestor-context('.selector', fn())
changes the context thatfn()
is evaluated to be the first ancestor that matchesdescendant-context('.selector', fn())
target-context(attr(href), fn())
. you can specify any selector as the 1st argument as long as it evaluates to 1 element
The rest:
attr(href)
orattr(src)
. Looks up an attribute on the current element. Errors if the attribute does not exist.x-attr-ensure-id()
. Looks up theid
attribute on the current element. If the attribute does not exist, a string is created.text-contents()
all text nodes combined together as a stringmove-here('.selector')
finds all descendants (useancestor-context(...)
if you need to find non-descendants)- similar in concept to
move-to: bucketName
and thencontent: pending(bucketName)
but very different in implementation
- similar in concept to
move-here-sorted('.selector', '.guard1-selector' fn1(), '.guard2-selector' fn2(), ...)
- sorts the items by
fn#()
based on if the item matches.guard#-selector
. Useful for ordering the answers at the back of the book
- sorts the items by
count-of-type('body .counting-context', '.selector')
counts the number of items matching this selector up until the current element (used for numbering)- this gives a similar set of features as
counter-reset: counterName;
andcontent: counter(counterName);
but very different implementation
- this gives a similar set of features as
count-all-of-type('body .counting-context', '.selector')
counts the number of items matching this selector (used for offsetting numbers as a HACK around not being able to number after elements have moved)add(12, 23, 34)
adds 2 or more numbers (used for adjusting the result ofcount-of-type(...)
)number-to-letter(12, upper-latin)
converts a number to another format. Valid values aredecimal
,decimal-leading-zero
,lower-roman
,upper-roman
,lower-latin
,upper-latin
. In the future it might be necessary to implement https://www.w3.org/TR/css-counter-styles-3/collect-all("Figure" 1 "." 3)
collects all the fields into a string. Useful for computing a link target becausetarget-context(...)
only allows 2 argumentsif(1, true-condition, false-condition)
allows you to conditionally return something. An example would be to combine this withis-even(...)
to remove all even answers from the back of the Bookis-even(123)
returns0
if the number is odd, and111111
otherwise (truthy)x-tag-name()
find out current elements' tag name (ie 'div', 'pre', 'h1')x-throw()
throws an error (useful for unit tests)
TODO: Consider dropping the x-
prefix because it is cumbersome to type