root/trunk/proj/w3c/cssDOMElementInterface.py

Revision 404, 3.7 kB (checked in by sholloway, 5 years ago)

Significant refactoring for CSSParser and CSSCascadeStrategy acquisition from SkinModel? instead of xmlSkinner.
Transitioned use of "href" to simply "ref"
Tested the wx skinning demos, fixing some
All tests pass.

Line 
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 from TG.w3c import css
15
16 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 #~ Definitions
18 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19
20 class CSSDOMElementInterface(css.CSSElementInterfaceBase):
21     """An implementation of css.CSSElementInterfaceBase for xml.dom Element Nodes"""
22
23     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24     #~ Constants / Variables / Etc.
25     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26
27     style = None
28
29     _pseudoStateHandlerLookup = {
30         'first-child':
31             lambda self: not bool(self.getPreviousSibling()),
32         'not-first-child':
33             lambda self: bool(self.getPreviousSibling()),
34
35         'last-child':
36             lambda self: not bool(self.getNextSibling()),
37         'not-last-child':
38             lambda self: bool(self.getNextSibling()),
39
40         'middle-child':
41             lambda self: not bool(self.getPreviousSibling()) and not bool(self.getNextSibling()),
42         'not-middle-child':
43             lambda self: bool(self.getPreviousSibling()) or bool(self.getNextSibling()),
44         }
45
46     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47     #~ Definitions
48     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
50     def __init__(self, domElement):
51         self.domElement = domElement
52    
53     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54
55     def matchesNode(self, (namespace, tagName)):
56         if tagName not in ('*', self.domElement.tagName):
57             return False
58         if namespace in (None, '', '*'):
59             # matches any namespace
60             return True
61         else: # full compare
62             return namespace == self.domElement.namespaceURI
63
64     def getAttr(self, name, default=NotImplemented):
65         attrValue = self.domElement.attributes.get(name)
66         if attrValue is not None:
67             return attrValue.value
68         else:
69             return default
70
71     def inPseudoState(self, name, params=()):
72         handler = self._pseudoStateHandlerLookup.get(name, lambda self: False)
73         return handler(self)
74
75     def iterXMLParents(self, includeSelf=False):
76         klass = self.__class__
77         current = self.domElement
78         if not includeSelf:
79             current = current.parentNode
80         while (current is not None) and (current.nodeType == current.ELEMENT_NODE):
81             yield klass(current)
82             current = current.parentNode
83
84     def getPreviousSibling(self):
85         sibling = self.domElement.previousSibling
86         while sibling:
87             if sibling.nodeType == sibling.ELEMENT_NODE:
88                 return sibling
89             else:
90                 sibling = sibling.previousSibling
91         return None
92     def getNextSibling(self):
93         sibling = self.domElement.nextSibling
94         while sibling:
95             if sibling.nodeType == sibling.ELEMENT_NODE:
96                 return sibling
97             else:
98                 sibling = sibling.nextSibling
99         return None
100
101 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
103 class CSSDOMCascadeStrategy(css.CSSCascadeStrategy):
104     def _normalizeCSSElement(self, element):
105         if not isinstance(elemnet, CSSDOMElementInterface):
106             return
Note: See TracBrowser for help on using the browser.