| 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 |
from tinycss.media3 import CSSMedia3Parser |
| 9 |
from tinycss.media3 import MediaQuery as MQ |
| 10 |
from tinycss.tests import BaseTest, jsonify |
| 11 |
|
| 12 |
|
| 13 |
def jsonify_expr(e): |
| 14 |
if e is None: |
| 15 |
return None |
| 16 |
return next(jsonify([e])) |
| 17 |
|
| 18 |
def jsonify_expressions(mqlist): |
| 19 |
for mq in mqlist: |
| 20 |
mq.expressions = tuple( |
| 21 |
(k, jsonify_expr(e)) for k, e in mq.expressions) |
| 22 |
return mqlist |
| 23 |
|
| 24 |
class TestFonts3(BaseTest): |
| 25 |
|
| 26 |
def test_media_queries(self): |
| 27 |
'Test parsing of media queries from the CSS 3 media module' |
| 28 |
for css, media_query_list, expected_errors in [ |
| 29 |
# CSS 2.1 (simple media queries) |
| 30 |
('@media {}', [MQ()], []), |
| 31 |
('@media all {}', [MQ()], []), |
| 32 |
('@media screen {}', [MQ('screen')], []), |
| 33 |
('@media , screen {}', [MQ(), MQ('screen')], []), |
| 34 |
('@media screen, {}', [MQ('screen'), MQ()], []), |
| 35 |
|
| 36 |
# Examples from the CSS 3 specs |
| 37 |
('@media screen and (color) {}', [MQ('screen', (('color', None),))], []), |
| 38 |
('@media all and (min-width:500px) {}', [ |
| 39 |
MQ('all', (('min-width', ('DIMENSION', 500)),))], []), |
| 40 |
('@media (min-width:500px) {}', [ |
| 41 |
MQ('all', (('min-width', ('DIMENSION', 500)),))], []), |
| 42 |
('@media (orientation: portrait) {}', [ |
| 43 |
MQ('all', (('orientation', ('IDENT', 'portrait')),))], []), |
| 44 |
('@media screen and (color), projection and (color) {}', [ |
| 45 |
MQ('screen', (('color', None),)), MQ('projection', (('color', None),)),], []), |
| 46 |
('@media not screen and (color) {}', [ |
| 47 |
MQ('screen', (('color', None),), True)], []), |
| 48 |
('@media only screen and (color) {}', [ |
| 49 |
MQ('screen', (('color', None),))], []), |
| 50 |
('@media aural and (device-aspect-ratio: 16/9) {}', [ |
| 51 |
MQ('aural', (('device-aspect-ratio', ('RATIO', (16, 9))),))], []), |
| 52 |
('@media (resolution: 166dpi) {}', [ |
| 53 |
MQ('all', (('resolution', ('DIMENSION', 166)),))], []), |
| 54 |
('@media (min-resolution: 166DPCM) {}', [ |
| 55 |
MQ('all', (('min-resolution', ('DIMENSION', 166)),))], []), |
| 56 |
|
| 57 |
# Malformed media queries |
| 58 |
('@media (example, all,), speech {}', [MQ(negated=True), MQ('speech')], ['expected a :']), |
| 59 |
('@media &test, screen {}', [MQ(negated=True), MQ('screen')], ['expected a media expression not a DELIM']), |
| 60 |
|
| 61 |
]: |
| 62 |
stylesheet = CSSMedia3Parser().parse_stylesheet(css) |
| 63 |
self.assert_errors(stylesheet.errors, expected_errors) |
| 64 |
self.ae(len(stylesheet.rules), 1) |
| 65 |
rule = stylesheet.rules[0] |
| 66 |
self.ae(jsonify_expressions(rule.media), media_query_list) |
| 67 |
|