Skip to content

Commit 493eace

Browse files
committed
Polish the docs for CSS 2.1
1 parent 61c6f88 commit 493eace

File tree

5 files changed

+76
-90
lines changed

5 files changed

+76
-90
lines changed

‎README.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ It is designed to be easy to extend for new CSS modules.
1515
Quick facts:
1616

1717
* Free software: BSD licensed
18-
* Compatible Python 2.6+ and 3.x
18+
* Compatible with Python 2.6+ and 3.x
1919
* `Latest documentation <http://packages.python.org/tinycss/>`_
2020
* Source, issues and pull requests `on Github
2121
<https://github.com/SimonSapin/tinycss/>`_

‎docs/parsing.rst‎

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,79 +26,68 @@ all the parsed content.
2626
Parsers
2727
-------
2828

29-
Parsers are subclasses of :class:`tinycss.css21.CSS21Parser`. Various subclasses
30-
add support for more syntax. You can choose which features to enable
31-
by making a new parser class with multiple inheritance, but there is also
32-
a convenience function to do that:
29+
Parsers are subclasses of :class:`tinycss.css21.CSS21Parser`. Various
30+
subclasses add support for more syntax. You can choose which features to
31+
enable by making a new parser class with multiple inheritance, but there
32+
is also a convenience function to do that:
3333

3434
.. module:: tinycss
3535

3636
.. autofunction:: make_parser(*base_classes, with_selectors3=False, with_page3=False, **kwargs)
3737

3838

39-
.. module:: tinycss.core
39+
.. module:: tinycss.css21
4040

4141
Parsing a stylesheet
4242
~~~~~~~~~~~~~~~~~~~~
4343

4444
Parser classes have three different methods to parse CSS stylesheet,
4545
depending on whether you have a file, a byte string, or an Unicode string.
4646

47-
.. automethod:: CoreParser.parse_stylesheet_file
48-
.. automethod:: CoreParser.parse_stylesheet_bytes
49-
.. automethod:: CoreParser.parse_stylesheet
47+
.. automethod:: CSS21Parser.parse_stylesheet_file
48+
.. automethod:: CSS21Parser.parse_stylesheet_bytes
49+
.. automethod:: CSS21Parser.parse_stylesheet
5050

5151

5252
Parsing a ``style`` attribute
5353
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5454

55-
.. automethod:: CoreParser.parse_style_attr
55+
.. automethod:: CSS21Parser.parse_style_attr
5656

5757

58-
Tokens
59-
------
58+
Parsed objects
59+
--------------
6060

61-
Some parts of a stylesheet (such as property values) are not parsed
62-
by tinycss. They appear as tokens instead.
61+
These data structures make up the results of the various parsing methods.
6362
All of these objects have :obj:`line` and :obj:`column` attributes (not
6463
repeated every time fore brevity) that indicate where in the CSS source this
6564
object was read.
6665

67-
tinycss does not know which properties and which values a given User Agent
68-
wants to support: it is the UA’s responsibility to ignore unknown and
69-
unsupported values, and fall back on any previous declaration.
66+
.. autoclass:: Stylesheet()
67+
.. autoclass:: ParseError()
68+
.. autoclass:: RuleSet()
69+
.. autoclass:: ImportRule()
70+
.. autoclass:: MediaRule()
71+
.. autoclass:: PageRule()
72+
.. autoclass:: Declaration()
73+
74+
75+
Tokens
76+
------
77+
78+
Some parts of a stylesheet (such as selectors in CSS 2.1 or property values)
79+
are not parsed by tinycss. They appear as tokens instead.
7080

7181
.. module:: tinycss.token_data
7282

7383
.. autoclass:: Token()
7484
.. autoclass:: tinycss.speedups.CToken()
7585
.. autoclass:: ContainerToken()
7686

87+
.. autoattribute:: as_css
88+
7789
.. method:: __iter__, __len__
7890

7991
Shortcuts for accessing :attr:`content`.
8092

8193
.. autoclass:: FunctionToken()
82-
83-
84-
Parsed objects
85-
--------------
86-
87-
These data structures make up the results of the various parsing methods.
88-
All of these objects have :obj:`line` and :obj:`column` attributes (not
89-
repeated every time fore brevity) that indicate where in the CSS source this
90-
object was read.
91-
92-
.. currentmodule:: tinycss.core
93-
94-
.. autoclass:: Stylesheet()
95-
.. autoclass:: ParseError()
96-
.. autoclass:: RuleSet()
97-
.. autoclass:: Declaration()
98-
99-
.. module:: tinycss.css21
100-
101-
.. autoclass:: PropertyDeclaration()
102-
.. autoclass:: PageRule()
103-
.. autoclass:: MediaRule()
104-
.. autoclass:: ImportRule()

