/usr/share/pyshared/cssutils/stylesheets/mediaquery.py is in python-cssutils 0.9.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | """Implements a DOM for MediaQuery, see
http://www.w3.org/TR/css3-mediaqueries/.
A cssutils implementation, not defined in official DOM.
"""
__all__ = ['MediaQuery']
__docformat__ = 'restructuredtext'
__version__ = '$Id$'
import cssutils
import re
import xml.dom
class MediaQuery(cssutils.util.Base):
"""
A Media Query consists of one of :const:`MediaQuery.MEDIA_TYPES`
and one or more expressions involving media features.
Format::
media_query: [[only | not]? <media_type> [ and <expression> ]*]
| <expression> [ and <expression> ]*
expression: ( <media_feature> [: <value>]? )
media_type: all | braille | handheld | print |
projection | speech | screen | tty | tv | embossed
media_feature: width | min-width | max-width
| height | min-height | max-height
| device-width | min-device-width | max-device-width
| device-height | min-device-height | max-device-height
| device-aspect-ratio | min-device-aspect-ratio | max-device-aspect-ratio
| color | min-color | max-color
| color-index | min-color-index | max-color-index
| monochrome | min-monochrome | max-monochrome
| resolution | min-resolution | max-resolution
| scan | grid
"""
MEDIA_TYPES = [u'all', u'braille', u'embossed', u'handheld',
u'print', u'projection', u'screen', u'speech', u'tty', u'tv']
# From the HTML spec (see MediaQuery):
# "[...] character that isn't a US ASCII letter [a-zA-Z] (Unicode
# decimal 65-90, 97-122), digit [0-9] (Unicode hex 30-39), or hyphen (45)."
# so the following is a valid mediaType
__mediaTypeMatch = re.compile(ur'^[-a-zA-Z0-9]+$', re.U).match
def __init__(self, mediaText=None, readonly=False):
"""
:param mediaText:
unicodestring of parsable media
"""
super(MediaQuery, self).__init__()
self.seq = []
self._mediaType = u''
if mediaText:
self.mediaText = mediaText # sets self._mediaType too
self._readonly = readonly
def __repr__(self):
return "cssutils.stylesheets.%s(mediaText=%r)" % (
self.__class__.__name__, self.mediaText)
def __str__(self):
return "<cssutils.stylesheets.%s object mediaText=%r at 0x%x>" % (
self.__class__.__name__, self.mediaText, id(self))
def _getMediaText(self):
return cssutils.ser.do_stylesheets_mediaquery(self)
def _setMediaText(self, mediaText):
"""
:param mediaText:
a single media query string, e.g. ``print and (min-width: 25cm)``
:exceptions:
- :exc:`~xml.dom.SyntaxErr`:
Raised if the specified string value has a syntax error and is
unparsable.
- :exc:`~xml.dom.InvalidCharacterErr`:
Raised if the given mediaType is unknown.
- :exc:`~xml.dom.NoModificationAllowedErr`:
Raised if this media query is readonly.
"""
self._checkReadonly()
tokenizer = self._tokenize2(mediaText)
if not tokenizer:
self._log.error(u'MediaQuery: No MediaText given.')
else:
# for closures: must be a mutable
new = {'mediatype': None,
'wellformed': True }
def _ident_or_dim(expected, seq, token, tokenizer=None):
# only|not or mediatype or and
val = self._tokenvalue(token)
nval = self._normalize(val)
if expected.endswith('mediatype'):
if nval in (u'only', u'not'):
# only or not
seq.append(val)
return 'mediatype'
else:
# mediatype
new['mediatype'] = val
seq.append(val)
return 'and'
elif 'and' == nval and expected.startswith('and'):
seq.append(u'and')
return 'feature'
else:
new['wellformed'] = False
self._log.error(
u'MediaQuery: Unexpected syntax.', token=token)
return expected
def _char(expected, seq, token, tokenizer=None):
# starting a feature which basically is a CSS Property
# but may simply be a property name too
val = self._tokenvalue(token)
if val == u'(' and expected == 'feature':
proptokens = self._tokensupto2(
tokenizer, funcendonly=True)
if proptokens and u')' == self._tokenvalue(proptokens[-1]):
proptokens.pop()
property = cssutils.css.Property(_mediaQuery=True)
property.cssText = proptokens
seq.append(property)
return 'and or EOF'
else:
new['wellformed'] = False
self._log.error(
u'MediaQuery: Unexpected syntax, expected "and" but found "%s".' %
val, token)
return expected
# expected: only|not or mediatype, mediatype, feature, and
newseq = []
wellformed, expected = self._parse(expected='only|not or mediatype',
seq=newseq, tokenizer=tokenizer,
productions={'IDENT': _ident_or_dim, # e.g. "print"
'DIMENSION': _ident_or_dim, # e.g. "3d"
'CHAR': _char})
wellformed = wellformed and new['wellformed']
# post conditions
if not new['mediatype']:
wellformed = False
self._log.error(u'MediaQuery: No mediatype found: %s' %
self._valuestr(mediaText))
if wellformed:
# set
self.mediaType = new['mediatype']
self.seq = newseq
mediaText = property(_getMediaText, _setMediaText,
doc="The parsable textual representation of the media list.")
def _setMediaType(self, mediaType):
"""
:param mediaType:
one of :attr:`MEDIA_TYPES`
:exceptions:
- :exc:`~xml.dom.SyntaxErr`:
Raised if the specified string value has a syntax error and is
unparsable.
- :exc:`~xml.dom.InvalidCharacterErr`:
Raised if the given mediaType is unknown.
- :exc:`~xml.dom.NoModificationAllowedErr`:
Raised if this media query is readonly.
"""
self._checkReadonly()
nmediaType = self._normalize(mediaType)
if not MediaQuery.__mediaTypeMatch(nmediaType):
self._log.error(
u'MediaQuery: Syntax Error in media type "%s".' % mediaType,
error=xml.dom.SyntaxErr)
else:
if nmediaType not in MediaQuery.MEDIA_TYPES:
self._log.warn(
u'MediaQuery: Unknown media type "%s".' % mediaType,
error=xml.dom.InvalidCharacterErr)
return
# set
self._mediaType = mediaType
# update seq
for i, x in enumerate(self.seq):
if isinstance(x, basestring):
if self._normalize(x) in (u'only', u'not'):
continue
else:
self.seq[i] = mediaType
break
else:
self.seq.insert(0, mediaType)
mediaType = property(lambda self: self._mediaType, _setMediaType,
doc="The media type of this MediaQuery (one of "
":attr:`MEDIA_TYPES`).")
wellformed = property(lambda self: bool(len(self.seq)))
|