blob: 390d4ccc488b4e9119d184031f5d3450d564f8a9 [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 Melotti3861d8b2012-06-23 15:27:51 +020012import warnings
Ezio Melotti4a9ee262013-11-19 20:28:45 +020013import _markupbase
14
15from html import unescape
16
Guido van Rossum8846d712001-05-18 14:50:52 +000017
Ezio Melotti1698bab2013-05-01 16:09:34 +030018__all__ = ['HTMLParser']
19
Guido van Rossum8846d712001-05-18 14:50:52 +000020# Regular expressions used for parsing
21
22interesting_normal = re.compile('[&<]')
Fred Drake68eac2b2001-09-04 15:10:16 +000023incomplete = re.compile('&[a-zA-Z#]')
Guido van Rossum8846d712001-05-18 14:50:52 +000024
25entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
Fred Drake1d4601d2001-08-03 19:50:59 +000026charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
Guido van Rossum8846d712001-05-18 14:50:52 +000027
28starttagopen = re.compile('<[a-zA-Z]')
Guido van Rossum8846d712001-05-18 14:50:52 +000029piclose = re.compile('>')
Guido van Rossum8846d712001-05-18 14:50:52 +000030commentclose = re.compile(r'--\s*>')
Ezio Melotti29877e82012-02-21 09:25:00 +020031# Note:
Ezio Melotti73a43592014-08-02 14:10:30 +030032# 1) if you change tagfind/attrfind remember to update locatestarttagend too;
33# 2) if you change tagfind/attrfind and/or locatestarttagend the parser will
Ezio Melotti29877e82012-02-21 09:25:00 +020034# explode, so don't do it.
Ezio Melotti7165d8b2013-11-07 18:33:24 +020035# see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
36# and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
37tagfind_tolerant = re.compile('([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*')
R. David Murrayb579dba2010-12-03 04:06:39 +000038attrfind_tolerant = re.compile(
Ezio Melotti0780b6b2012-04-18 19:18:22 -060039 r'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
Ezio Melotti29877e82012-02-21 09:25:00 +020040 r'(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*')
R. David Murrayb579dba2010-12-03 04:06:39 +000041locatestarttagend_tolerant = re.compile(r"""
Ezio Melotti7165d8b2013-11-07 18:33:24 +020042 <[a-zA-Z][^\t\n\r\f />\x00]* # tag name
Ezio Melotti29877e82012-02-21 09:25:00 +020043 (?:[\s/]* # optional whitespace before attribute name
44 (?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name
Ezio Melottic2fe5772011-11-14 18:53:33 +020045 (?:\s*=+\s* # value indicator
R. David Murrayb579dba2010-12-03 04:06:39 +000046 (?:'[^']*' # LITA-enclosed value
Ezio Melottic2fe5772011-11-14 18:53:33 +020047 |"[^"]*" # LIT-enclosed value
48 |(?!['"])[^>\s]* # bare value
R. David Murrayb579dba2010-12-03 04:06:39 +000049 )
50 (?:\s*,)* # possibly followed by a comma
Ezio Melotti29877e82012-02-21 09:25:00 +020051 )?(?:\s|/(?!>))*
Ezio Melottic2fe5772011-11-14 18:53:33 +020052 )*
53 )?
R. David Murrayb579dba2010-12-03 04:06:39 +000054 \s* # trailing whitespace
55""", re.VERBOSE)
Guido van Rossum8846d712001-05-18 14:50:52 +000056endendtag = re.compile('>')
Ezio Melotti7de56f62011-11-01 14:12:22 +020057# the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between
58# </ and the tag name, so maybe this should be fixed
Guido van Rossum8846d712001-05-18 14:50:52 +000059endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
60
Guido van Rossum8846d712001-05-18 14:50:52 +000061
Ezio Melotti88ebfb12013-11-02 17:08:24 +020062
Fred Drakecb5c80f2007-12-07 11:10:11 +000063class HTMLParser(_markupbase.ParserBase):
Fred Drake1d4601d2001-08-03 19:50:59 +000064 """Find tags and other markup and call handler functions.
65
66 Usage:
67 p = HTMLParser()
68 p.feed(data)
69 ...
70 p.close()
71
72 Start tags are handled by calling self.handle_starttag() or
73 self.handle_startendtag(); end tags by self.handle_endtag(). The
74 data between tags is passed from the parser to the derived class
75 by calling self.handle_data() with the data as argument (the data
Ezio Melotti95401c52013-11-23 19:52:05 +020076 may be split up in arbitrary chunks). If convert_charrefs is
77 True the character references are converted automatically to the
78 corresponding Unicode character (and self.handle_data() is no
79 longer split in chunks), otherwise they are passed by calling
80 self.handle_entityref() or self.handle_charref() with the string
81 containing respectively the named or numeric reference as the
82 argument.
Fred Drake1d4601d2001-08-03 19:50:59 +000083 """
Guido van Rossum8846d712001-05-18 14:50:52 +000084
85 CDATA_CONTENT_ELEMENTS = ("script", "style")
86
Ezio Melotti6fc16d82014-08-02 18:36:12 +030087 def __init__(self, *, convert_charrefs=True):
R. David Murrayb579dba2010-12-03 04:06:39 +000088 """Initialize and reset this instance.
Guido van Rossum8846d712001-05-18 14:50:52 +000089
Ezio Melotti6fc16d82014-08-02 18:36:12 +030090 If convert_charrefs is True (the default), all character references
Ezio Melotti95401c52013-11-23 19:52:05 +020091 are automatically converted to the corresponding Unicode characters.
R. David Murrayb579dba2010-12-03 04:06:39 +000092 """
Ezio Melotti95401c52013-11-23 19:52:05 +020093 self.convert_charrefs = convert_charrefs
Guido van Rossum8846d712001-05-18 14:50:52 +000094 self.reset()
95
Guido van Rossum8846d712001-05-18 14:50:52 +000096 def reset(self):
Fred Drake1d4601d2001-08-03 19:50:59 +000097 """Reset this instance. Loses all unprocessed data."""
Guido van Rossum8846d712001-05-18 14:50:52 +000098 self.rawdata = ''
Guido van Rossum8846d712001-05-18 14:50:52 +000099 self.lasttag = '???'
Guido van Rossum8846d712001-05-18 14:50:52 +0000100 self.interesting = interesting_normal
Ezio Melotti7de56f62011-11-01 14:12:22 +0200101 self.cdata_elem = None
Fred Drakecb5c80f2007-12-07 11:10:11 +0000102 _markupbase.ParserBase.reset(self)
Guido van Rossum8846d712001-05-18 14:50:52 +0000103
Guido van Rossum8846d712001-05-18 14:50:52 +0000104 def feed(self, data):
Éric Araujo39f180b2011-05-04 15:55:47 +0200105 r"""Feed data to the parser.
Fred Drake1d4601d2001-08-03 19:50:59 +0000106
107 Call this as often as you want, with as little or as much text
108 as you want (may include '\n').
109 """
Guido van Rossum8846d712001-05-18 14:50:52 +0000110 self.rawdata = self.rawdata + data
111 self.goahead(0)
112
Guido van Rossum8846d712001-05-18 14:50:52 +0000113 def close(self):
Fred Drake1d4601d2001-08-03 19:50:59 +0000114 """Handle any buffered data."""
Guido van Rossum8846d712001-05-18 14:50:52 +0000115 self.goahead(1)
116
Guido van Rossum8846d712001-05-18 14:50:52 +0000117 __starttag_text = None
118
Guido van Rossum8846d712001-05-18 14:50:52 +0000119 def get_starttag_text(self):
Fred Drake1d4601d2001-08-03 19:50:59 +0000120 """Return full source of start tag: '<...>'."""
Guido van Rossum8846d712001-05-18 14:50:52 +0000121 return self.__starttag_text
122
Ezio Melotti7de56f62011-11-01 14:12:22 +0200123 def set_cdata_mode(self, elem):
Ezio Melotti7de56f62011-11-01 14:12:22 +0200124 self.cdata_elem = elem.lower()
Ezio Melotti15cb4892011-11-18 18:01:49 +0200125 self.interesting = re.compile(r'</\s*%s\s*>' % self.cdata_elem, re.I)
Guido van Rossum8846d712001-05-18 14:50:52 +0000126
127 def clear_cdata_mode(self):
128 self.interesting = interesting_normal
Ezio Melotti7de56f62011-11-01 14:12:22 +0200129 self.cdata_elem = None
Guido van Rossum8846d712001-05-18 14:50:52 +0000130
131 # Internal -- handle data as far as reasonable. May leave state
132 # and data to be processed by a subsequent call. If 'end' is
133 # true, force handling all data as if followed by EOF marker.
134 def goahead(self, end):
135 rawdata = self.rawdata
136 i = 0
137 n = len(rawdata)
138 while i < n:
Ezio Melotti95401c52013-11-23 19:52:05 +0200139 if self.convert_charrefs and not self.cdata_elem:
140 j = rawdata.find('<', i)
141 if j < 0:
142 if not end:
143 break # wait till we get all the text
144 j = n
Guido van Rossum8846d712001-05-18 14:50:52 +0000145 else:
Ezio Melotti95401c52013-11-23 19:52:05 +0200146 match = self.interesting.search(rawdata, i) # < or &
147 if match:
148 j = match.start()
149 else:
150 if self.cdata_elem:
151 break
152 j = n
153 if i < j:
154 if self.convert_charrefs and not self.cdata_elem:
155 self.handle_data(unescape(rawdata[i:j]))
156 else:
157 self.handle_data(rawdata[i:j])
Guido van Rossum8846d712001-05-18 14:50:52 +0000158 i = self.updatepos(i, j)
159 if i == n: break
Fred Drake248b0432001-12-03 17:09:50 +0000160 startswith = rawdata.startswith
161 if startswith('<', i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000162 if starttagopen.match(rawdata, i): # < + letter
163 k = self.parse_starttag(i)
Fred Drake248b0432001-12-03 17:09:50 +0000164 elif startswith("</", i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000165 k = self.parse_endtag(i)
Fred Drake248b0432001-12-03 17:09:50 +0000166 elif startswith("<!--", i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000167 k = self.parse_comment(i)
Fred Drake248b0432001-12-03 17:09:50 +0000168 elif startswith("<?", i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000169 k = self.parse_pi(i)
Fred Drake248b0432001-12-03 17:09:50 +0000170 elif startswith("<!", i):
Ezio Melotti73a43592014-08-02 14:10:30 +0300171 k = self.parse_html_declaration(i)
Fred Drake68eac2b2001-09-04 15:10:16 +0000172 elif (i + 1) < n:
Fred Drake029acfb2001-08-20 21:24:19 +0000173 self.handle_data("<")
174 k = i + 1
Fred Drake68eac2b2001-09-04 15:10:16 +0000175 else:
176 break
Guido van Rossum8846d712001-05-18 14:50:52 +0000177 if k < 0:
R. David Murrayb579dba2010-12-03 04:06:39 +0000178 if not end:
179 break
R. David Murrayb579dba2010-12-03 04:06:39 +0000180 k = rawdata.find('>', i + 1)
181 if k < 0:
182 k = rawdata.find('<', i + 1)
183 if k < 0:
184 k = i + 1
185 else:
186 k += 1
Ezio Melotti95401c52013-11-23 19:52:05 +0200187 if self.convert_charrefs and not self.cdata_elem:
188 self.handle_data(unescape(rawdata[i:k]))
189 else:
190 self.handle_data(rawdata[i:k])
Guido van Rossum8846d712001-05-18 14:50:52 +0000191 i = self.updatepos(i, k)
Fred Drake248b0432001-12-03 17:09:50 +0000192 elif startswith("&#", i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000193 match = charref.match(rawdata, i)
194 if match:
Fred Drake1d4601d2001-08-03 19:50:59 +0000195 name = match.group()[2:-1]
Guido van Rossum8846d712001-05-18 14:50:52 +0000196 self.handle_charref(name)
197 k = match.end()
Fred Drake248b0432001-12-03 17:09:50 +0000198 if not startswith(';', k-1):
Fred Drake029acfb2001-08-20 21:24:19 +0000199 k = k - 1
Guido van Rossum8846d712001-05-18 14:50:52 +0000200 i = self.updatepos(i, k)
201 continue
Fred Drake68eac2b2001-09-04 15:10:16 +0000202 else:
Ezio Melottif27b9a72014-02-01 21:21:01 +0200203 if ";" in rawdata[i:]: # bail by consuming &#
204 self.handle_data(rawdata[i:i+2])
205 i = self.updatepos(i, i+2)
Fred Drake68eac2b2001-09-04 15:10:16 +0000206 break
Fred Drake248b0432001-12-03 17:09:50 +0000207 elif startswith('&', i):
Guido van Rossum8846d712001-05-18 14:50:52 +0000208 match = entityref.match(rawdata, i)
209 if match:
210 name = match.group(1)
211 self.handle_entityref(name)
212 k = match.end()
Fred Drake248b0432001-12-03 17:09:50 +0000213 if not startswith(';', k-1):
Fred Drake029acfb2001-08-20 21:24:19 +0000214 k = k - 1
Guido van Rossum8846d712001-05-18 14:50:52 +0000215 i = self.updatepos(i, k)
216 continue
Fred Drake029acfb2001-08-20 21:24:19 +0000217 match = incomplete.match(rawdata, i)
218 if match:
Fred Drake68eac2b2001-09-04 15:10:16 +0000219 # match.group() will contain at least 2 chars
Fred Drake248b0432001-12-03 17:09:50 +0000220 if end and match.group() == rawdata[i:]:
Ezio Melotti73a43592014-08-02 14:10:30 +0300221 k = match.end()
222 if k <= i:
223 k = n
224 i = self.updatepos(i, i + 1)
Fred Drake68eac2b2001-09-04 15:10:16 +0000225 # incomplete
226 break
227 elif (i + 1) < n:
228 # not the end of the buffer, and can't be confused
229 # with some other construct
230 self.handle_data("&")
231 i = self.updatepos(i, i + 1)
232 else:
233 break
Guido van Rossum8846d712001-05-18 14:50:52 +0000234 else:
235 assert 0, "interesting.search() lied"
236 # end while
Ezio Melotti15cb4892011-11-18 18:01:49 +0200237 if end and i < n and not self.cdata_elem:
Ezio Melotti95401c52013-11-23 19:52:05 +0200238 if self.convert_charrefs and not self.cdata_elem:
239 self.handle_data(unescape(rawdata[i:n]))
240 else:
241 self.handle_data(rawdata[i:n])
Guido van Rossum8846d712001-05-18 14:50:52 +0000242 i = self.updatepos(i, n)
243 self.rawdata = rawdata[i:]
244
Ezio Melottif4ab4912012-02-13 15:50:37 +0200245 # Internal -- parse html declarations, return length or -1 if not terminated
246 # See w3.org/TR/html5/tokenization.html#markup-declaration-open-state
247 # See also parse_declaration in _markupbase
248 def parse_html_declaration(self, i):
249 rawdata = self.rawdata
Ezio Melotti3861d8b2012-06-23 15:27:51 +0200250 assert rawdata[i:i+2] == '<!', ('unexpected call to '
251 'parse_html_declaration()')
Ezio Melottif4ab4912012-02-13 15:50:37 +0200252 if rawdata[i:i+4] == '<!--':
Ezio Melottie31dded2012-02-13 20:20:00 +0200253 # this case is actually already handled in goahead()
Ezio Melottif4ab4912012-02-13 15:50:37 +0200254 return self.parse_comment(i)
255 elif rawdata[i:i+3] == '<![':
256 return self.parse_marked_section(i)
257 elif rawdata[i:i+9].lower() == '<!doctype':
258 # find the closing >
Ezio Melottie31dded2012-02-13 20:20:00 +0200259 gtpos = rawdata.find('>', i+9)
Ezio Melottif4ab4912012-02-13 15:50:37 +0200260 if gtpos == -1:
261 return -1
262 self.handle_decl(rawdata[i+2:gtpos])
263 return gtpos+1
264 else:
265 return self.parse_bogus_comment(i)
266
Ezio Melottifa3702d2012-02-10 10:45:44 +0200267 # Internal -- parse bogus comment, return length or -1 if not terminated
268 # see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state
269 def parse_bogus_comment(self, i, report=1):
270 rawdata = self.rawdata
Ezio Melotti3861d8b2012-06-23 15:27:51 +0200271 assert rawdata[i:i+2] in ('<!', '</'), ('unexpected call to '
272 'parse_comment()')
Ezio Melottifa3702d2012-02-10 10:45:44 +0200273 pos = rawdata.find('>', i+2)
274 if pos == -1:
275 return -1
276 if report:
277 self.handle_comment(rawdata[i+2:pos])
278 return pos + 1
279
Guido van Rossum8846d712001-05-18 14:50:52 +0000280 # Internal -- parse processing instr, return end or -1 if not terminated
281 def parse_pi(self, i):
282 rawdata = self.rawdata
283 assert rawdata[i:i+2] == '<?', 'unexpected call to parse_pi()'
284 match = piclose.search(rawdata, i+2) # >
285 if not match:
286 return -1
287 j = match.start()
288 self.handle_pi(rawdata[i+2: j])
289 j = match.end()
290 return j
291
292 # Internal -- handle starttag, return end or -1 if not terminated
293 def parse_starttag(self, i):
294 self.__starttag_text = None
295 endpos = self.check_for_whole_start_tag(i)
296 if endpos < 0:
297 return endpos
298 rawdata = self.rawdata
299 self.__starttag_text = rawdata[i:endpos]
300
301 # Now parse the data between i+1 and j into a tag and attrs
302 attrs = []
Ezio Melotti73a43592014-08-02 14:10:30 +0300303 match = tagfind_tolerant.match(rawdata, i+1)
Guido van Rossum8846d712001-05-18 14:50:52 +0000304 assert match, 'unexpected call to parse_starttag()'
305 k = match.end()
Ezio Melotti0780b6b2012-04-18 19:18:22 -0600306 self.lasttag = tag = match.group(1).lower()
Guido van Rossum8846d712001-05-18 14:50:52 +0000307 while k < endpos:
Ezio Melotti73a43592014-08-02 14:10:30 +0300308 m = attrfind_tolerant.match(rawdata, k)
Guido van Rossum8846d712001-05-18 14:50:52 +0000309 if not m:
310 break
311 attrname, rest, attrvalue = m.group(1, 2, 3)
312 if not rest:
313 attrvalue = None
314 elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
315 attrvalue[:1] == '"' == attrvalue[-1:]:
316 attrvalue = attrvalue[1:-1]
Ezio Melottic2fe5772011-11-14 18:53:33 +0200317 if attrvalue:
Ezio Melotti4a9ee262013-11-19 20:28:45 +0200318 attrvalue = unescape(attrvalue)
Fred Drake248b0432001-12-03 17:09:50 +0000319 attrs.append((attrname.lower(), attrvalue))
Guido van Rossum8846d712001-05-18 14:50:52 +0000320 k = m.end()
321
Fred Drake248b0432001-12-03 17:09:50 +0000322 end = rawdata[k:endpos].strip()
Guido van Rossum8846d712001-05-18 14:50:52 +0000323 if end not in (">", "/>"):
324 lineno, offset = self.getpos()
325 if "\n" in self.__starttag_text:
Fred Drake248b0432001-12-03 17:09:50 +0000326 lineno = lineno + self.__starttag_text.count("\n")
Guido van Rossum8846d712001-05-18 14:50:52 +0000327 offset = len(self.__starttag_text) \
Fred Drake248b0432001-12-03 17:09:50 +0000328 - self.__starttag_text.rfind("\n")
Guido van Rossum8846d712001-05-18 14:50:52 +0000329 else:
330 offset = offset + len(self.__starttag_text)
R. David Murrayb579dba2010-12-03 04:06:39 +0000331 self.handle_data(rawdata[i:endpos])
332 return endpos
Fred Drake248b0432001-12-03 17:09:50 +0000333 if end.endswith('/>'):
Guido van Rossum8846d712001-05-18 14:50:52 +0000334 # XHTML-style empty tag: <span attr="value" />
335 self.handle_startendtag(tag, attrs)
336 else:
337 self.handle_starttag(tag, attrs)
338 if tag in self.CDATA_CONTENT_ELEMENTS:
Ezio Melotti7de56f62011-11-01 14:12:22 +0200339 self.set_cdata_mode(tag)
Guido van Rossum8846d712001-05-18 14:50:52 +0000340 return endpos
341
342 # Internal -- check to see if we have a complete starttag; return end
343 # or -1 if incomplete.
344 def check_for_whole_start_tag(self, i):
345 rawdata = self.rawdata
Ezio Melotti73a43592014-08-02 14:10:30 +0300346 m = locatestarttagend_tolerant.match(rawdata, i)
Guido van Rossum8846d712001-05-18 14:50:52 +0000347 if m:
348 j = m.end()
349 next = rawdata[j:j+1]
350 if next == ">":
351 return j + 1
352 if next == "/":
Fred Drake248b0432001-12-03 17:09:50 +0000353 if rawdata.startswith("/>", j):
Guido van Rossum8846d712001-05-18 14:50:52 +0000354 return j + 2
Fred Drake248b0432001-12-03 17:09:50 +0000355 if rawdata.startswith("/", j):
Guido van Rossum8846d712001-05-18 14:50:52 +0000356 # buffer boundary
357 return -1
358 # else bogus input
R. David Murrayb579dba2010-12-03 04:06:39 +0000359 if j > i:
360 return j
361 else:
362 return i + 1
Guido van Rossum8846d712001-05-18 14:50:52 +0000363 if next == "":
364 # end of input
365 return -1
366 if next in ("abcdefghijklmnopqrstuvwxyz=/"
367 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
368 # end of input in or before attribute value, or we have the
369 # '/' from a '/>' ending
370 return -1
R. David Murrayb579dba2010-12-03 04:06:39 +0000371 if j > i:
372 return j
373 else:
374 return i + 1
Fred Drakebfc8fea2001-09-24 20:10:28 +0000375 raise AssertionError("we should not get here!")
Guido van Rossum8846d712001-05-18 14:50:52 +0000376
377 # Internal -- parse endtag, return end or -1 if incomplete
378 def parse_endtag(self, i):
379 rawdata = self.rawdata
380 assert rawdata[i:i+2] == "</", "unexpected call to parse_endtag"
381 match = endendtag.search(rawdata, i+1) # >
382 if not match:
383 return -1
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200384 gtpos = match.end()
Guido van Rossum8846d712001-05-18 14:50:52 +0000385 match = endtagfind.match(rawdata, i) # </ + tag + >
386 if not match:
Ezio Melotti7de56f62011-11-01 14:12:22 +0200387 if self.cdata_elem is not None:
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200388 self.handle_data(rawdata[i:gtpos])
389 return gtpos
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200390 # find the name: w3.org/TR/html5/tokenization.html#tag-name-state
391 namematch = tagfind_tolerant.match(rawdata, i+2)
392 if not namematch:
393 # w3.org/TR/html5/tokenization.html#end-tag-open-state
394 if rawdata[i:i+3] == '</>':
395 return i+3
396 else:
397 return self.parse_bogus_comment(i)
Ezio Melotti7165d8b2013-11-07 18:33:24 +0200398 tagname = namematch.group(1).lower()
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200399 # consume and ignore other stuff between the name and the >
400 # Note: this is not 100% correct, since we might have things like
401 # </tag attr=">">, but looking for > after tha name should cover
402 # most of the cases and is much simpler
403 gtpos = rawdata.find('>', namematch.end())
404 self.handle_endtag(tagname)
405 return gtpos+1
Ezio Melotti7de56f62011-11-01 14:12:22 +0200406
407 elem = match.group(1).lower() # script or style
408 if self.cdata_elem is not None:
409 if elem != self.cdata_elem:
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200410 self.handle_data(rawdata[i:gtpos])
411 return gtpos
Ezio Melotti7de56f62011-11-01 14:12:22 +0200412
413 self.handle_endtag(elem.lower())
Fred Drake30d59ba2002-05-14 15:50:11 +0000414 self.clear_cdata_mode()
Ezio Melotti5211ffe2012-02-13 11:24:50 +0200415 return gtpos
Guido van Rossum8846d712001-05-18 14:50:52 +0000416
417 # Overridable -- finish processing of start+end tag: <tag.../>
418 def handle_startendtag(self, tag, attrs):
419 self.handle_starttag(tag, attrs)
420 self.handle_endtag(tag)
421
422 # Overridable -- handle start tag
423 def handle_starttag(self, tag, attrs):
424 pass
425
426 # Overridable -- handle end tag
427 def handle_endtag(self, tag):
428 pass
429
430 # Overridable -- handle character reference
431 def handle_charref(self, name):
432 pass
433
434 # Overridable -- handle entity reference
435 def handle_entityref(self, name):
436 pass
437
438 # Overridable -- handle data
439 def handle_data(self, data):
440 pass
441
442 # Overridable -- handle comment
443 def handle_comment(self, data):
444 pass
445
446 # Overridable -- handle declaration
447 def handle_decl(self, decl):
448 pass
449
450 # Overridable -- handle processing instruction
451 def handle_pi(self, data):
452 pass
453
Fred Drakebfc8fea2001-09-24 20:10:28 +0000454 def unknown_decl(self, data):
Ezio Melotti73a43592014-08-02 14:10:30 +0300455 pass
Ezio Melottif6de9eb2013-11-22 05:49:29 +0200456
457 # Internal -- helper to remove special character quoting
458 def unescape(self, s):
459 warnings.warn('The unescape method is deprecated and will be removed '
460 'in 3.5, use html.unescape() instead.',
461 DeprecationWarning, stacklevel=2)
462 return unescape(s)