| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
|
| 4 |
|
| 5 |
__license__ = 'GPL v3' |
| 6 |
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' |
| 7 |
|
| 8 |
import io |
| 9 |
import os |
| 10 |
import tempfile |
| 11 |
|
| 12 |
from tinycss.css21 import CSS21Parser |
| 13 |
from tinycss.tests import BaseTest |
| 14 |
from tinycss.tests.tokenizing import jsonify |
| 15 |
|
| 16 |
|
| 17 |
class CoreParser(CSS21Parser): |
| 18 |
"""A parser that always accepts unparsed at-rules.""" |
| 19 |
def parse_at_rule(self, rule, stylesheet_rules, errors, context): |
| 20 |
return rule |
| 21 |
|
| 22 |
def parse_bytes(css_bytes, kwargs): |
| 23 |
return CSS21Parser().parse_stylesheet_bytes(css_bytes, **kwargs) |
| 24 |
|
| 25 |
|
| 26 |
def parse_bytesio_file(css_bytes, kwargs): |
| 27 |
css_file = io.BytesIO(css_bytes) |
| 28 |
return CSS21Parser().parse_stylesheet_file(css_file, **kwargs) |
| 29 |
|
| 30 |
|
| 31 |
def parse_filename(css_bytes, kwargs): |
| 32 |
css_file = tempfile.NamedTemporaryFile(delete=False) |
| 33 |
try: |
| 34 |
css_file.write(css_bytes) |
| 35 |
# Windows can not open the filename a second time while |
| 36 |
# it is still open for writing. |
| 37 |
css_file.close() |
| 38 |
return CSS21Parser().parse_stylesheet_file(css_file.name, **kwargs) |
| 39 |
finally: |
| 40 |
os.remove(css_file.name) |
| 41 |
|
| 42 |
|
| 43 |
class TestCSS21(BaseTest): |
| 44 |
|
| 45 |
def test_bytes(self): |
| 46 |
for (css_bytes, kwargs, expected_result, parse) in [ |
| 47 |
params + (parse,) |
| 48 |
for parse in [parse_bytes, parse_bytesio_file, parse_filename] |
| 49 |
for params in [ |
| 50 |
('@import "é";'.encode('utf8'), {}, 'é'), |
| 51 |
('@import "é";'.encode('utf16'), {}, 'é'), # with a BOM |
| 52 |
('@import "é";'.encode('latin1'), {}, 'é'), |
| 53 |
('@import "£";'.encode('Shift-JIS'), {}, '\x81\x92'), # latin1 mojibake |
| 54 |
('@charset "Shift-JIS";@import "£";'.encode('Shift-JIS'), {}, '£'), |
| 55 |
(' @charset "Shift-JIS";@import "£";'.encode('Shift-JIS'), {}, |
| 56 |
'\x81\x92'), |
| 57 |
('@import "£";'.encode('Shift-JIS'), |
| 58 |
{'document_encoding': 'Shift-JIS'}, '£'), |
| 59 |
('@import "£";'.encode('Shift-JIS'), |
| 60 |
{'document_encoding': 'utf8'}, '\x81\x92'), |
| 61 |
('@charset "utf8"; @import "£";'.encode('utf8'), |
| 62 |
{'document_encoding': 'latin1'}, '£'), |
| 63 |
# Mojibake yay! |
| 64 |
(' @charset "utf8"; @import "é";'.encode('utf8'), |
| 65 |
{'document_encoding': 'latin1'}, 'é'), |
| 66 |
('@import "é";'.encode('utf8'), {'document_encoding': 'latin1'}, 'é'), |
| 67 |
] |
| 68 |
]: |
| 69 |
stylesheet = parse(css_bytes, kwargs) |
| 70 |
self.ae(stylesheet.rules[0].at_keyword, '@import') |
| 71 |
self.ae(stylesheet.rules[0].uri, expected_result) |
| 72 |
|
| 73 |
def test_at_rules(self): |
| 74 |
for (css_source, expected_rules, expected_errors) in [ |
| 75 |
(' /* hey */\n', 0, []), |
| 76 |
('foo {}', 1, []), |
| 77 |
('foo{} @lipsum{} bar{}', 2, |
| 78 |
['unknown at-rule in stylesheet context: @lipsum']), |
| 79 |
('@charset "ascii"; foo {}', 1, []), |
| 80 |
(' @charset "ascii"; foo {}', 1, ['mis-placed or malformed @charset rule']), |
| 81 |
('@charset ascii; foo {}', 1, ['mis-placed or malformed @charset rule']), |
| 82 |
('foo {} @charset "ascii";', 1, ['mis-placed or malformed @charset rule']), |
| 83 |
]: |
| 84 |
# Pass 'encoding' to allow @charset |
| 85 |
stylesheet = CSS21Parser().parse_stylesheet(css_source, encoding='utf8') |
| 86 |
self.assert_errors(stylesheet.errors, expected_errors) |
| 87 |
self.ae(len(stylesheet.rules), expected_rules) |
| 88 |
|
| 89 |
def test_core_parser(self): |
| 90 |
for (css_source, expected_rules, expected_errors) in [ |
| 91 |
(' /* hey */\n', [], []), |
| 92 |
|
| 93 |
('foo{} /* hey */\n@bar;@baz{}', |
| 94 |
[('foo', []), ('@bar', [], None), ('@baz', [], [])], []), |
| 95 |
|
| 96 |
('@import "foo.css"/**/;', [ |
| 97 |
('@import', [('STRING', 'foo.css')], None)], []), |
| 98 |
|
| 99 |
('@import "foo.css"/**/', [ |
| 100 |
('@import', [('STRING', 'foo.css')], None)], []), |
| 101 |
|
| 102 |
('@import "foo.css', [ |
| 103 |
('@import', [('STRING', 'foo.css')], None)], []), |
| 104 |
|
| 105 |
('{}', [], ['empty selector']), |
| 106 |
|
| 107 |
('a{b:4}', [('a', [('b', [('INTEGER', 4)])])], []), |
| 108 |
|
| 109 |
('@page {\t b: 4; @margin}', [('@page', [], [ |
| 110 |
('S', '\t '), ('IDENT', 'b'), (':', ':'), ('S', ' '), ('INTEGER', 4), |
| 111 |
(';', ';'), ('S', ' '), ('ATKEYWORD', '@margin'), |
| 112 |
])], []), |
| 113 |
|
| 114 |
('foo', [], ['no declaration block found']), |
| 115 |
|
| 116 |
('foo @page {} bar {}', [('bar', [])], |
| 117 |
['unexpected ATKEYWORD token in selector']), |
| 118 |
|
| 119 |
('foo { content: "unclosed string;\n color:red; ; margin/**/\n: 2cm; }', |
| 120 |
[('foo', [('margin', [('DIMENSION', 2)])])], |
| 121 |
['unexpected BAD_STRING token in property value']), |
| 122 |
|
| 123 |
('foo { 4px; bar: 12% }', |
| 124 |
[('foo', [('bar', [('PERCENTAGE', 12)])])], |
| 125 |
['expected a property name, got DIMENSION']), |
| 126 |
|
| 127 |
('foo { bar! 3cm auto ; baz: 7px }', |
| 128 |
[('foo', [('baz', [('DIMENSION', 7)])])], |
| 129 |
["expected ':', got DELIM"]), |
| 130 |
|
| 131 |
('foo { bar ; baz: {("}"/* comment */) {0@fizz}} }', |
| 132 |
[('foo', [('baz', [('{', [ |
| 133 |
('(', [('STRING', '}')]), ('S', ' '), |
| 134 |
('{', [('INTEGER', 0), ('ATKEYWORD', '@fizz')]) |
| 135 |
])])])], |
| 136 |
["expected ':'"]), |
| 137 |
|
| 138 |
('foo { bar: ; baz: not(z) }', |
| 139 |
[('foo', [('baz', [('FUNCTION', 'not', [('IDENT', 'z')])])])], |
| 140 |
['expected a property value']), |
| 141 |
|
| 142 |
('foo { bar: (]) ; baz: U+20 }', |
| 143 |
[('foo', [('baz', [('UNICODE-RANGE', 'U+20')])])], |
| 144 |
['unmatched ] token in (']), |
| 145 |
]: |
| 146 |
stylesheet = CoreParser().parse_stylesheet(css_source) |
| 147 |
self.assert_errors(stylesheet.errors, expected_errors) |
| 148 |
result = [ |
| 149 |
(rule.at_keyword, list(jsonify(rule.head)), |
| 150 |
list(jsonify(rule.body)) |
| 151 |
if rule.body is not None else None) |
| 152 |
if rule.at_keyword else |
| 153 |
(rule.selector.as_css(), [ |
| 154 |
(decl.name, list(jsonify(decl.value))) |
| 155 |
for decl in rule.declarations]) |
| 156 |
for rule in stylesheet.rules |
| 157 |
] |
| 158 |
self.ae(result, expected_rules) |
| 159 |
|
| 160 |
def test_parse_style_attr(self): |
| 161 |
for (css_source, expected_declarations, expected_errors) in [ |
| 162 |
(' /* hey */\n', [], []), |
| 163 |
|
| 164 |
('b:4', [('b', [('INTEGER', 4)])], []), |
| 165 |
|
| 166 |
('{b:4}', [], ['expected a property name, got {']), |
| 167 |
|
| 168 |
('b:4} c:3', [], ['unmatched } token in property value']), |
| 169 |
|
| 170 |
(' 4px; bar: 12% ', |
| 171 |
[('bar', [('PERCENTAGE', 12)])], |
| 172 |
['expected a property name, got DIMENSION']), |
| 173 |
|
| 174 |
('bar! 3cm auto ; baz: 7px', |
| 175 |
[('baz', [('DIMENSION', 7)])], |
| 176 |
["expected ':', got DELIM"]), |
| 177 |
|
| 178 |
('foo; bar ; baz: {("}"/* comment */) {0@fizz}}', |
| 179 |
[('baz', [('{', [ |
| 180 |
('(', [('STRING', '}')]), ('S', ' '), |
| 181 |
('{', [('INTEGER', 0), ('ATKEYWORD', '@fizz')]) |
| 182 |
])])], |
| 183 |
["expected ':'", "expected ':'"]), |
| 184 |
|
| 185 |
('bar: ; baz: not(z)', |
| 186 |
[('baz', [('FUNCTION', 'not', [('IDENT', 'z')])])], |
| 187 |
['expected a property value']), |
| 188 |
|
| 189 |
('bar: (]) ; baz: U+20', |
| 190 |
[('baz', [('UNICODE-RANGE', 'U+20')])], |
| 191 |
['unmatched ] token in (']), |
| 192 |
]: |
| 193 |
declarations, errors = CSS21Parser().parse_style_attr(css_source) |
| 194 |
self.assert_errors(errors, expected_errors) |
| 195 |
result = [(decl.name, list(jsonify(decl.value))) |
| 196 |
for decl in declarations] |
| 197 |
self.ae(result, expected_declarations) |
| 198 |
|
| 199 |
def test_important(self): |
| 200 |
for (css_source, expected_declarations, expected_errors) in [ |
| 201 |
(' /* hey */\n', [], []), |
| 202 |
|
| 203 |
('a:1; b:2', |
| 204 |
[('a', [('INTEGER', 1)], None), ('b', [('INTEGER', 2)], None)], []), |
| 205 |
|
| 206 |
('a:1 important; b: important', |
| 207 |
[('a', [('INTEGER', 1), ('S', ' '), ('IDENT', 'important')], None), |
| 208 |
('b', [('IDENT', 'important')], None)], |
| 209 |
[]), |
| 210 |
|
| 211 |
('a:1 !important; b:2', |
| 212 |
[('a', [('INTEGER', 1)], 'important'), ('b', [('INTEGER', 2)], None)], |
| 213 |
[]), |
| 214 |
|
| 215 |
('a:1!\t Im\\50 O\\RTant; b:2', |
| 216 |
[('a', [('INTEGER', 1)], 'important'), ('b', [('INTEGER', 2)], None)], |
| 217 |
[]), |
| 218 |
|
| 219 |
('a: !important; b:2', |
| 220 |
[('b', [('INTEGER', 2)], None)], |
| 221 |
['expected a value before !important']), |
| 222 |
|
| 223 |
]: |
| 224 |
declarations, errors = CSS21Parser().parse_style_attr(css_source) |
| 225 |
self.assert_errors(errors, expected_errors) |
| 226 |
result = [(decl.name, list(jsonify(decl.value)), decl.priority) |
| 227 |
for decl in declarations] |
| 228 |
self.ae(result, expected_declarations) |
| 229 |
|
| 230 |
def test_at_import(self): |
| 231 |
for (css_source, expected_rules, expected_errors) in [ |
| 232 |
(' /* hey */\n', [], []), |
| 233 |
('@import "foo.css";', [('foo.css', ['all'])], []), |
| 234 |
('@import url(foo.css);', [('foo.css', ['all'])], []), |
| 235 |
('@import "foo.css" screen, print;', |
| 236 |
[('foo.css', ['screen', 'print'])], []), |
| 237 |
('@charset "ascii"; @import "foo.css"; @import "bar.css";', |
| 238 |
[('foo.css', ['all']), ('bar.css', ['all'])], []), |
| 239 |
('foo {} @import "foo.css";', |
| 240 |
[], ['@import rule not allowed after a ruleset']), |
| 241 |
('@page {} @import "foo.css";', |
| 242 |
[], ['@import rule not allowed after an @page rule']), |
| 243 |
('@import ;', |
| 244 |
[], ['expected URI or STRING for @import rule']), |
| 245 |
('@import foo.css;', |
| 246 |
[], ['expected URI or STRING for @import rule, got IDENT']), |
| 247 |
('@import "foo.css" {}', |
| 248 |
[], ["expected ';', got a block"]), |
| 249 |
]: |
| 250 |
# Pass 'encoding' to allow @charset |
| 251 |
stylesheet = CSS21Parser().parse_stylesheet(css_source, encoding='utf8') |
| 252 |
self.assert_errors(stylesheet.errors, expected_errors) |
| 253 |
|
| 254 |
result = [ |
| 255 |
(rule.uri, rule.media) |
| 256 |
for rule in stylesheet.rules |
| 257 |
if rule.at_keyword == '@import' |
| 258 |
] |
| 259 |
self.ae(result, expected_rules) |
| 260 |
|
| 261 |
def test_at_page(self): |
| 262 |
for (css, expected_result, expected_errors) in [ |
| 263 |
('@page {}', (None, (0, 0), []), []), |
| 264 |
('@page:first {}', ('first', (1, 0), []), []), |
| 265 |
('@page :left{}', ('left', (0, 1), []), []), |
| 266 |
('@page\t\n:right {}', ('right', (0, 1), []), []), |
| 267 |
('@page :last {}', None, ['invalid @page selector']), |
| 268 |
('@page : right {}', None, ['invalid @page selector']), |
| 269 |
('@page table:left {}', None, ['invalid @page selector']), |
| 270 |
|
| 271 |
('@page;', None, ['invalid @page rule: missing block']), |
| 272 |
('@page { a:1; ; b: 2 }', |
| 273 |
(None, (0, 0), [('a', [('INTEGER', 1)]), ('b', [('INTEGER', 2)])]), |
| 274 |
[]), |
| 275 |
('@page { a:1; c: ; b: 2 }', |
| 276 |
(None, (0, 0), [('a', [('INTEGER', 1)]), ('b', [('INTEGER', 2)])]), |
| 277 |
['expected a property value']), |
| 278 |
('@page { a:1; @top-left {} b: 2 }', |
| 279 |
(None, (0, 0), [('a', [('INTEGER', 1)]), ('b', [('INTEGER', 2)])]), |
| 280 |
['unknown at-rule in @page context: @top-left']), |
| 281 |
('@page { a:1; @top-left {}; b: 2 }', |
| 282 |
(None, (0, 0), [('a', [('INTEGER', 1)]), ('b', [('INTEGER', 2)])]), |
| 283 |
['unknown at-rule in @page context: @top-left']), |
| 284 |
]: |
| 285 |
stylesheet = CSS21Parser().parse_stylesheet(css) |
| 286 |
self.assert_errors(stylesheet.errors, expected_errors) |
| 287 |
|
| 288 |
if expected_result is None: |
| 289 |
self.assertFalse(stylesheet.rules) |
| 290 |
else: |
| 291 |
self.ae(len(stylesheet.rules), 1) |
| 292 |
rule = stylesheet.rules[0] |
| 293 |
self.ae(rule.at_keyword, '@page') |
| 294 |
self.ae(rule.at_rules, []) # in CSS 2.1 |
| 295 |
result = ( |
| 296 |
rule.selector, |
| 297 |
rule.specificity, |
| 298 |
[(decl.name, list(jsonify(decl.value))) |
| 299 |
for decl in rule.declarations], |
| 300 |
) |
| 301 |
self.ae(result, expected_result) |
| 302 |
|
| 303 |
def test_at_media(self): |
| 304 |
for (css_source, expected_rules, expected_errors) in [ |
| 305 |
(' /* hey */\n', [], []), |
| 306 |
('@media {}', [(['all'], [])], []), |
| 307 |
('@media all {}', [(['all'], [])], []), |
| 308 |
('@media screen, print {}', [(['screen', 'print'], [])], []), |
| 309 |
('@media all;', [], ['invalid @media rule: missing block']), |
| 310 |
('@media 4 {}', [], ['expected a media type, got INTEGER']), |
| 311 |
('@media , screen {}', [], ['expected a media type']), |
| 312 |
('@media screen, {}', [], ['expected a media type']), |
| 313 |
('@media screen print {}', [], |
| 314 |
['expected a media type, got IDENT, IDENT']), |
| 315 |
|
| 316 |
('@media all { @page { a: 1 } @media; @import; foo { a: 1 } }', |
| 317 |
[(['all'], [('foo', [('a', [('INTEGER', 1)])])])], |
| 318 |
['@page rule not allowed in @media', |
| 319 |
'@media rule not allowed in @media', |
| 320 |
'@import rule not allowed in @media']), |
| 321 |
|
| 322 |
]: |
| 323 |
stylesheet = CSS21Parser().parse_stylesheet(css_source) |
| 324 |
self.assert_errors(stylesheet.errors, expected_errors) |
| 325 |
|
| 326 |
for rule in stylesheet.rules: |
| 327 |
self.ae(rule.at_keyword, '@media') |
| 328 |
result = [ |
| 329 |
(rule.media, [ |
| 330 |
(sub_rule.selector.as_css(), [ |
| 331 |
(decl.name, list(jsonify(decl.value))) |
| 332 |
for decl in sub_rule.declarations]) |
| 333 |
for sub_rule in rule.rules |
| 334 |
]) |
| 335 |
for rule in stylesheet.rules |
| 336 |
] |
| 337 |
self.ae(result, expected_rules) |