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
|
"""website example tests
Log output cannot be tested!
#doctest: +ELLIPSIS
"""
import cssutils
cssutils.ser.prefs.useDefaults()
def profile():
"""
>>> import cssutils
>>> sheet = cssutils.parseString('x { -test-custommacro: x }')
>>> print sheet.cssRules[0].style.getProperties()[0].valid
False
>>> M1 = {
... 'testvalue': 'x'
... }
>>> P1 = {
... '-test-tokenmacro': '({num}{w}){1,2}',
... '-test-macro': '{ident}|{percentage}',
... '-test-custommacro': '{testvalue}',
... # custom validation function
... '-test-funcval': lambda(v): int(v) > 0
... }
>>> cssutils.profile.addProfile('test', P1, M1)
>>> sheet = cssutils.parseString('x { -test-custommacro: x }')
>>> print sheet.cssRules[0].style.getProperties()[0].valid
True
"""
def cssparse_example():
"""
>>> import cssutils, logging
>>> cssutils.log.setLevel(logging.FATAL)
>>> sheet = cssutils.parseString('@import url(example.css); body { color: red }')
>>> # log output not shown
>>> print sheet.cssText
@import url(example.css);
body {
color: red
}
"""
def logging():
"""
>>> import cssutils, logging
>>> cssutils.log.setLevel(logging.FATAL)
>>> import logging, StringIO, cssutils
>>> mylog = StringIO.StringIO()
>>> h = logging.StreamHandler(mylog)
>>> h.setFormatter(logging.Formatter('%(levelname)s %(message)s'))
>>> cssutils.log.addHandler(h)
>>> cssutils.log.setLevel(logging.INFO)
>>> sheet = cssutils.parseString('a { x: 1; } @import "http://cthedot.de/not-present.css";')
>>> print mylog.getvalue()
WARNING Property: Unknown Property name. [1:5: x]
WARNING HTTPError opening url=http://cthedot.de/not-present.css: 404 Not Found
WARNING CSSImportRule: While processing imported style sheet href=http://cthedot.de/not-present.css: IOError('Cannot read Stylesheet.',)
ERROR CSSStylesheet: CSSImportRule not allowed here. [1:13: @import]
<BLANKLINE>
"""
def prefs():
"""
>>> import cssutils, logging
>>> cssutils.log.setLevel(logging.FATAL)
>>> css = '@import "example.css"; body { color: red }'
>>> sheet = cssutils.parseString(css)
>>> cssutils.ser.prefs.indent = 2*' '
>>> # used to set indentation string, default is 4*' '
>>> cssutils.ser.prefs.importHrefFormat = 'uri'
>>> # or 'string', defaults to the format used in parsed stylesheet
>>> cssutils.ser.prefs.lineNumbers = True
>>> print sheet.cssText
1: @import url(example.css);
2: body {
3: color: red
4: }
"""
def work_and_build():
"""
>>> import cssutils, logging
>>> cssutils.log.setLevel(logging.FATAL)
>>> cssutils.ser.prefs.useDefaults()
>>> # --- RESET ---
>>> from cssutils import css, stylesheets
>>> sheet = css.CSSStyleSheet()
>>> sheet.cssText = u'@import url(example.css) tv;'
>>> print sheet.cssText
@import url(example.css) tv;
>>> style = css.CSSStyleDeclaration()
>>> style['color'] = 'red' # until 0.9.5: setProperty(u'color', u'red')
>>> stylerule = css.CSSStyleRule(selectorText=u'body', style=style)
>>> sheet.add(stylerule) # use this from 0.9.5 which always succeeds
1
>>> # OR THIS IS THE OFFICIAL DOM METHOD IF YOU WANT TO USE IT:
>>> # sheet.insertRule(stylerule, 0) # try before @import
>>> # xml.dom.HierarchyRequestErr: CSSStylesheet: Found @charset, @import or @namespace before index 0.
>>> # sheet.insertRule(stylerule) # at end of rules, returns index
>>> print sheet.cssText
@import url(example.css) tv;
body {
color: red
}
>>> # returns if new Medium is wellformed and has been added
>>> sheet.cssRules[0].media.appendMedium('print')
True
>>> # returns the new Selector:
>>> sheet.cssRules[1].selectorList.appendSelector('a')
cssutils.css.Selector(selectorText=u'a')
>>> print sheet.cssText
@import url(example.css) tv, print;
body, a {
color: red
}
"""
def api_addons():
"""
>>> import cssutils, logging
>>> cssutils.log.setLevel(logging.FATAL)
>>> cssText = '''background: white url(paper.png) scroll; /* for all UAs */
... background: white url(ledger.png) fixed; /* for UAs that do fixed backgrounds */
... '''
>>> # omit comments for this example
>>> cssutils.ser.prefs.keepComments = False
>>> style = cssutils.css.CSSStyleDeclaration(cssText=cssText)
>>> print style.cssText
background: white url(paper.png) scroll;
background: white url(ledger.png) fixed;
>>> # work with properties:
>>> proplist = style.getProperties('background', all=True)
>>> proplist
[cssutils.css.Property(name='background', value=u'white url(paper.png) scroll', priority=u''), cssutils.css.Property(name='background', value=u'white url(ledger.png) fixed', priority=u'')]
>>> for prop in proplist: print prop.value
white url(paper.png) scroll
white url(ledger.png) fixed
>>> # overwrite the current property, to overwrite all iterate over proplist
>>> style['background'] = ('red', '!important')
>>> # importance in DOM ist 'important' but '!important' works too
>>> print style['background'], style.getPropertyPriority('background')
red important
>>> print style.cssText
background: white url(paper.png) scroll;
background: red !important;
>>> # output only "effective" properties
>>> cssutils.ser.prefs.keepAllProperties = False
>>> print style.cssText
background: red !important;
"""
if __name__ == '__main__':
import doctest
doctest.testmod()
|