‎tinycss/css21.py‎

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class Stylesheet(object):
4141
4242
.. attribute:: rules
4343
44-
A mixed list of :class:`RuleSet` and various at-rules, in source order.
44+
A mixed list, in source order, of :class:`RuleSet` and various
45+
at-rules such as :class:`ImportRule`, :class:`MediaRule`
46+
and :class:`PageRule`.
4547
Use their :obj:`at_keyword` attribute to distinguish them.
4648
4749
.. attribute:: errors
@@ -74,7 +76,7 @@ def pretty(self): # pragma: no cover
7476
classParseError(ValueError):
7577
"""Details about a CSS syntax error. Usually indicates that something
7678
(a rule or a declaration) was ignored and will not appear as a parsed
77-
objects.
79+
object.
7880
7981
.. attribute:: line
8082
@@ -161,13 +163,12 @@ class RuleSet(object):
161163
162164
.. attribute:: selector
163165
164-
A (possibly empty) :class:`ContainerToken` object.
166+
The selecor as a :class:`~tinycss.token_data.ContainerToken` object.
165167
In CSS 3 terminology, this is actually a selector group.
166168
167169
.. attribute:: declarations
168170
169-
The list of :class:`Declaration` as returned by
170-
:func:`parse_declaration_list`, in source order.
171+
The list of :class:`Declaration`, in source order.
171172
172173
"""
173174
def__init__(self, selector, declarations, line, column):
@@ -201,19 +202,33 @@ class Declaration(object):
201202
202203
.. attribute:: value
203204
204-
The property value: a list of tokens as returned by
205-
:func:`parse_value`.
205+
The property value as a :class:`~tinycss.token_data.ContainerToken`.
206+
207+
The value is not parsed. UAs using tinycss may only support
208+
some properties or some values and tinycss does not know which.
209+
They need to parse values themselves and ignore declarations with
210+
unknown or unsupported properties or values, and fall back
211+
on any previous declaration.
212+
213+
:mod:`tinycss.colors3` parses color values, but other values
214+
will need specific parsing/validation code.
215+
216+
.. attribute:: priority
217+
218+
Either the string ``'important'`` or ``None``.
206219
207220
"""
208-
def__init__(self, name, value, line, column):
221+
def__init__(self, name, value, priority, line, column):
209222
self.name=name
210223
self.value=value
224+
self.priority=priority
211225
self.line=line
212226
self.column=column
213227

214228
def__repr__(self): # pragma: no cover
229+
priority=' !'+self.priorityifself.priorityelse''
215230
return ('<{0.__class__.__name__}{0.line}:{0.column}'
216-
'{0.name}:{0.value.as_css}>'.format(self))
231+
'{0.name}:{0.value.as_css}{1}>'.format(self, priority))
217232

218233
defpretty(self): # pragma: no cover
219234
"""Return an indented string representation for debugging"""
@@ -224,26 +239,6 @@ def pretty(self): # pragma: no cover
224239
return'\n'.join(lines)
225240

226241

227-
classPropertyDeclaration(Declaration):
228-
"""A CSS 2.1 property declaration.
229-
230-
Same as :class:`Declaration` with an additional attribute:
231-
232-
.. attribute:: priority
233-
234-
Either the string ``'important'`` or ``None``.
235-
236-
"""
237-
def__init__(self, name, value, priority, line, column):
238-
super(PropertyDeclaration, self).__init__(name, value, line, column)
239-
self.priority=priority
240-
241-
def__repr__(self): # pragma: no cover
242-
priority=' !'+self.priorityifself.priorityelse''
243-
return ('<{0.__class__.__name__}{0.line}:{0.column}'
244-
'{0.name}:{0.value.as_css}{1}>'.format(self, priority))
245-
246-
247242
classPageRule(object):
248243
"""A parsed CSS 2.1 @page rule.
249244
@@ -265,11 +260,11 @@ class PageRule(object):
265260
266261
.. attribute:: declarations
267262
268-
A list of :class:`PropertyDeclaration`
263+
A list of :class:`Declaration`, in source order.
269264
270265
.. attribute:: at_rules
271266
272-
The list of parsed at-rules inside the @page block.
267+
The list of parsed at-rules inside the @page block, in source order.
273268
Always empty for CSS 2.1.
274269
275270
"""
@@ -303,7 +298,8 @@ class MediaRule(object):
303298
304299
.. attribute:: rules
305300
306-
The list rulesets and at-rules inside the @media block.
301+
The list :class:`RuleSet` and various at-rules inside the @media
302+
block, in source order.
307303
308304
"""
309305
at_keyword='@media'
@@ -390,11 +386,11 @@ class CSS21Parser(object):
390386
# User API:
391387

