# HG changeset patch # User paulb # Date 1116251646 0 # Node ID bfa572d6bd0af953649f2411e0d7d8d63dd5d994 # Parent 861b2a08a959f3c96a982108c445ee8404850414 [project @ 2005-05-16 13:54:06 by paulb] Added setting of nodeValue, value and data. Removed attribute creation for now - libxml2 doesn't support "live" attribute nodes very well. Removed TemporaryNode. diff -r 861b2a08a959 -r bfa572d6bd0a libxml2dom/macrolib/macrolib.py --- a/libxml2dom/macrolib/macrolib.py Mon May 16 13:53:48 2005 +0000 +++ b/libxml2dom/macrolib/macrolib.py Mon May 16 13:54:06 2005 +0000 @@ -7,16 +7,6 @@ import xml.dom import libxml2mod -class TemporaryNode: - def __init__(self, ns, name): - self._ns = ns - self.name = name - self.type = "attribute" - self.nodeValue = None - - def ns(self): - return self._ns - # NOTE: libxml2 seems to use UTF-8 throughout. def from_unicode(s): @@ -95,6 +85,13 @@ def Node_nodeValue(node): return to_unicode(libxml2mod.xmlNodeGetContent(node)) +# NOTE: This is not properly exposed in the libxml2macro interface as the +# NOTE: writable form of nodeValue. + +def Node_setNodeValue(node, value): + # NOTE: Cannot set attribute node values. + libxml2mod.xmlNodeSetContent(node, from_unicode(value)) + # NOTE: Verify this. Node_data = Node_nodeValue @@ -154,11 +151,11 @@ def Node_getAttributeNodeNS(node, ns, localName): # NOTE: Needs verifying. - return libxml2mod.xmlGetNsProp(node, localName, ns) + return Node_attributes(node)[(ns, localName)] def Node_getAttributeNode(node, name): # NOTE: Needs verifying. - return libxml2mod.xmlGetProp(node, name) + return Node_attributes(node)[(None, name)] def Node_setAttributeNS(node, ns, name, value): # NOTE: Need to convert from Unicode. @@ -183,23 +180,13 @@ libxml2mod.xmlSetProp(node, name, value) -def _add_node(node, tmp): - if tmp.ns is not None: - child = libxml2mod.xmlNewNsProp(node, None, Node_localName(tmp), None) - ns = libxml2mod.xmlNewNs(child, Node_namespaceURI(tmp), Node_prefix(tmp)) - libxml2mod.xmlNodeSetNs(child, ns) - else: - child = libxml2mod.xmlNewProp(node, libxml2mod.name(tmp)) +def Node_setAttributeNodeNS(node, attr): + # NOTE: Not actually putting the node on the element. + Node_setAttributeNS(node, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) - return child - -def Node_setAttributeNodeNS(node, ns, name, attr): +def Node_setAttributeNode(node, attr): # NOTE: Not actually putting the node on the element. - Node_setAttributeNS(node, ns, name, attr.nodeValue) # Node_nodeValue(attr) - -def Node_setAttributeNode(node, name, attr): - # NOTE: Not actually putting the node on the element. - Node_setAttribute(node, name, attr.nodeValue) # Node_nodeValue(attr) + Node_setAttribute(node, Node_nodeName(attr), Node_nodeValue(attr)) def Node_createElementNS(node, ns, name): # NOTE: Need to convert from Unicode. @@ -221,22 +208,28 @@ return new_node def Node_createAttributeNS(node, ns, name): + raise NotImplementedError, "Node_createAttributeNS" + # NOTE: Need to convert from Unicode. ns, name = map(from_unicode, [ns, name]) prefix, localName = _get_prefix_and_localName(name) # NOTE: Does it make sense to set the namespace if it is empty? if ns is not None: - new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix) + new_ns = libxml2mod.xmlNewNs(node, ns, prefix) else: new_ns = None - return TemporaryNode(new_ns, localName) + new_node = libxml2mod.xmlNewNsProp(node, new_ns, name, None) + return new_node def Node_createAttribute(node, name): + raise NotImplementedError, "Node_createAttribute" + # NOTE: Need to convert from Unicode. name = from_unicode(name) - return TemporaryNode(None, name) + # NOTE: xmlNewProp does not seem to work. + return Node_createAttributeNS(node, None, name) def Node_createTextNode(node, value): # NOTE: Need to convert from Unicode. @@ -251,16 +244,10 @@ return libxml2mod.xmlNewComment(value) def Node_insertBefore(node, tmp, oldNode): - if not isinstance(tmp, TemporaryNode): - return libxml2mod.xmlAddPrevSibling(oldNode, tmp) - else: - return None + return libxml2mod.xmlAddPrevSibling(oldNode, tmp) def Node_replaceChild(node, tmp, oldNode): - if not isinstance(tmp, TemporaryNode): - return libxml2mod.xmlReplaceNode(oldNode, tmp) - else: - return None + return libxml2mod.xmlReplaceNode(oldNode, tmp) def Node_appendChild(node, tmp): return libxml2mod.xmlAddChild(node, tmp)