| 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, cssDOMElementInterface |
|---|
| 17 |
|
|---|
| 18 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 19 |
#~ Constants / Variables / Etc. |
|---|
| 20 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
document = """<?xml version='1.0' ?> |
|---|
| 23 |
<body> |
|---|
| 24 |
<h1>First</h1> |
|---|
| 25 |
<h2>Second</h2> |
|---|
| 26 |
</body> |
|---|
| 27 |
""" |
|---|
| 28 |
|
|---|
| 29 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 30 |
#~ Definitions |
|---|
| 31 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 32 |
|
|---|
| 33 |
def getCSSAttr(self, cssCascade, attrName, default=NotImplemented): |
|---|
| 34 |
"""We attach this method to xml.dom.minidom.Element""" |
|---|
| 35 |
if attrName in self.cssAttrs: |
|---|
| 36 |
return self.cssAttrs[attrName] |
|---|
| 37 |
|
|---|
| 38 |
result = None |
|---|
| 39 |
|
|---|
| 40 |
attrValue = self.attributes.get(attrName, None) |
|---|
| 41 |
if attrValue is not None: |
|---|
| 42 |
result = cssCascade.parser.parseSingleAttr(attrValue.value) |
|---|
| 43 |
|
|---|
| 44 |
if result is None: |
|---|
| 45 |
result = cssCascade.findStyleFor(self.cssElement, attrName, default) |
|---|
| 46 |
|
|---|
| 47 |
if result == 'inherit': |
|---|
| 48 |
if hasattr(self.parentNode, 'getCSSAttr'): |
|---|
| 49 |
result = self.parentNode.getCSSAttr(cssCascade, attrName, default) |
|---|
| 50 |
elif default is not NotImplemented: |
|---|
| 51 |
return default |
|---|
| 52 |
else: |
|---|
| 53 |
raise LookupError("Could not find inherited CSS attribute value for '%s'" % (attrName,)) |
|---|
| 54 |
|
|---|
| 55 |
self.cssAttrs[attrName] = result |
|---|
| 56 |
return result |
|---|
| 57 |
|
|---|
| 58 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 59 |
#~ Setup code derived from a demo |
|---|
| 60 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 61 |
|
|---|
| 62 |
# this is an informal schema that maps tag names to the css attributes they |
|---|
| 63 |
# should have |
|---|
| 64 |
tagToAttrNames = { |
|---|
| 65 |
'h1': 'color align'.split(), |
|---|
| 66 |
'h2': 'color align'.split(), |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
def visitElement(element, children, cssCascade): |
|---|
| 70 |
"""Visits individual elements, and sets css attributes on them""" |
|---|
| 71 |
element.cssElement = cssDOMElementInterface.CSSDOMElementInterface(element) |
|---|
| 72 |
element.cssAttrs = {} |
|---|
| 73 |
|
|---|
| 74 |
cssAttrMap = {} |
|---|
| 75 |
for cssAttrName in tagToAttrNames.get(element.tagName, []): |
|---|
| 76 |
cssAttrMap[cssAttrName] = element.getCSSAttr(cssCascade, cssAttrName) |
|---|
| 77 |
|
|---|
| 78 |
return (element.tagName, cssAttrMap, list(children)) |
|---|
| 79 |
|
|---|
| 80 |
def visitElementNodes(root, visit=visitElement, **kw): |
|---|
| 81 |
"""Visits all ELEMENT_NODEs of a dom recursively""" |
|---|
| 82 |
for node in root.childNodes: |
|---|
| 83 |
if node.nodeType == node.ELEMENT_NODE: |
|---|
| 84 |
children = visitElementNodes(node, visit, **kw) |
|---|
| 85 |
yield visit(node, children, **kw) |
|---|
| 86 |
|
|---|
| 87 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 88 |
#~ Unittest |
|---|
| 89 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 90 |
|
|---|
| 91 |
class TestCSSWithMinidom(unittest.TestCase): |
|---|
| 92 |
def setUp(self): |
|---|
| 93 |
# parse the document into a dom |
|---|
| 94 |
self.dom = xml.dom.minidom.parseString(document) |
|---|
| 95 |
|
|---|
| 96 |
# parse the stylesheet |
|---|
| 97 |
self.cssParser = css.CSSParser() |
|---|
| 98 |
# create the css cascade from the stylesheet |
|---|
| 99 |
self.cssCascade = css.CSSCascadeStrategy() |
|---|
| 100 |
self.cssCascade.parser = self.cssParser |
|---|
| 101 |
|
|---|
| 102 |
xml.dom.minidom.Element.getCSSAttr = getCSSAttr |
|---|
| 103 |
|
|---|
| 104 |
def tearDown(self): |
|---|
| 105 |
del xml.dom.minidom.Element.getCSSAttr |
|---|
| 106 |
|
|---|
| 107 |
def css(self, **kw): |
|---|
| 108 |
for cssOrigin, stylesheet in kw.iteritems(): |
|---|
| 109 |
setattr(self.cssCascade, cssOrigin, self.cssParser.parse(stylesheet)) |
|---|
| 110 |
return self.cssCascade |
|---|
| 111 |
|
|---|
| 112 |
def test_UserAgent_Author(self): |
|---|
| 113 |
css = self.css( |
|---|
| 114 |
userAgent='h1 { align: left; color: red } h2 { align: right; color: orange !important}', |
|---|
| 115 |
author='h1 { align: right; color: green } h2 { align: left !important; color: blue}') |
|---|
| 116 |
|
|---|
| 117 |
result = visitElementNodes(self.dom, cssCascade=css).next() |
|---|
| 118 |
headings = result[-1] |
|---|
| 119 |
self.assertEqual(result[0], 'body') |
|---|
| 120 |
self.assertEqual(headings[0], (u'h1', {'align': 'right', 'color': 'green'}, [])) |
|---|
| 121 |
self.assertEqual(headings[1], (u'h2', {'align': 'left', 'color': 'blue'}, [])) |
|---|
| 122 |
|
|---|
| 123 |
def test_Author_User(self): |
|---|
| 124 |
css = self.css( |
|---|
| 125 |
author='h1 { align: left; color: red } h2 { align: right; color: orange !important}', |
|---|
| 126 |
user='h1 { align: right; color: green } h2 { align: left !important; color: blue }') |
|---|
| 127 |
|
|---|
| 128 |
result = visitElementNodes(self.dom, cssCascade=css).next() |
|---|
| 129 |
headings = result[-1] |
|---|
| 130 |
self.assertEqual(result[0], 'body') |
|---|
| 131 |
self.assertEqual(headings[0], (u'h1', {'align': 'left', 'color': 'red'}, [])) |
|---|
| 132 |
self.assertEqual(headings[1], (u'h2', {'align': 'left', 'color': 'orange'}, [])) |
|---|
| 133 |
|
|---|
| 134 |
def test_UserAgent_User(self): |
|---|
| 135 |
css = self.css( |
|---|
| 136 |
userAgent='h1 { align: left; color: red } h2 { align: right; color: orange !important}', |
|---|
| 137 |
user='h1 { align: right; color: green } h2 { align: left !important; color: blue }') |
|---|
| 138 |
|
|---|
| 139 |
result = visitElementNodes(self.dom, cssCascade=css).next() |
|---|
| 140 |
headings = result[-1] |
|---|
| 141 |
self.assertEqual(result[0], 'body') |
|---|
| 142 |
self.assertEqual(headings[0], (u'h1', {'align': 'right', 'color': 'green'}, [])) |
|---|
| 143 |
self.assertEqual(headings[1], (u'h2', {'align': 'left', 'color': 'blue'}, [])) |
|---|
| 144 |
|
|---|
| 145 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 146 |
#~ Unittest Main |
|---|
| 147 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 148 |
|
|---|
| 149 |
if __name__=='__main__': |
|---|
| 150 |
unittest.main() |
|---|
| 151 |
|
|---|