blob: 60830779816a03e61e128801db514b0609fdfa9b [file] [log] [blame]
Fred Drake1d4601d2001-08-03 19:50:59 +00001"""A parser for HTML and XHTML."""
Guido van Rossum8846d712001-05-18 14:50:52 +00002
3# This file is based on sgmllib.py, but the API is slightly different.
4
5# XXX There should be a way to distinguish between PCDATA (parsed
6# character data -- the normal case), RCDATA (replaceable character
7# data -- only char and entity references and end tags are special)
8# and CDATA (character data -- only end tags are special).
9
10
11import re
Ezio Melotti4a9ee262013-11-19 20:28:45 +020012import _markupbase
13
14from html import unescape
15
Guido van Rossum8846d712001-05-18 14:50:52 +000016
Ezio Melotti1698bab2013-05-01 16:09:34 +030017__all__ = ['HTMLParser']
18
Guido van Rossum8846d712001-05-18 14:50:52 +000019# Regular expressions used for parsing
20
21interesting_normal = re.compile('[&<]')
Fred Drake68eac2b2001-09-04 15:10:16 +000022incomplete = re.compile('&[a-zA-Z#]')
Guido van Rossum8846d712001-05-18 14:50:52 +000023
24entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
Fred Drake1d4601d2001-08-03 19:50:59 +000025charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
Guido van Rossum8846d712001-05-18 14:50:52 +000026
27starttagopen = re.compile('<[a-zA-Z]')
Guido van Rossum8846d712001-05-18 14:50:52 +000028piclose = re.compile('>')
Guido van Rossum8846d712001-05-18 14:50:52 +000029commentclose = re.compile(r'--\s*>')
Ezio Melotti29877e82012-02-21 09:25:00 +020030# Note:
Ezio Melotti73a43592014-08-02 14:10:30 +030031# 1) if you change tagfind/attrfind remember to update locatestarttagend too;
32# 2) if you change tagfind/attrfind and/or locatestarttagend the parser will
Ezio Melotti29877e82012-02-21 09:25:00 +020033# explode, so don't do it.
Ezio Melotti7165d8b2013-11-07 18:33:24 +020034# see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
35# and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
R David Murray44b548d2016-09-08 13:59:53 -040036tagfind_tolerant = re.compile(r'([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*')
R. David Murrayb579dba2010-12-03 04:06:39 +000037attrfind_tolerant = re.compile(
Ezio Melotti0780b6b2012-04-18 19:18:22 -060038 r'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
Ezio Melotti29877e82012-02-21 09:25:00 +020039 r'(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*')
R. David Murrayb579dba2010-12-03 04:06:39 +000040locatestarttagend_tolerant = re.compile(r"""
Ezio Melotti7165d8b2013-11-07 18:33:24 +020041 <[a-zA-Z][^\t\n\r\f />\x00]* # tag name
Ezio Melotti29877e82012-02-21 09:25:00 +020042 (?:[\s/]* # optional whitespace before attribute name
43 (?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name
Ezio Melottic2fe5772011-11-14 18:53:33 +020044 (?:\s*=+\s* # value indicator
R. David Murrayb579dba2010-12-03 04:06:39 +000045 (?:'[^']*' # LITA-enclosed value
Ezio Melottic2fe5772011-11-14 18:53:33 +020046 |"[^"]*" # LIT-enclosed value
47 |(?!['"])[^>\s]* # bare value
R. David Murrayb579dba2010-12-03 04:06:39 +000048 )
49 (?:\s*,)* # possibly followed by a comma
Ezio Melotti29877e82012-02-21 09:25:00 +020050 )?(?:\s|/(?!>))*
Ezio Melottic2fe5772011-11-14 18:53:33 +020051 )*
52 )?
R. David Murrayb579dba2010-12-03 04:06:39 +000053 \s* # trailing whitespace
54""", re.VERBOSE)
Guido van Rossum8846d712001-05-18 14:50:52 +000055endendtag = re.compile('>')
Ezio Melotti7de56f62011-11-01 14:12:22 +020056# the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between
57# </ and the tag name, so maybe this should be fixed
R David Murray44b548d2016-09-08 13:59:53 -040058endtagfind = re.compile(r'</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
Guido van Rossum8846d712001-05-18 14:50:52 +000059
Guido van Rossum8846d712001-05-18 14:50:52 +000060
Ezio Melotti88ebfb12013-11-02 17:08:24 +020061
Fred Drakecb5c80f2007-12-07 11:10:11 +000062class HTMLParser(_markupbase.ParserBase):
Fred Drake1d4601d2001-08-03 19:50:59 +000063 """Find tags and other markup and call handler functions.
64
65 Usage:
66 p = HTMLParser()
67 p.feed(data)
68 ...
69 p.close()
70
71 Start tags are handled by calling self.handle_starttag() or
72 self.handle_startendtag(); end tags by self.handle_endtag(). The
73 data between tags is passed from the parser to the derived class
74 by calling self.handle_data() with the data as argument (the data
Ezio Melotti95401c52013-11-23 19:52:05 +020075 may be split up in arbitrary chunks). If convert_charrefs is
76 True the character references are converted automatically to the
77 corresponding Unicode character (and self.handle_data() is no
78 longer split in chunks), otherwise they are passed by calling
79 self.handle_entityref() or self.handle_charref() with the string
80 containing respectively the named or numeric reference as the
81 argument.
Fred Drake1d4601d2001-08-03 19:50:59 +000082 """
Guido van Rossum8846d712001-05-18 14:50:52 +000083
84 CDATA_CONTENT_ELEMENTS = ("script", "style")
85
Ezio Melotti6fc16d82014-08-02 18:36:12 +030086 def __init__(self, *, convert_charrefs=True):
R. David Murrayb579dba2010-12-03 04:06:39 +000087 """Initialize and reset this instance.
Guido van Rossum8846d712001-05-18 14:50:52 +000088
Ezio Melotti6fc16d82014-08-02 18:36:12 +030089 If convert_charrefs is True (the default), all character references
Ezio Melotti95401c52013-11-23 19:52:05 +020090 are automatically converted to the corresponding Unicode characters.
R. David Murrayb579dba2010-12-03 04:06:39 +000091 """
Ezio Melotti95401c52013-11-23 19:52:05 +020092 self.convert_charrefs = convert_charrefs
Guido van Rossum8846d712001-05-18 14:50:52 +000093 self.reset()
94
Guido van Rossum8846d712001-05-18 14:50:52 +000095 def reset(self):
Fred Drake1d4601d2001-08-03 19:50:59 +000096 """Reset this instance. Loses all unprocessed data."""
Guido van Rossum8846d712001-05-18 14:50:52 +000097 self.rawdata = ''
Guido van Rossum8846d712001-05-18 14:50:52 +000098 self.lasttag = '???'
Guido van Rossum8846d712001-05-18 14:50:52 +000099 self.interesting = interesting_normal
Ezio Melotti7de56f62011-11-01 14:12:22 +0200100 self.cdata_elem = None
Fred Drakecb5c80f2007-12-07 11:10:11 +0000101 _markupbase.ParserBase.reset(self)
Guido van Rossum8846d712001-05-18 14:50:52 +0000102
Guido van Rossum8846d712001-05-18 14:50:52 +0000103 def feed(self, data):
Serhiy Storchakac842efc2017-05-24 07:20:45 +0300104 r"""Feed data to the parser.
Fred Drake1d4601d2001-08-03 19:50:59 +0000105
106 Call this as often as you want, with as little or as much text
107 as you want (may include '\n').
108 """
Guido van Rossum8846d712001-05-18 14:50:52 +0000109 self.rawdata = self.rawdata + data
110 self.goahead(0)
111
Guido van Rossum8846d712001-05-18 14:50:52 +0000112 def close(self):
Fred Drake1d4601d2001-08-03 19:50:59 +0000113 """Handle any buffered data."""
Guido van Rossum8846d712001-05-18 14:50:52 +0000114 self.goahead(1)
115
Guido van Rossum8846d712001-05-18 14:50:52 +0000116 __starttag_text = None
117
Guido van Rossum8846d712001-05-18 14:50:52 +0000118 def get_starttag_text(self):
Fred Drake1d4601d2001-08-03 19:50:59 +0000119 """Return full source of start tag: '<...>'."""
Guido van Rossum8846d712001-05-18 14:50:52 +0000120 return self.__starttag_text
121
Ezio Melotti7de56f62011-11-01 14:12:22 +0200122 def set_cdata_mode(self, elem):
Ezio Melotti7de56f62011-11-01 14:12:22 +0200123 self.cdata_elem = elem.lower()
Ezio Melotti15cb4892011-11-18 18:01:49 +0200124 self.interesting = re.compile(r'</\s*%s\s*>' % self.cdata_elem, re.I)
Guido van Rossum8846d712001-05-18 14:50:52 +0000125
126 def clear_cdata_mode(self):
127 self.interesting = interesting_normal
Ezio Melotti7de56f62011-11-01 14:12:22 +0200128 self.cdata_elem = None
Guido van Rossum8846d712001-05-18 14:50:52 +0000129
130 # Internal -- handle data as far as reasonable. May leave state
131 # and data to be processed by a subsequent call. If 'end' is
132 # true, force handling all data as if followed by EOF marker.
133 def goahead(self, end):
134 rawdata = self.rawdata
135 i = 0
136 n = len(rawdata)
137 while i < n:
Ezio Melotti95401c52013-11-23 19:52:05 +0200138 if self.convert_charrefs and not self.cdata_elem:
139 j = rawdata.find('<', i)
140 if j < 0:
Ezio Melotti6f2bb982015-09-06 21:38:06 +0300141 # if we can't find the next <, either we are at the end
142 # or there's more text incoming. If the latter is True,
143 # we can't pass the text to handle_data in case we have
144 # a charref cut in half at end. Try to determine if
Martin Panter46f50722016-05-26 05:35:26 +0000145 # this is the case before proceeding by looking for an
Ezio Melotti6f2bb982015-09-06 21:38:06 +0300146 # & near the end and see if it's followed by a space or ;.
147 amppos = rawdata.rfind('&', max(i, n-34))
148 if (amppos >= 0 and
149 not re.compile(r'[\s;]').search(rawdata, amppos)):
Ezio Melotti95401c52013-11-23 19:52:05 +0200150 break # wait till we get all the text
151 j = n
Guido van Rossum8846d712001-05-18 14:50:52 +0000152 else:
Ezio Melotti95401c52013-11-23 19:52:05 +0200153 match = self.interesting.search(rawdata, i) # < or &
154 if match:
155 j = match.start()
156 else:
157 if self.cdata_elem:
158 break
159 j = n
160 if i < j:
161 if self.convert_charrefs and not self.cdata_elem:
162 self.handle_data(unescape(rawdata[i:j]))
163 else:
164 self.handle_data(rawdata[i:j])
Guido van Rossum8846d712001-05-18 14:50:52 +0000165 i = self.updatepos(i, j)
166 if i == n: break
Fred Drake248b0432001-12-03 17:09:50 +0000167 startswith = rawdata.startswith
168 if startswith('<', i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000169 if starttagopen.match(rawdata, i): # < + letter
170 k = self.parse_starttag(i)
Fred Drake248b0432001-12-03 17:09:50 +0000171 elif startswith("</", i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000172 k = self.parse_endtag(i)
Fred Drake248b0432001-12-03 17:09:50 +0000173 elif startswith("<!--", i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000174 k = self.parse_comment(i)
Fred Drake248b0432001-12-03 17:09:50 +0000175 elif startswith("<?", i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000176 k = self.parse_pi(i)
Fred Drake248b0432001-12-03 17:09:50 +0000177 elif startswith("<!", i):
Ezio Melotti73a43592014-08-02 14:10:30 +0300178 k = self.parse_html_declaration(i)
Fred Drake68eac2b2001-09-04 15:10:16 +0000179 elif (i + 1) < n:
Fred Drake029acfb2001-08-20 21:24:19 +0000180 self.handle_data("<")
181 k = i + 1
Fred Drake68eac2b2001-09-04 15:10:16 +0000182 else:
183 break
Guido van Rossum8846d712001-05-18 14:50:52 +0000184 if k < 0:
R. David Murrayb579dba2010-12-03 04:06:39 +0000185 if not end:
186 break
R. David Murrayb579dba2010-12-03 04:06:39 +0000187 k = rawdata.find('>', i + 1)
188 if k < 0:
189 k = rawdata.find('<', i + 1)
190 if k < 0:
191 k = i + 1
192 else:
193 k += 1
Ezio Melotti95401c52013-11-23 19:52:05 +0200194 if self.convert_charrefs and not self.cdata_elem:
195 self.handle_data(unescape(rawdata[i:k]))
196 else:
197 self.handle_data(rawdata[i:k])
Guido van Rossum8846d712001-05-18 14:50:52 +0000198 i = self.updatepos(i, k)
Fred Drake248b0432001-12-03 17:09:50 +0000199 elif startswith("&#", i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000200 match = charref.match(rawdata, i)
201 if match:
Fred Drake1d4601d2001-08-03 19:50:59 +0000202 name = match.group()[2:-1]
Guido van Rossum8846d712001-05-18 14:50:52 +0000203 self.handle_charref(name)
204 k = match.end()
Fred Drake248b0432001-12-03 17:09:50 +0000205 if not startswith(';', k-1):
Fred Drake029acfb2001-08-20 21:24:19 +0000206 k = k - 1
Guido van Rossum8846d712001-05-18 14:50:52 +0000207 i = self.updatepos(i, k)
208 continue
Fred Drake68eac2b2001-09-04 15:10:16 +0000209 else:
Ezio Melottif27b9a72014-02-01 21:21:01 +0200210 if ";" in rawdata[i:]: # bail by consuming &#
211 self.handle_data(rawdata[i:i+2])
212 i = self.updatepos(i, i+2)
Fred Drake68eac2b2001-09-04 15:10:16 +0000213 break
Fred Drake248b0432001-12-03 17:09:50 +0000214 elif startswith('&', i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000215 match = entityref.match(rawdata, i)
216 if match:
217 name = match.group(1)
218 self.handle_entityref(name)
219 k = match.end()
Fred Drake248b0432001-12-03 17:09:50 +0000220 if not startswith(';', k-1):
Fred Drake029acfb2001-08-20 21:24:19 +0000221 k = k - 1
Guido van Rossum8846d712001-05-18 14:50:52 +0000222 i = self.updatepos(i, k)
223 continue
Fred Drake029acfb2001-08-20 21:24:19 +0000224 match = incomplete.match(rawdata, i)
225 if match:
Fred Drake68eac2b2001-09-04 15:10:16 +0000226 # match.group() will contain at least 2 chars
Fred Drake248b0432001-12-03 17:09:50 +0000227 if end and match.group() == rawdata[i:]:
Ezio Melotti73a43592014-08-02 14:10:30 +0300228 k = match.end()
229 if k <= i:
230 k = n
231 i = self.updatepos(i, i + 1)
Fred Drake68eac2b2001-09-04 15:10:16 +0000232 # incomplete
233 break
234 elif (i + 1) < n:
235 # not the end of the buffer, and can't be confused
236 # with some other construct
237 self.handle_data("&")
238 i = self.updatepos(i, i + 1)
239 else:
240 break
Guido van Rossum8846d712001-05-18 14:50:52 +0000241 else:
242 assert 0, "interesting.search() lied"
243 # end while
Ezio Melotti15cb4892011-11-18 18:01:49 +0200244 if end and i < n and not self.cdata_elem:
Ezio Melotti95401c52013-11-23 19:52:05 +0200245 if self.convert_charrefs and not self.cdata_elem:
246 self.handle_data(unescape(rawdata[i:n]))
247 else:
248 self.handle_data(rawdata[i:n])
Guido van Rossum8846d712001-05-18 14:50:52 +0000249 i = self.updatepos(i, n)
250 self.rawdata = rawdata[i:]
251
Ezio Melottif4ab4912012-02-13 15:50:37 +0200252 # Internal -- parse html declarations, return length or -1 if not terminated
253 # See w3.org/TR/html5/tokenization.html#markup-declaration-open-state
254 # See also parse_declaration in _markupbase
255 def parse_html_declaration(self, i):
256 rawdata = self.rawdata
Ezio Melotti3861d8b2012-06-23 15:27:51 +0200257 assert rawdata[i:i+2] == '<!', ('unexpected call to '
258 'parse_html_declaration()')
Ezio Melottif4ab4912012-02-13 15:50:37 +0200259 if rawdata[i:i+4] == '<!--':
Ezio Melottie31dded2012-02-13 20:20:00 +0200260 # this case is actually already handled in goahead()
Ezio Melottif4ab4912012-02-13 15:50:37 +0200261 return self.parse_comment(i)
262 elif rawdata[i:i+3] == '<![':
263 return self.parse_marked_section(i)
264 elif rawdata[i:i+9].lower() == '<!doctype':
265 # find the closing >
Ezio Melottie31dded2012-02-13 20:20:00 +0200266 gtpos = rawdata.find('>', i+9)
Ezio Melottif4ab4912012-02-13 15:50:37 +0200267 if gtpos == -1:
268 return -1
269 self.handle_decl(rawdata[i+2:gtpos])
270 return gtpos+1
271 else:
272 return self.parse_bogus_comment(i)
273
Ezio Melottifa3702d2012-02-10 10:45:44 +0200274 # Internal -- parse bogus comment, return length or -1 if not terminated
275 # see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state
276 def parse_bogus_comment(self, i, report=1):
277 rawdata = self.rawdata
Ezio Melotti3861d8b2012-06-23 15:27:51 +0200278 assert rawdata[i:i+2] in ('<!', '</'), ('unexpected call to '
279 'parse_comment()')
Ezio Melottifa3702d2012-02-10 10:45:44 +0200280 pos = rawdata.find('>', i+2)
281 if pos == -1:
282 return -1
283 if report:
284 self.handle_comment(rawdata[i+2:pos])
285 return pos + 1
286
Guido van Rossum8846d712001-05-18 14:50:52 +0000287 # Internal -- parse processing instr, return end or -1 if not terminated
288 def parse_pi(self, i):
289 rawdata = self.rawdata
290 assert rawdata[i:i+2] == '<?', 'unexpected call to parse_pi()'
291 match = piclose.search(rawdata, i+2) # >
292 if not match:
293 return -1
294 j = match.start()
295 self.handle_pi(rawdata[i+2: j])
296 j = match.end()
297 return j
298
299 # Internal -- handle starttag, return end or -1 if not terminated
300 def parse_starttag(self, i):
301 self.__starttag_text = None
302 endpos = self.check_for_whole_start_tag(i)
303 if endpos < 0:
304 return endpos
305 rawdata = self.rawdata
306 self.__starttag_text = rawdata[i:endpos]
307
308 # Now parse the data between i+1 and j into a tag and attrs
309 attrs = []
Ezio Melotti73a43592014-08-02 14:10:30 +0300310 match = tagfind_tolerant.match(rawdata, i+1)
Guido van Rossum8846d712001-05-18 14:50:52 +0000311 assert match, 'unexpected call to parse_starttag()'
312 k = match.end()
Ezio Melotti0780b6b2012-04-18 19:18:22 -0600313 self.lasttag = tag = match.group(1).lower()
Guido van Rossum8846d712001-05-18 14:50:52 +0000314 while k < endpos:
Ezio Melotti73a43592014-08-02 14:10:30 +0300315 m = attrfind_tolerant.match(rawdata, k)
Guido van Rossum8846d712001-05-18 14:50:52 +0000316 if not m:
317 break
318 attrname, rest, attrvalue = m.group(1, 2, 3)
319 if not rest:
320 attrvalue = None
321 elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
322 attrvalue[:1] == '"' == attrvalue[-1:]:
323 attrvalue = attrvalue[1:-1]
Ezio Melottic2fe5772011-11-14 18:53:33 +0200324 if attrvalue:
Ezio Melotti4a9ee262013-11-19 20:28:45 +0200325 attrvalue = unescape(attrvalue)
Fred Drake248b0432001-12-03 17:09:50 +0000326 attrs.append((attrname.lower(), attrvalue))
Guido van Rossum8846d712001-05-18 14:50:52 +0000327 k = m.end()
328
Fred Drake248b0432001-12-03 17:09:50 +0000329 end = rawdata[k:endpos].strip()
Guido van Rossum8846d712001-05-18 14:50:52 +0000330 if end not in (">", "/>"):
331 lineno, offset = self.getpos()
332 if "\n" in self.__starttag_text:
Fred Drake248b0432001-12-03 17:09:50 +0000333 lineno = lineno + self.__starttag_text.count("\n")
Guido van Rossum8846d712001-05-18 14:50:52 +0000334 offset = len(self.__starttag_text) \
Fred Drake248b0432001-12-03 17:09:50 +0000335 - self.__starttag_text.rfind("\n")
Guido van Rossum8846d712001-05-18 14:50:52 +0000336 else:
337 offset = offset + len(self.__starttag_text)
R. David Murrayb579dba2010-12-03 04:06:39 +0000338 self.handle_data(rawdata[i:endpos])
339 return endpos
Fred Drake248b0432001-12-03 17:09:50 +0000340 if end.endswith('/>'):
Guido van Rossum8846d712001-05-18 14:50:52 +0000341 # XHTML-style empty tag: <span attr="value" />
342 self.handle_startendtag(tag, attrs)
343 else:
344 self.handle_starttag(tag, attrs)
345 if tag in self.CDATA_CONTENT_ELEMENTS:
Ezio Melotti7de56f62011-11-01 14:12:22 +0200346 self.set_cdata_mode(tag)
Guido van Rossum8846d712001-05-18 14:50:52 +0000347 return endpos
348
349 # Internal -- check to see if we have a complete starttag; return end
350 # or -1 if incomplete.
351 def check_for_whole_start_tag(self, i):
352 rawdata = self.rawdata
Ezio Melotti73a43592014-08-02 14:10:30 +0300353 m = locatestarttagend_tolerant.match(rawdata, i)
Guido van Rossum8846d712001-05-18 14:50:52 +0000354 if m:
355 j = m.end()
356 next = rawdata[j:j+1]
357 if next == ">":
358 return j + 1
359 if next == "/":
Fred Drake248b0432001-12-03 17:09:50 +0000360 if rawdata.startswith("/>", j):
Guido van Rossum8846d712001-05-18 14:50:52 +0000361 return j + 2
Fred Drake248b0432001-12-03 17:09:50 +0000362 if rawdata.startswith("/", j):
Guido van Rossum8846d712001-05-18 14:50:52 +0000363 # buffer boundary
364 return -1
365 # else bogus input
R. David Murrayb579dba2010-12-03 04:06:39 +0000366 if j > i:
367 return j
368 else:
369 return i + 1
Guido van Rossum8846d712001-05-18 14:50:52 +0000370 if next == "":
371 # end of input
372 return -1
373 if next in ("abcdefghijklmnopqrstuvwxyz=/"
374 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
375 # end of input in or before attribute value, or we have the
376 # '/' from a '/>' ending
377 return -1
R. David Murrayb579dba2010-12-03 04:06:39 +0000378 if j > i:
379 return j
380 else:
381 return i + 1
Fred Drakebfc8fea2001-09-24 20:10:28 +0000382 raise AssertionError("we should not get here!")
Guido van Rossum8846d712001-05-18 14:50:52 +0000383
384 # Internal -- parse endtag, return end or -1 if incomplete
385 def parse_endtag(self, i):
386 rawdata = self.rawdata
387 assert rawdata[i:i+2] == "</", "unexpected call to parse_endtag"
388 match = endendtag.search(rawdata, i+1) # >
389 if not match:
390 return -1
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200391 gtpos = match.end()
Guido van Rossum8846d712001-05-18 14:50:52 +0000392 match = endtagfind.match(rawdata, i) # </ + tag + >
393 if not match:
Ezio Melotti7de56f62011-11-01 14:12:22 +0200394 if self.cdata_elem is not None:
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200395 self.handle_data(rawdata[i:gtpos])
396 return gtpos
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200397 # find the name: w3.org/TR/html5/tokenization.html#tag-name-state
398 namematch = tagfind_tolerant.match(rawdata, i+2)
399 if not namematch:
400 # w3.org/TR/html5/tokenization.html#end-tag-open-state
401 if rawdata[i:i+3] == '</>':
402 return i+3
403 else:
404 return self.parse_bogus_comment(i)
Ezio Melotti7165d8b2013-11-07 18:33:24 +0200405 tagname = namematch.group(1).lower()
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200406 # consume and ignore other stuff between the name and the >
407 # Note: this is not 100% correct, since we might have things like
408 # </tag attr=">">, but looking for > after tha name should cover
409 # most of the cases and is much simpler
410 gtpos = rawdata.find('>', namematch.end())
411 self.handle_endtag(tagname)
412 return gtpos+1
Ezio Melotti7de56f62011-11-01 14:12:22 +0200413
414 elem = match.group(1).lower() # script or style
415 if self.cdata_elem is not None:
416 if elem != self.cdata_elem:
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200417 self.handle_data(rawdata[i:gtpos])
418 return gtpos
Ezio Melotti7de56f62011-11-01 14:12:22 +0200419
Motoki Naruse3358d582017-06-17 10:15:25 +0900420 self.handle_endtag(elem)
Fred Drake30d59ba2002-05-14 15:50:11 +0000421 self.clear_cdata_mode()
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200422 return gtpos
Guido van Rossum8846d712001-05-18 14:50:52 +0000423
424 # Overridable -- finish processing of start+end tag: <tag.../>
425 def handle_startendtag(self, tag, attrs):
426 self.handle_starttag(tag, attrs)
427 self.handle_endtag(tag)
428
429 # Overridable -- handle start tag
430 def handle_starttag(self, tag, attrs):
431 pass
432
433 # Overridable -- handle end tag
434 def handle_endtag(self, tag):
435 pass
436
437 # Overridable -- handle character reference
438 def handle_charref(self, name):
439 pass
440
441 # Overridable -- handle entity reference
442 def handle_entityref(self, name):
443 pass
444
445 # Overridable -- handle data
446 def handle_data(self, data):
447 pass
448
449 # Overridable -- handle comment
450 def handle_comment(self, data):
451 pass
452
453 # Overridable -- handle declaration
454 def handle_decl(self, decl):
455 pass
456
457 # Overridable -- handle processing instruction
458 def handle_pi(self, data):
459 pass
460
Fred Drakebfc8fea2001-09-24 20:10:28 +0000461 def unknown_decl(self, data):
Ezio Melotti73a43592014-08-02 14:10:30 +0300462 pass