Python: Pull group, extract and filter order fixes from JavaScript
This commit is contained in:
@@ -73,6 +73,15 @@ class Element(object):
|
|||||||
oldNode.previousSibling = None
|
oldNode.previousSibling = None
|
||||||
oldNode.nextSibling = 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):
|
def normalize(self):
|
||||||
lastTextNode = None
|
lastTextNode = None
|
||||||
for childNode in list(self.childNodes):
|
for childNode in list(self.childNodes):
|
||||||
@@ -320,7 +329,7 @@ def ExtractElement(nodeName):
|
|||||||
return
|
return
|
||||||
parentNode = node.parentNode
|
parentNode = node.parentNode
|
||||||
for childNode in node.childNodes:
|
for childNode in node.childNodes:
|
||||||
parentNode.appendChild(childNode)
|
parentNode.insertBefore(childNode, node)
|
||||||
parentNode.removeChild(node)
|
parentNode.removeChild(node)
|
||||||
parentNode.normalize()
|
parentNode.normalize()
|
||||||
return Filter
|
return Filter
|
||||||
@@ -330,13 +339,15 @@ def GroupSiblings(parentName, childNames):
|
|||||||
def Filter(node):
|
def Filter(node):
|
||||||
if node.nodeName not in childNames:
|
if node.nodeName not in childNames:
|
||||||
return
|
return
|
||||||
if (node.previousSibling and
|
nodes = []
|
||||||
node.previousSibling.nodeName == parentName):
|
iterNode = node
|
||||||
node.previousSibling.appendChild(node)
|
while iterNode and iterNode.nodeName in childNames:
|
||||||
return
|
nodes.append(iterNode)
|
||||||
|
iterNode = iterNode.nextSibling
|
||||||
newNode = Element(parentName)
|
newNode = Element(parentName)
|
||||||
node.parentNode.replaceChild(newNode, node)
|
node.parentNode.replaceChild(newNode, node)
|
||||||
newNode.appendChild(node)
|
for childNode in nodes:
|
||||||
|
newNode.appendChild(childNode)
|
||||||
return Filter
|
return Filter
|
||||||
|
|
||||||
|
|
||||||
@@ -372,8 +383,10 @@ def SplitElementAndNest(originalName, newNames):
|
|||||||
|
|
||||||
def ApplyFilter(node, callback):
|
def ApplyFilter(node, callback):
|
||||||
callback(node)
|
callback(node)
|
||||||
for childNode in list(node.childNodes):
|
i = 0
|
||||||
ApplyFilter(childNode, callback)
|
while i < len(node.childNodes):
|
||||||
|
ApplyFilter(node.childNodes[i], callback)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
def ApplyFilters(node, filters):
|
def ApplyFilters(node, filters):
|
||||||
|
|||||||
2
test.py
2
test.py
@@ -48,6 +48,6 @@ code looks like <code>this</code>. Fixed width text looks like
|
|||||||
<tt>this</tt>. <pre>This sentence is inline pre-formatted, which stops
|
<tt>this</tt>. <pre>This sentence is inline pre-formatted, which stops
|
||||||
'''''this from being bold and italic.'''''</pre> We can also
|
'''''this from being bold and italic.'''''</pre> We can also
|
||||||
stop <u>this from being underlined</u>, or just try
|
stop <u>this from being underlined</u>, or just try
|
||||||
<pre>interrupting cow style.</pre><blockquote>This is a blockquote</blockquote></p><p><h2>Header 2</h2><h3>Header 3 <i>with italics</i></h3><h4>Header 4</h4><h5>Header 5</h5><h6>Header 6</h6><hr></hr><ul><li>Item 1a</li><li>Item 1b</li><ul><li>Item 2</li><ul><li>Item 3</li></ul></ul><li>Item 1c</li></ul><ol><li>Item 1a</li><li>Item 1b</li><ol><li>Item 2</li><ol><li>Item 3</li></ol></ol><li>Item 1c</li></ol><def>I don't really understand what a definition is</def><blockquote>But blockquotes are easy</blockquote><blockquote2>Even larger ones</blockquote2><blockquote5>And really huge ones</blockquote5><pre>This line is pre-formatted and <del>not interpolated</del>
|
<pre>interrupting cow style.</pre><blockquote>This is a blockquote</blockquote></p><p><h2>Header 2</h2><h3>Header 3 <i>with italics</i></h3><h4>Header 4</h4><h5>Header 5</h5><h6>Header 6</h6><hr></hr><ul><li>Item 1a</li><li>Item 1b</li><ul><li>Item 2</li><ul><li>Item 3</li></ul></ul><li>Item 1c</li></ul><ol><li>Item 1a</li><li>Item 1b</li><ol><li>Item 2</li><ol><li>Item 3</li></ol></ol><li>Item 1c</li></ol><def>I don't really understand what a definition is</def><blockquote>But blockquotes are easy<blockquote>Even larger ones<blockquote><blockquote><blockquote>And really huge ones</blockquote></blockquote></blockquote></blockquote></blockquote><pre>This line is pre-formatted and <del>not interpolated</del>
|
||||||
This line is also pre-formatted
|
This line is also pre-formatted
|
||||||
</pre></p></wikidoc>""", result
|
</pre></p></wikidoc>""", result
|
||||||
|
|||||||
Reference in New Issue
Block a user