Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc6da8a

Browse files
committed
Merged revisions 71414 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r71414 | r.david.murray | 2009-04-09 17:54:50 -0400 (Thu, 09 Apr 2009) | 3 lines Issue #2170: refactored xml.dom.minidom.normalize, increasing both its clarity and its speed. ........
1 parent a619180 commit dc6da8a

3 files changed

Lines changed: 175 additions & 18 deletions

File tree

Lib/test/test_minidom.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,167 @@ def testNormalize(self):
789789
"testNormalize -- single empty node removed")
790790
doc.unlink()
791791

792+
def testNormalizeCombineAndNextSibling(self):
793+
doc = parseString("<doc/>")
794+
root = doc.documentElement
795+
root.appendChild(doc.createTextNode("first"))
796+
root.appendChild(doc.createTextNode("second"))
797+
root.appendChild(doc.createElement("i"))
798+
self.confirm(len(root.childNodes) == 3
799+
and root.childNodes.length == 3,
800+
"testNormalizeCombineAndNextSibling -- preparation")
801+
doc.normalize()
802+
self.confirm(len(root.childNodes) == 2
803+
and root.childNodes.length == 2
804+
and root.firstChild.data == "firstsecond"
805+
and root.firstChild is not root.lastChild
806+
and root.firstChild.nextSibling is root.lastChild
807+
and root.firstChild.previousSibling is None
808+
and root.lastChild.previousSibling is root.firstChild
809+
and root.lastChild.nextSibling is None
810+
, "testNormalizeCombinedAndNextSibling -- result")
811+
doc.unlink()
812+
813+
def testNormalizeDeleteWithPrevSibling(self):
814+
doc = parseString("<doc/>")
815+
root = doc.documentElement
816+
root.appendChild(doc.createTextNode("first"))
817+
root.appendChild(doc.createTextNode(""))
818+
self.confirm(len(root.childNodes) == 2
819+
and root.childNodes.length == 2,
820+
"testNormalizeDeleteWithPrevSibling -- preparation")
821+
doc.normalize()
822+
self.confirm(len(root.childNodes) == 1
823+
and root.childNodes.length == 1
824+
and root.firstChild.data == "first"
825+
and root.firstChild is root.lastChild
826+
and root.firstChild.nextSibling is None
827+
and root.firstChild.previousSibling is None
828+
, "testNormalizeDeleteWithPrevSibling -- result")
829+
doc.unlink()
830+
831+
def testNormalizeDeleteWithNextSibling(self):
832+
doc = parseString("<doc/>")
833+
root = doc.documentElement
834+
root.appendChild(doc.createTextNode(""))
835+
root.appendChild(doc.createTextNode("second"))
836+
self.confirm(len(root.childNodes) == 2
837+
and root.childNodes.length == 2,
838+
"testNormalizeDeleteWithNextSibling -- preparation")
839+
doc.normalize()
840+
self.confirm(len(root.childNodes) == 1
841+
and root.childNodes.length == 1
842+
and root.firstChild.data == "second"
843+
and root.firstChild is root.lastChild
844+
and root.firstChild.nextSibling is None
845+
and root.firstChild.previousSibling is None
846+
, "testNormalizeDeleteWithNextSibling -- result")
847+
doc.unlink()
848+
849+
def testNormalizeDeleteWithTwoNonTextSiblings(self):
850+
doc = parseString("<doc/>")
851+
root = doc.documentElement
852+
root.appendChild(doc.createElement("i"))
853+
root.appendChild(doc.createTextNode(""))
854+
root.appendChild(doc.createElement("i"))
855+
self.confirm(len(root.childNodes) == 3
856+
and root.childNodes.length == 3,
857+
"testNormalizeDeleteWithTwoSiblings -- preparation")
858+
doc.normalize()
859+
self.confirm(len(root.childNodes) == 2
860+
and root.childNodes.length == 2
861+
and root.firstChild is not root.lastChild
862+
and root.firstChild.nextSibling is root.lastChild
863+
and root.firstChild.previousSibling is None
864+
and root.lastChild.previousSibling is root.firstChild
865+
and root.lastChild.nextSibling is None
866+
, "testNormalizeDeleteWithTwoSiblings -- result")
867+
doc.unlink()
868+
869+
def testNormalizeDeleteAndCombine(self):
870+
doc = parseString("<doc/>")
871+
root = doc.documentElement
872+
root.appendChild(doc.createTextNode(""))
873+
root.appendChild(doc.createTextNode("second"))
874+
root.appendChild(doc.createTextNode(""))
875+
root.appendChild(doc.createTextNode("fourth"))
876+
root.appendChild(doc.createTextNode(""))
877+
self.confirm(len(root.childNodes) == 5
878+
and root.childNodes.length == 5,
879+
"testNormalizeDeleteAndCombine -- preparation")
880+
doc.normalize()
881+
self.confirm(len(root.childNodes) == 1
882+
and root.childNodes.length == 1
883+
and root.firstChild is root.lastChild
884+
and root.firstChild.data == "secondfourth"
885+
and root.firstChild.previousSibling is None
886+
and root.firstChild.nextSibling is None
887+
, "testNormalizeDeleteAndCombine -- result")
888+
doc.unlink()
889+
890+
def testNormalizeRecursion(self):
891+
doc = parseString("<doc>"
892+
"<o>"
893+
"<i/>"
894+
"t"
895+
#
896+
#x
897+
"</o>"
898+
"<o>"
899+
"<o>"
900+
"t2"
901+
#x2
902+
"</o>"
903+
"t3"
904+
#x3
905+
"</o>"
906+
#
907+
"</doc>")
908+
root = doc.documentElement
909+
root.childNodes[0].appendChild(doc.createTextNode(""))
910+
root.childNodes[0].appendChild(doc.createTextNode("x"))
911+
root.childNodes[1].childNodes[0].appendChild(doc.createTextNode("x2"))
912+
root.childNodes[1].appendChild(doc.createTextNode("x3"))
913+
root.appendChild(doc.createTextNode(""))
914+
self.confirm(len(root.childNodes) == 3
915+
and root.childNodes.length == 3
916+
and len(root.childNodes[0].childNodes) == 4
917+
and root.childNodes[0].childNodes.length == 4
918+
and len(root.childNodes[1].childNodes) == 3
919+
and root.childNodes[1].childNodes.length == 3
920+
and len(root.childNodes[1].childNodes[0].childNodes) == 2
921+
and root.childNodes[1].childNodes[0].childNodes.length == 2
922+
, "testNormalize2 -- preparation")
923+
doc.normalize()
924+
self.confirm(len(root.childNodes) == 2
925+
and root.childNodes.length == 2
926+
and len(root.childNodes[0].childNodes) == 2
927+
and root.childNodes[0].childNodes.length == 2
928+
and len(root.childNodes[1].childNodes) == 2
929+
and root.childNodes[1].childNodes.length == 2
930+
and len(root.childNodes[1].childNodes[0].childNodes) == 1
931+
and root.childNodes[1].childNodes[0].childNodes.length == 1
932+
, "testNormalize2 -- childNodes lengths")
933+
self.confirm(root.childNodes[0].childNodes[1].data == "tx"
934+
and root.childNodes[1].childNodes[0].childNodes[0].data == "t2x2"
935+
and root.childNodes[1].childNodes[1].data == "t3x3"
936+
, "testNormalize2 -- joined text fields")
937+
self.confirm(root.childNodes[0].childNodes[1].nextSibling is None
938+
and root.childNodes[0].childNodes[1].previousSibling
939+
is root.childNodes[0].childNodes[0]
940+
and root.childNodes[0].childNodes[0].previousSibling is None
941+
and root.childNodes[0].childNodes[0].nextSibling
942+
is root.childNodes[0].childNodes[1]
943+
and root.childNodes[1].childNodes[1].nextSibling is None
944+
and root.childNodes[1].childNodes[1].previousSibling
945+
is root.childNodes[1].childNodes[0]
946+
and root.childNodes[1].childNodes[0].previousSibling is None
947+
and root.childNodes[1].childNodes[0].nextSibling
948+
is root.childNodes[1].childNodes[1]
949+
, "testNormalize2 -- sibling pointers")
950+
doc.unlink()
951+
952+
792953
def testBug1433694(self):
793954
doc = parseString("<o><i/>t</o>")
794955
node = doc.documentElement

