[Checkins] SVN: Sandbox/malthe/chameleon.core/ Added namespace attribute support.
Malthe Borch
mborch at gmail.com
Tue Nov 25 09:20:12 EST 2008
Log message for revision 93340:
Added namespace attribute support.
Changed:
U Sandbox/malthe/chameleon.core/CHANGES.txt
U Sandbox/malthe/chameleon.core/src/chameleon/core/testing.py
U Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py
U Sandbox/malthe/chameleon.core/src/chameleon/core/utils.py
-=-
Modified: Sandbox/malthe/chameleon.core/CHANGES.txt
===================================================================
--- Sandbox/malthe/chameleon.core/CHANGES.txt 2008-11-25 14:07:18 UTC (rev 93339)
+++ Sandbox/malthe/chameleon.core/CHANGES.txt 2008-11-25 14:20:12 UTC (rev 93340)
@@ -4,6 +4,8 @@
HEAD
~~~~
+- Added namespace attribute support. [malthe]
+
- Static attributes are now computed such that attributes are omitted
according to a `ns_omit` flag, and they are always prefixed
according to the element's `nsmap`. [malthe]
Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/testing.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/testing.py 2008-11-25 14:07:18 UTC (rev 93339)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/testing.py 2008-11-25 14:20:12 UTC (rev 93340)
@@ -55,6 +55,10 @@
class MockElement(translation.Element):
class Node(translation.Node):
+ ns_omit = (
+ "http://xml.zope.org/namespaces/meta",
+ "http://www.w3.org/2001/XInclude")
+
def __getattr__(self, name):
return None
Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py 2008-11-25 14:07:18 UTC (rev 93339)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py 2008-11-25 14:20:12 UTC (rev 93340)
@@ -44,8 +44,7 @@
dict_attributes = None
dynamic_attributes = utils.emptydict()
- ns_omit = (
- "http://xml.zope.org/namespaces/meta")
+ ns_omit = "http://xml.zope.org/namespaces/meta",
def __init__(self, element):
self.element = element
@@ -73,6 +72,23 @@
return (self.element.tail,)
@property
+ def ns_attributes(self):
+ prefix_omit = set()
+ namespaces = self.element.nsmap.values()
+
+ parent = self.element.getparent()
+ while parent is not None:
+ for prefix, ns in parent.nsmap.items():
+ if ns in namespaces:
+ prefix_omit.add(prefix)
+ parent = parent.getparent()
+
+ return dict(
+ ((prefix and "xmlns:%s" % prefix or "xmlns", ns) for (prefix, ns) in \
+ self.element.nsmap.items() if \
+ ns not in self.ns_omit and prefix not in prefix_omit)
+
+ @property
def static_attributes(self):
result = {}
@@ -86,7 +102,11 @@
result["%s:%s" % (prefix, name)] = value
else:
result[name] = value
-
+
+ if self.element.prefix is None:
+ result.update(
+ utils.get_attributes_from_namespace(self.element, None))
+
return result
@property
@@ -213,6 +233,7 @@
# static attributes are at the bottom of the food chain
attributes = utils.odict()
attributes.update(self.static_attributes)
+ attributes.update(self.ns_attributes)
# dynamic attributes
dynamic_attrs = self.dynamic_attributes or ()
@@ -677,13 +698,6 @@
else:
del element.attrib[utils.metal_attr('define-macro')]
- if macro is None or 'include_ns_attribute' in parameters:
- # is root element carries a namespace, add 'xmlns'
- # attribute to it
- if '}' in self.root.tag:
- namespace = self.root.tag.split('}')[0][1:]
- self.root.attrib['xmlns'] = namespace
-
if global_scope:
wrapper = generation.template_wrapper
else:
Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/utils.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/utils.py 2008-11-25 14:07:18 UTC (rev 93339)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/utils.py 2008-11-25 14:20:12 UTC (rev 93340)
@@ -200,7 +200,7 @@
except TypeError:
raise TypeError(
"Can only repeat over an iterable object (%s)." % iterable)
-
+
self[key] = (iterator, length)
return iterator
@@ -284,10 +284,6 @@
attrs = dict([
(name, value) for (name, value) in element.attrib.items() \
if name.startswith('{%s}' % namespace)])
-
- if namespace == config.XHTML_NS and element.prefix is None:
- attrs.update(get_attributes_from_namespace(
- element, None))
return attrs
More information about the Checkins
mailing list