Ezio Melotti | da4b5b8 | 2013-01-22 22:47:57 +0200 | [diff] [blame] | 1 | """Simple implementation of the Level 1 DOM. |
| 2 | |
| 3 | Namespaces and other minor Level 2 features are also supported. |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 5 | parse("foo.xml") |
Paul Prescod | 623511b | 2000-07-21 22:05:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 7 | parseString("<foo><bar/></foo>") |
Paul Prescod | 623511b | 2000-07-21 22:05:49 +0000 | [diff] [blame] | 8 | |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 9 | Todo: |
| 10 | ===== |
| 11 | * convenience methods for getting elements and text. |
| 12 | * more testing |
| 13 | * bring some of the writer and linearizer code into conformance with this |
| 14 | interface |
| 15 | * SAX 2 namespaces |
| 16 | """ |
| 17 | |
Guido van Rossum | 3e1f85e | 2007-07-27 18:03:11 +0000 | [diff] [blame] | 18 | import io |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 19 | import xml.dom |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | from xml.dom import EMPTY_NAMESPACE, EMPTY_PREFIX, XMLNS_NAMESPACE, domreg |
| 22 | from xml.dom.minicompat import * |
| 23 | from xml.dom.xmlbuilder import DOMImplementationLS, DocumentLS |
Fred Drake | 3ac6a09 | 2001-09-28 04:33:06 +0000 | [diff] [blame] | 24 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 25 | # This is used by the ID-cache invalidation checks; the list isn't |
| 26 | # actually complete, since the nodes being checked will never be the |
| 27 | # DOCUMENT_NODE or DOCUMENT_FRAGMENT_NODE. (The node being checked is |
| 28 | # the node being added or removed, not the node being modified.) |
| 29 | # |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 30 | _nodeTypes_with_children = (xml.dom.Node.ELEMENT_NODE, |
| 31 | xml.dom.Node.ENTITY_REFERENCE_NODE) |
Martin v. Löwis | 95700f7 | 2002-03-15 13:51:59 +0000 | [diff] [blame] | 32 | |
Fred Drake | 3ac6a09 | 2001-09-28 04:33:06 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 34 | class Node(xml.dom.Node): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 35 | namespaceURI = None # this is non-null only for elements and attributes |
Fred Drake | 575712e | 2001-09-28 20:25:45 +0000 | [diff] [blame] | 36 | parentNode = None |
| 37 | ownerDocument = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 38 | nextSibling = None |
| 39 | previousSibling = None |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 40 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 41 | prefix = EMPTY_PREFIX # non-null only for NS elements and attributes |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 42 | |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 43 | def __bool__(self): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 44 | return True |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 45 | |
Georg Brandl | fe99105 | 2009-09-16 15:54:04 +0000 | [diff] [blame] | 46 | def toxml(self, encoding=None): |
Martin v. Löwis | 7d650ca | 2002-06-30 15:05:00 +0000 | [diff] [blame] | 47 | return self.toprettyxml("", "", encoding) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 3e1f85e | 2007-07-27 18:03:11 +0000 | [diff] [blame] | 49 | def toprettyxml(self, indent="\t", newl="\n", encoding=None): |
Eli Bendersky | 8a80502 | 2012-07-13 09:52:39 +0300 | [diff] [blame] | 50 | if encoding is None: |
| 51 | writer = io.StringIO() |
| 52 | else: |
| 53 | writer = io.TextIOWrapper(io.BytesIO(), |
| 54 | encoding=encoding, |
| 55 | errors="xmlcharrefreplace", |
| 56 | newline='\n') |
Martin v. Löwis | 7d650ca | 2002-06-30 15:05:00 +0000 | [diff] [blame] | 57 | if self.nodeType == Node.DOCUMENT_NODE: |
| 58 | # Can pass encoding only to document, to put it into XML header |
| 59 | self.writexml(writer, "", indent, newl, encoding) |
| 60 | else: |
| 61 | self.writexml(writer, "", indent, newl) |
Guido van Rossum | 3e1f85e | 2007-07-27 18:03:11 +0000 | [diff] [blame] | 62 | if encoding is None: |
Eli Bendersky | 8a80502 | 2012-07-13 09:52:39 +0300 | [diff] [blame] | 63 | return writer.getvalue() |
Guido van Rossum | 3e1f85e | 2007-07-27 18:03:11 +0000 | [diff] [blame] | 64 | else: |
Eli Bendersky | 8a80502 | 2012-07-13 09:52:39 +0300 | [diff] [blame] | 65 | return writer.detach().getvalue() |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 66 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 67 | def hasChildNodes(self): |
Florent Xicluna | 8cf4b51 | 2012-03-05 12:37:02 +0100 | [diff] [blame] | 68 | return bool(self.childNodes) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 69 | |
| 70 | def _get_childNodes(self): |
| 71 | return self.childNodes |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 72 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 73 | def _get_firstChild(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 74 | if self.childNodes: |
| 75 | return self.childNodes[0] |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 76 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 77 | def _get_lastChild(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 78 | if self.childNodes: |
| 79 | return self.childNodes[-1] |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 80 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 81 | def insertBefore(self, newChild, refChild): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 82 | if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
Fred Drake | e50959a | 2001-12-06 04:32:18 +0000 | [diff] [blame] | 83 | for c in tuple(newChild.childNodes): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 84 | self.insertBefore(c, refChild) |
| 85 | ### The DOM does not clearly specify what to return in this case |
| 86 | return newChild |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 87 | if newChild.nodeType not in self._child_node_types: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 88 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 89 | "%s cannot be child of %s" % (repr(newChild), repr(self))) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 90 | if newChild.parentNode is not None: |
| 91 | newChild.parentNode.removeChild(newChild) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 92 | if refChild is None: |
| 93 | self.appendChild(newChild) |
| 94 | else: |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 95 | try: |
| 96 | index = self.childNodes.index(refChild) |
| 97 | except ValueError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 98 | raise xml.dom.NotFoundErr() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 99 | if newChild.nodeType in _nodeTypes_with_children: |
| 100 | _clear_id_cache(self) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 101 | self.childNodes.insert(index, newChild) |
| 102 | newChild.nextSibling = refChild |
| 103 | refChild.previousSibling = newChild |
| 104 | if index: |
| 105 | node = self.childNodes[index-1] |
| 106 | node.nextSibling = newChild |
| 107 | newChild.previousSibling = node |
| 108 | else: |
| 109 | newChild.previousSibling = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 110 | newChild.parentNode = self |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 111 | return newChild |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 112 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 113 | def appendChild(self, node): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 114 | if node.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
Fred Drake | e50959a | 2001-12-06 04:32:18 +0000 | [diff] [blame] | 115 | for c in tuple(node.childNodes): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 116 | self.appendChild(c) |
| 117 | ### The DOM does not clearly specify what to return in this case |
| 118 | return node |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 119 | if node.nodeType not in self._child_node_types: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 120 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 121 | "%s cannot be child of %s" % (repr(node), repr(self))) |
| 122 | elif node.nodeType in _nodeTypes_with_children: |
| 123 | _clear_id_cache(self) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 124 | if node.parentNode is not None: |
| 125 | node.parentNode.removeChild(node) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 126 | _append_child(self, node) |
Fred Drake | 13a3069 | 2000-10-09 20:04:16 +0000 | [diff] [blame] | 127 | node.nextSibling = None |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 128 | return node |
| 129 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 130 | def replaceChild(self, newChild, oldChild): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 131 | if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
| 132 | refChild = oldChild.nextSibling |
| 133 | self.removeChild(oldChild) |
| 134 | return self.insertBefore(newChild, refChild) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 135 | if newChild.nodeType not in self._child_node_types: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 136 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 137 | "%s cannot be child of %s" % (repr(newChild), repr(self))) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 138 | if newChild is oldChild: |
| 139 | return |
Andrew M. Kuchling | 841d25e | 2005-11-22 19:03:16 +0000 | [diff] [blame] | 140 | if newChild.parentNode is not None: |
| 141 | newChild.parentNode.removeChild(newChild) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 142 | try: |
| 143 | index = self.childNodes.index(oldChild) |
| 144 | except ValueError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 145 | raise xml.dom.NotFoundErr() |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 146 | self.childNodes[index] = newChild |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 147 | newChild.parentNode = self |
| 148 | oldChild.parentNode = None |
| 149 | if (newChild.nodeType in _nodeTypes_with_children |
| 150 | or oldChild.nodeType in _nodeTypes_with_children): |
| 151 | _clear_id_cache(self) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 152 | newChild.nextSibling = oldChild.nextSibling |
| 153 | newChild.previousSibling = oldChild.previousSibling |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 154 | oldChild.nextSibling = None |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 155 | oldChild.previousSibling = None |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 156 | if newChild.previousSibling: |
| 157 | newChild.previousSibling.nextSibling = newChild |
| 158 | if newChild.nextSibling: |
| 159 | newChild.nextSibling.previousSibling = newChild |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 160 | return oldChild |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 161 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 162 | def removeChild(self, oldChild): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 163 | try: |
| 164 | self.childNodes.remove(oldChild) |
| 165 | except ValueError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 166 | raise xml.dom.NotFoundErr() |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 167 | if oldChild.nextSibling is not None: |
| 168 | oldChild.nextSibling.previousSibling = oldChild.previousSibling |
| 169 | if oldChild.previousSibling is not None: |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 170 | oldChild.previousSibling.nextSibling = oldChild.nextSibling |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 171 | oldChild.nextSibling = oldChild.previousSibling = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 172 | if oldChild.nodeType in _nodeTypes_with_children: |
| 173 | _clear_id_cache(self) |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 174 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 175 | oldChild.parentNode = None |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 176 | return oldChild |
| 177 | |
| 178 | def normalize(self): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 179 | L = [] |
| 180 | for child in self.childNodes: |
| 181 | if child.nodeType == Node.TEXT_NODE: |
R. David Murray | dc6da8a | 2009-04-09 22:16:43 +0000 | [diff] [blame] | 182 | if not child.data: |
| 183 | # empty text node; discard |
| 184 | if L: |
| 185 | L[-1].nextSibling = child.nextSibling |
| 186 | if child.nextSibling: |
| 187 | child.nextSibling.previousSibling = child.previousSibling |
| 188 | child.unlink() |
| 189 | elif L and L[-1].nodeType == child.nodeType: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 190 | # collapse text node |
| 191 | node = L[-1] |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 192 | node.data = node.data + child.data |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 193 | node.nextSibling = child.nextSibling |
R. David Murray | dc6da8a | 2009-04-09 22:16:43 +0000 | [diff] [blame] | 194 | if child.nextSibling: |
| 195 | child.nextSibling.previousSibling = node |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 196 | child.unlink() |
R. David Murray | dc6da8a | 2009-04-09 22:16:43 +0000 | [diff] [blame] | 197 | else: |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 198 | L.append(child) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 199 | else: |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 200 | L.append(child) |
| 201 | if child.nodeType == Node.ELEMENT_NODE: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 202 | child.normalize() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 203 | self.childNodes[:] = L |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 204 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 205 | def cloneNode(self, deep): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 206 | return _clone_node(self, deep, self.ownerDocument or self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 207 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 208 | def isSupported(self, feature, version): |
| 209 | return self.ownerDocument.implementation.hasFeature(feature, version) |
| 210 | |
| 211 | def _get_localName(self): |
| 212 | # Overridden in Element and Attr where localName can be Non-Null |
| 213 | return None |
| 214 | |
| 215 | # Node interfaces from Level 3 (WD 9 April 2002) |
Fred Drake | 2523977 | 2001-02-02 19:40:19 +0000 | [diff] [blame] | 216 | |
| 217 | def isSameNode(self, other): |
| 218 | return self is other |
| 219 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 220 | def getInterface(self, feature): |
| 221 | if self.isSupported(feature, None): |
| 222 | return self |
| 223 | else: |
| 224 | return None |
| 225 | |
| 226 | # The "user data" functions use a dictionary that is only present |
| 227 | # if some user data has been set, so be careful not to assume it |
| 228 | # exists. |
| 229 | |
| 230 | def getUserData(self, key): |
| 231 | try: |
| 232 | return self._user_data[key][0] |
| 233 | except (AttributeError, KeyError): |
| 234 | return None |
| 235 | |
| 236 | def setUserData(self, key, data, handler): |
| 237 | old = None |
| 238 | try: |
| 239 | d = self._user_data |
| 240 | except AttributeError: |
| 241 | d = {} |
| 242 | self._user_data = d |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 243 | if key in d: |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 244 | old = d[key][0] |
| 245 | if data is None: |
| 246 | # ignore handlers passed for None |
| 247 | handler = None |
| 248 | if old is not None: |
| 249 | del d[key] |
| 250 | else: |
| 251 | d[key] = (data, handler) |
| 252 | return old |
| 253 | |
| 254 | def _call_user_data_handler(self, operation, src, dst): |
| 255 | if hasattr(self, "_user_data"): |
Brett Cannon | 861fd6f | 2007-02-21 22:05:37 +0000 | [diff] [blame] | 256 | for key, (data, handler) in list(self._user_data.items()): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 257 | if handler is not None: |
| 258 | handler.handle(operation, key, data, src, dst) |
| 259 | |
Fred Drake | 2523977 | 2001-02-02 19:40:19 +0000 | [diff] [blame] | 260 | # minidom-specific API: |
| 261 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 262 | def unlink(self): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 263 | self.parentNode = self.ownerDocument = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 264 | if self.childNodes: |
| 265 | for child in self.childNodes: |
| 266 | child.unlink() |
| 267 | self.childNodes = NodeList() |
Paul Prescod | 4221ff0 | 2000-10-13 20:11:42 +0000 | [diff] [blame] | 268 | self.previousSibling = None |
| 269 | self.nextSibling = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 270 | |
Kristján Valur Jónsson | 17173cf | 2010-06-09 08:13:42 +0000 | [diff] [blame] | 271 | # A Node is its own context manager, to ensure that an unlink() call occurs. |
| 272 | # This is similar to how a file object works. |
| 273 | def __enter__(self): |
| 274 | return self |
| 275 | |
| 276 | def __exit__(self, et, ev, tb): |
| 277 | self.unlink() |
| 278 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 279 | defproperty(Node, "firstChild", doc="First child node, or None.") |
| 280 | defproperty(Node, "lastChild", doc="Last child node, or None.") |
| 281 | defproperty(Node, "localName", doc="Namespace-local name of this node.") |
| 282 | |
| 283 | |
| 284 | def _append_child(self, node): |
| 285 | # fast path with less checks; usable by DOM builders if careful |
| 286 | childNodes = self.childNodes |
| 287 | if childNodes: |
| 288 | last = childNodes[-1] |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 289 | node.previousSibling = last |
| 290 | last.nextSibling = node |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 291 | childNodes.append(node) |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 292 | node.parentNode = self |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 293 | |
| 294 | def _in_document(node): |
| 295 | # return True iff node is part of a document tree |
| 296 | while node is not None: |
| 297 | if node.nodeType == Node.DOCUMENT_NODE: |
| 298 | return True |
| 299 | node = node.parentNode |
| 300 | return False |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 301 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 302 | def _write_data(writer, data): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 303 | "Writes datachars to writer." |
Georg Brandl | b9cd72a | 2010-10-15 17:58:45 +0000 | [diff] [blame] | 304 | if data: |
| 305 | data = data.replace("&", "&").replace("<", "<"). \ |
| 306 | replace("\"", """).replace(">", ">") |
| 307 | writer.write(data) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 308 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 309 | def _get_elements_by_tagName_helper(parent, name, rc): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 310 | for node in parent.childNodes: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 311 | if node.nodeType == Node.ELEMENT_NODE and \ |
| 312 | (name == "*" or node.tagName == name): |
| 313 | rc.append(node) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 314 | _get_elements_by_tagName_helper(node, name, rc) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 315 | return rc |
| 316 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 317 | def _get_elements_by_tagName_ns_helper(parent, nsURI, localName, rc): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 318 | for node in parent.childNodes: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 319 | if node.nodeType == Node.ELEMENT_NODE: |
Martin v. Löwis | ed525fb | 2001-06-03 14:06:42 +0000 | [diff] [blame] | 320 | if ((localName == "*" or node.localName == localName) and |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 321 | (nsURI == "*" or node.namespaceURI == nsURI)): |
| 322 | rc.append(node) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 323 | _get_elements_by_tagName_ns_helper(node, nsURI, localName, rc) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 324 | return rc |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 325 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 326 | class DocumentFragment(Node): |
| 327 | nodeType = Node.DOCUMENT_FRAGMENT_NODE |
| 328 | nodeName = "#document-fragment" |
| 329 | nodeValue = None |
| 330 | attributes = None |
| 331 | parentNode = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 332 | _child_node_types = (Node.ELEMENT_NODE, |
| 333 | Node.TEXT_NODE, |
| 334 | Node.CDATA_SECTION_NODE, |
| 335 | Node.ENTITY_REFERENCE_NODE, |
| 336 | Node.PROCESSING_INSTRUCTION_NODE, |
| 337 | Node.COMMENT_NODE, |
| 338 | Node.NOTATION_NODE) |
| 339 | |
| 340 | def __init__(self): |
| 341 | self.childNodes = NodeList() |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 342 | |
| 343 | |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 344 | class Attr(Node): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 345 | __slots__=('_name', '_value', 'namespaceURI', |
| 346 | '_prefix', 'childNodes', '_localName', 'ownerDocument', 'ownerElement') |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 347 | nodeType = Node.ATTRIBUTE_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 348 | attributes = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 349 | specified = False |
| 350 | _is_id = False |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 351 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 352 | _child_node_types = (Node.TEXT_NODE, Node.ENTITY_REFERENCE_NODE) |
| 353 | |
| 354 | def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None, |
| 355 | prefix=None): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 356 | self.ownerElement = None |
| 357 | self._name = qName |
| 358 | self.namespaceURI = namespaceURI |
| 359 | self._prefix = prefix |
| 360 | self.childNodes = NodeList() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 361 | |
| 362 | # Add the single child node that represents the value of the attr |
| 363 | self.childNodes.append(Text()) |
| 364 | |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 365 | # nodeValue and value are set elsewhere |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 366 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 367 | def _get_localName(self): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 368 | try: |
| 369 | return self._localName |
| 370 | except AttributeError: |
| 371 | return self.nodeName.split(":", 1)[-1] |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 372 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 373 | def _get_specified(self): |
| 374 | return self.specified |
| 375 | |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 376 | def _get_name(self): |
| 377 | return self._name |
| 378 | |
| 379 | def _set_name(self, value): |
| 380 | self._name = value |
| 381 | if self.ownerElement is not None: |
| 382 | _clear_id_cache(self.ownerElement) |
| 383 | |
| 384 | nodeName = name = property(_get_name, _set_name) |
| 385 | |
| 386 | def _get_value(self): |
| 387 | return self._value |
| 388 | |
| 389 | def _set_value(self, value): |
| 390 | self._value = value |
| 391 | self.childNodes[0].data = value |
| 392 | if self.ownerElement is not None: |
| 393 | _clear_id_cache(self.ownerElement) |
| 394 | self.childNodes[0].data = value |
| 395 | |
| 396 | nodeValue = value = property(_get_value, _set_value) |
| 397 | |
| 398 | def _get_prefix(self): |
| 399 | return self._prefix |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 400 | |
Martin v. Löwis | 995359c | 2003-01-26 08:59:32 +0000 | [diff] [blame] | 401 | def _set_prefix(self, prefix): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 402 | nsuri = self.namespaceURI |
Martin v. Löwis | 995359c | 2003-01-26 08:59:32 +0000 | [diff] [blame] | 403 | if prefix == "xmlns": |
| 404 | if nsuri and nsuri != XMLNS_NAMESPACE: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 405 | raise xml.dom.NamespaceErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 406 | "illegal use of 'xmlns' prefix for the wrong namespace") |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 407 | self._prefix = prefix |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 408 | if prefix is None: |
| 409 | newName = self.localName |
| 410 | else: |
Martin v. Löwis | 995359c | 2003-01-26 08:59:32 +0000 | [diff] [blame] | 411 | newName = "%s:%s" % (prefix, self.localName) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 412 | if self.ownerElement: |
| 413 | _clear_id_cache(self.ownerElement) |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 414 | self.name = newName |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 415 | |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 416 | prefix = property(_get_prefix, _set_prefix) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 417 | |
| 418 | def unlink(self): |
| 419 | # This implementation does not call the base implementation |
| 420 | # since most of that is not needed, and the expense of the |
| 421 | # method call is not warranted. We duplicate the removal of |
| 422 | # children, but that's all we needed from the base class. |
| 423 | elem = self.ownerElement |
| 424 | if elem is not None: |
| 425 | del elem._attrs[self.nodeName] |
| 426 | del elem._attrsNS[(self.namespaceURI, self.localName)] |
| 427 | if self._is_id: |
| 428 | self._is_id = False |
| 429 | elem._magic_id_nodes -= 1 |
| 430 | self.ownerDocument._magic_id_count -= 1 |
| 431 | for child in self.childNodes: |
| 432 | child.unlink() |
| 433 | del self.childNodes[:] |
| 434 | |
| 435 | def _get_isId(self): |
| 436 | if self._is_id: |
| 437 | return True |
| 438 | doc = self.ownerDocument |
| 439 | elem = self.ownerElement |
| 440 | if doc is None or elem is None: |
| 441 | return False |
| 442 | |
| 443 | info = doc._get_elem_info(elem) |
| 444 | if info is None: |
| 445 | return False |
| 446 | if self.namespaceURI: |
| 447 | return info.isIdNS(self.namespaceURI, self.localName) |
| 448 | else: |
| 449 | return info.isId(self.nodeName) |
| 450 | |
| 451 | def _get_schemaType(self): |
| 452 | doc = self.ownerDocument |
| 453 | elem = self.ownerElement |
| 454 | if doc is None or elem is None: |
| 455 | return _no_type |
| 456 | |
| 457 | info = doc._get_elem_info(elem) |
| 458 | if info is None: |
| 459 | return _no_type |
| 460 | if self.namespaceURI: |
| 461 | return info.getAttributeTypeNS(self.namespaceURI, self.localName) |
| 462 | else: |
| 463 | return info.getAttributeType(self.nodeName) |
| 464 | |
| 465 | defproperty(Attr, "isId", doc="True if this attribute is an ID.") |
| 466 | defproperty(Attr, "localName", doc="Namespace-local name of this attribute.") |
| 467 | defproperty(Attr, "schemaType", doc="Schema type for this attribute.") |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 468 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 469 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 470 | class NamedNodeMap(object): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 471 | """The attribute list is a transient interface to the underlying |
| 472 | dictionaries. Mutations here will change the underlying element's |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 473 | dictionary. |
| 474 | |
| 475 | Ordering is imposed artificially and does not reflect the order of |
| 476 | attributes as found in an input document. |
| 477 | """ |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 478 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 479 | __slots__ = ('_attrs', '_attrsNS', '_ownerElement') |
| 480 | |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 481 | def __init__(self, attrs, attrsNS, ownerElement): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 482 | self._attrs = attrs |
| 483 | self._attrsNS = attrsNS |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 484 | self._ownerElement = ownerElement |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 485 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 486 | def _get_length(self): |
| 487 | return len(self._attrs) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 488 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 489 | def item(self, index): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 490 | try: |
Brett Cannon | 861fd6f | 2007-02-21 22:05:37 +0000 | [diff] [blame] | 491 | return self[list(self._attrs.keys())[index]] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 492 | except IndexError: |
| 493 | return None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 494 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 495 | def items(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 496 | L = [] |
| 497 | for node in self._attrs.values(): |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 498 | L.append((node.nodeName, node.value)) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 499 | return L |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 500 | |
| 501 | def itemsNS(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 502 | L = [] |
| 503 | for node in self._attrs.values(): |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame] | 504 | L.append(((node.namespaceURI, node.localName), node.value)) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 505 | return L |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 506 | |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 507 | def __contains__(self, key): |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 508 | if isinstance(key, str): |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 509 | return key in self._attrs |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 510 | else: |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 511 | return key in self._attrsNS |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 512 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 513 | def keys(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 514 | return self._attrs.keys() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 515 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 516 | def keysNS(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 517 | return self._attrsNS.keys() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 518 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 519 | def values(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 520 | return self._attrs.values() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 521 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 522 | def get(self, name, value=None): |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 523 | return self._attrs.get(name, value) |
| 524 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 525 | __len__ = _get_length |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 526 | |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 527 | def _cmp(self, other): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 528 | if self._attrs is getattr(other, "_attrs", None): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 529 | return 0 |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 530 | else: |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 531 | return (id(self) > id(other)) - (id(self) < id(other)) |
| 532 | |
| 533 | def __eq__(self, other): |
| 534 | return self._cmp(other) == 0 |
| 535 | |
| 536 | def __ge__(self, other): |
| 537 | return self._cmp(other) >= 0 |
| 538 | |
| 539 | def __gt__(self, other): |
| 540 | return self._cmp(other) > 0 |
| 541 | |
| 542 | def __le__(self, other): |
| 543 | return self._cmp(other) <= 0 |
| 544 | |
| 545 | def __lt__(self, other): |
| 546 | return self._cmp(other) < 0 |
| 547 | |
| 548 | def __ne__(self, other): |
| 549 | return self._cmp(other) != 0 |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 550 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 551 | def __getitem__(self, attname_or_tuple): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 552 | if isinstance(attname_or_tuple, tuple): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 553 | return self._attrsNS[attname_or_tuple] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 554 | else: |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 555 | return self._attrs[attname_or_tuple] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 556 | |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 557 | # same as set |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 558 | def __setitem__(self, attname, value): |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 559 | if isinstance(value, str): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 560 | try: |
| 561 | node = self._attrs[attname] |
| 562 | except KeyError: |
| 563 | node = Attr(attname) |
| 564 | node.ownerDocument = self._ownerElement.ownerDocument |
Martin v. Löwis | 995359c | 2003-01-26 08:59:32 +0000 | [diff] [blame] | 565 | self.setNamedItem(node) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 566 | node.value = value |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 567 | else: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 568 | if not isinstance(value, Attr): |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 569 | raise TypeError("value must be a string or Attr object") |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 570 | node = value |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 571 | self.setNamedItem(node) |
| 572 | |
| 573 | def getNamedItem(self, name): |
| 574 | try: |
| 575 | return self._attrs[name] |
| 576 | except KeyError: |
| 577 | return None |
| 578 | |
| 579 | def getNamedItemNS(self, namespaceURI, localName): |
| 580 | try: |
| 581 | return self._attrsNS[(namespaceURI, localName)] |
| 582 | except KeyError: |
| 583 | return None |
| 584 | |
| 585 | def removeNamedItem(self, name): |
| 586 | n = self.getNamedItem(name) |
| 587 | if n is not None: |
| 588 | _clear_id_cache(self._ownerElement) |
| 589 | del self._attrs[n.nodeName] |
| 590 | del self._attrsNS[(n.namespaceURI, n.localName)] |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 591 | if hasattr(n, 'ownerElement'): |
| 592 | n.ownerElement = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 593 | return n |
| 594 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 595 | raise xml.dom.NotFoundErr() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 596 | |
| 597 | def removeNamedItemNS(self, namespaceURI, localName): |
| 598 | n = self.getNamedItemNS(namespaceURI, localName) |
| 599 | if n is not None: |
| 600 | _clear_id_cache(self._ownerElement) |
| 601 | del self._attrsNS[(n.namespaceURI, n.localName)] |
| 602 | del self._attrs[n.nodeName] |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 603 | if hasattr(n, 'ownerElement'): |
| 604 | n.ownerElement = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 605 | return n |
| 606 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 607 | raise xml.dom.NotFoundErr() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 608 | |
| 609 | def setNamedItem(self, node): |
Andrew M. Kuchling | bc8f72c | 2001-02-21 01:30:26 +0000 | [diff] [blame] | 610 | if not isinstance(node, Attr): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 611 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 612 | "%s cannot be child of %s" % (repr(node), repr(self))) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 613 | old = self._attrs.get(node.name) |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 614 | if old: |
| 615 | old.unlink() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 616 | self._attrs[node.name] = node |
| 617 | self._attrsNS[(node.namespaceURI, node.localName)] = node |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 618 | node.ownerElement = self._ownerElement |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 619 | _clear_id_cache(node.ownerElement) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 620 | return old |
| 621 | |
| 622 | def setNamedItemNS(self, node): |
| 623 | return self.setNamedItem(node) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 624 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 625 | def __delitem__(self, attname_or_tuple): |
| 626 | node = self[attname_or_tuple] |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 627 | _clear_id_cache(node.ownerElement) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 628 | node.unlink() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 629 | |
| 630 | def __getstate__(self): |
| 631 | return self._attrs, self._attrsNS, self._ownerElement |
| 632 | |
| 633 | def __setstate__(self, state): |
| 634 | self._attrs, self._attrsNS, self._ownerElement = state |
| 635 | |
| 636 | defproperty(NamedNodeMap, "length", |
| 637 | doc="Number of nodes in the NamedNodeMap.") |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 638 | |
| 639 | AttributeList = NamedNodeMap |
| 640 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 641 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 642 | class TypeInfo(object): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 643 | __slots__ = 'namespace', 'name' |
| 644 | |
| 645 | def __init__(self, namespace, name): |
| 646 | self.namespace = namespace |
| 647 | self.name = name |
| 648 | |
| 649 | def __repr__(self): |
| 650 | if self.namespace: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 651 | return "<TypeInfo %r (from %r)>" % (self.name, self.namespace) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 652 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 653 | return "<TypeInfo %r>" % self.name |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 654 | |
| 655 | def _get_name(self): |
| 656 | return self.name |
| 657 | |
| 658 | def _get_namespace(self): |
| 659 | return self.namespace |
| 660 | |
| 661 | _no_type = TypeInfo(None, None) |
| 662 | |
Martin v. Löwis | a2fda0d | 2000-10-07 12:10:28 +0000 | [diff] [blame] | 663 | class Element(Node): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 664 | __slots__=('ownerDocument', 'parentNode', 'tagName', 'nodeName', 'prefix', |
| 665 | 'namespaceURI', '_localName', 'childNodes', '_attrs', '_attrsNS', |
| 666 | 'nextSibling', 'previousSibling') |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 667 | nodeType = Node.ELEMENT_NODE |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 668 | nodeValue = None |
| 669 | schemaType = _no_type |
| 670 | |
| 671 | _magic_id_nodes = 0 |
| 672 | |
| 673 | _child_node_types = (Node.ELEMENT_NODE, |
| 674 | Node.PROCESSING_INSTRUCTION_NODE, |
| 675 | Node.COMMENT_NODE, |
| 676 | Node.TEXT_NODE, |
| 677 | Node.CDATA_SECTION_NODE, |
| 678 | Node.ENTITY_REFERENCE_NODE) |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 679 | |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame] | 680 | def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None, |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 681 | localName=None): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 682 | self.parentNode = None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 683 | self.tagName = self.nodeName = tagName |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 684 | self.prefix = prefix |
| 685 | self.namespaceURI = namespaceURI |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 686 | self.childNodes = NodeList() |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 687 | self.nextSibling = self.previousSibling = None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 688 | |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 689 | # Attribute dictionaries are lazily created |
| 690 | # attributes are double-indexed: |
| 691 | # tagName -> Attribute |
| 692 | # URI,localName -> Attribute |
| 693 | # in the future: consider lazy generation |
| 694 | # of attribute objects this is too tricky |
| 695 | # for now because of headaches with |
| 696 | # namespaces. |
| 697 | self._attrs = None |
| 698 | self._attrsNS = None |
| 699 | |
| 700 | def _ensure_attributes(self): |
| 701 | if self._attrs is None: |
| 702 | self._attrs = {} |
| 703 | self._attrsNS = {} |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 704 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 705 | def _get_localName(self): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 706 | try: |
| 707 | return self._localName |
| 708 | except AttributeError: |
| 709 | return self.tagName.split(":", 1)[-1] |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 710 | |
| 711 | def _get_tagName(self): |
| 712 | return self.tagName |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 713 | |
| 714 | def unlink(self): |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 715 | if self._attrs is not None: |
| 716 | for attr in list(self._attrs.values()): |
| 717 | attr.unlink() |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 718 | self._attrs = None |
| 719 | self._attrsNS = None |
| 720 | Node.unlink(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 721 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 722 | def getAttribute(self, attname): |
Martin v. Löwis | 67245a6 | 2012-03-05 07:01:49 +0100 | [diff] [blame] | 723 | if self._attrs is None: |
| 724 | return "" |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 725 | try: |
| 726 | return self._attrs[attname].value |
| 727 | except KeyError: |
| 728 | return "" |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 729 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 730 | def getAttributeNS(self, namespaceURI, localName): |
Martin v. Löwis | 67245a6 | 2012-03-05 07:01:49 +0100 | [diff] [blame] | 731 | if self._attrsNS is None: |
| 732 | return "" |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 733 | try: |
| 734 | return self._attrsNS[(namespaceURI, localName)].value |
| 735 | except KeyError: |
| 736 | return "" |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 737 | |
| 738 | def setAttribute(self, attname, value): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 739 | attr = self.getAttributeNode(attname) |
| 740 | if attr is None: |
| 741 | attr = Attr(attname) |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 742 | attr.value = value # also sets nodeValue |
| 743 | attr.ownerDocument = self.ownerDocument |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 744 | self.setAttributeNode(attr) |
| 745 | elif value != attr.value: |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 746 | attr.value = value |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 747 | if attr.isId: |
| 748 | _clear_id_cache(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 749 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 750 | def setAttributeNS(self, namespaceURI, qualifiedName, value): |
| 751 | prefix, localname = _nssplit(qualifiedName) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 752 | attr = self.getAttributeNodeNS(namespaceURI, localname) |
| 753 | if attr is None: |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 754 | attr = Attr(qualifiedName, namespaceURI, localname, prefix) |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 755 | attr.value = value |
| 756 | attr.ownerDocument = self.ownerDocument |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 757 | self.setAttributeNode(attr) |
| 758 | else: |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 759 | if value != attr.value: |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 760 | attr.value = value |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 761 | if attr.isId: |
| 762 | _clear_id_cache(self) |
| 763 | if attr.prefix != prefix: |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 764 | attr.prefix = prefix |
| 765 | attr.nodeName = qualifiedName |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 766 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 767 | def getAttributeNode(self, attrname): |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 768 | if self._attrs is None: |
| 769 | return None |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 770 | return self._attrs.get(attrname) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 771 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 772 | def getAttributeNodeNS(self, namespaceURI, localName): |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 773 | if self._attrsNS is None: |
| 774 | return None |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 775 | return self._attrsNS.get((namespaceURI, localName)) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 776 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 777 | def setAttributeNode(self, attr): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 778 | if attr.ownerElement not in (None, self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 779 | raise xml.dom.InuseAttributeErr("attribute node already owned") |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 780 | self._ensure_attributes() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 781 | old1 = self._attrs.get(attr.name, None) |
| 782 | if old1 is not None: |
| 783 | self.removeAttributeNode(old1) |
| 784 | old2 = self._attrsNS.get((attr.namespaceURI, attr.localName), None) |
| 785 | if old2 is not None and old2 is not old1: |
| 786 | self.removeAttributeNode(old2) |
| 787 | _set_attribute_node(self, attr) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 788 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 789 | if old1 is not attr: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 790 | # It might have already been part of this node, in which case |
| 791 | # it doesn't represent a change, and should not be returned. |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 792 | return old1 |
| 793 | if old2 is not attr: |
| 794 | return old2 |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 795 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 796 | setAttributeNodeNS = setAttributeNode |
| 797 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 798 | def removeAttribute(self, name): |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 799 | if self._attrsNS is None: |
| 800 | raise xml.dom.NotFoundErr() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 801 | try: |
| 802 | attr = self._attrs[name] |
| 803 | except KeyError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 804 | raise xml.dom.NotFoundErr() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 805 | self.removeAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 806 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 807 | def removeAttributeNS(self, namespaceURI, localName): |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 808 | if self._attrsNS is None: |
| 809 | raise xml.dom.NotFoundErr() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 810 | try: |
| 811 | attr = self._attrsNS[(namespaceURI, localName)] |
| 812 | except KeyError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 813 | raise xml.dom.NotFoundErr() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 814 | self.removeAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 815 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 816 | def removeAttributeNode(self, node): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 817 | if node is None: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 818 | raise xml.dom.NotFoundErr() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 819 | try: |
| 820 | self._attrs[node.name] |
| 821 | except KeyError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 822 | raise xml.dom.NotFoundErr() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 823 | _clear_id_cache(self) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 824 | node.unlink() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 825 | # Restore this since the node is still useful and otherwise |
| 826 | # unlinked |
| 827 | node.ownerDocument = self.ownerDocument |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 828 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 829 | removeAttributeNodeNS = removeAttributeNode |
| 830 | |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 831 | def hasAttribute(self, name): |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 832 | if self._attrs is None: |
| 833 | return False |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 834 | return name in self._attrs |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 835 | |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 836 | def hasAttributeNS(self, namespaceURI, localName): |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 837 | if self._attrsNS is None: |
| 838 | return False |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 839 | return (namespaceURI, localName) in self._attrsNS |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 840 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 841 | def getElementsByTagName(self, name): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 842 | return _get_elements_by_tagName_helper(self, name, NodeList()) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 843 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 844 | def getElementsByTagNameNS(self, namespaceURI, localName): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 845 | return _get_elements_by_tagName_ns_helper( |
| 846 | self, namespaceURI, localName, NodeList()) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 847 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 848 | def __repr__(self): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 849 | return "<DOM Element: %s at %#x>" % (self.tagName, id(self)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 850 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 851 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 852 | # indent = current indentation |
| 853 | # addindent = indentation to add to higher levels |
| 854 | # newl = newline string |
| 855 | writer.write(indent+"<" + self.tagName) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 856 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 857 | attrs = self._get_attributes() |
Brett Cannon | 861fd6f | 2007-02-21 22:05:37 +0000 | [diff] [blame] | 858 | a_names = sorted(attrs.keys()) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 859 | |
| 860 | for a_name in a_names: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 861 | writer.write(" %s=\"" % a_name) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 862 | _write_data(writer, attrs[a_name].value) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 863 | writer.write("\"") |
| 864 | if self.childNodes: |
R David Murray | 791744b | 2011-10-01 16:19:51 -0400 | [diff] [blame] | 865 | writer.write(">") |
Ezio Melotti | 8008f2a | 2011-11-18 17:34:26 +0200 | [diff] [blame] | 866 | if (len(self.childNodes) == 1 and |
| 867 | self.childNodes[0].nodeType == Node.TEXT_NODE): |
| 868 | self.childNodes[0].writexml(writer, '', '', '') |
| 869 | else: |
R David Murray | 791744b | 2011-10-01 16:19:51 -0400 | [diff] [blame] | 870 | writer.write(newl) |
Ezio Melotti | 8008f2a | 2011-11-18 17:34:26 +0200 | [diff] [blame] | 871 | for node in self.childNodes: |
| 872 | node.writexml(writer, indent+addindent, addindent, newl) |
| 873 | writer.write(indent) |
| 874 | writer.write("</%s>%s" % (self.tagName, newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 875 | else: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 876 | writer.write("/>%s"%(newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 877 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 878 | def _get_attributes(self): |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 879 | self._ensure_attributes() |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 880 | return NamedNodeMap(self._attrs, self._attrsNS, self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 881 | |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 882 | def hasAttributes(self): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 883 | if self._attrs: |
| 884 | return True |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 885 | else: |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 886 | return False |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 887 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 888 | # DOM Level 3 attributes, based on the 22 Oct 2002 draft |
| 889 | |
| 890 | def setIdAttribute(self, name): |
| 891 | idAttr = self.getAttributeNode(name) |
| 892 | self.setIdAttributeNode(idAttr) |
| 893 | |
| 894 | def setIdAttributeNS(self, namespaceURI, localName): |
| 895 | idAttr = self.getAttributeNodeNS(namespaceURI, localName) |
| 896 | self.setIdAttributeNode(idAttr) |
| 897 | |
| 898 | def setIdAttributeNode(self, idAttr): |
| 899 | if idAttr is None or not self.isSameNode(idAttr.ownerElement): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 900 | raise xml.dom.NotFoundErr() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 901 | if _get_containing_entref(self) is not None: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 902 | raise xml.dom.NoModificationAllowedErr() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 903 | if not idAttr._is_id: |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 904 | idAttr._is_id = True |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 905 | self._magic_id_nodes += 1 |
| 906 | self.ownerDocument._magic_id_count += 1 |
| 907 | _clear_id_cache(self) |
| 908 | |
| 909 | defproperty(Element, "attributes", |
| 910 | doc="NamedNodeMap of attributes on the element.") |
| 911 | defproperty(Element, "localName", |
| 912 | doc="Namespace-local name of this element.") |
| 913 | |
| 914 | |
| 915 | def _set_attribute_node(element, attr): |
| 916 | _clear_id_cache(element) |
Martin v. Löwis | 7b77188 | 2012-02-19 20:55:05 +0100 | [diff] [blame] | 917 | element._ensure_attributes() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 918 | element._attrs[attr.name] = attr |
| 919 | element._attrsNS[(attr.namespaceURI, attr.localName)] = attr |
| 920 | |
| 921 | # This creates a circular reference, but Element.unlink() |
| 922 | # breaks the cycle since the references to the attribute |
| 923 | # dictionaries are tossed. |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 924 | attr.ownerElement = element |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 925 | |
| 926 | class Childless: |
| 927 | """Mixin that makes childless-ness easy to implement and avoids |
| 928 | the complexity of the Node methods that deal with children. |
| 929 | """ |
Florent Xicluna | 8cf4b51 | 2012-03-05 12:37:02 +0100 | [diff] [blame] | 930 | __slots__ = () |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 931 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 932 | attributes = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 933 | childNodes = EmptyNodeList() |
| 934 | firstChild = None |
| 935 | lastChild = None |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 936 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 937 | def _get_firstChild(self): |
| 938 | return None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 939 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 940 | def _get_lastChild(self): |
| 941 | return None |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 942 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 943 | def appendChild(self, node): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 944 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 945 | self.nodeName + " nodes cannot have children") |
| 946 | |
| 947 | def hasChildNodes(self): |
| 948 | return False |
| 949 | |
| 950 | def insertBefore(self, newChild, refChild): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 951 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 952 | self.nodeName + " nodes do not have children") |
| 953 | |
| 954 | def removeChild(self, oldChild): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 955 | raise xml.dom.NotFoundErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 956 | self.nodeName + " nodes do not have children") |
| 957 | |
Andrew M. Kuchling | 688b9e3 | 2010-07-25 23:38:47 +0000 | [diff] [blame] | 958 | def normalize(self): |
| 959 | # For childless nodes, normalize() has nothing to do. |
| 960 | pass |
| 961 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 962 | def replaceChild(self, newChild, oldChild): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 963 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 964 | self.nodeName + " nodes do not have children") |
| 965 | |
| 966 | |
| 967 | class ProcessingInstruction(Childless, Node): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 968 | nodeType = Node.PROCESSING_INSTRUCTION_NODE |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 969 | __slots__ = ('target', 'data') |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 970 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 971 | def __init__(self, target, data): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 972 | self.target = target |
| 973 | self.data = data |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 974 | |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 975 | # nodeValue is an alias for data |
| 976 | def _get_nodeValue(self): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 977 | return self.data |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 978 | def _set_nodeValue(self, value): |
Raymond Hettinger | 92a4055 | 2014-06-15 14:48:19 -0700 | [diff] [blame] | 979 | self.data = value |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 980 | nodeValue = property(_get_nodeValue, _set_nodeValue) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 981 | |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 982 | # nodeName is an alias for target |
| 983 | def _get_nodeName(self): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 984 | return self.target |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 985 | def _set_nodeName(self, value): |
| 986 | self.target = value |
| 987 | nodeName = property(_get_nodeName, _set_nodeName) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 988 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 989 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 990 | writer.write("%s<?%s %s?>%s" % (indent,self.target, self.data, newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 991 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 992 | |
| 993 | class CharacterData(Childless, Node): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 994 | __slots__=('_data', 'ownerDocument','parentNode', 'previousSibling', 'nextSibling') |
| 995 | |
| 996 | def __init__(self): |
| 997 | self.ownerDocument = self.parentNode = None |
| 998 | self.previousSibling = self.nextSibling = None |
| 999 | self._data = '' |
| 1000 | Node.__init__(self) |
| 1001 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1002 | def _get_length(self): |
| 1003 | return len(self.data) |
| 1004 | __len__ = _get_length |
| 1005 | |
| 1006 | def _get_data(self): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1007 | return self._data |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1008 | def _set_data(self, data): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1009 | self._data = data |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1010 | |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1011 | data = nodeValue = property(_get_data, _set_data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1012 | |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1013 | def __repr__(self): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1014 | data = self.data |
| 1015 | if len(data) > 10: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 1016 | dotdotdot = "..." |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1017 | else: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 1018 | dotdotdot = "" |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1019 | return '<DOM %s node "%r%s">' % ( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1020 | self.__class__.__name__, data[0:10], dotdotdot) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1021 | |
| 1022 | def substringData(self, offset, count): |
| 1023 | if offset < 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1024 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1025 | if offset >= len(self.data): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1026 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1027 | if count < 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1028 | raise xml.dom.IndexSizeErr("count cannot be negative") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1029 | return self.data[offset:offset+count] |
| 1030 | |
| 1031 | def appendData(self, arg): |
| 1032 | self.data = self.data + arg |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1033 | |
| 1034 | def insertData(self, offset, arg): |
| 1035 | if offset < 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1036 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1037 | if offset >= len(self.data): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1038 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1039 | if arg: |
| 1040 | self.data = "%s%s%s" % ( |
| 1041 | self.data[:offset], arg, self.data[offset:]) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1042 | |
| 1043 | def deleteData(self, offset, count): |
| 1044 | if offset < 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1045 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1046 | if offset >= len(self.data): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1047 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1048 | if count < 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1049 | raise xml.dom.IndexSizeErr("count cannot be negative") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1050 | if count: |
| 1051 | self.data = self.data[:offset] + self.data[offset+count:] |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1052 | |
| 1053 | def replaceData(self, offset, count, arg): |
| 1054 | if offset < 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1055 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1056 | if offset >= len(self.data): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1057 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1058 | if count < 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1059 | raise xml.dom.IndexSizeErr("count cannot be negative") |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1060 | if count: |
| 1061 | self.data = "%s%s%s" % ( |
| 1062 | self.data[:offset], arg, self.data[offset+count:]) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1063 | |
| 1064 | defproperty(CharacterData, "length", doc="Length of the string data.") |
| 1065 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1066 | |
| 1067 | class Text(CharacterData): |
Florent Xicluna | 8cf4b51 | 2012-03-05 12:37:02 +0100 | [diff] [blame] | 1068 | __slots__ = () |
| 1069 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1070 | nodeType = Node.TEXT_NODE |
| 1071 | nodeName = "#text" |
| 1072 | attributes = None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1073 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1074 | def splitText(self, offset): |
| 1075 | if offset < 0 or offset > len(self.data): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1076 | raise xml.dom.IndexSizeErr("illegal offset value") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1077 | newText = self.__class__() |
| 1078 | newText.data = self.data[offset:] |
| 1079 | newText.ownerDocument = self.ownerDocument |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1080 | next = self.nextSibling |
| 1081 | if self.parentNode and self in self.parentNode.childNodes: |
| 1082 | if next is None: |
| 1083 | self.parentNode.appendChild(newText) |
| 1084 | else: |
| 1085 | self.parentNode.insertBefore(newText, next) |
| 1086 | self.data = self.data[:offset] |
| 1087 | return newText |
| 1088 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 1089 | def writexml(self, writer, indent="", addindent="", newl=""): |
Ezio Melotti | 8008f2a | 2011-11-18 17:34:26 +0200 | [diff] [blame] | 1090 | _write_data(writer, "%s%s%s" % (indent, self.data, newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1091 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1092 | # DOM Level 3 (WD 9 April 2002) |
| 1093 | |
| 1094 | def _get_wholeText(self): |
| 1095 | L = [self.data] |
| 1096 | n = self.previousSibling |
| 1097 | while n is not None: |
| 1098 | if n.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): |
| 1099 | L.insert(0, n.data) |
| 1100 | n = n.previousSibling |
| 1101 | else: |
| 1102 | break |
| 1103 | n = self.nextSibling |
| 1104 | while n is not None: |
| 1105 | if n.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): |
| 1106 | L.append(n.data) |
| 1107 | n = n.nextSibling |
| 1108 | else: |
| 1109 | break |
| 1110 | return ''.join(L) |
| 1111 | |
| 1112 | def replaceWholeText(self, content): |
| 1113 | # XXX This needs to be seriously changed if minidom ever |
| 1114 | # supports EntityReference nodes. |
| 1115 | parent = self.parentNode |
| 1116 | n = self.previousSibling |
| 1117 | while n is not None: |
| 1118 | if n.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): |
| 1119 | next = n.previousSibling |
| 1120 | parent.removeChild(n) |
| 1121 | n = next |
| 1122 | else: |
| 1123 | break |
| 1124 | n = self.nextSibling |
| 1125 | if not content: |
| 1126 | parent.removeChild(self) |
| 1127 | while n is not None: |
| 1128 | if n.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): |
| 1129 | next = n.nextSibling |
| 1130 | parent.removeChild(n) |
| 1131 | n = next |
| 1132 | else: |
| 1133 | break |
| 1134 | if content: |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1135 | self.data = content |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1136 | return self |
| 1137 | else: |
| 1138 | return None |
| 1139 | |
| 1140 | def _get_isWhitespaceInElementContent(self): |
| 1141 | if self.data.strip(): |
| 1142 | return False |
| 1143 | elem = _get_containing_element(self) |
| 1144 | if elem is None: |
| 1145 | return False |
| 1146 | info = self.ownerDocument._get_elem_info(elem) |
| 1147 | if info is None: |
| 1148 | return False |
| 1149 | else: |
| 1150 | return info.isElementContent() |
| 1151 | |
| 1152 | defproperty(Text, "isWhitespaceInElementContent", |
| 1153 | doc="True iff this text node contains only whitespace" |
| 1154 | " and is in element content.") |
| 1155 | defproperty(Text, "wholeText", |
| 1156 | doc="The text of all logically-adjacent text nodes.") |
| 1157 | |
| 1158 | |
| 1159 | def _get_containing_element(node): |
| 1160 | c = node.parentNode |
| 1161 | while c is not None: |
| 1162 | if c.nodeType == Node.ELEMENT_NODE: |
| 1163 | return c |
| 1164 | c = c.parentNode |
| 1165 | return None |
| 1166 | |
| 1167 | def _get_containing_entref(node): |
| 1168 | c = node.parentNode |
| 1169 | while c is not None: |
| 1170 | if c.nodeType == Node.ENTITY_REFERENCE_NODE: |
| 1171 | return c |
| 1172 | c = c.parentNode |
| 1173 | return None |
| 1174 | |
| 1175 | |
Alex Martelli | 0ee4351 | 2006-08-21 19:53:20 +0000 | [diff] [blame] | 1176 | class Comment(CharacterData): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1177 | nodeType = Node.COMMENT_NODE |
| 1178 | nodeName = "#comment" |
| 1179 | |
| 1180 | def __init__(self, data): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1181 | CharacterData.__init__(self) |
| 1182 | self._data = data |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1183 | |
| 1184 | def writexml(self, writer, indent="", addindent="", newl=""): |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1185 | if "--" in self.data: |
| 1186 | raise ValueError("'--' is not allowed in a comment node") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1187 | writer.write("%s<!--%s-->%s" % (indent, self.data, newl)) |
| 1188 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1189 | |
| 1190 | class CDATASection(Text): |
Florent Xicluna | 8cf4b51 | 2012-03-05 12:37:02 +0100 | [diff] [blame] | 1191 | __slots__ = () |
| 1192 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1193 | nodeType = Node.CDATA_SECTION_NODE |
| 1194 | nodeName = "#cdata-section" |
| 1195 | |
| 1196 | def writexml(self, writer, indent="", addindent="", newl=""): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1197 | if self.data.find("]]>") >= 0: |
| 1198 | raise ValueError("']]>' not allowed in a CDATA section") |
Guido van Rossum | 5b5e0b9 | 2001-09-19 13:28:25 +0000 | [diff] [blame] | 1199 | writer.write("<![CDATA[%s]]>" % self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1200 | |
| 1201 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1202 | class ReadOnlySequentialNamedNodeMap(object): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1203 | __slots__ = '_seq', |
| 1204 | |
| 1205 | def __init__(self, seq=()): |
| 1206 | # seq should be a list or tuple |
| 1207 | self._seq = seq |
| 1208 | |
| 1209 | def __len__(self): |
| 1210 | return len(self._seq) |
| 1211 | |
| 1212 | def _get_length(self): |
| 1213 | return len(self._seq) |
| 1214 | |
| 1215 | def getNamedItem(self, name): |
| 1216 | for n in self._seq: |
| 1217 | if n.nodeName == name: |
| 1218 | return n |
| 1219 | |
| 1220 | def getNamedItemNS(self, namespaceURI, localName): |
| 1221 | for n in self._seq: |
| 1222 | if n.namespaceURI == namespaceURI and n.localName == localName: |
| 1223 | return n |
| 1224 | |
| 1225 | def __getitem__(self, name_or_tuple): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1226 | if isinstance(name_or_tuple, tuple): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1227 | node = self.getNamedItemNS(*name_or_tuple) |
| 1228 | else: |
| 1229 | node = self.getNamedItem(name_or_tuple) |
| 1230 | if node is None: |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 1231 | raise KeyError(name_or_tuple) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1232 | return node |
| 1233 | |
| 1234 | def item(self, index): |
| 1235 | if index < 0: |
| 1236 | return None |
| 1237 | try: |
| 1238 | return self._seq[index] |
| 1239 | except IndexError: |
| 1240 | return None |
| 1241 | |
| 1242 | def removeNamedItem(self, name): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1243 | raise xml.dom.NoModificationAllowedErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1244 | "NamedNodeMap instance is read-only") |
| 1245 | |
| 1246 | def removeNamedItemNS(self, namespaceURI, localName): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1247 | raise xml.dom.NoModificationAllowedErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1248 | "NamedNodeMap instance is read-only") |
| 1249 | |
| 1250 | def setNamedItem(self, node): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1251 | raise xml.dom.NoModificationAllowedErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1252 | "NamedNodeMap instance is read-only") |
| 1253 | |
| 1254 | def setNamedItemNS(self, node): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1255 | raise xml.dom.NoModificationAllowedErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1256 | "NamedNodeMap instance is read-only") |
| 1257 | |
| 1258 | def __getstate__(self): |
| 1259 | return [self._seq] |
| 1260 | |
| 1261 | def __setstate__(self, state): |
| 1262 | self._seq = state[0] |
| 1263 | |
| 1264 | defproperty(ReadOnlySequentialNamedNodeMap, "length", |
| 1265 | doc="Number of entries in the NamedNodeMap.") |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 1266 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1267 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1268 | class Identified: |
| 1269 | """Mix-in class that supports the publicId and systemId attributes.""" |
| 1270 | |
Florent Xicluna | 8cf4b51 | 2012-03-05 12:37:02 +0100 | [diff] [blame] | 1271 | __slots__ = 'publicId', 'systemId' |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1272 | |
| 1273 | def _identified_mixin_init(self, publicId, systemId): |
| 1274 | self.publicId = publicId |
| 1275 | self.systemId = systemId |
| 1276 | |
| 1277 | def _get_publicId(self): |
| 1278 | return self.publicId |
| 1279 | |
| 1280 | def _get_systemId(self): |
| 1281 | return self.systemId |
| 1282 | |
| 1283 | class DocumentType(Identified, Childless, Node): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1284 | nodeType = Node.DOCUMENT_TYPE_NODE |
| 1285 | nodeValue = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1286 | name = None |
| 1287 | publicId = None |
| 1288 | systemId = None |
Fred Drake | dc80670 | 2001-04-05 14:41:30 +0000 | [diff] [blame] | 1289 | internalSubset = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1290 | |
| 1291 | def __init__(self, qualifiedName): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1292 | self.entities = ReadOnlySequentialNamedNodeMap() |
| 1293 | self.notations = ReadOnlySequentialNamedNodeMap() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1294 | if qualifiedName: |
| 1295 | prefix, localname = _nssplit(qualifiedName) |
| 1296 | self.name = localname |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1297 | self.nodeName = self.name |
| 1298 | |
| 1299 | def _get_internalSubset(self): |
| 1300 | return self.internalSubset |
| 1301 | |
| 1302 | def cloneNode(self, deep): |
| 1303 | if self.ownerDocument is None: |
| 1304 | # it's ok |
| 1305 | clone = DocumentType(None) |
| 1306 | clone.name = self.name |
| 1307 | clone.nodeName = self.name |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1308 | operation = xml.dom.UserDataHandler.NODE_CLONED |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1309 | if deep: |
| 1310 | clone.entities._seq = [] |
| 1311 | clone.notations._seq = [] |
| 1312 | for n in self.notations._seq: |
| 1313 | notation = Notation(n.nodeName, n.publicId, n.systemId) |
| 1314 | clone.notations._seq.append(notation) |
| 1315 | n._call_user_data_handler(operation, n, notation) |
| 1316 | for e in self.entities._seq: |
| 1317 | entity = Entity(e.nodeName, e.publicId, e.systemId, |
| 1318 | e.notationName) |
| 1319 | entity.actualEncoding = e.actualEncoding |
| 1320 | entity.encoding = e.encoding |
| 1321 | entity.version = e.version |
| 1322 | clone.entities._seq.append(entity) |
| 1323 | e._call_user_data_handler(operation, n, entity) |
| 1324 | self._call_user_data_handler(operation, self, clone) |
| 1325 | return clone |
| 1326 | else: |
| 1327 | return None |
| 1328 | |
| 1329 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 1330 | writer.write("<!DOCTYPE ") |
| 1331 | writer.write(self.name) |
| 1332 | if self.publicId: |
Georg Brandl | 175a7dc | 2005-08-25 22:02:43 +0000 | [diff] [blame] | 1333 | writer.write("%s PUBLIC '%s'%s '%s'" |
| 1334 | % (newl, self.publicId, newl, self.systemId)) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1335 | elif self.systemId: |
Georg Brandl | 175a7dc | 2005-08-25 22:02:43 +0000 | [diff] [blame] | 1336 | writer.write("%s SYSTEM '%s'" % (newl, self.systemId)) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1337 | if self.internalSubset is not None: |
| 1338 | writer.write(" [") |
| 1339 | writer.write(self.internalSubset) |
| 1340 | writer.write("]") |
Georg Brandl | 175a7dc | 2005-08-25 22:02:43 +0000 | [diff] [blame] | 1341 | writer.write(">"+newl) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1342 | |
| 1343 | class Entity(Identified, Node): |
| 1344 | attributes = None |
| 1345 | nodeType = Node.ENTITY_NODE |
| 1346 | nodeValue = None |
| 1347 | |
| 1348 | actualEncoding = None |
| 1349 | encoding = None |
| 1350 | version = None |
| 1351 | |
| 1352 | def __init__(self, name, publicId, systemId, notation): |
| 1353 | self.nodeName = name |
| 1354 | self.notationName = notation |
| 1355 | self.childNodes = NodeList() |
| 1356 | self._identified_mixin_init(publicId, systemId) |
| 1357 | |
| 1358 | def _get_actualEncoding(self): |
| 1359 | return self.actualEncoding |
| 1360 | |
| 1361 | def _get_encoding(self): |
| 1362 | return self.encoding |
| 1363 | |
| 1364 | def _get_version(self): |
| 1365 | return self.version |
| 1366 | |
| 1367 | def appendChild(self, newChild): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1368 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1369 | "cannot append children to an entity node") |
| 1370 | |
| 1371 | def insertBefore(self, newChild, refChild): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1372 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1373 | "cannot insert children below an entity node") |
| 1374 | |
| 1375 | def removeChild(self, oldChild): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1376 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1377 | "cannot remove children from an entity node") |
| 1378 | |
| 1379 | def replaceChild(self, newChild, oldChild): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1380 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1381 | "cannot replace children of an entity node") |
| 1382 | |
| 1383 | class Notation(Identified, Childless, Node): |
| 1384 | nodeType = Node.NOTATION_NODE |
| 1385 | nodeValue = None |
| 1386 | |
| 1387 | def __init__(self, name, publicId, systemId): |
| 1388 | self.nodeName = name |
| 1389 | self._identified_mixin_init(publicId, systemId) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1390 | |
| 1391 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1392 | class DOMImplementation(DOMImplementationLS): |
| 1393 | _features = [("core", "1.0"), |
| 1394 | ("core", "2.0"), |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1395 | ("core", None), |
| 1396 | ("xml", "1.0"), |
| 1397 | ("xml", "2.0"), |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1398 | ("xml", None), |
| 1399 | ("ls-load", "3.0"), |
| 1400 | ("ls-load", None), |
| 1401 | ] |
| 1402 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1403 | def hasFeature(self, feature, version): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1404 | if version == "": |
| 1405 | version = None |
| 1406 | return (feature.lower(), version) in self._features |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1407 | |
| 1408 | def createDocument(self, namespaceURI, qualifiedName, doctype): |
| 1409 | if doctype and doctype.parentNode is not None: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1410 | raise xml.dom.WrongDocumentErr( |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 1411 | "doctype object owned by another DOM tree") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1412 | doc = self._create_document() |
| 1413 | |
| 1414 | add_root_element = not (namespaceURI is None |
| 1415 | and qualifiedName is None |
| 1416 | and doctype is None) |
| 1417 | |
| 1418 | if not qualifiedName and add_root_element: |
Martin v. Löwis | b417be2 | 2001-02-06 01:16:06 +0000 | [diff] [blame] | 1419 | # The spec is unclear what to raise here; SyntaxErr |
| 1420 | # would be the other obvious candidate. Since Xerces raises |
| 1421 | # InvalidCharacterErr, and since SyntaxErr is not listed |
| 1422 | # for createDocument, that seems to be the better choice. |
| 1423 | # XXX: need to check for illegal characters here and in |
| 1424 | # createElement. |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1425 | |
| 1426 | # DOM Level III clears this up when talking about the return value |
| 1427 | # of this function. If namespaceURI, qName and DocType are |
| 1428 | # Null the document is returned without a document element |
| 1429 | # Otherwise if doctype or namespaceURI are not None |
| 1430 | # Then we go back to the above problem |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1431 | raise xml.dom.InvalidCharacterErr("Element with no name") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1432 | |
| 1433 | if add_root_element: |
| 1434 | prefix, localname = _nssplit(qualifiedName) |
| 1435 | if prefix == "xml" \ |
| 1436 | and namespaceURI != "http://www.w3.org/XML/1998/namespace": |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1437 | raise xml.dom.NamespaceErr("illegal use of 'xml' prefix") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1438 | if prefix and not namespaceURI: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1439 | raise xml.dom.NamespaceErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1440 | "illegal use of prefix without namespaces") |
| 1441 | element = doc.createElementNS(namespaceURI, qualifiedName) |
| 1442 | if doctype: |
| 1443 | doc.appendChild(doctype) |
| 1444 | doc.appendChild(element) |
| 1445 | |
| 1446 | if doctype: |
| 1447 | doctype.parentNode = doctype.ownerDocument = doc |
| 1448 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1449 | doc.doctype = doctype |
| 1450 | doc.implementation = self |
| 1451 | return doc |
| 1452 | |
| 1453 | def createDocumentType(self, qualifiedName, publicId, systemId): |
| 1454 | doctype = DocumentType(qualifiedName) |
| 1455 | doctype.publicId = publicId |
| 1456 | doctype.systemId = systemId |
| 1457 | return doctype |
| 1458 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1459 | # DOM Level 3 (WD 9 April 2002) |
| 1460 | |
| 1461 | def getInterface(self, feature): |
| 1462 | if self.hasFeature(feature, None): |
| 1463 | return self |
| 1464 | else: |
| 1465 | return None |
| 1466 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1467 | # internal |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1468 | def _create_document(self): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1469 | return Document() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1470 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1471 | class ElementInfo(object): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1472 | """Object that represents content-model information for an element. |
| 1473 | |
| 1474 | This implementation is not expected to be used in practice; DOM |
| 1475 | builders should provide implementations which do the right thing |
| 1476 | using information available to it. |
| 1477 | |
| 1478 | """ |
| 1479 | |
| 1480 | __slots__ = 'tagName', |
| 1481 | |
| 1482 | def __init__(self, name): |
| 1483 | self.tagName = name |
| 1484 | |
| 1485 | def getAttributeType(self, aname): |
| 1486 | return _no_type |
| 1487 | |
| 1488 | def getAttributeTypeNS(self, namespaceURI, localName): |
| 1489 | return _no_type |
| 1490 | |
| 1491 | def isElementContent(self): |
| 1492 | return False |
| 1493 | |
| 1494 | def isEmpty(self): |
| 1495 | """Returns true iff this element is declared to have an EMPTY |
| 1496 | content model.""" |
| 1497 | return False |
| 1498 | |
| 1499 | def isId(self, aname): |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1500 | """Returns true iff the named attribute is a DTD-style ID.""" |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1501 | return False |
| 1502 | |
| 1503 | def isIdNS(self, namespaceURI, localName): |
| 1504 | """Returns true iff the identified attribute is a DTD-style ID.""" |
| 1505 | return False |
| 1506 | |
| 1507 | def __getstate__(self): |
| 1508 | return self.tagName |
| 1509 | |
| 1510 | def __setstate__(self, state): |
| 1511 | self.tagName = state |
| 1512 | |
| 1513 | def _clear_id_cache(node): |
| 1514 | if node.nodeType == Node.DOCUMENT_NODE: |
| 1515 | node._id_cache.clear() |
| 1516 | node._id_search_stack = None |
| 1517 | elif _in_document(node): |
| 1518 | node.ownerDocument._id_cache.clear() |
| 1519 | node.ownerDocument._id_search_stack= None |
| 1520 | |
| 1521 | class Document(Node, DocumentLS): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1522 | __slots__ = ('_elem_info', 'doctype', |
| 1523 | '_id_search_stack', 'childNodes', '_id_cache') |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1524 | _child_node_types = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, |
| 1525 | Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE) |
| 1526 | |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1527 | implementation = DOMImplementation() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 1528 | nodeType = Node.DOCUMENT_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 1529 | nodeName = "#document" |
| 1530 | nodeValue = None |
| 1531 | attributes = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1532 | parentNode = None |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1533 | previousSibling = nextSibling = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1534 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1535 | |
| 1536 | # Document attributes from Level 3 (WD 9 April 2002) |
| 1537 | |
| 1538 | actualEncoding = None |
| 1539 | encoding = None |
| 1540 | standalone = None |
| 1541 | version = None |
| 1542 | strictErrorChecking = False |
| 1543 | errorHandler = None |
| 1544 | documentURI = None |
| 1545 | |
| 1546 | _magic_id_count = 0 |
| 1547 | |
| 1548 | def __init__(self): |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1549 | self.doctype = None |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1550 | self.childNodes = NodeList() |
| 1551 | # mapping of (namespaceURI, localName) -> ElementInfo |
| 1552 | # and tagName -> ElementInfo |
| 1553 | self._elem_info = {} |
| 1554 | self._id_cache = {} |
| 1555 | self._id_search_stack = None |
| 1556 | |
| 1557 | def _get_elem_info(self, element): |
| 1558 | if element.namespaceURI: |
| 1559 | key = element.namespaceURI, element.localName |
| 1560 | else: |
| 1561 | key = element.tagName |
| 1562 | return self._elem_info.get(key) |
| 1563 | |
| 1564 | def _get_actualEncoding(self): |
| 1565 | return self.actualEncoding |
| 1566 | |
| 1567 | def _get_doctype(self): |
| 1568 | return self.doctype |
| 1569 | |
| 1570 | def _get_documentURI(self): |
| 1571 | return self.documentURI |
| 1572 | |
| 1573 | def _get_encoding(self): |
| 1574 | return self.encoding |
| 1575 | |
| 1576 | def _get_errorHandler(self): |
| 1577 | return self.errorHandler |
| 1578 | |
| 1579 | def _get_standalone(self): |
| 1580 | return self.standalone |
| 1581 | |
| 1582 | def _get_strictErrorChecking(self): |
| 1583 | return self.strictErrorChecking |
| 1584 | |
| 1585 | def _get_version(self): |
| 1586 | return self.version |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1587 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 1588 | def appendChild(self, node): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1589 | if node.nodeType not in self._child_node_types: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1590 | raise xml.dom.HierarchyRequestErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1591 | "%s cannot be child of %s" % (repr(node), repr(self))) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 1592 | if node.parentNode is not None: |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1593 | # This needs to be done before the next test since this |
| 1594 | # may *be* the document element, in which case it should |
| 1595 | # end up re-ordered to the end. |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 1596 | node.parentNode.removeChild(node) |
| 1597 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1598 | if node.nodeType == Node.ELEMENT_NODE \ |
| 1599 | and self._get_documentElement(): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1600 | raise xml.dom.HierarchyRequestErr( |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 1601 | "two document elements disallowed") |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 1602 | return Node.appendChild(self, node) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 1603 | |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 1604 | def removeChild(self, oldChild): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1605 | try: |
| 1606 | self.childNodes.remove(oldChild) |
| 1607 | except ValueError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1608 | raise xml.dom.NotFoundErr() |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 1609 | oldChild.nextSibling = oldChild.previousSibling = None |
| 1610 | oldChild.parentNode = None |
| 1611 | if self.documentElement is oldChild: |
| 1612 | self.documentElement = None |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 1613 | |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 1614 | return oldChild |
| 1615 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1616 | def _get_documentElement(self): |
| 1617 | for node in self.childNodes: |
| 1618 | if node.nodeType == Node.ELEMENT_NODE: |
| 1619 | return node |
| 1620 | |
| 1621 | def unlink(self): |
| 1622 | if self.doctype is not None: |
| 1623 | self.doctype.unlink() |
| 1624 | self.doctype = None |
| 1625 | Node.unlink(self) |
| 1626 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1627 | def cloneNode(self, deep): |
| 1628 | if not deep: |
| 1629 | return None |
| 1630 | clone = self.implementation.createDocument(None, None, None) |
| 1631 | clone.encoding = self.encoding |
| 1632 | clone.standalone = self.standalone |
| 1633 | clone.version = self.version |
| 1634 | for n in self.childNodes: |
| 1635 | childclone = _clone_node(n, deep, clone) |
| 1636 | assert childclone.ownerDocument.isSameNode(clone) |
| 1637 | clone.childNodes.append(childclone) |
| 1638 | if childclone.nodeType == Node.DOCUMENT_NODE: |
| 1639 | assert clone.documentElement is None |
| 1640 | elif childclone.nodeType == Node.DOCUMENT_TYPE_NODE: |
| 1641 | assert clone.doctype is None |
| 1642 | clone.doctype = childclone |
| 1643 | childclone.parentNode = clone |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1644 | self._call_user_data_handler(xml.dom.UserDataHandler.NODE_CLONED, |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1645 | self, clone) |
| 1646 | return clone |
| 1647 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1648 | def createDocumentFragment(self): |
| 1649 | d = DocumentFragment() |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1650 | d.ownerDocument = self |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1651 | return d |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1652 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1653 | def createElement(self, tagName): |
| 1654 | e = Element(tagName) |
| 1655 | e.ownerDocument = self |
| 1656 | return e |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1657 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1658 | def createTextNode(self, data): |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 1659 | if not isinstance(data, str): |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 1660 | raise TypeError("node contents must be a string") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1661 | t = Text() |
| 1662 | t.data = data |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1663 | t.ownerDocument = self |
| 1664 | return t |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1665 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1666 | def createCDATASection(self, data): |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 1667 | if not isinstance(data, str): |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 1668 | raise TypeError("node contents must be a string") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1669 | c = CDATASection() |
| 1670 | c.data = data |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 1671 | c.ownerDocument = self |
| 1672 | return c |
| 1673 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1674 | def createComment(self, data): |
| 1675 | c = Comment(data) |
| 1676 | c.ownerDocument = self |
| 1677 | return c |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1678 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1679 | def createProcessingInstruction(self, target, data): |
| 1680 | p = ProcessingInstruction(target, data) |
| 1681 | p.ownerDocument = self |
| 1682 | return p |
| 1683 | |
| 1684 | def createAttribute(self, qName): |
| 1685 | a = Attr(qName) |
| 1686 | a.ownerDocument = self |
Martin v. Löwis | cb67ea1 | 2001-03-31 16:30:40 +0000 | [diff] [blame] | 1687 | a.value = "" |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1688 | return a |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1689 | |
| 1690 | def createElementNS(self, namespaceURI, qualifiedName): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 1691 | prefix, localName = _nssplit(qualifiedName) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1692 | e = Element(qualifiedName, namespaceURI, prefix) |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1693 | e.ownerDocument = self |
| 1694 | return e |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1695 | |
| 1696 | def createAttributeNS(self, namespaceURI, qualifiedName): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 1697 | prefix, localName = _nssplit(qualifiedName) |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1698 | a = Attr(qualifiedName, namespaceURI, localName, prefix) |
| 1699 | a.ownerDocument = self |
Martin v. Löwis | cb67ea1 | 2001-03-31 16:30:40 +0000 | [diff] [blame] | 1700 | a.value = "" |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 1701 | return a |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1702 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1703 | # A couple of implementation-specific helpers to create node types |
| 1704 | # not supported by the W3C DOM specs: |
| 1705 | |
| 1706 | def _create_entity(self, name, publicId, systemId, notationName): |
| 1707 | e = Entity(name, publicId, systemId, notationName) |
| 1708 | e.ownerDocument = self |
| 1709 | return e |
| 1710 | |
| 1711 | def _create_notation(self, name, publicId, systemId): |
| 1712 | n = Notation(name, publicId, systemId) |
| 1713 | n.ownerDocument = self |
| 1714 | return n |
| 1715 | |
| 1716 | def getElementById(self, id): |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 1717 | if id in self._id_cache: |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1718 | return self._id_cache[id] |
| 1719 | if not (self._elem_info or self._magic_id_count): |
| 1720 | return None |
| 1721 | |
| 1722 | stack = self._id_search_stack |
| 1723 | if stack is None: |
| 1724 | # we never searched before, or the cache has been cleared |
| 1725 | stack = [self.documentElement] |
| 1726 | self._id_search_stack = stack |
| 1727 | elif not stack: |
| 1728 | # Previous search was completed and cache is still valid; |
| 1729 | # no matching node. |
| 1730 | return None |
| 1731 | |
| 1732 | result = None |
| 1733 | while stack: |
| 1734 | node = stack.pop() |
| 1735 | # add child elements to stack for continued searching |
| 1736 | stack.extend([child for child in node.childNodes |
| 1737 | if child.nodeType in _nodeTypes_with_children]) |
| 1738 | # check this node |
| 1739 | info = self._get_elem_info(node) |
| 1740 | if info: |
| 1741 | # We have to process all ID attributes before |
| 1742 | # returning in order to get all the attributes set to |
| 1743 | # be IDs using Element.setIdAttribute*(). |
| 1744 | for attr in node.attributes.values(): |
| 1745 | if attr.namespaceURI: |
| 1746 | if info.isIdNS(attr.namespaceURI, attr.localName): |
| 1747 | self._id_cache[attr.value] = node |
| 1748 | if attr.value == id: |
| 1749 | result = node |
| 1750 | elif not node._magic_id_nodes: |
| 1751 | break |
| 1752 | elif info.isId(attr.name): |
| 1753 | self._id_cache[attr.value] = node |
| 1754 | if attr.value == id: |
| 1755 | result = node |
| 1756 | elif not node._magic_id_nodes: |
| 1757 | break |
| 1758 | elif attr._is_id: |
| 1759 | self._id_cache[attr.value] = node |
| 1760 | if attr.value == id: |
| 1761 | result = node |
| 1762 | elif node._magic_id_nodes == 1: |
| 1763 | break |
| 1764 | elif node._magic_id_nodes: |
| 1765 | for attr in node.attributes.values(): |
| 1766 | if attr._is_id: |
| 1767 | self._id_cache[attr.value] = node |
| 1768 | if attr.value == id: |
| 1769 | result = node |
| 1770 | if result is not None: |
| 1771 | break |
| 1772 | return result |
| 1773 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 1774 | def getElementsByTagName(self, name): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1775 | return _get_elements_by_tagName_helper(self, name, NodeList()) |
Fred Drake | fbe7b4f | 2001-07-04 06:25:53 +0000 | [diff] [blame] | 1776 | |
| 1777 | def getElementsByTagNameNS(self, namespaceURI, localName): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1778 | return _get_elements_by_tagName_ns_helper( |
| 1779 | self, namespaceURI, localName, NodeList()) |
| 1780 | |
| 1781 | def isSupported(self, feature, version): |
| 1782 | return self.implementation.hasFeature(feature, version) |
| 1783 | |
| 1784 | def importNode(self, node, deep): |
| 1785 | if node.nodeType == Node.DOCUMENT_NODE: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1786 | raise xml.dom.NotSupportedErr("cannot import document nodes") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1787 | elif node.nodeType == Node.DOCUMENT_TYPE_NODE: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1788 | raise xml.dom.NotSupportedErr("cannot import document type nodes") |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1789 | return _clone_node(node, deep, self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1790 | |
Eli Bendersky | 8a80502 | 2012-07-13 09:52:39 +0300 | [diff] [blame] | 1791 | def writexml(self, writer, indent="", addindent="", newl="", encoding=None): |
Martin v. Löwis | 7d650ca | 2002-06-30 15:05:00 +0000 | [diff] [blame] | 1792 | if encoding is None: |
Georg Brandl | 175a7dc | 2005-08-25 22:02:43 +0000 | [diff] [blame] | 1793 | writer.write('<?xml version="1.0" ?>'+newl) |
Martin v. Löwis | 7d650ca | 2002-06-30 15:05:00 +0000 | [diff] [blame] | 1794 | else: |
Eli Bendersky | 8a80502 | 2012-07-13 09:52:39 +0300 | [diff] [blame] | 1795 | writer.write('<?xml version="1.0" encoding="%s"?>%s' % ( |
| 1796 | encoding, newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1797 | for node in self.childNodes: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 1798 | node.writexml(writer, indent, addindent, newl) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1799 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1800 | # DOM Level 3 (WD 9 April 2002) |
| 1801 | |
| 1802 | def renameNode(self, n, namespaceURI, name): |
| 1803 | if n.ownerDocument is not self: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1804 | raise xml.dom.WrongDocumentErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1805 | "cannot rename nodes from other documents;\n" |
| 1806 | "expected %s,\nfound %s" % (self, n.ownerDocument)) |
| 1807 | if n.nodeType not in (Node.ELEMENT_NODE, Node.ATTRIBUTE_NODE): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1808 | raise xml.dom.NotSupportedErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1809 | "renameNode() only applies to element and attribute nodes") |
| 1810 | if namespaceURI != EMPTY_NAMESPACE: |
| 1811 | if ':' in name: |
| 1812 | prefix, localName = name.split(':', 1) |
| 1813 | if ( prefix == "xmlns" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1814 | and namespaceURI != xml.dom.XMLNS_NAMESPACE): |
| 1815 | raise xml.dom.NamespaceErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1816 | "illegal use of 'xmlns' prefix") |
| 1817 | else: |
| 1818 | if ( name == "xmlns" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1819 | and namespaceURI != xml.dom.XMLNS_NAMESPACE |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1820 | and n.nodeType == Node.ATTRIBUTE_NODE): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1821 | raise xml.dom.NamespaceErr( |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1822 | "illegal use of the 'xmlns' attribute") |
| 1823 | prefix = None |
| 1824 | localName = name |
| 1825 | else: |
| 1826 | prefix = None |
| 1827 | localName = None |
| 1828 | if n.nodeType == Node.ATTRIBUTE_NODE: |
| 1829 | element = n.ownerElement |
| 1830 | if element is not None: |
| 1831 | is_id = n._is_id |
| 1832 | element.removeAttributeNode(n) |
| 1833 | else: |
| 1834 | element = None |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1835 | n.prefix = prefix |
| 1836 | n._localName = localName |
| 1837 | n.namespaceURI = namespaceURI |
| 1838 | n.nodeName = name |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1839 | if n.nodeType == Node.ELEMENT_NODE: |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1840 | n.tagName = name |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1841 | else: |
| 1842 | # attribute node |
Martin v. Löwis | 14aa280 | 2012-02-19 20:25:12 +0100 | [diff] [blame] | 1843 | n.name = name |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1844 | if element is not None: |
| 1845 | element.setAttributeNode(n) |
| 1846 | if is_id: |
| 1847 | element.setIdAttributeNode(n) |
| 1848 | # It's not clear from a semantic perspective whether we should |
| 1849 | # call the user data handlers for the NODE_RENAMED event since |
| 1850 | # we're re-using the existing node. The draft spec has been |
| 1851 | # interpreted as meaning "no, don't call the handler unless a |
| 1852 | # new node is created." |
| 1853 | return n |
| 1854 | |
| 1855 | defproperty(Document, "documentElement", |
| 1856 | doc="Top-level element of this document.") |
| 1857 | |
| 1858 | |
| 1859 | def _clone_node(node, deep, newOwnerDocument): |
| 1860 | """ |
| 1861 | Clone a node and give it the new owner document. |
| 1862 | Called by Node.cloneNode and Document.importNode |
| 1863 | """ |
| 1864 | if node.ownerDocument.isSameNode(newOwnerDocument): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1865 | operation = xml.dom.UserDataHandler.NODE_CLONED |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1866 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1867 | operation = xml.dom.UserDataHandler.NODE_IMPORTED |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1868 | if node.nodeType == Node.ELEMENT_NODE: |
| 1869 | clone = newOwnerDocument.createElementNS(node.namespaceURI, |
| 1870 | node.nodeName) |
| 1871 | for attr in node.attributes.values(): |
| 1872 | clone.setAttributeNS(attr.namespaceURI, attr.nodeName, attr.value) |
| 1873 | a = clone.getAttributeNodeNS(attr.namespaceURI, attr.localName) |
| 1874 | a.specified = attr.specified |
| 1875 | |
| 1876 | if deep: |
| 1877 | for child in node.childNodes: |
| 1878 | c = _clone_node(child, deep, newOwnerDocument) |
| 1879 | clone.appendChild(c) |
| 1880 | |
| 1881 | elif node.nodeType == Node.DOCUMENT_FRAGMENT_NODE: |
| 1882 | clone = newOwnerDocument.createDocumentFragment() |
| 1883 | if deep: |
| 1884 | for child in node.childNodes: |
| 1885 | c = _clone_node(child, deep, newOwnerDocument) |
| 1886 | clone.appendChild(c) |
| 1887 | |
| 1888 | elif node.nodeType == Node.TEXT_NODE: |
| 1889 | clone = newOwnerDocument.createTextNode(node.data) |
| 1890 | elif node.nodeType == Node.CDATA_SECTION_NODE: |
| 1891 | clone = newOwnerDocument.createCDATASection(node.data) |
| 1892 | elif node.nodeType == Node.PROCESSING_INSTRUCTION_NODE: |
| 1893 | clone = newOwnerDocument.createProcessingInstruction(node.target, |
| 1894 | node.data) |
| 1895 | elif node.nodeType == Node.COMMENT_NODE: |
| 1896 | clone = newOwnerDocument.createComment(node.data) |
| 1897 | elif node.nodeType == Node.ATTRIBUTE_NODE: |
| 1898 | clone = newOwnerDocument.createAttributeNS(node.namespaceURI, |
| 1899 | node.nodeName) |
| 1900 | clone.specified = True |
| 1901 | clone.value = node.value |
| 1902 | elif node.nodeType == Node.DOCUMENT_TYPE_NODE: |
| 1903 | assert node.ownerDocument is not newOwnerDocument |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1904 | operation = xml.dom.UserDataHandler.NODE_IMPORTED |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1905 | clone = newOwnerDocument.implementation.createDocumentType( |
| 1906 | node.name, node.publicId, node.systemId) |
| 1907 | clone.ownerDocument = newOwnerDocument |
| 1908 | if deep: |
| 1909 | clone.entities._seq = [] |
| 1910 | clone.notations._seq = [] |
| 1911 | for n in node.notations._seq: |
| 1912 | notation = Notation(n.nodeName, n.publicId, n.systemId) |
| 1913 | notation.ownerDocument = newOwnerDocument |
| 1914 | clone.notations._seq.append(notation) |
| 1915 | if hasattr(n, '_call_user_data_handler'): |
| 1916 | n._call_user_data_handler(operation, n, notation) |
| 1917 | for e in node.entities._seq: |
| 1918 | entity = Entity(e.nodeName, e.publicId, e.systemId, |
| 1919 | e.notationName) |
| 1920 | entity.actualEncoding = e.actualEncoding |
| 1921 | entity.encoding = e.encoding |
| 1922 | entity.version = e.version |
| 1923 | entity.ownerDocument = newOwnerDocument |
| 1924 | clone.entities._seq.append(entity) |
| 1925 | if hasattr(e, '_call_user_data_handler'): |
| 1926 | e._call_user_data_handler(operation, n, entity) |
| 1927 | else: |
| 1928 | # Note the cloning of Document and DocumentType nodes is |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1929 | # implementation specific. minidom handles those cases |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1930 | # directly in the cloneNode() methods. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1931 | raise xml.dom.NotSupportedErr("Cannot clone node %s" % repr(node)) |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1932 | |
| 1933 | # Check for _call_user_data_handler() since this could conceivably |
| 1934 | # used with other DOM implementations (one of the FourThought |
| 1935 | # DOMs, perhaps?). |
| 1936 | if hasattr(node, '_call_user_data_handler'): |
| 1937 | node._call_user_data_handler(operation, node, clone) |
| 1938 | return clone |
| 1939 | |
| 1940 | |
| 1941 | def _nssplit(qualifiedName): |
| 1942 | fields = qualifiedName.split(':', 1) |
| 1943 | if len(fields) == 2: |
| 1944 | return fields |
| 1945 | else: |
| 1946 | return (None, fields[0]) |
| 1947 | |
| 1948 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1949 | def _do_pulldom_parse(func, args, kwargs): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1950 | events = func(*args, **kwargs) |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 1951 | toktype, rootNode = events.getEvent() |
| 1952 | events.expandNode(rootNode) |
Martin v. Löwis | b417be2 | 2001-02-06 01:16:06 +0000 | [diff] [blame] | 1953 | events.clear() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1954 | return rootNode |
| 1955 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1956 | def parse(file, parser=None, bufsize=None): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1957 | """Parse a file into a DOM by filename or file object.""" |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1958 | if parser is None and not bufsize: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1959 | from xml.dom import expatbuilder |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1960 | return expatbuilder.parse(file) |
| 1961 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1962 | from xml.dom import pulldom |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1963 | return _do_pulldom_parse(pulldom.parse, (file,), |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1964 | {'parser': parser, 'bufsize': bufsize}) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 1965 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1966 | def parseString(string, parser=None): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 1967 | """Parse a file into a DOM from a string.""" |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1968 | if parser is None: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1969 | from xml.dom import expatbuilder |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1970 | return expatbuilder.parseString(string) |
| 1971 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1972 | from xml.dom import pulldom |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1973 | return _do_pulldom_parse(pulldom.parseString, (string,), |
| 1974 | {'parser': parser}) |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 1975 | |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1976 | def getDOMImplementation(features=None): |
| 1977 | if features: |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 1978 | if isinstance(features, str): |
Martin v. Löwis | 787354c | 2003-01-25 15:28:29 +0000 | [diff] [blame] | 1979 | features = domreg._parse_feature_string(features) |
| 1980 | for f, v in features: |
| 1981 | if not Document.implementation.hasFeature(f, v): |
| 1982 | return None |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 1983 | return Document.implementation |