jpayne@69
|
1 """A parser for HTML and XHTML."""
|
jpayne@69
|
2
|
jpayne@69
|
3 # This file is based on sgmllib.py, but the API is slightly different.
|
jpayne@69
|
4
|
jpayne@69
|
5 # XXX There should be a way to distinguish between PCDATA (parsed
|
jpayne@69
|
6 # character data -- the normal case), RCDATA (replaceable character
|
jpayne@69
|
7 # data -- only char and entity references and end tags are special)
|
jpayne@69
|
8 # and CDATA (character data -- only end tags are special).
|
jpayne@69
|
9
|
jpayne@69
|
10
|
jpayne@69
|
11 import re
|
jpayne@69
|
12 import warnings
|
jpayne@69
|
13 import _markupbase
|
jpayne@69
|
14
|
jpayne@69
|
15 from html import unescape
|
jpayne@69
|
16
|
jpayne@69
|
17
|
jpayne@69
|
18 __all__ = ['HTMLParser']
|
jpayne@69
|
19
|
jpayne@69
|
20 # Regular expressions used for parsing
|
jpayne@69
|
21
|
jpayne@69
|
22 interesting_normal = re.compile('[&<]')
|
jpayne@69
|
23 incomplete = re.compile('&[a-zA-Z#]')
|
jpayne@69
|
24
|
jpayne@69
|
25 entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
|
jpayne@69
|
26 charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
|
jpayne@69
|
27
|
jpayne@69
|
28 starttagopen = re.compile('<[a-zA-Z]')
|
jpayne@69
|
29 piclose = re.compile('>')
|
jpayne@69
|
30 commentclose = re.compile(r'--\s*>')
|
jpayne@69
|
31 # Note:
|
jpayne@69
|
32 # 1) if you change tagfind/attrfind remember to update locatestarttagend too;
|
jpayne@69
|
33 # 2) if you change tagfind/attrfind and/or locatestarttagend the parser will
|
jpayne@69
|
34 # explode, so don't do it.
|
jpayne@69
|
35 # see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
|
jpayne@69
|
36 # and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
|
jpayne@69
|
37 tagfind_tolerant = re.compile(r'([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*')
|
jpayne@69
|
38 attrfind_tolerant = re.compile(
|
jpayne@69
|
39 r'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
|
jpayne@69
|
40 r'(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*')
|
jpayne@69
|
41 locatestarttagend_tolerant = re.compile(r"""
|
jpayne@69
|
42 <[a-zA-Z][^\t\n\r\f />\x00]* # tag name
|
jpayne@69
|
43 (?:[\s/]* # optional whitespace before attribute name
|
jpayne@69
|
44 (?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name
|
jpayne@69
|
45 (?:\s*=+\s* # value indicator
|
jpayne@69
|
46 (?:'[^']*' # LITA-enclosed value
|
jpayne@69
|
47 |"[^"]*" # LIT-enclosed value
|
jpayne@69
|
48 |(?!['"])[^>\s]* # bare value
|
jpayne@69
|
49 )
|
jpayne@69
|
50 (?:\s*,)* # possibly followed by a comma
|
jpayne@69
|
51 )?(?:\s|/(?!>))*
|
jpayne@69
|
52 )*
|
jpayne@69
|
53 )?
|
jpayne@69
|
54 \s* # trailing whitespace
|
jpayne@69
|
55 """, re.VERBOSE)
|
jpayne@69
|
56 endendtag = re.compile('>')
|
jpayne@69
|
57 # the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between
|
jpayne@69
|
58 # </ and the tag name, so maybe this should be fixed
|
jpayne@69
|
59 endtagfind = re.compile(r'</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
|
jpayne@69
|
60
|
jpayne@69
|
61
|
jpayne@69
|
62
|
jpayne@69
|
63 class HTMLParser(_markupbase.ParserBase):
|
jpayne@69
|
64 """Find tags and other markup and call handler functions.
|
jpayne@69
|
65
|
jpayne@69
|
66 Usage:
|
jpayne@69
|
67 p = HTMLParser()
|
jpayne@69
|
68 p.feed(data)
|
jpayne@69
|
69 ...
|
jpayne@69
|
70 p.close()
|
jpayne@69
|
71
|
jpayne@69
|
72 Start tags are handled by calling self.handle_starttag() or
|
jpayne@69
|
73 self.handle_startendtag(); end tags by self.handle_endtag(). The
|
jpayne@69
|
74 data between tags is passed from the parser to the derived class
|
jpayne@69
|
75 by calling self.handle_data() with the data as argument (the data
|
jpayne@69
|
76 may be split up in arbitrary chunks). If convert_charrefs is
|
jpayne@69
|
77 True the character references are converted automatically to the
|
jpayne@69
|
78 corresponding Unicode character (and self.handle_data() is no
|
jpayne@69
|
79 longer split in chunks), otherwise they are passed by calling
|
jpayne@69
|
80 self.handle_entityref() or self.handle_charref() with the string
|
jpayne@69
|
81 containing respectively the named or numeric reference as the
|
jpayne@69
|
82 argument.
|
jpayne@69
|
83 """
|
jpayne@69
|
84
|
jpayne@69
|
85 CDATA_CONTENT_ELEMENTS = ("script", "style")
|
jpayne@69
|
86
|
jpayne@69
|
87 def __init__(self, *, convert_charrefs=True):
|
jpayne@69
|
88 """Initialize and reset this instance.
|
jpayne@69
|
89
|
jpayne@69
|
90 If convert_charrefs is True (the default), all character references
|
jpayne@69
|
91 are automatically converted to the corresponding Unicode characters.
|
jpayne@69
|
92 """
|
jpayne@69
|
93 self.convert_charrefs = convert_charrefs
|
jpayne@69
|
94 self.reset()
|
jpayne@69
|
95
|
jpayne@69
|
96 def reset(self):
|
jpayne@69
|
97 """Reset this instance. Loses all unprocessed data."""
|
jpayne@69
|
98 self.rawdata = ''
|
jpayne@69
|
99 self.lasttag = '???'
|
jpayne@69
|
100 self.interesting = interesting_normal
|
jpayne@69
|
101 self.cdata_elem = None
|
jpayne@69
|
102 _markupbase.ParserBase.reset(self)
|
jpayne@69
|
103
|
jpayne@69
|
104 def feed(self, data):
|
jpayne@69
|
105 r"""Feed data to the parser.
|
jpayne@69
|
106
|
jpayne@69
|
107 Call this as often as you want, with as little or as much text
|
jpayne@69
|
108 as you want (may include '\n').
|
jpayne@69
|
109 """
|
jpayne@69
|
110 self.rawdata = self.rawdata + data
|
jpayne@69
|
111 self.goahead(0)
|
jpayne@69
|
112
|
jpayne@69
|
113 def close(self):
|
jpayne@69
|
114 """Handle any buffered data."""
|
jpayne@69
|
115 self.goahead(1)
|
jpayne@69
|
116
|
jpayne@69
|
117 __starttag_text = None
|
jpayne@69
|
118
|
jpayne@69
|
119 def get_starttag_text(self):
|
jpayne@69
|
120 """Return full source of start tag: '<...>'."""
|
jpayne@69
|
121 return self.__starttag_text
|
jpayne@69
|
122
|
jpayne@69
|
123 def set_cdata_mode(self, elem):
|
jpayne@69
|
124 self.cdata_elem = elem.lower()
|
jpayne@69
|
125 self.interesting = re.compile(r'</\s*%s\s*>' % self.cdata_elem, re.I)
|
jpayne@69
|
126
|
jpayne@69
|
127 def clear_cdata_mode(self):
|
jpayne@69
|
128 self.interesting = interesting_normal
|
jpayne@69
|
129 self.cdata_elem = None
|
jpayne@69
|
130
|
jpayne@69
|
131 # Internal -- handle data as far as reasonable. May leave state
|
jpayne@69
|
132 # and data to be processed by a subsequent call. If 'end' is
|
jpayne@69
|
133 # true, force handling all data as if followed by EOF marker.
|
jpayne@69
|
134 def goahead(self, end):
|
jpayne@69
|
135 rawdata = self.rawdata
|
jpayne@69
|
136 i = 0
|
jpayne@69
|
137 n = len(rawdata)
|
jpayne@69
|
138 while i < n:
|
jpayne@69
|
139 if self.convert_charrefs and not self.cdata_elem:
|
jpayne@69
|
140 j = rawdata.find('<', i)
|
jpayne@69
|
141 if j < 0:
|
jpayne@69
|
142 # if we can't find the next <, either we are at the end
|
jpayne@69
|
143 # or there's more text incoming. If the latter is True,
|
jpayne@69
|
144 # we can't pass the text to handle_data in case we have
|
jpayne@69
|
145 # a charref cut in half at end. Try to determine if
|
jpayne@69
|
146 # this is the case before proceeding by looking for an
|
jpayne@69
|
147 # & near the end and see if it's followed by a space or ;.
|
jpayne@69
|
148 amppos = rawdata.rfind('&', max(i, n-34))
|
jpayne@69
|
149 if (amppos >= 0 and
|
jpayne@69
|
150 not re.compile(r'[\s;]').search(rawdata, amppos)):
|
jpayne@69
|
151 break # wait till we get all the text
|
jpayne@69
|
152 j = n
|
jpayne@69
|
153 else:
|
jpayne@69
|
154 match = self.interesting.search(rawdata, i) # < or &
|
jpayne@69
|
155 if match:
|
jpayne@69
|
156 j = match.start()
|
jpayne@69
|
157 else:
|
jpayne@69
|
158 if self.cdata_elem:
|
jpayne@69
|
159 break
|
jpayne@69
|
160 j = n
|
jpayne@69
|
161 if i < j:
|
jpayne@69
|
162 if self.convert_charrefs and not self.cdata_elem:
|
jpayne@69
|
163 self.handle_data(unescape(rawdata[i:j]))
|
jpayne@69
|
164 else:
|
jpayne@69
|
165 self.handle_data(rawdata[i:j])
|
jpayne@69
|
166 i = self.updatepos(i, j)
|
jpayne@69
|
167 if i == n: break
|
jpayne@69
|
168 startswith = rawdata.startswith
|
jpayne@69
|
169 if startswith('<', i):
|
jpayne@69
|
170 if starttagopen.match(rawdata, i): # < + letter
|
jpayne@69
|
171 k = self.parse_starttag(i)
|
jpayne@69
|
172 elif startswith("</", i):
|
jpayne@69
|
173 k = self.parse_endtag(i)
|
jpayne@69
|
174 elif startswith("<!--", i):
|
jpayne@69
|
175 k = self.parse_comment(i)
|
jpayne@69
|
176 elif startswith("<?", i):
|
jpayne@69
|
177 k = self.parse_pi(i)
|
jpayne@69
|
178 elif startswith("<!", i):
|
jpayne@69
|
179 k = self.parse_html_declaration(i)
|
jpayne@69
|
180 elif (i + 1) < n:
|
jpayne@69
|
181 self.handle_data("<")
|
jpayne@69
|
182 k = i + 1
|
jpayne@69
|
183 else:
|
jpayne@69
|
184 break
|
jpayne@69
|
185 if k < 0:
|
jpayne@69
|
186 if not end:
|
jpayne@69
|
187 break
|
jpayne@69
|
188 k = rawdata.find('>', i + 1)
|
jpayne@69
|
189 if k < 0:
|
jpayne@69
|
190 k = rawdata.find('<', i + 1)
|
jpayne@69
|
191 if k < 0:
|
jpayne@69
|
192 k = i + 1
|
jpayne@69
|
193 else:
|
jpayne@69
|
194 k += 1
|
jpayne@69
|
195 if self.convert_charrefs and not self.cdata_elem:
|
jpayne@69
|
196 self.handle_data(unescape(rawdata[i:k]))
|
jpayne@69
|
197 else:
|
jpayne@69
|
198 self.handle_data(rawdata[i:k])
|
jpayne@69
|
199 i = self.updatepos(i, k)
|
jpayne@69
|
200 elif startswith("&#", i):
|
jpayne@69
|
201 match = charref.match(rawdata, i)
|
jpayne@69
|
202 if match:
|
jpayne@69
|
203 name = match.group()[2:-1]
|
jpayne@69
|
204 self.handle_charref(name)
|
jpayne@69
|
205 k = match.end()
|
jpayne@69
|
206 if not startswith(';', k-1):
|
jpayne@69
|
207 k = k - 1
|
jpayne@69
|
208 i = self.updatepos(i, k)
|
jpayne@69
|
209 continue
|
jpayne@69
|
210 else:
|
jpayne@69
|
211 if ";" in rawdata[i:]: # bail by consuming &#
|
jpayne@69
|
212 self.handle_data(rawdata[i:i+2])
|
jpayne@69
|
213 i = self.updatepos(i, i+2)
|
jpayne@69
|
214 break
|
jpayne@69
|
215 elif startswith('&', i):
|
jpayne@69
|
216 match = entityref.match(rawdata, i)
|
jpayne@69
|
217 if match:
|
jpayne@69
|
218 name = match.group(1)
|
jpayne@69
|
219 self.handle_entityref(name)
|
jpayne@69
|
220 k = match.end()
|
jpayne@69
|
221 if not startswith(';', k-1):
|
jpayne@69
|
222 k = k - 1
|
jpayne@69
|
223 i = self.updatepos(i, k)
|
jpayne@69
|
224 continue
|
jpayne@69
|
225 match = incomplete.match(rawdata, i)
|
jpayne@69
|
226 if match:
|
jpayne@69
|
227 # match.group() will contain at least 2 chars
|
jpayne@69
|
228 if end and match.group() == rawdata[i:]:
|
jpayne@69
|
229 k = match.end()
|
jpayne@69
|
230 if k <= i:
|
jpayne@69
|
231 k = n
|
jpayne@69
|
232 i = self.updatepos(i, i + 1)
|
jpayne@69
|
233 # incomplete
|
jpayne@69
|
234 break
|
jpayne@69
|
235 elif (i + 1) < n:
|
jpayne@69
|
236 # not the end of the buffer, and can't be confused
|
jpayne@69
|
237 # with some other construct
|
jpayne@69
|
238 self.handle_data("&")
|
jpayne@69
|
239 i = self.updatepos(i, i + 1)
|
jpayne@69
|
240 else:
|
jpayne@69
|
241 break
|
jpayne@69
|
242 else:
|
jpayne@69
|
243 assert 0, "interesting.search() lied"
|
jpayne@69
|
244 # end while
|
jpayne@69
|
245 if end and i < n and not self.cdata_elem:
|
jpayne@69
|
246 if self.convert_charrefs and not self.cdata_elem:
|
jpayne@69
|
247 self.handle_data(unescape(rawdata[i:n]))
|
jpayne@69
|
248 else:
|
jpayne@69
|
249 self.handle_data(rawdata[i:n])
|
jpayne@69
|
250 i = self.updatepos(i, n)
|
jpayne@69
|
251 self.rawdata = rawdata[i:]
|
jpayne@69
|
252
|
jpayne@69
|
253 # Internal -- parse html declarations, return length or -1 if not terminated
|
jpayne@69
|
254 # See w3.org/TR/html5/tokenization.html#markup-declaration-open-state
|
jpayne@69
|
255 # See also parse_declaration in _markupbase
|
jpayne@69
|
256 def parse_html_declaration(self, i):
|
jpayne@69
|
257 rawdata = self.rawdata
|
jpayne@69
|
258 assert rawdata[i:i+2] == '<!', ('unexpected call to '
|
jpayne@69
|
259 'parse_html_declaration()')
|
jpayne@69
|
260 if rawdata[i:i+4] == '<!--':
|
jpayne@69
|
261 # this case is actually already handled in goahead()
|
jpayne@69
|
262 return self.parse_comment(i)
|
jpayne@69
|
263 elif rawdata[i:i+3] == '<![':
|
jpayne@69
|
264 return self.parse_marked_section(i)
|
jpayne@69
|
265 elif rawdata[i:i+9].lower() == '<!doctype':
|
jpayne@69
|
266 # find the closing >
|
jpayne@69
|
267 gtpos = rawdata.find('>', i+9)
|
jpayne@69
|
268 if gtpos == -1:
|
jpayne@69
|
269 return -1
|
jpayne@69
|
270 self.handle_decl(rawdata[i+2:gtpos])
|
jpayne@69
|
271 return gtpos+1
|
jpayne@69
|
272 else:
|
jpayne@69
|
273 return self.parse_bogus_comment(i)
|
jpayne@69
|
274
|
jpayne@69
|
275 # Internal -- parse bogus comment, return length or -1 if not terminated
|
jpayne@69
|
276 # see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state
|
jpayne@69
|
277 def parse_bogus_comment(self, i, report=1):
|
jpayne@69
|
278 rawdata = self.rawdata
|
jpayne@69
|
279 assert rawdata[i:i+2] in ('<!', '</'), ('unexpected call to '
|
jpayne@69
|
280 'parse_comment()')
|
jpayne@69
|
281 pos = rawdata.find('>', i+2)
|
jpayne@69
|
282 if pos == -1:
|
jpayne@69
|
283 return -1
|
jpayne@69
|
284 if report:
|
jpayne@69
|
285 self.handle_comment(rawdata[i+2:pos])
|
jpayne@69
|
286 return pos + 1
|
jpayne@69
|
287
|
jpayne@69
|
288 # Internal -- parse processing instr, return end or -1 if not terminated
|
jpayne@69
|
289 def parse_pi(self, i):
|
jpayne@69
|
290 rawdata = self.rawdata
|
jpayne@69
|
291 assert rawdata[i:i+2] == '<?', 'unexpected call to parse_pi()'
|
jpayne@69
|
292 match = piclose.search(rawdata, i+2) # >
|
jpayne@69
|
293 if not match:
|
jpayne@69
|
294 return -1
|
jpayne@69
|
295 j = match.start()
|
jpayne@69
|
296 self.handle_pi(rawdata[i+2: j])
|
jpayne@69
|
297 j = match.end()
|
jpayne@69
|
298 return j
|
jpayne@69
|
299
|
jpayne@69
|
300 # Internal -- handle starttag, return end or -1 if not terminated
|
jpayne@69
|
301 def parse_starttag(self, i):
|
jpayne@69
|
302 self.__starttag_text = None
|
jpayne@69
|
303 endpos = self.check_for_whole_start_tag(i)
|
jpayne@69
|
304 if endpos < 0:
|
jpayne@69
|
305 return endpos
|
jpayne@69
|
306 rawdata = self.rawdata
|
jpayne@69
|
307 self.__starttag_text = rawdata[i:endpos]
|
jpayne@69
|
308
|
jpayne@69
|
309 # Now parse the data between i+1 and j into a tag and attrs
|
jpayne@69
|
310 attrs = []
|
jpayne@69
|
311 match = tagfind_tolerant.match(rawdata, i+1)
|
jpayne@69
|
312 assert match, 'unexpected call to parse_starttag()'
|
jpayne@69
|
313 k = match.end()
|
jpayne@69
|
314 self.lasttag = tag = match.group(1).lower()
|
jpayne@69
|
315 while k < endpos:
|
jpayne@69
|
316 m = attrfind_tolerant.match(rawdata, k)
|
jpayne@69
|
317 if not m:
|
jpayne@69
|
318 break
|
jpayne@69
|
319 attrname, rest, attrvalue = m.group(1, 2, 3)
|
jpayne@69
|
320 if not rest:
|
jpayne@69
|
321 attrvalue = None
|
jpayne@69
|
322 elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
|
jpayne@69
|
323 attrvalue[:1] == '"' == attrvalue[-1:]:
|
jpayne@69
|
324 attrvalue = attrvalue[1:-1]
|
jpayne@69
|
325 if attrvalue:
|
jpayne@69
|
326 attrvalue = unescape(attrvalue)
|
jpayne@69
|
327 attrs.append((attrname.lower(), attrvalue))
|
jpayne@69
|
328 k = m.end()
|
jpayne@69
|
329
|
jpayne@69
|
330 end = rawdata[k:endpos].strip()
|
jpayne@69
|
331 if end not in (">", "/>"):
|
jpayne@69
|
332 lineno, offset = self.getpos()
|
jpayne@69
|
333 if "\n" in self.__starttag_text:
|
jpayne@69
|
334 lineno = lineno + self.__starttag_text.count("\n")
|
jpayne@69
|
335 offset = len(self.__starttag_text) \
|
jpayne@69
|
336 - self.__starttag_text.rfind("\n")
|
jpayne@69
|
337 else:
|
jpayne@69
|
338 offset = offset + len(self.__starttag_text)
|
jpayne@69
|
339 self.handle_data(rawdata[i:endpos])
|
jpayne@69
|
340 return endpos
|
jpayne@69
|
341 if end.endswith('/>'):
|
jpayne@69
|
342 # XHTML-style empty tag: <span attr="value" />
|
jpayne@69
|
343 self.handle_startendtag(tag, attrs)
|
jpayne@69
|
344 else:
|
jpayne@69
|
345 self.handle_starttag(tag, attrs)
|
jpayne@69
|
346 if tag in self.CDATA_CONTENT_ELEMENTS:
|
jpayne@69
|
347 self.set_cdata_mode(tag)
|
jpayne@69
|
348 return endpos
|
jpayne@69
|
349
|
jpayne@69
|
350 # Internal -- check to see if we have a complete starttag; return end
|
jpayne@69
|
351 # or -1 if incomplete.
|
jpayne@69
|
352 def check_for_whole_start_tag(self, i):
|
jpayne@69
|
353 rawdata = self.rawdata
|
jpayne@69
|
354 m = locatestarttagend_tolerant.match(rawdata, i)
|
jpayne@69
|
355 if m:
|
jpayne@69
|
356 j = m.end()
|
jpayne@69
|
357 next = rawdata[j:j+1]
|
jpayne@69
|
358 if next == ">":
|
jpayne@69
|
359 return j + 1
|
jpayne@69
|
360 if next == "/":
|
jpayne@69
|
361 if rawdata.startswith("/>", j):
|
jpayne@69
|
362 return j + 2
|
jpayne@69
|
363 if rawdata.startswith("/", j):
|
jpayne@69
|
364 # buffer boundary
|
jpayne@69
|
365 return -1
|
jpayne@69
|
366 # else bogus input
|
jpayne@69
|
367 if j > i:
|
jpayne@69
|
368 return j
|
jpayne@69
|
369 else:
|
jpayne@69
|
370 return i + 1
|
jpayne@69
|
371 if next == "":
|
jpayne@69
|
372 # end of input
|
jpayne@69
|
373 return -1
|
jpayne@69
|
374 if next in ("abcdefghijklmnopqrstuvwxyz=/"
|
jpayne@69
|
375 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
|
jpayne@69
|
376 # end of input in or before attribute value, or we have the
|
jpayne@69
|
377 # '/' from a '/>' ending
|
jpayne@69
|
378 return -1
|
jpayne@69
|
379 if j > i:
|
jpayne@69
|
380 return j
|
jpayne@69
|
381 else:
|
jpayne@69
|
382 return i + 1
|
jpayne@69
|
383 raise AssertionError("we should not get here!")
|
jpayne@69
|
384
|
jpayne@69
|
385 # Internal -- parse endtag, return end or -1 if incomplete
|
jpayne@69
|
386 def parse_endtag(self, i):
|
jpayne@69
|
387 rawdata = self.rawdata
|
jpayne@69
|
388 assert rawdata[i:i+2] == "</", "unexpected call to parse_endtag"
|
jpayne@69
|
389 match = endendtag.search(rawdata, i+1) # >
|
jpayne@69
|
390 if not match:
|
jpayne@69
|
391 return -1
|
jpayne@69
|
392 gtpos = match.end()
|
jpayne@69
|
393 match = endtagfind.match(rawdata, i) # </ + tag + >
|
jpayne@69
|
394 if not match:
|
jpayne@69
|
395 if self.cdata_elem is not None:
|
jpayne@69
|
396 self.handle_data(rawdata[i:gtpos])
|
jpayne@69
|
397 return gtpos
|
jpayne@69
|
398 # find the name: w3.org/TR/html5/tokenization.html#tag-name-state
|
jpayne@69
|
399 namematch = tagfind_tolerant.match(rawdata, i+2)
|
jpayne@69
|
400 if not namematch:
|
jpayne@69
|
401 # w3.org/TR/html5/tokenization.html#end-tag-open-state
|
jpayne@69
|
402 if rawdata[i:i+3] == '</>':
|
jpayne@69
|
403 return i+3
|
jpayne@69
|
404 else:
|
jpayne@69
|
405 return self.parse_bogus_comment(i)
|
jpayne@69
|
406 tagname = namematch.group(1).lower()
|
jpayne@69
|
407 # consume and ignore other stuff between the name and the >
|
jpayne@69
|
408 # Note: this is not 100% correct, since we might have things like
|
jpayne@69
|
409 # </tag attr=">">, but looking for > after tha name should cover
|
jpayne@69
|
410 # most of the cases and is much simpler
|
jpayne@69
|
411 gtpos = rawdata.find('>', namematch.end())
|
jpayne@69
|
412 self.handle_endtag(tagname)
|
jpayne@69
|
413 return gtpos+1
|
jpayne@69
|
414
|
jpayne@69
|
415 elem = match.group(1).lower() # script or style
|
jpayne@69
|
416 if self.cdata_elem is not None:
|
jpayne@69
|
417 if elem != self.cdata_elem:
|
jpayne@69
|
418 self.handle_data(rawdata[i:gtpos])
|
jpayne@69
|
419 return gtpos
|
jpayne@69
|
420
|
jpayne@69
|
421 self.handle_endtag(elem)
|
jpayne@69
|
422 self.clear_cdata_mode()
|
jpayne@69
|
423 return gtpos
|
jpayne@69
|
424
|
jpayne@69
|
425 # Overridable -- finish processing of start+end tag: <tag.../>
|
jpayne@69
|
426 def handle_startendtag(self, tag, attrs):
|
jpayne@69
|
427 self.handle_starttag(tag, attrs)
|
jpayne@69
|
428 self.handle_endtag(tag)
|
jpayne@69
|
429
|
jpayne@69
|
430 # Overridable -- handle start tag
|
jpayne@69
|
431 def handle_starttag(self, tag, attrs):
|
jpayne@69
|
432 pass
|
jpayne@69
|
433
|
jpayne@69
|
434 # Overridable -- handle end tag
|
jpayne@69
|
435 def handle_endtag(self, tag):
|
jpayne@69
|
436 pass
|
jpayne@69
|
437
|
jpayne@69
|
438 # Overridable -- handle character reference
|
jpayne@69
|
439 def handle_charref(self, name):
|
jpayne@69
|
440 pass
|
jpayne@69
|
441
|
jpayne@69
|
442 # Overridable -- handle entity reference
|
jpayne@69
|
443 def handle_entityref(self, name):
|
jpayne@69
|
444 pass
|
jpayne@69
|
445
|
jpayne@69
|
446 # Overridable -- handle data
|
jpayne@69
|
447 def handle_data(self, data):
|
jpayne@69
|
448 pass
|
jpayne@69
|
449
|
jpayne@69
|
450 # Overridable -- handle comment
|
jpayne@69
|
451 def handle_comment(self, data):
|
jpayne@69
|
452 pass
|
jpayne@69
|
453
|
jpayne@69
|
454 # Overridable -- handle declaration
|
jpayne@69
|
455 def handle_decl(self, decl):
|
jpayne@69
|
456 pass
|
jpayne@69
|
457
|
jpayne@69
|
458 # Overridable -- handle processing instruction
|
jpayne@69
|
459 def handle_pi(self, data):
|
jpayne@69
|
460 pass
|
jpayne@69
|
461
|
jpayne@69
|
462 def unknown_decl(self, data):
|
jpayne@69
|
463 pass
|
jpayne@69
|
464
|
jpayne@69
|
465 # Internal -- helper to remove special character quoting
|
jpayne@69
|
466 def unescape(self, s):
|
jpayne@69
|
467 warnings.warn('The unescape method is deprecated and will be removed '
|
jpayne@69
|
468 'in 3.5, use html.unescape() instead.',
|
jpayne@69
|
469 DeprecationWarning, stacklevel=2)
|
jpayne@69
|
470 return unescape(s)
|