| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ Copyright (C) 2002-2004 TechGame Networks, LLC. |
|---|
| 4 |
##~ |
|---|
| 5 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 6 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 7 |
##~ LICENSE file included with this distribution. |
|---|
| 8 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 9 |
|
|---|
| 10 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 11 |
#~ Imports |
|---|
| 12 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 13 |
|
|---|
| 14 |
import unittest |
|---|
| 15 |
import xml.dom.minidom |
|---|
| 16 |
from TG.w3c import css |
|---|
| 17 |
from TG.w3c.cssDOMElementInterface import CSSDOMElementInterface |
|---|
| 18 |
|
|---|
| 19 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 20 |
#~ Constants / Variables / Etc. |
|---|
| 21 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 22 |
|
|---|
| 23 |
stylesheet = """ |
|---|
| 24 |
* { |
|---|
| 25 |
font-size: medium; |
|---|
| 26 |
bgcolor: orange; |
|---|
| 27 |
fgcolor: blue; |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
slideshow { |
|---|
| 31 |
bgcolor: black; |
|---|
| 32 |
fgcolor: white; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
slideshow * { |
|---|
| 36 |
fgcolor: inherit; |
|---|
| 37 |
bgcolor: inherit; |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
slideshow > title { |
|---|
| 41 |
font-size: 80pt; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
slide { |
|---|
| 45 |
image: url('page-image.png'); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
slide:last-child { |
|---|
| 49 |
fgcolor: yellow; |
|---|
| 50 |
image: url('last-page-image.png'); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
slide > title { |
|---|
| 54 |
font-size: 40pt; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
point { |
|---|
| 58 |
font-size: medium; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
point#special { |
|---|
| 62 |
font-size: xx-large; |
|---|
| 63 |
fgcolor: red; |
|---|
| 64 |
bgcolor: yellow; |
|---|
| 65 |
} |
|---|
| 66 |
""" |
|---|
| 67 |
|
|---|
| 68 |
document = """<?xml version='1.0' ?> |
|---|
| 69 |
<!-- Derived from 13.7.2 DOM Example of the Python 2.3 Documentation --> |
|---|
| 70 |
<slideshow> |
|---|
| 71 |
<title>Demo slideshow combined with CSS</title> |
|---|
| 72 |
|
|---|
| 73 |
<slide> |
|---|
| 74 |
<title>Intro CSS Slide</title> |
|---|
| 75 |
<point>This is a CSS demo for xml.dom</point> |
|---|
| 76 |
<point>for processing slides with CSS!</point> |
|---|
| 77 |
</slide> |
|---|
| 78 |
|
|---|
| 79 |
<slide> |
|---|
| 80 |
<title>Another CSS demo slide</title> |
|---|
| 81 |
|
|---|
| 82 |
<point>It is important</point> |
|---|
| 83 |
<point>To have more than</point> |
|---|
| 84 |
<point>one slide</point> |
|---|
| 85 |
</slide> |
|---|
| 86 |
|
|---|
| 87 |
<slide> |
|---|
| 88 |
<title>Last CSS demo slide title</title> |
|---|
| 89 |
<point id='special'>Thanks for looking at CSS!</point> |
|---|
| 90 |
</slide> |
|---|
| 91 |
|
|---|
| 92 |
</slideshow> |
|---|
| 93 |
""" |
|---|
| 94 |
|
|---|
| 95 |
documentParsedWithCSS = (u'slideshow', |
|---|
| 96 |
{'bgcolor': 'black', 'fgcolor': 'white'}, |
|---|
| 97 |
[(u'title', |
|---|
| 98 |
{'bgcolor': 'black', 'font-size': ('80', 'pt'), 'fgcolor': 'white'}, |
|---|
| 99 |
[]), |
|---|
| 100 |
(u'slide', |
|---|
| 101 |
{'bgcolor': 'black', 'image': 'page-image.png', 'fgcolor': 'white'}, |
|---|
| 102 |
[(u'title', |
|---|
| 103 |
{'bgcolor': 'black', 'font-size': ('40', 'pt'), 'fgcolor': 'white'}, |
|---|
| 104 |
[]), |
|---|
| 105 |
(u'point', |
|---|
| 106 |
{'bgcolor': 'black', 'font-size': 'medium', 'fgcolor': 'white'}, |
|---|
| 107 |
[]), |
|---|
| 108 |
(u'point', |
|---|
| 109 |
{'bgcolor': 'black', 'font-size': 'medium', 'fgcolor': 'white'}, |
|---|
| 110 |
[])]), |
|---|
| 111 |
(u'slide', |
|---|
| 112 |
{'bgcolor': 'black', 'image': 'page-image.png', 'fgcolor': 'white'}, |
|---|
| 113 |
[(u'title', |
|---|
| 114 |
{'bgcolor': 'black', 'font-size': ('40', 'pt'), 'fgcolor': 'white'}, |
|---|
| 115 |
[]), |
|---|
| 116 |
(u'point', |
|---|
| 117 |
{'bgcolor': 'black', 'font-size': 'medium', 'fgcolor': 'white'}, |
|---|
| 118 |
[]), |
|---|
| 119 |
(u'point', |
|---|
| 120 |
{'bgcolor': 'black', 'font-size': 'medium', 'fgcolor': 'white'}, |
|---|
| 121 |
[]), |
|---|
| 122 |
(u'point', |
|---|
| 123 |
{'bgcolor': 'black', 'font-size': 'medium', 'fgcolor': 'white'}, |
|---|
| 124 |
[])]), |
|---|
| 125 |
(u'slide', |
|---|
| 126 |
{'bgcolor': 'black', 'image': 'last-page-image.png', 'fgcolor': 'yellow'}, |
|---|
| 127 |
[(u'title', |
|---|
| 128 |
{'bgcolor': 'black', 'font-size': ('40', 'pt'), 'fgcolor': 'yellow'}, |
|---|
| 129 |
[]), |
|---|
| 130 |
(u'point', |
|---|
| 131 |
{'bgcolor': 'yellow', 'font-size': 'xx-large', 'fgcolor': 'red'}, |
|---|
| 132 |
[]) |
|---|
| 133 |
] |
|---|
| 134 |
) |
|---|
| 135 |
] |
|---|
| 136 |
) |
|---|
| 137 |
|
|---|
| 138 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 139 |
#~ Definitions |
|---|
| 140 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 141 |
|
|---|
| 142 |
def getCSSAttr(self, cssCascade, attrName, default=NotImplemented): |
|---|
| 143 |
if attrName in self.cssAttrs: |
|---|
| 144 |
return self.cssAttrs[attrName] |
|---|
| 145 |
|
|---|
| 146 |
result = None |
|---|
| 147 |
|
|---|
| 148 |
attrValue = self.attributes.get(attrName, None) |
|---|
| 149 |
if attrValue is not None: |
|---|
| 150 |
result = cssCascade.parser.parseSingleAttr(attrValue.value) |
|---|
| 151 |
|
|---|
| 152 |
if result is None: |
|---|
| 153 |
result = cssCascade.findStyleFor(self.cssElement, attrName, default) |
|---|
| 154 |
|
|---|
| 155 |
if result == 'inherit': |
|---|
| 156 |
if hasattr(self.parentNode, 'getCSSAttr'): |
|---|
| 157 |
result = self.parentNode.getCSSAttr(cssCascade, attrName, default) |
|---|
| 158 |
elif default is not NotImplemented: |
|---|
| 159 |
return default |
|---|
| 160 |
else: |
|---|
| 161 |
raise LookupError("Could not find inherited CSS attribute value for '%s'" % (attrName,)) |
|---|
| 162 |
|
|---|
| 163 |
self.cssAttrs[attrName] = result |
|---|
| 164 |
return result |
|---|
| 165 |
|
|---|
| 166 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 167 |
#~ Setup code derived from a demo |
|---|
| 168 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 169 |
|
|---|
| 170 |
# this is an informal schema that maps tag names to the css attributes they |
|---|
| 171 |
# should have |
|---|
| 172 |
tagToAttrNames = { |
|---|
| 173 |
'slideshow': 'fgcolor bgcolor'.split(), |
|---|
| 174 |
'title': 'font-size fgcolor bgcolor'.split(), |
|---|
| 175 |
'slide': 'image fgcolor bgcolor'.split(), |
|---|
| 176 |
'point': 'font-size fgcolor bgcolor'.split(), |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
def visitElement(element, children, cssCascade): |
|---|
| 180 |
"""Visits individual elements, and sets css attributes on them""" |
|---|
| 181 |
element.cssElement = CSSDOMElementInterface(element) |
|---|
| 182 |
element.cssAttrs = {} |
|---|
| 183 |
|
|---|
| 184 |
cssAttrMap = {} |
|---|
| 185 |
for cssAttrName in tagToAttrNames.get(element.tagName, []): |
|---|
| 186 |
cssAttrMap[cssAttrName] = element.getCSSAttr(cssCascade, cssAttrName) |
|---|
| 187 |
|
|---|
| 188 |
return (element.tagName, cssAttrMap, list(children)) |
|---|
| 189 |
|
|---|
| 190 |
def visitElementNodes(root, visit=visitElement, **kw): |
|---|
| 191 |
"""Visits all ELEMENT_NODEs of a dom recursively""" |
|---|
| 192 |
for node in root.childNodes: |
|---|
| 193 |
if node.nodeType == node.ELEMENT_NODE: |
|---|
| 194 |
children = visitElementNodes(node, visit, **kw) |
|---|
| 195 |
yield visit(node, children, **kw) |
|---|
| 196 |
|
|---|
| 197 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 198 |
#~ Unittest |
|---|
| 199 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 200 |
|
|---|
| 201 |
class TestCSSWithMinidom(unittest.TestCase): |
|---|
| 202 |
def setUp(self): |
|---|
| 203 |
# parse the document into a dom |
|---|
| 204 |
self.dom = xml.dom.minidom.parseString(document) |
|---|
| 205 |
|
|---|
| 206 |
# parse the stylesheet |
|---|
| 207 |
cssParser = css.CSSParser() |
|---|
| 208 |
authorSS = cssParser.parse(stylesheet) |
|---|
| 209 |
# create the css cascade from the stylesheet |
|---|
| 210 |
self.cssCascade = css.CSSCascadeStrategy(authorSS) |
|---|
| 211 |
self.cssCascade.parser = cssParser |
|---|
| 212 |
|
|---|
| 213 |
xml.dom.minidom.Element.getCSSAttr = getCSSAttr |
|---|
| 214 |
|
|---|
| 215 |
def tearDown(self): |
|---|
| 216 |
del xml.dom.minidom.Element.getCSSAttr |
|---|
| 217 |
|
|---|
| 218 |
def test(self): |
|---|
| 219 |
slideshow = visitElementNodes(self.dom, cssCascade=self.cssCascade).next() |
|---|
| 220 |
self.assertEqual(slideshow, documentParsedWithCSS) |
|---|
| 221 |
|
|---|
| 222 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 223 |
#~ Unittest Main |
|---|
| 224 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 225 |
|
|---|
| 226 |
if __name__=='__main__': |
|---|
| 227 |
unittest.main() |
|---|
| 228 |
|
|---|