392388
defparse_stylesheet_file(self, css_file, protocol_encoding=None,
393-
linking_encoding=None, document_encoding=None):
389+
linking_encoding=None, document_encoding=None):
394390
"""Parse a stylesheet from a file or filename.
395391
396-
The character encoding is determined as in
397-
:meth:`parse_stylesheet_bytes`.
392+
Character encoding-related parameters and behavior are the same
393+
as in :meth:`parse_stylesheet_bytes`.
398394
399395
:param css_file:
400396
Either a file (any object with a :meth:`~file.read` method)
@@ -417,6 +413,8 @@ def parse_stylesheet_bytes(self, css_bytes, protocol_encoding=None,
417413
418414
The character encoding is determined from the passed metadata and the
419415
``@charset`` rule in the stylesheet (if any).
416+
If no encoding information is available or decoding fails,
417+
decoding defaults to UTF-8 and then fall back on ISO-8859-1.
420418
421419
:param css_bytes:
422420
A CSS stylesheet as a byte string.
@@ -428,8 +426,6 @@ def parse_stylesheet_bytes(self, css_bytes, protocol_encoding=None,
428426
(if any)
429427
:param document_encoding:
430428
Encoding of the referring style sheet or document (if any)
431-
:raises:
432-
:class:`UnicodeDecodeError` if decoding failed
433429
:return:
434430
A :class:`Stylesheet`.
435431
@@ -467,8 +463,8 @@ def parse_style_attr(self, css_source):
467463
:param css_source:
468464
The attribute value, as an unicode string.
469465
:return:
470-
A tuple of the list of valid :class:`Declaration` and a list
471-
of :class:`ParseError`.
466+
A tuple of the list of valid :class:`Declaration` and
467+
a list of :class:`ParseError`.
472468
"""
473469
returnself.parse_declaration_list(tokenize_grouped(css_source))
474470

@@ -711,7 +707,7 @@ def parse_page_block(self, body, errors):
711707
:returns:
712708
A tuple of:
713709
714-
* A list of :class:`PropertyDeclaration`
710+
* A list of :class:`Declaration`
715711
* A list of parsed at-rules (empty for CSS 2.1)
716712
* A list of :class:`ParseError`
717713
@@ -864,7 +860,7 @@ def parse_declaration(self, tokens):
864860
value, priority=self.parse_value_priority(value)
865861
value=ContainerToken(
866862
'VALUES', '', '', value, value[0].line, value[0].column)
867-
returnPropertyDeclaration(
863+
returnDeclaration(
868864
property_name, value, priority, name_token.line, name_token.column)
869865

870866
defparse_value_priority(self, original_value):

‎tinycss/decoding.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def decode(css_bytes, protocol_encoding=None,
3434
"""
3535
Determine the character encoding from the passed metadata and the
3636
``@charset`` rule in the stylesheet (if any); and decode accordingly.
37+
If no encoding information is available or decoding fails,
38+
decoding defaults to UTF-8 and then fall back on ISO-8859-1.
3739
3840
:param css_bytes:
3941
a CSS stylesheet as a byte string
@@ -45,8 +47,6 @@ def decode(css_bytes, protocol_encoding=None,
4547
(if any)
4648
:param document_encoding:
4749
Encoding of the referring style sheet or document (if any)
48-
:raises:
49-
:class:`UnicodeDecodeError` if decoding failed
5050
:return:
5151
A tuple of an Unicode string, with any BOM removed, and the
5252
encoding that was used.

‎tinycss/token_data.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,6 @@ class Token(object):
203203
``HASH``
204204
``#`` followed immediately by a name. Eg: ``#ff8800``
205205
206-
``FUNCTION``
207-
An identifier followed immediately by ``(``. Eg: ``rgba(``
208-
209206
``ATKEYWORD``
210207
``@`` followed immediately by an identifier. Eg: ``@page``
211208
@@ -250,16 +247,18 @@ class Token(object):
250247
251248
The parsed value:
252249
253-
* All backslash-escapes are unescaped.
254250
* INTEGER, NUMBER, PERCENTAGE or DIMENSION tokens: the numeric value
255251
as an int or float.
256252
* STRING tokens: the unescaped string without quotes
257253
* URI tokens: the unescaped URI without quotes or
258254
``url(`` and ``)`` markers.
259-
* IDENT, ATKEYWORD, HASH or FUNCTION tokens: the unescaped token,
260-
with ``@``, ``#`` or ``(`` markers left as-is
255+
* IDENT, ATKEYWORDor HASH tokens: the unescaped token,
256+
with ``@``or ``#`` markers left as-is
261257
* Other tokens: same as :attr:`as_css`
262258
259+
*Unescaped* refers to the various escaping methods based on the
260+
backslash ``\`` character in CSS syntax.
261+
263262
.. attribute:: unit
264263
265264
* DIMENSION tokens: the normalized (unescaped, lower-case)
@@ -311,11 +310,13 @@ class ContainerToken(object):
311310
312311
.. attribute:: css_start
313312
314-
The string for the opening token as it was read from the CSS source
313+
The string for the opening token as it was read from the CSS source.
314+
Eg: ``{``
315315
316316
.. attribute:: css_end
317317
318318
The string for the closing token as it was read from the CSS source
319+
Eg: ``}``
319320
320321
.. attribute:: content
321322

0 commit comments

Comments
(0)