From ae2828b2c2b154b81b1759ddeb94cc6fd3f3468e Mon Sep 17 00:00:00 2001
From: Ian Gulliver
Date: Wed, 2 Jul 2014 14:01:19 -0700
Subject: [PATCH] Python: fix DOM bugs that were breaking grouping filter
---
recentrunes.py | 24 +++++++++++++++++++++---
test.py | 4 ++--
2 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/recentrunes.py b/recentrunes.py
index 8bf1b8c..4de5baa 100644
--- a/recentrunes.py
+++ b/recentrunes.py
@@ -9,8 +9,10 @@ class MatchResult(collections.namedtuple('MatchResult', ['context', 'nodes'])):
class TextNode(object):
def __init__(self, textContent):
self.nodeName = '#text'
+ self.parentNode = None
self.textContent = textContent
self.previousSibling = None
+ self.nextSibling = None
self.childNodes = []
def cloneNode(self, deep):
@@ -31,29 +33,45 @@ class Element(object):
self.nodeName = nodeName
self.parentNode = None
self.previousSibling = None
+ self.nextSibling = None
self.childNodes = []
self.attributes = {}
def appendChild(self, child):
+ if child.parentNode:
+ child.parentNode.removeChild(child)
child.parentNode = self
self.childNodes.append(child)
if len(self.childNodes) == 1:
child.previousSibling = None
else:
- child.previousSibling = self.childNodes[len(self.childNodes) - 2]
+ beforeChild = self.childNodes[len(self.childNodes) - 2]
+ child.previousSibling = beforeChild
+ beforeChild.nextSibling = child
def removeChild(self, child):
self.childNodes.remove(child)
+ if child.previousSibling:
+ child.previousSibling.nextSibling = child.nextSibling
+ if child.nextSibling:
+ child.nextSibling.previousSibling = child.previousSibling
child.parentNode = None
child.previousSibling = None
+ child.nextSibling = None
def replaceChild(self, newNode, oldNode):
index = self.childNodes.index(oldNode)
self.childNodes[index] = newNode
newNode.parentNode = self
- oldNode.parentNode = None
newNode.previousSibling = oldNode.previousSibling
+ newNode.nextSibling = oldNode.nextSibling
+ if newNode.previousSibling:
+ newNode.previousSibling.nextSibling = newNode
+ if newNode.nextSibling:
+ newNode.nextSibling.previousSibling = newNode
+ oldNode.parentNode = None
oldNode.previousSibling = None
+ oldNode.nextSibling = None
def normalize(self):
lastTextNode = None
@@ -354,7 +372,7 @@ def SplitElementAndNest(originalName, newNames):
def ApplyFilter(node, callback):
callback(node)
- for childNode in node.childNodes:
+ for childNode in list(node.childNodes):
ApplyFilter(childNode, callback)
diff --git a/test.py b/test.py
index 34f6f89..63a4d5e 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
Header 2
Header 3 with italics
Header 4
Header 5
Header 6
- Item 1a
- Item 1b
- Item 2
- Item 3
- Item 1c
I don't really understand what a definition isBut blockquotes are easy
Even larger onesAnd really huge onesThis line is pre-formatted and <del>not interpolated</del>
-
This line is also pre-formatted
+<pre>interrupting cow style.</pre>This is a blockquote
Header 2
Header 3 with italics
Header 4
Header 5
Header 6
- Item 1a
- Item 1b
- Item 2
- Item 3
- Item 1c
I don't really understand what a definition isBut blockquotes are easy
Even larger onesAnd really huge onesThis line is pre-formatted and <del>not interpolated</del>
+This line is also pre-formatted
"""