1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 |
|
4 | import unittest
|
5 |
|
6 | from lazylex import html # module under test log = html.log
|
7 |
|
8 | log = html.log
|
9 |
|
10 | with open('lazylex/testdata.html') as f:
|
11 | TEST_HTML = f.read()
|
12 |
|
13 |
|
14 | def _MakeTagLexer(s):
|
15 | lex = html.TagLexer(s)
|
16 | lex.Reset(0, len(s))
|
17 | return lex
|
18 |
|
19 |
|
20 | def _PrintTokens(lex):
|
21 | log('')
|
22 | log('tag = %r', lex.TagName())
|
23 | for tok, start, end in lex.Tokens():
|
24 | log('%s %r', tok, lex.s[start:end])
|
25 |
|
26 |
|
27 | class RegexTest(unittest.TestCase):
|
28 |
|
29 | def testDotAll(self):
|
30 | import re
|
31 |
|
32 | # Note that $ matches end of line, not end of string
|
33 | p1 = re.compile(r'.')
|
34 | print(p1.match('\n'))
|
35 |
|
36 | p2 = re.compile(r'.', re.DOTALL)
|
37 | print(p2.match('\n'))
|
38 |
|
39 | #p3 = re.compile(r'[.\n]', re.VERBOSE)
|
40 | p3 = re.compile(r'[.\n]')
|
41 | print(p3.match('\n'))
|
42 |
|
43 | print('Negation')
|
44 |
|
45 | p4 = re.compile(r'[^>]')
|
46 | print(p4.match('\n'))
|
47 |
|
48 |
|
49 | class FunctionsTest(unittest.TestCase):
|
50 |
|
51 | def testFindLineNum(self):
|
52 | s = 'foo\n' * 3
|
53 | for pos in [1, 5, 10, 50]: # out of bounds
|
54 | line_num = html.FindLineNum(s, pos)
|
55 | print(line_num)
|
56 |
|
57 |
|
58 | class TagLexerTest(unittest.TestCase):
|
59 |
|
60 | def testTagLexer(self):
|
61 | # Invalid!
|
62 | #lex = _MakeTagLexer('< >')
|
63 | #print(lex.Tag())
|
64 |
|
65 | lex = _MakeTagLexer('<a>')
|
66 | _PrintTokens(lex)
|
67 |
|
68 | lex = _MakeTagLexer('<a novalue>')
|
69 | _PrintTokens(lex)
|
70 |
|
71 | # Note: we could have a different HasAttr() method
|
72 | # <a novalue> means lex.Get('novalue') == None
|
73 | # https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute
|
74 | self.assertEqual(None, lex.GetAttrRaw('novalue'))
|
75 |
|
76 | lex = _MakeTagLexer('<a href="double quoted">')
|
77 | _PrintTokens(lex)
|
78 |
|
79 | self.assertEqual('double quoted', lex.GetAttrRaw('href'))
|
80 | self.assertEqual(None, lex.GetAttrRaw('oops'))
|
81 |
|
82 | lex = _MakeTagLexer('<a href=foo class="bar">')
|
83 | _PrintTokens(lex)
|
84 |
|
85 | lex = _MakeTagLexer('<a href=foo class="bar" />')
|
86 | _PrintTokens(lex)
|
87 |
|
88 | lex = _MakeTagLexer('<a href="?foo=1&bar=2" />')
|
89 | self.assertEqual('?foo=1&bar=2', lex.GetAttrRaw('href'))
|
90 |
|
91 | def testTagName(self):
|
92 | lex = _MakeTagLexer('<a href=foo class="bar" />')
|
93 | self.assertEqual('a', lex.TagName())
|
94 |
|
95 | def testAllAttrs(self):
|
96 | """
|
97 | [('key', 'value')] for all
|
98 | """
|
99 | # closed
|
100 | lex = _MakeTagLexer('<a href=foo class="bar" />')
|
101 | self.assertEqual([('href', 'foo'), ('class', 'bar')],
|
102 | lex.AllAttrsRaw())
|
103 |
|
104 | lex = _MakeTagLexer('<a href="?foo=1&bar=2" />')
|
105 | self.assertEqual([('href', '?foo=1&bar=2')], lex.AllAttrsRaw())
|
106 |
|
107 |
|
108 | def Lex(h):
|
109 | print(repr(h))
|
110 | lex = html.ValidTokens(h)
|
111 | tokens = list(lex)
|
112 | start_pos = 0
|
113 | for tok_id, end_pos in tokens:
|
114 | frag = h[start_pos:end_pos]
|
115 | log('%d %s %r', end_pos, html.TokenName(tok_id), frag)
|
116 | start_pos = end_pos
|
117 | return tokens
|
118 |
|
119 |
|
120 | class LexerTest(unittest.TestCase):
|
121 |
|
122 | # IndexLinker in devtools/make_help.py
|
123 | # <pre> sections in doc/html_help.py
|
124 | # TocExtractor in devtools/cmark.py
|
125 |
|
126 | def testPstrip(self):
|
127 | """Remove anything like this.
|
128 |
|
129 | <p><pstrip> </pstrip></p>
|
130 | """
|
131 | pass
|
132 |
|
133 | def testCommentParse(self):
|
134 | n = len(TEST_HTML)
|
135 | for tok_id, end_pos in html._Tokens(TEST_HTML, 0, n):
|
136 | if tok_id == html.Invalid:
|
137 | raise RuntimeError()
|
138 | print(tok_id)
|
139 |
|
140 | def testCommentParse2(self):
|
141 |
|
142 | Tok = html.Tok
|
143 | h = '''
|
144 | hi <!-- line 1
|
145 | line 2 --><br/>'''
|
146 | tokens = Lex(h)
|
147 |
|
148 | self.assertEqual(
|
149 | [
|
150 | (Tok.RawData, 12),
|
151 | (Tok.Comment, 50), # <? err ?>
|
152 | (Tok.StartEndTag, 55),
|
153 | (Tok.EndOfStream, 55),
|
154 | ],
|
155 | tokens)
|
156 |
|
157 | def testProcessingInstruction(self):
|
158 | # <?xml ?> header
|
159 | Tok = html.Tok
|
160 | h = 'hi <? err ?>'
|
161 | tokens = Lex(h)
|
162 |
|
163 | self.assertEqual(
|
164 | [
|
165 | (Tok.RawData, 3),
|
166 | (Tok.Processing, 12), # <? err ?>
|
167 | (Tok.EndOfStream, 12),
|
168 | ],
|
169 | tokens)
|
170 |
|
171 | def testScriptStyle(self):
|
172 | Tok = html.Tok
|
173 | h = '''
|
174 | hi <script src=""> if (x < 1 && y > 2 ) { console.log(""); }
|
175 | </script>
|
176 | '''
|
177 | tokens = Lex(h)
|
178 |
|
179 | self.assertEqual(
|
180 | [
|
181 | (Tok.RawData, 12),
|
182 | (Tok.StartTag, 27), # <script>
|
183 | (Tok.HtmlCData, 78), # JavaScript code is HTML CData
|
184 | (Tok.EndTag, 87), # </script>
|
185 | (Tok.RawData, 96), # \n
|
186 | (Tok.EndOfStream, 96), # \n
|
187 | ],
|
188 | tokens)
|
189 |
|
190 | def testCData(self):
|
191 | Tok = html.Tok
|
192 |
|
193 | # from
|
194 | # /home/andy/src/languages/Python-3.11.5/Lib/test/xmltestdata/c14n-20/inC14N4.xml
|
195 | h = '<compute><![CDATA[value>"0" && value<"10" ?"valid":"error"]]></compute>'
|
196 | tokens = Lex(h)
|
197 |
|
198 | self.assertEqual([
|
199 | (Tok.StartTag, 9),
|
200 | (Tok.CData, 61),
|
201 | (Tok.EndTag, 71),
|
202 | (Tok.EndOfStream, 71),
|
203 | ], tokens)
|
204 |
|
205 | def testEntity(self):
|
206 | Tok = html.Tok
|
207 |
|
208 | # from
|
209 | # /home/andy/src/Python-3.12.4/Lib/test/xmltestdata/c14n-20/inC14N5.xml
|
210 | h = '&ent1;, &ent2;!'
|
211 |
|
212 | tokens = Lex(h)
|
213 |
|
214 | self.assertEqual([
|
215 | (Tok.CharEntity, 6),
|
216 | (Tok.RawData, 8),
|
217 | (Tok.CharEntity, 14),
|
218 | (Tok.RawData, 15),
|
219 | (Tok.EndOfStream, 15),
|
220 | ], tokens)
|
221 |
|
222 | def testStartTag(self):
|
223 | Tok = html.Tok
|
224 |
|
225 | h = '<a>hi</a>'
|
226 | tokens = Lex(h)
|
227 |
|
228 | self.assertEqual([
|
229 | (Tok.StartTag, 3),
|
230 | (Tok.RawData, 5),
|
231 | (Tok.EndTag, 9),
|
232 | (Tok.EndOfStream, 9),
|
233 | ], tokens)
|
234 |
|
235 | # Make sure we don't consume too much
|
236 | h = '<a><source>1.7</source></a>'
|
237 |
|
238 | tokens = Lex(h)
|
239 |
|
240 | self.assertEqual([
|
241 | (Tok.StartTag, 3),
|
242 | (Tok.StartTag, 11),
|
243 | (Tok.RawData, 14),
|
244 | (Tok.EndTag, 23),
|
245 | (Tok.EndTag, 27),
|
246 | (Tok.EndOfStream, 27),
|
247 | ], tokens)
|
248 |
|
249 | return
|
250 |
|
251 | h = '''
|
252 | <configuration>
|
253 | <source>1.7</source>
|
254 | </configuration>'''
|
255 |
|
256 | tokens = Lex(h)
|
257 |
|
258 | self.assertEqual([
|
259 | (Tok.RawData, 9),
|
260 | (Tok.StartTag, 24),
|
261 | (Tok.RawData, 9),
|
262 | (Tok.EndOfStream, 9),
|
263 | ], tokens)
|
264 |
|
265 | def testInvalid(self):
|
266 | Tok = html.Tok
|
267 |
|
268 | INVALID = [
|
269 | # Should be &
|
270 | '<a>&',
|
271 | '&', # not finished
|
272 | '&#', # not finished
|
273 | # Hm > is allowed?
|
274 | #'a > b',
|
275 | 'a < b',
|
276 | '<!-- unfinished comment',
|
277 | '<? unfinished processing',
|
278 | '</div bad=attr> <a> <b>',
|
279 | ]
|
280 |
|
281 | for s in INVALID:
|
282 | lex = html.ValidTokens(s)
|
283 | try:
|
284 | for i in xrange(5):
|
285 | tok_id, pos = next(lex)
|
286 | except html.LexError as e:
|
287 | print(e)
|
288 | else:
|
289 | self.fail('Expected LexError')
|
290 |
|
291 |
|
292 | if __name__ == '__main__':
|
293 | unittest.main()
|