File: css3.rst

package info (click to toggle)
python-tinycss 0.4-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 716 kB
  • sloc: python: 2,476; makefile: 7
file content (116 lines) | stat: -rw-r--r-- 3,207 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
CSS 3 Modules
=============

.. _selectors3:

Selectors 3
-----------

.. currentmodule:: tinycss.css21

On :attr:`RuleSet.selector`, the :meth:`~.token_data.TokenList.as_css` method
can be used to serialize a selector back to an Unicode string.

    >>> import tinycss
    >>> stylesheet = tinycss.make_parser().parse_stylesheet(
    ...     'div.error, #root > section:first-letter { color: red }')
    >>> selector_string = stylesheet.rules[0].selector.as_css()
    >>> selector_string
    'div.error, #root > section:first-letter'

This string can be parsed by cssselect_. The parsed objects have information
about pseudo-elements and selector specificity.

.. _cssselect: http://packages.python.org/cssselect/

    >>> import cssselect
    >>> selectors = cssselect.parse(selector_string)
    >>> [s.specificity() for s in selectors]
    [(0, 1, 1), (1, 0, 2)]
    >>> [s.pseudo_element for s in selectors]
    [None, 'first-letter']

These objects can in turn be translated to XPath expressions. Note that
the translation ignores pseudo-elements, you have to account for them
somehow or reject selectors with pseudo-elements.

    >>> xpath = cssselect.HTMLTranslator().selector_to_xpath(selectors[1])
    >>> xpath
    "descendant-or-self::*[@id = 'root']/section"

Finally, the XPath expressions can be used with lxml_ to find the matching
elements.

    >>> from lxml import etree
    >>> compiled_selector = etree.XPath(xpath)
    >>> document = etree.fromstring('''<section id="root">
    ...   <section id="head">Title</section>
    ...   <section id="content">
    ...     Lorem <section id="sub-section">ipsum</section>
    ...   </section>
    ... </section>''')
    >>> [el.get('id') for el in compiled_selector(document)]
    ['head', 'content']

.. _lxml: http://lxml.de/xpathxslt.html#xpath

Find more details in the `cssselect documentation`_.

.. _cssselect documentation: http://packages.python.org/cssselect/


.. module:: tinycss.color3

Color 3
-------

This module implements parsing for the *<color>* values, as defined in
`CSS 3 Color <http://www.w3.org/TR/css3-color/>`_.

The (deprecated) CSS2 system colors are not supported, but you can
easily test for them if you want as they are simple ``IDENT`` tokens.
For example::

    if token.type == 'IDENT' and token.value == 'ButtonText':
        return ...

All other values types *are* supported:

* Basic, extended (X11) and transparent color keywords;
* 3-digit and 6-digit hexadecimal notations;
* ``rgb()``, ``rgba()``, ``hsl()`` and ``hsla()`` functional notations.
* ``currentColor``

This module does not integrate with a parser class. Instead, it provides
a function that can parse tokens as found in :attr:`.css21.Declaration.value`,
for example.

.. autofunction:: parse_color
.. autofunction:: parse_color_string
.. autoclass:: RGBA


.. module:: tinycss.page3

Paged Media 3
-------------

.. autoclass:: CSSPage3Parser
.. autoclass:: MarginRule


.. module:: tinycss.fonts3

Fonts 3
-------

.. autoclass:: CSSFonts3Parser
.. autoclass:: FontFaceRule
.. autoclass:: FontFeatureValuesRule
.. autoclass:: FontFeatureRule


Other CSS modules
-----------------

To add support for new CSS syntax, see :ref:`extending`.