1.1 --- a/libxml2dom/macrolib/macrolib.py Mon May 16 13:53:48 2005 +0000
1.2 +++ b/libxml2dom/macrolib/macrolib.py Mon May 16 13:54:06 2005 +0000
1.3 @@ -7,16 +7,6 @@
1.4 import xml.dom
1.5 import libxml2mod
1.6
1.7 -class TemporaryNode:
1.8 - def __init__(self, ns, name):
1.9 - self._ns = ns
1.10 - self.name = name
1.11 - self.type = "attribute"
1.12 - self.nodeValue = None
1.13 -
1.14 - def ns(self):
1.15 - return self._ns
1.16 -
1.17 # NOTE: libxml2 seems to use UTF-8 throughout.
1.18
1.19 def from_unicode(s):
1.20 @@ -95,6 +85,13 @@
1.21 def Node_nodeValue(node):
1.22 return to_unicode(libxml2mod.xmlNodeGetContent(node))
1.23
1.24 +# NOTE: This is not properly exposed in the libxml2macro interface as the
1.25 +# NOTE: writable form of nodeValue.
1.26 +
1.27 +def Node_setNodeValue(node, value):
1.28 + # NOTE: Cannot set attribute node values.
1.29 + libxml2mod.xmlNodeSetContent(node, from_unicode(value))
1.30 +
1.31 # NOTE: Verify this.
1.32
1.33 Node_data = Node_nodeValue
1.34 @@ -154,11 +151,11 @@
1.35
1.36 def Node_getAttributeNodeNS(node, ns, localName):
1.37 # NOTE: Needs verifying.
1.38 - return libxml2mod.xmlGetNsProp(node, localName, ns)
1.39 + return Node_attributes(node)[(ns, localName)]
1.40
1.41 def Node_getAttributeNode(node, name):
1.42 # NOTE: Needs verifying.
1.43 - return libxml2mod.xmlGetProp(node, name)
1.44 + return Node_attributes(node)[(None, name)]
1.45
1.46 def Node_setAttributeNS(node, ns, name, value):
1.47 # NOTE: Need to convert from Unicode.
1.48 @@ -183,23 +180,13 @@
1.49
1.50 libxml2mod.xmlSetProp(node, name, value)
1.51
1.52 -def _add_node(node, tmp):
1.53 - if tmp.ns is not None:
1.54 - child = libxml2mod.xmlNewNsProp(node, None, Node_localName(tmp), None)
1.55 - ns = libxml2mod.xmlNewNs(child, Node_namespaceURI(tmp), Node_prefix(tmp))
1.56 - libxml2mod.xmlNodeSetNs(child, ns)
1.57 - else:
1.58 - child = libxml2mod.xmlNewProp(node, libxml2mod.name(tmp))
1.59 +def Node_setAttributeNodeNS(node, attr):
1.60 + # NOTE: Not actually putting the node on the element.
1.61 + Node_setAttributeNS(node, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr))
1.62
1.63 - return child
1.64 -
1.65 -def Node_setAttributeNodeNS(node, ns, name, attr):
1.66 +def Node_setAttributeNode(node, attr):
1.67 # NOTE: Not actually putting the node on the element.
1.68 - Node_setAttributeNS(node, ns, name, attr.nodeValue) # Node_nodeValue(attr)
1.69 -
1.70 -def Node_setAttributeNode(node, name, attr):
1.71 - # NOTE: Not actually putting the node on the element.
1.72 - Node_setAttribute(node, name, attr.nodeValue) # Node_nodeValue(attr)
1.73 + Node_setAttribute(node, Node_nodeName(attr), Node_nodeValue(attr))
1.74
1.75 def Node_createElementNS(node, ns, name):
1.76 # NOTE: Need to convert from Unicode.
1.77 @@ -221,22 +208,28 @@
1.78 return new_node
1.79
1.80 def Node_createAttributeNS(node, ns, name):
1.81 + raise NotImplementedError, "Node_createAttributeNS"
1.82 +
1.83 # NOTE: Need to convert from Unicode.
1.84 ns, name = map(from_unicode, [ns, name])
1.85
1.86 prefix, localName = _get_prefix_and_localName(name)
1.87 # NOTE: Does it make sense to set the namespace if it is empty?
1.88 if ns is not None:
1.89 - new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix)
1.90 + new_ns = libxml2mod.xmlNewNs(node, ns, prefix)
1.91 else:
1.92 new_ns = None
1.93 - return TemporaryNode(new_ns, localName)
1.94 + new_node = libxml2mod.xmlNewNsProp(node, new_ns, name, None)
1.95 + return new_node
1.96
1.97 def Node_createAttribute(node, name):
1.98 + raise NotImplementedError, "Node_createAttribute"
1.99 +
1.100 # NOTE: Need to convert from Unicode.
1.101 name = from_unicode(name)
1.102
1.103 - return TemporaryNode(None, name)
1.104 + # NOTE: xmlNewProp does not seem to work.
1.105 + return Node_createAttributeNS(node, None, name)
1.106
1.107 def Node_createTextNode(node, value):
1.108 # NOTE: Need to convert from Unicode.
1.109 @@ -251,16 +244,10 @@
1.110 return libxml2mod.xmlNewComment(value)
1.111
1.112 def Node_insertBefore(node, tmp, oldNode):
1.113 - if not isinstance(tmp, TemporaryNode):
1.114 - return libxml2mod.xmlAddPrevSibling(oldNode, tmp)
1.115 - else:
1.116 - return None
1.117 + return libxml2mod.xmlAddPrevSibling(oldNode, tmp)
1.118
1.119 def Node_replaceChild(node, tmp, oldNode):
1.120 - if not isinstance(tmp, TemporaryNode):
1.121 - return libxml2mod.xmlReplaceNode(oldNode, tmp)
1.122 - else:
1.123 - return None
1.124 + return libxml2mod.xmlReplaceNode(oldNode, tmp)
1.125
1.126 def Node_appendChild(node, tmp):
1.127 return libxml2mod.xmlAddChild(node, tmp)