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