Python: Pull group, extract and filter order fixes from JavaScript

This commit is contained in:
Ian Gulliver
2014-07-03 01:20:40 -04:00
parent 71a687cc25
commit 1db15bee52
2 changed files with 22 additions and 9 deletions

View File

@@ -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):

View File

@@ -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
'''''this from being bold and italic.'''''</pre> We can also
stop &lt;u&gt;this from being underlined&lt;/u&gt;, or just try
&lt;pre&gt;interrupting cow style.&lt;/pre&gt;<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 &lt;del&gt;not interpolated&lt;/del&gt;
&lt;pre&gt;interrupting cow style.&lt;/pre&gt;<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 &lt;del&gt;not interpolated&lt;/del&gt;
This line is also pre-formatted
</pre></p></wikidoc>""", result