Skip to content

Commit 84d8a74

Browse files
ambvgsnedders
authored andcommitted
Add test for sax treeadapter.
1 parent 0c8d7bc commit 84d8a74

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

html5lib/tests/support.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import codecs
66
import glob
7+
import xml.sax.handler
78

89
base_path = os.path.split(__file__)[0]
910

@@ -130,3 +131,47 @@ def errorMessage(input, expected, actual):
130131
if sys.version_info.major == 2:
131132
msg = msg.encode("ascii", "backslashreplace")
132133
return msg
134+
135+
136+
class TracingSaxHandler(xml.sax.handler.ContentHandler):
137+
def __init__(self):
138+
xml.sax.handler.ContentHandler.__init__(self)
139+
self.visited = []
140+
141+
def startDocument(self):
142+
self.visited.append('startDocument')
143+
144+
def endDocument(self):
145+
self.visited.append('endDocument')
146+
147+
def startPrefixMapping(self, prefix, uri):
148+
# These are ignored as their order is not guaranteed
149+
pass
150+
151+
def endPrefixMapping(self, prefix):
152+
# These are ignored as their order is not guaranteed
153+
pass
154+
155+
def startElement(self, name, attrs):
156+
self.visited.append(('startElement', name, attrs))
157+
158+
def endElement(self, name):
159+
self.visited.append(('endElement', name))
160+
161+
def startElementNS(self, name, qname, attrs):
162+
self.visited.append(('startElementNS', name, qname, dict(attrs)))
163+
164+
def endElementNS(self, name, qname):
165+
self.visited.append(('endElementNS', name, qname))
166+
167+
def characters(self, content):
168+
self.visited.append(('characters', content))
169+
170+
def ignorableWhitespace(self, whitespace):
171+
self.visited.append(('ignorableWhitespace', whitespace))
172+
173+
def processingInstruction(self, target, data):
174+
self.visited.append(('processingInstruction', target, data))
175+
176+
def skippedEntity(self, name):
177+
self.visited.append(('skippedEntity', name))

html5lib/tests/test_treeadapters.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import absolute_import, division, unicode_literals
2+
3+
from . import support # flake8: noqa
4+
5+
import html5lib
6+
from html5lib.treeadapters import sax
7+
from html5lib.treewalkers import getTreeWalker
8+
9+
10+
def test_to_sax():
11+
handler = support.TracingSaxHandler()
12+
tree = html5lib.parse("""<html xml:lang="en">
13+
<title>Directory Listing</title>
14+
<a href="/"><b/></p>
15+
""", treebuilder="etree")
16+
walker = getTreeWalker("etree")
17+
sax.to_sax(walker(tree), handler)
18+
expected = [
19+
'startDocument',
20+
('startElementNS', ('http://www.w3.org/1999/xhtml', 'html'),
21+
'html', {(None, 'xml:lang'): 'en'}),
22+
('startElementNS', ('http://www.w3.org/1999/xhtml', 'head'), 'head', {}),
23+
('startElementNS', ('http://www.w3.org/1999/xhtml', 'title'), 'title', {}),
24+
('characters', 'Directory Listing'),
25+
('endElementNS', ('http://www.w3.org/1999/xhtml', 'title'), 'title'),
26+
('characters', '\n '),
27+
('endElementNS', ('http://www.w3.org/1999/xhtml', 'head'), 'head'),
28+
('startElementNS', ('http://www.w3.org/1999/xhtml', 'body'), 'body', {}),
29+
('startElementNS', ('http://www.w3.org/1999/xhtml', 'a'), 'a', {(None, 'href'): '/'}),
30+
('startElementNS', ('http://www.w3.org/1999/xhtml', 'b'), 'b', {}),
31+
('startElementNS', ('http://www.w3.org/1999/xhtml', 'p'), 'p', {}),
32+
('endElementNS', ('http://www.w3.org/1999/xhtml', 'p'), 'p'),
33+
('characters', '\n '),
34+
('endElementNS', ('http://www.w3.org/1999/xhtml', 'b'), 'b'),
35+
('endElementNS', ('http://www.w3.org/1999/xhtml', 'a'), 'a'),
36+
('endElementNS', ('http://www.w3.org/1999/xhtml', 'body'), 'body'),
37+
('endElementNS', ('http://www.w3.org/1999/xhtml', 'html'), 'html'),
38+
'endDocument',
39+
]
40+
assert expected == handler.visited

0 commit comments

Comments
 (0)