| 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 polyglot.builtins import error_message |
| 9 |
from tinycss.css21 import CSS21Parser |
| 10 |
from tinycss.parsing import ParseError, remove_whitespace, split_on_comma |
| 11 |
|
| 12 |
|
| 13 |
class MediaQuery: |
| 14 |
|
| 15 |
__slots__ = 'media_type', 'expressions', 'negated' |
| 16 |
|
| 17 |
def __init__(self, media_type='all', expressions=(), negated=False): |
| 18 |
self.media_type = media_type |
| 19 |
self.expressions = expressions |
| 20 |
self.negated = negated |
| 21 |
|
| 22 |
def __repr__(self): |
| 23 |
return '<MediaQuery type=%s negated=%s expressions=%s>' % ( |
| 24 |
self.media_type, self.negated, self.expressions) |
| 25 |
|
| 26 |
def __eq__(self, other): |
| 27 |
return self.media_type == getattr(other, 'media_type', None) and \ |
| 28 |
self.negated == getattr(other, 'negated', None) and \ |
| 29 |
self.expressions == getattr(other, 'expressions', None) |
| 30 |
|
| 31 |
|
| 32 |
class MalformedExpression(Exception): |
| 33 |
|
| 34 |
def __init__(self, tok, msg): |
| 35 |
Exception.__init__(self, msg) |
| 36 |
self.tok = tok |
| 37 |
|
| 38 |
|
| 39 |
class CSSMedia3Parser(CSS21Parser): |
| 40 |
|
| 41 |
''' Parse media queries as defined by the CSS 3 media module ''' |
| 42 |
|
| 43 |
def parse_media(self, tokens, errors): |
| 44 |
if not tokens: |
| 45 |
return [MediaQuery('all')] |
| 46 |
queries = [] |
| 47 |
|
| 48 |
for part in split_on_comma(remove_whitespace(tokens)): |
| 49 |
negated = False |
| 50 |
media_type = None |
| 51 |
expressions = [] |
| 52 |
try: |
| 53 |
for i, tok in enumerate(part): |
| 54 |
if i == 0 and tok.type == 'IDENT': |
| 55 |
val = tok.value.lower() |
| 56 |
if val == 'only': |
| 57 |
continue # ignore leading ONLY |
| 58 |
if val == 'not': |
| 59 |
negated = True |
| 60 |
continue |
| 61 |
if media_type is None and tok.type == 'IDENT': |
| 62 |
media_type = tok.value |
| 63 |
continue |
| 64 |
elif media_type is None: |
| 65 |
media_type = 'all' |
| 66 |
|
| 67 |
if tok.type == 'IDENT' and tok.value.lower() == 'and': |
| 68 |
continue |
| 69 |
if not tok.is_container: |
| 70 |
raise MalformedExpression(tok, 'expected a media expression not a %s' % tok.type) |
| 71 |
if tok.type != '(': |
| 72 |
raise MalformedExpression(tok, 'media expressions must be in parentheses not %s' % tok.type) |
| 73 |
content = remove_whitespace(tok.content) |
| 74 |
if len(content) == 0: |
| 75 |
raise MalformedExpression(tok, 'media expressions cannot be empty') |
| 76 |
if content[0].type != 'IDENT': |
| 77 |
raise MalformedExpression(content[0], 'expected a media feature not a %s' % tok.type) |
| 78 |
media_feature, expr = content[0].value, None |
| 79 |
if len(content) > 1: |
| 80 |
if len(content) < 3: |
| 81 |
raise MalformedExpression(content[1], 'malformed media feature definition') |
| 82 |
if content[1].type != ':': |
| 83 |
raise MalformedExpression(content[1], 'expected a :') |
| 84 |
expr = content[2:] |
| 85 |
if len(expr) == 1: |
| 86 |
expr = expr[0] |
| 87 |
elif len(expr) == 3 and (expr[0].type, expr[1].type, expr[1].value, expr[2].type) == ( |
| 88 |
'INTEGER', 'DELIM', '/', 'INTEGER'): |
| 89 |
# This should really be moved into token_data, but |
| 90 |
# since RATIO is not part of CSS 2.1 and does not |
| 91 |
# occur anywhere else, we special case it here. |
| 92 |
r = expr[0] |
| 93 |
r.value = (expr[0].value, expr[2].value) |
| 94 |
r.type = 'RATIO' |
| 95 |
r._as_css = expr[0]._as_css + expr[1]._as_css + expr[2]._as_css |
| 96 |
expr = r |
| 97 |
else: |
| 98 |
raise MalformedExpression(expr[0], 'malformed media feature definition') |
| 99 |
|
| 100 |
expressions.append((media_feature, expr)) |
| 101 |
except MalformedExpression as err: |
| 102 |
errors.append(ParseError(err.tok, error_message(err))) |
| 103 |
media_type, negated, expressions = 'all', True, () |
| 104 |
queries.append(MediaQuery(media_type or 'all', expressions=tuple(expressions), negated=negated)) |
| 105 |
|
| 106 |
return queries |