forked from html5lib/html5lib-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lxp.py
More file actions
243 lines (202 loc) · 7.19 KB
/
test_lxp.py
File metadata and controls
243 lines (202 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import support
from html5lib.treebuilders import dom
from html5lib.liberalxmlparser import *
import unittest, re
def sortattrs(match):
name = match.group(1)
attrs = re.findall('([-:\w]+)="([^"]*)"', match.group(2))
if not attrs: return "<%s%s%s>" % match.groups()
attrs.sort()
attrs = ' '.join(['%s="%s"' % (n,v) for n,v in attrs])
return "<%s %s%s>" % (name, attrs, match.group(3))
def ncr(match):
return unichr(int(match.group(1))).encode('utf-8')
xmlelem = re.compile(r'<(\w+)((?: [-:\w]+="[^"]*")+)(/?)>')
class Xhtml5Test(unittest.TestCase):
def assertXmlEquals(self, input, expected=None, parser=XMLParser):
document = parser(tree=dom.TreeBuilder).parse(input).documentElement
#print document.toxml('utf-8')
if not expected:
expected = xmlelem.sub(sortattrs, input)
expected = re.sub('&#(\d+);', ncr, expected)
output = xmlelem.sub(sortattrs, document.toxml('utf-8'))
self.assertEquals(expected, output)
else:
self.assertEquals(expected, document.toxml('utf-8'))
pass
def assertXhtmlEquals(self, input, expected=None, parser=XHTMLParser):
self.assertXmlEquals(input, expected, parser)
class BasicXhtml5Test(Xhtml5Test):
def test_title_body_mismatched_close(self):
self.assertXhtmlEquals(
'<title>Xhtml</title><b><i>content</b></i>',
'<html xmlns="http://www.w3.org/1999/xhtml">'
'<head><title>Xhtml</title></head>' +
'<body><b><i>content</i></b></body>' +
'</html>')
def test_title_body_named_charref(self):
self.assertXhtmlEquals(
'<title>ntilde</title>A ñ B',
'<html xmlns="http://www.w3.org/1999/xhtml">'
'<head><title>ntilde</title></head>' +
'<body>A '+ unichr(0xf1).encode('utf-8') + ' B</body>' +
'</html>')
class BasicXmlTest(Xhtml5Test):
def test_comment(self):
self.assertXmlEquals("<x><!-- foo --></x>")
def test_cdata(self):
self.assertXmlEquals("<x><![CDATA[foo]]></x>","<x>foo</x>")
def test_simple_text(self):
self.assertXmlEquals("<p>foo</p>","<p>foo</p>")
def test_optional_close(self):
self.assertXmlEquals("<p>foo","<p>foo</p>")
def test_html_mismatched(self):
self.assertXmlEquals("<b><i>foo</b></i>","<b><i>foo</i></b>")
class OpmlTest(Xhtml5Test):
def test_mixedCaseElement(self):
self.assertXmlEquals(
'<opml version="1.0">' +
'<head><ownerName>Dave Winer</ownerName></head>' +
'</opml>')
def test_mixedCaseAttribute(self):
self.assertXmlEquals(
'<opml version="1.0">' +
'<body><outline isComment="true"/></body>' +
'</opml>')
def test_malformed(self):
self.assertXmlEquals(
'<opml version="1.0">' +
'<body><outline text="Odds & Ends"/></body>' +
'</opml>',
'<opml version="1.0">' +
'<body><outline text="Odds & Ends"/></body>' +
'</opml>',)
class XhtmlTest(Xhtml5Test):
def test_mathml(self):
self.assertXhtmlEquals("""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>MathML</title></head>
<body>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mi>x</mi>
<mo>=</mo>
<mfrac>
<mrow>
<mrow>
<mo>-</mo>
<mi>b</mi>
</mrow>
<mo>±</mo>
<msqrt>
<mrow>
<msup>
<mi>b</mi>
<mn>2</mn>
</msup>
<mo>-</mo>
<mrow>
<mn>4</mn>
<mo>⁢</mo>
<mi>a</mi>
<mo>⁢</mo>
<mi>c</mi>
</mrow>
</mrow>
</msqrt>
</mrow>
<mrow>
<mn>2</mn>
<mo>⁢</mo>
<mi>a</mi>
</mrow>
</mfrac>
</mrow>
</math>
</body></html>""")
def test_svg(self):
self.assertXhtmlEquals("""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>SVG</title></head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<path d="M38,38c0-12,24-15,23-2c0,9-16,13-16,23v7h11v-4c0-9,17-12,17-27
c-2-22-45-22-45,3zM45,70h11v11h-11z" fill="#371">
</path>
<circle cx="50" cy="50" r="45" fill="none" stroke="#371" stroke-width="10">
</circle>
</svg>
</body></html>""")
def test_xlink(self):
self.assertXhtmlEquals("""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>XLINK</title></head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<defs xmlns:l="http://www.w3.org/1999/xlink">
<radialGradient id="s1" fx=".4" fy=".2" r=".7">
<stop stop-color="#FE8"/>
<stop stop-color="#D70" offset="1"/>
</radialGradient>
<radialGradient id="s2" fx=".8" fy=".5" l:href="#s1"/>
<radialGradient id="s3" fx=".5" fy=".9" l:href="#s1"/>
<radialGradient id="s4" fx=".1" fy=".5" l:href="#s1"/>
</defs>
<g stroke="#940">
<path d="M73,29c-37-40-62-24-52,4l6-7c-8-16,7-26,42,9z" fill="url(#s1)"/>
<path d="M47,8c33-16,48,21,9,47l-6-5c38-27,20-44,5-37z" fill="url(#s2)"/>
<path d="M77,32c22,30,10,57-39,51l-1-8c3,3,67,5,36-36z" fill="url(#s3)"/>
<path d="M58,84c-4,20-38-4-8-24l-6-5c-36,43,15,56,23,27z" fill="url(#s4)"/>
<path d="M40,14c-40,37-37,52-9,68l1-8c-16-13-29-21,16-56z" fill="url(#s1)"/>
<path d="M31,33c19,23,20,7,35,41l-9,1.7c-4-19-8-14-31-37z" fill="url(#s2)"/>
</g>
</svg>
</body></html>""")
def test_br(self):
self.assertXhtmlEquals("""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>BR</title></head>
<body>
<br/>
</body></html>""")
def test_strong(self):
self.assertXhtmlEquals("""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>STRONG</title></head>
<body>
<strong></strong>
</body></html>""")
def test_script(self):
self.assertXhtmlEquals("""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>SCRIPT</title></head>
<body>
<script>1 < 2 & 3</script>
</body></html>""")
def test_script_src(self):
self.assertXhtmlEquals("""<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>SCRIPT</title><script src="http://example.com"/></head>
<body>
</body></html>""",
"""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>SCRIPT</title><script src="http://example.com"></script></head>
<body>
</body></html>""")
def test_title(self):
self.assertXhtmlEquals("""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>1 < 2 & 3</title></head>
<body>
</body></html>""")
def test_prolog(self):
self.assertXhtmlEquals("""<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>PROLOG</title></head>
<body>
</body></html>""",
"""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>PROLOG</title></head>
<body>
</body></html>""")
def test_tagsoup(self):
self.assertXhtmlEquals(
"""<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>TAGSOUP</title></head>
<body>
<u><blockquote><p></u>
</body></html>""",
"""<html xmlns="http://www.w3.org/1999/xhtml"><head><title>TAGSOUP</title></head>
<body>
<u/><blockquote><u/><p><u/>
</p></blockquote></body></html>""")
def buildTestSuite():
return unittest.defaultTestLoader.loadTestsFromName(__name__)
def main():
buildTestSuite()
unittest.main()
if __name__ == '__main__':
main()