Selectors syntax
- selector
[selector-step (:>? selector-step)*]#{selector*}; groupinga-symbol; must evaluate to a state-machine(some clojure code); must evaluate to a state-machine- selector-step
:a-keyword; can be :* :.class :tag :#id or any combination eg :div#foo.bar.baz#{selector-step*}; union[selector-step*]; intersectiona-symbol; must evaluate to a state-machine — some are already defined(some clojure code); must evaluate to a state-machine — better built using some predefined functions, predicate builders or macros
Predefined selector-steps
root, first-child,last-child,first-of-type,last-of-type,only-child,only-of-type,void (CSS’s :empty),odd and even.
Predefined functions
attr?CSS-
(attr?attribute-keyword*)sample usage:
(attr? :href) ; *[href] (attr? :href :title) ; *[href][title]
attr=CSS-
(attr=(attribute-keyword value)*)sample usage:
(attr= :href "foo") ; *[href=foo] (attr= :href "foo" :title "bar") ; *[href=foo][title=bar]
attr-hasCSS-
(attr-has(attribute-keyword value)*)sample usage:
(attr-has :foo "bar" "baz") ; *[foo~=bar][foo~=baz]
attr-startsCSS-
(attr-starts(attribute-keyword value)*)sample usage:
(attr-starts :href "foo" :title "bar"); *[href^=foo][title^=bar]
attr-endsCSS-
(attr-ends(attribute-keyword value)*)sample usage:
(attr-ends :href "foo" :title "bar") ; *[href$=foo][title$=bar]
attr-containsCSS-
(attr-contains(attribute-keyword value)*)sample usage:
(attr-contains :href "foo" :title "bar") ; *[href*=foo][title*=bar]
attr|=CSS-
(attr|=(attribute-keyword value)*)sample usage:
(attr|= :lang "fr") ; *[lang|=fr]
nth-childCSS-
(nth-childstride? offset)sample usage:
(nth-child 3) ; *:nth-child(3) (nth-child 4 2) ; *:nth-child(4n+2)
nth-last-childCSS-
(nth-last-childstride? offset)sample usage:
(nth-last-child 3) ; *:nth-last-child(3) (nth-last-child 4 2) ; *:nth-last-child(4n+2)
nth-of-typeCSS-
(nth-of-typestride? offset)sample usage:
(nth-of-type 3) ; *:nth-of-type(3) (nth-of-type 4 2) ; *:nth-of-type(4n+2)
nth-last-of-typeCSS-
(nth-last-of-typestride? offset)sample usage:
(nth-last-of-type 3) ; *:nth-last-of-type(3) (nth-last-of-type 4 2) ; *:nth-last-of-type(4n+2)
butCSS-
sample usage:
(but :a) ; :not(a)
has-
sample usage:
(has [:a])
Predicate builders
These functions take a predicate and return a state-machine.
pred-
(predpredicate-on-elements)sample usage:
(pred #(= (:tag %) tag-name))
text-pred-
(text-predpredicate-on-text-nodes)sample usage:
(text-pred #(re-matches #“\d+” %))
zip-pred-
(zip-predpredicate-on-elements-locs) sm/pred(where sm aliases net.cgrand.enlive-html.state-machine)-
(sm/predpredicate-on-locs)
Useful macros
selector takes a selector and evaluates to a state-machine, selector-step takes a selector-step and evaluates to a state-machine.
They are backed by compile-selector and compile-step.