Pyjo.DOM.CSS - CSS selector engine

import Pyjo.DOM.CSS

# Select elements from DOM tree
css = Pyjo.DOM.CSS.new(tree=tree)
elements = css.select('head > title')

Pyjo.DOM.CSS is the CSS selector engine used by Pyjo.DOM and based on Selectors Level 3.

Selectors

All CSS selectors that make sense for a standalone parser are supported.

*

Any element.

all = css.select('*')

E

An element of type E.

title = css.select('title')

E[foo]

An E element with a foo attribute.

links = css.select('a[href]')

E[foo=”bar”]

An E element whose foo attribute value is exactly equal to bar.

case_sensitive = css.select('input[type="hidden"]')
case_sensitive = css.select('input[type=hidden]')

E[foo=”bar” i]

An E element whose foo attribute value is exactly equal to any (ASCII-range) case-permutation of bar. Note that this selector is EXPERIMENTAL and might change without warning!

case_insensitive = css.select('input[type="hidden" i]')
case_insensitive = css.select('input[type=hidden i]')
case_insensitive = css.select('input[class~="foo" i]')

This selector is part of Selectors Level 4, which is still a work in progress.

E[foo~=”bar”]

An E element whose foo attribute value is a list of whitespace-separated values, one of which is exactly equal to bar.

foo = css.select('input[class~="foo"]')
foo = css.select('input[class~=foo]')

E[foo^=”bar”]

An E element whose foo attribute value begins exactly with the string bar.

begins_with = css.select('input[name^="f"]')
begins_with = css.select('input[name^=f]')

E[foo$=”bar”]

An E element whose foo attribute value ends exactly with the string bar.

ends_with = css.select('input[name$="o"]')
ends_with = css.select('input[name$=o]')

E[foo*=”bar”]

An E element whose foo attribute value contains the substring bar.

contains = css.select('input[name*="fo"]')
contains = css.select('input[name*=fo]')

E:root

An E element, root of the document.

root = css.select(':root')

E:checked

A user interface element E which is checked (for instance a radio-button or checkbox).

input = css.select(':checked')

E:empty

An E element that has no children (including text nodes).

empty = css.select(':empty')

E:nth-child(n)

An E element, the n-th child of its parent.

third = css.select('div:nth-child(3)')
odd   = css.select('div:nth-child(odd)')
even  = css.select('div:nth-child(even)')
top3  = css.select('div:nth-child(-n+3)')

E:nth-last-child(n)

An E element, the n-th child of its parent, counting from the last one.

third    = css.select('div:nth-last-child(3)')
odd      = css.select('div:nth-last-child(odd)')
even     = css.select('div:nth-last-child(even)')
bottom3  = css.select('div:nth-last-child(-n+3)')

E:nth-of-type(n)

An E element, the n-th sibling of its type.

third = css.select('div:nth-of-type(3)')
odd   = css.select('div:nth-of-type(odd)')
even  = css.select('div:nth-of-type(even)')
top3  = css.select('div:nth-of-type(-n+3)')

E:nth-last-of-type(n)

An E element, the n-th sibling of its type, counting from the last one.

third    = css.select('div:nth-last-of-type(3)')
odd      = css.select('div:nth-last-of-type(odd)')
even     = css.select('div:nth-last-of-type(even)')
bottom3  = css.select('div:nth-last-of-type(-n+3)')

E:first-child

An E element, first child of its parent.

first = css.select('div p:first-child')

E:last-child

An E element, last child of its parent.

last = css.select('div p:last-child')

E:first-of-type

An E element, first sibling of its type.

first = css.select('div p:first-of-type')

E:last-of-type

An E element, last sibling of its type.

last = css.select('div p:last-of-type')

E:only-child

An E element, only child of its parent.

lonely = css.select('div p:only-child')

E:only-of-type

An E element, only sibling of its type.

lonely = css.select('div p:only-of-type')

E.warning

An E element whose class is “warning”.

warning = css.select('div.warning')

E#myid

An E element with ID equal to “myid”.

foo = css.select('div#foo')

E:not(s)

An E element that does not match simple selector s.

others = css.select('div p:not(:first-child)')

E F

An F element descendant of an E element.

headlines = css.select('div h1')

E > F

An F element child of an E element.

headlines = css.select('html > body > div > h1')

E + F

An F element immediately preceded by an E element.

second = css.select('h1 + h2')

E ~ F

An F element preceded by an E element.

second = css.select('h1 ~ h2')

E, F, G

Elements of type E, F and G.

headlines = css.select('h1, h2, h3')

E[foo=bar][bar=baz]

An E element whose attributes match all following attribute selectors.

links = css.select('a[foo^=b][foo$=ar]')

Classes

class Pyjo.DOM.CSS.Pyjo_DOM_CSS(**kwargs)

Pyjo.DOM.CSS inherits all attributes and methods from Pyjo.Base and implements the following new ones.

matches(pattern)
bool = css.matches('head > title')

Check if first node in tree matches the CSS selector.

select(pattern)
results = css.select('head > title')

Run CSS selector against tree.

select_one(pattern)
results = css.select_one('head > title')

Run CSS selector against tree and stop as soon as the first node matched.

tree = None
tree = html.tree
html.tree = ['root']

Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.

Pyjo.DOM.CSS.object

alias of Pyjo_DOM_CSS