Permalink
Browse files

Make RuleSet.selector and Declaration.value plain lists i.s.o Contain…

…erToken
  • Loading branch information...
1 parent 25d6af8 commit 40d18205adb871891dcb9237156262431f9ac42a @SimonSapin committed Mar 31, 2012
Showing with 21 additions and 26 deletions.
  1. +11 −16 tinycss/css21.py
  2. +2 −2 tinycss/selectors3.py
  3. +7 −7 tinycss/tests/test_css21.py
  4. +1 −1 tinycss/tests/test_page3.py
View
27 tinycss/css21.py
@@ -15,7 +15,6 @@
from .decoding import decode
from .tokenizer import tokenize_grouped
-from .token_data import ContainerToken
# stylesheet : [ CDO | CDC | S | statement ]*;
@@ -145,8 +144,9 @@ class RuleSet(object):
.. attribute:: selector
- The selecor as a :class:`~tinycss.token_data.ContainerToken` object.
- In CSS 3 terminology, this is actually a selector group.
+ The selector as a list of :class:`~.token_data.Token` or
+ :class:`~.token_data.ContainerToken`.
+ In CSS 3, this is actually called a selector group.
.. attribute:: declarations
@@ -176,7 +176,8 @@ class Declaration(object):
.. attribute:: value
- The property value as a :class:`~tinycss.token_data.ContainerToken`.
+ The property value as a list of :class:`~.token_data.Token` or
+ :class:`~.token_data.ContainerToken`.
The value is not parsed. UAs using tinycss may only support
some properties or some values and tinycss does not know which.
@@ -724,27 +725,23 @@ def parse_ruleset(self, first_token, tokens):
not for CSS 2.1 or another level.
"""
- selector_parts = []
+ selector = []
for token in chain([first_token], tokens):
if token.type == '{':
# Parse/validate once we’ve read the whole rule
- while selector_parts and selector_parts[-1].type == 'S':
- selector_parts.pop()
- if not selector_parts:
+ while selector and selector[-1].type == 'S':
+ selector.pop()
+ if not selector:
raise ParseError(first_token, 'empty selector')
- for selector_token in selector_parts:
+ for selector_token in selector:
self.validate_any(selector_token, 'selector')
- start = selector_parts[0] if selector_parts else token
- selector = ContainerToken(
- 'SELECTOR', '', '', selector_parts,
- start.line, start.column)
declarations, errors = self.parse_declaration_list(
token.content)
ruleset = RuleSet(selector, declarations,
first_token.line, first_token.column)
return ruleset, errors
else:
- selector_parts.append(token)
+ selector.append(token)
raise ParseError(token, 'no declaration block found for ruleset')
def parse_declaration_list(self, tokens):
@@ -826,8 +823,6 @@ def parse_declaration(self, tokens):
if not value:
raise ParseError(token, 'expected a property value')
value, priority = self.parse_value_priority(value)
- value = ContainerToken(
- 'VALUES', '', '', value, value[0].line, value[0].column)
return Declaration(
property_name, value, priority, name_token.line, name_token.column)
View
4 tinycss/selectors3.py
@@ -212,9 +212,9 @@ def parse_ruleset(self, first_token, tokens):
first_token, tokens)
try:
ruleset.selector_list = _parse_selector_group_tokens(
- ruleset.selector.content)
+ ruleset.selector)
except InvalidSelectorError as exc:
# Invalidate the whole ruleset even if some selectors
# in the selector group are valid.
- raise ParseError(ruleset.selector, exc.args[0])
+ raise ParseError(ruleset, exc.args[0])
return ruleset, errors
View
14 tinycss/tests/test_css21.py
@@ -154,8 +154,8 @@ def parse_at_rule(self, rule, stylesheet_rules, errors, context):
list(jsonify(rule.body.content))
if rule.body is not None else None)
if rule.at_keyword else
- (rule.selector.as_css, [
- (decl.name, list(jsonify(decl.value.content)))
+ (''.join(s.as_css for s in rule.selector), [
+ (decl.name, list(jsonify(decl.value)))
for decl in rule.declarations])
for rule in stylesheet.rules
]
@@ -198,7 +198,7 @@ def parse_at_rule(self, rule, stylesheet_rules, errors, context):
def test_parse_style_attr(css_source, expected_declarations, expected_errors):
declarations, errors = CSS21Parser().parse_style_attr(css_source)
assert_errors(errors, expected_errors)
- result = [(decl.name, list(jsonify(decl.value.content)))
+ result = [(decl.name, list(jsonify(decl.value)))
for decl in declarations]
assert result == expected_declarations
@@ -231,7 +231,7 @@ def test_parse_style_attr(css_source, expected_declarations, expected_errors):
def test_important(css_source, expected_declarations, expected_errors):
declarations, errors = CSS21Parser().parse_style_attr(css_source)
assert_errors(errors, expected_errors)
- result = [(decl.name, list(jsonify(decl.value.content)), decl.priority)
+ result = [(decl.name, list(jsonify(decl.value)), decl.priority)
for decl in declarations]
assert result == expected_declarations
@@ -305,7 +305,7 @@ def test_at_page(css, expected_result, expected_errors):
result = (
rule.selector,
rule.specificity,
- [(decl.name, list(jsonify(decl.value.content)))
+ [(decl.name, list(jsonify(decl.value)))
for decl in rule.declarations],
)
assert result == expected_result
@@ -337,8 +337,8 @@ def test_at_media(css_source, expected_rules, expected_errors):
assert rule.at_keyword == '@media'
result = [
(rule.media, [
- (sub_rule.selector.as_css, [
- (decl.name, list(jsonify(decl.value.content)))
+ (''.join(s.as_css for s in sub_rule.selector), [
+ (decl.name, list(jsonify(decl.value)))
for decl in sub_rule.declarations])
for sub_rule in rule.rules
])
View
2 tinycss/tests/test_page3.py
@@ -88,7 +88,7 @@ def test_content(css, expected_declarations, expected_rules, expected_errors):
assert_errors(stylesheet.errors, expected_errors)
def declarations(rule):
- return [(decl.name, list(jsonify(decl.value.content)))
+ return [(decl.name, list(jsonify(decl.value)))
for decl in rule.declarations]
assert len(stylesheet.rules) == 1

0 comments on commit 40d1820

Please sign in to comment.