Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
Make RuleSet.selector and Declaration.value plain lists i.s.o Contain…
Browse files Browse the repository at this point in the history
…erToken
  • Loading branch information
SimonSapin committed Mar 31, 2012
1 parent 25d6af8 commit 40d1820
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
27 changes: 11 additions & 16 deletions tinycss/css21.py
Expand Up @@ -15,7 +15,6 @@

from .decoding import decode
from .tokenizer import tokenize_grouped
from .token_data import ContainerToken


# stylesheet : [ CDO | CDC | S | statement ]*;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions tinycss/selectors3.py
Expand Up @@ -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
14 changes: 7 additions & 7 deletions tinycss/tests/test_css21.py
Expand Up @@ -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
]
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
])
Expand Down
2 changes: 1 addition & 1 deletion tinycss/tests/test_page3.py
Expand Up @@ -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
Expand Down

0 comments on commit 40d1820

Please sign in to comment.