Skip to content

Commit ca84401

Browse files
author
James Graham
committed
Implement the Noah's Ark algorithm
--HG-- extra : transplant_source : %B2V%FAS%81%F9%8E%EB%C1%16%A6%84%1Bq%B9%06%8B%E4%D5%C9
1 parent abd0b0b commit ca84401

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

html5lib/html5parser.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,11 +977,35 @@ def __init__(self, parser, tree):
977977
])
978978
self.endTagHandler.default = self.endTagOther
979979

980+
def isMatchingFormattingElement(self, node1, node2):
981+
if node1.name != node2.name or node1.namespace != node2.namespace:
982+
return False
983+
elif len(node1.attributes) != len(node2.attributes):
984+
return False
985+
else:
986+
attributes1 = sorted(node1.attributes.items())
987+
attributes2 = sorted(node2.attributes.items())
988+
for attr1, attr2 in zip(attributes1, attributes2):
989+
if attr1 != attr2:
990+
return False
991+
return True
992+
980993
# helper
981994
def addFormattingElement(self, token):
982995
self.tree.insertElement(token)
983-
self.tree.activeFormattingElements.append(
984-
self.tree.openElements[-1])
996+
element = self.tree.openElements[-1]
997+
998+
matchingElements = []
999+
for node in self.tree.activeFormattingElements[::-1]:
1000+
if node is Marker:
1001+
break
1002+
elif self.isMatchingFormattingElement(node, element):
1003+
matchingElements.append(node)
1004+
1005+
assert len(matchingElements) <= 3
1006+
if len(matchingElements) == 3:
1007+
self.tree.activeFormattingElements.remove(matchingElements[-1])
1008+
self.tree.activeFormattingElements.append(element)
9851009

9861010
# the real deal
9871011
def processEOF(self):
@@ -2422,6 +2446,7 @@ def processEndTag(self, token):
24222446

24232447
while True:
24242448
if node.name.translate(asciiUpper2Lower) == token["name"]:
2449+
#XXX this isn't in the spec but it seems necessary
24252450
if self.parser.phase == self.parser.phases["inTableText"]:
24262451
self.parser.phase.flushCharacters()
24272452
self.parser.phase = self.parser.phase.originalPhase

html5lib/treebuilders/dom.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ def getDomModule(DomImplementation):
2323

2424
def getDomBuilder(DomImplementation):
2525
Dom = DomImplementation
26-
class AttrList:
26+
class AttrList(object):
2727
def __init__(self, element):
2828
self.element = element
2929
def __iter__(self):
3030
return self.element.attributes.items().__iter__()
3131
def __setitem__(self, name, value):
3232
self.element.setAttribute(name, value)
33+
def __len__(self):
34+
return len(self.element.attributes.items())
3335
def items(self):
3436
return [(item[0], item[1]) for item in
3537
self.element.attributes.items()]

0 commit comments

Comments
 (0)