diff --git a/recentrunes.py b/recentrunes.py
index 4de5baa..54ea7ac 100644
--- a/recentrunes.py
+++ b/recentrunes.py
@@ -73,6 +73,15 @@ class Element(object):
oldNode.previousSibling = None
oldNode.nextSibling = None
+ def insertBefore(self, newNode, oldNode):
+ index = self.childNodes.index(oldNode)
+ self.childNodes.insert(index, newNode)
+ if oldNode.previousSibling:
+ oldNode.previousSibling.nextSibling = newNode
+ newNode.previousSibling = oldNode.previousSibling
+ oldNode.previousSibling = newNode
+ newNode.nextSibling = oldNode
+
def normalize(self):
lastTextNode = None
for childNode in list(self.childNodes):
@@ -320,7 +329,7 @@ def ExtractElement(nodeName):
return
parentNode = node.parentNode
for childNode in node.childNodes:
- parentNode.appendChild(childNode)
+ parentNode.insertBefore(childNode, node)
parentNode.removeChild(node)
parentNode.normalize()
return Filter
@@ -330,13 +339,15 @@ def GroupSiblings(parentName, childNames):
def Filter(node):
if node.nodeName not in childNames:
return
- if (node.previousSibling and
- node.previousSibling.nodeName == parentName):
- node.previousSibling.appendChild(node)
- return
+ nodes = []
+ iterNode = node
+ while iterNode and iterNode.nodeName in childNames:
+ nodes.append(iterNode)
+ iterNode = iterNode.nextSibling
newNode = Element(parentName)
node.parentNode.replaceChild(newNode, node)
- newNode.appendChild(node)
+ for childNode in nodes:
+ newNode.appendChild(childNode)
return Filter
@@ -372,8 +383,10 @@ def SplitElementAndNest(originalName, newNames):
def ApplyFilter(node, callback):
callback(node)
- for childNode in list(node.childNodes):
- ApplyFilter(childNode, callback)
+ i = 0
+ while i < len(node.childNodes):
+ ApplyFilter(node.childNodes[i], callback)
+ i += 1
def ApplyFilters(node, filters):
diff --git a/test.py b/test.py
index 692023c..429b9ba 100755
--- a/test.py
+++ b/test.py
@@ -48,6 +48,6 @@ code looks like this. Fixed width text looks like
this.
This sentence is inline pre-formatted, which stops '''''this from being bold and italic.'''''We can also stop <u>this from being underlined</u>, or just try -<pre>interrupting cow style.</pre>
This is a blockquote
But blockquotes are easy
This line is pre-formatted and <del>not interpolated</del> +<pre>interrupting cow style.</pre>This is a blockquoteHeader 2
Header 3 with italics
Header 4
Header 5
Header 6
But blockquotes are easyEven larger onesAnd really huge ones
This line is pre-formatted and <del>not interpolated</del> This line is also pre-formatted""", result