Lib/xml/dom/minidom.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,34 +179,27 @@ def normalize(self):
179179
L = []
180180
for child in self.childNodes:
181181
if child.nodeType == Node.TEXT_NODE:
182-
data = child.data
183-
if data and L and L[-1].nodeType == child.nodeType:
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:
184190
# collapse text node
185191
node = L[-1]
186192
node.data = node.data + child.data
187193
node.nextSibling = child.nextSibling
194+
if child.nextSibling:
195+
child.nextSibling.previousSibling = node
188196
child.unlink()
189-
elif data:
190-
if L:
191-
L[-1].nextSibling = child
192-
child.previousSibling = L[-1]
193-
else:
194-
child.previousSibling = None
195-
L.append(child)
196197
else:
197-
# empty text node; discard
198-
child.unlink()
198+
L.append(child)
199199
else:
200-
if L:
201-
L[-1].nextSibling = child
202-
child.previousSibling = L[-1]
203-
else:
204-
child.previousSibling = None
205200
L.append(child)
206201
if child.nodeType == Node.ELEMENT_NODE:
207202
child.normalize()
208-
if L:
209-
L[-1].nextSibling = None
210203
self.childNodes[:] = L
211204

212205
def cloneNode(self, deep):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.1 beta 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #2170: refactored xml.dom.minidom.normalize, increasing both
16+
its clarity and its speed.
17+
1518
- Issue #1113244: Py_XINCREF, Py_DECREF, Py_XDECREF: Add `do { ... } while (0)'
1619
to avoid compiler warnings.
1720

0 commit comments

Comments
 (0)