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 | |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 11 | import markupbase |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 12 | import re |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 13 | |
| 14 | # Regular expressions used for parsing |
| 15 | |
| 16 | interesting_normal = re.compile('[&<]') |
| 17 | interesting_cdata = re.compile(r'<(/|\Z)') |
Fred Drake | 68eac2b | 2001-09-04 15:10:16 +0000 | [diff] [blame] | 18 | incomplete = re.compile('&[a-zA-Z#]') |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 19 | |
| 20 | 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] | 21 | 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] | 22 | |
| 23 | starttagopen = re.compile('<[a-zA-Z]') |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 24 | piclose = re.compile('>') |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 25 | commentclose = re.compile(r'--\s*>') |
| 26 | tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*') |
| 27 | attrfind = re.compile( |
| 28 | r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*' |
Andrew M. Kuchling | b7d8ce0 | 2004-06-05 15:31:45 +0000 | [diff] [blame] | 29 | r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\(\)_#=~@]*))?') |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 30 | |
| 31 | locatestarttagend = re.compile(r""" |
| 32 | <[a-zA-Z][-.a-zA-Z0-9:_]* # tag name |
| 33 | (?:\s+ # whitespace before attribute name |
| 34 | (?:[a-zA-Z_][-.:a-zA-Z0-9_]* # attribute name |
| 35 | (?:\s*=\s* # value indicator |
| 36 | (?:'[^']*' # LITA-enclosed value |
| 37 | |\"[^\"]*\" # LIT-enclosed value |
| 38 | |[^'\">\s]+ # bare value |
| 39 | ) |
| 40 | )? |
| 41 | ) |
| 42 | )* |
| 43 | \s* # trailing whitespace |
| 44 | """, re.VERBOSE) |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 45 | endendtag = re.compile('>') |
| 46 | endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>') |
| 47 | |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 48 | |
| 49 | class HTMLParseError(Exception): |
| 50 | """Exception raised for all parse errors.""" |
| 51 | |
| 52 | def __init__(self, msg, position=(None, None)): |
| 53 | assert msg |
| 54 | self.msg = msg |
| 55 | self.lineno = position[0] |
| 56 | self.offset = position[1] |
| 57 | |
| 58 | def __str__(self): |
| 59 | result = self.msg |
| 60 | if self.lineno is not None: |
| 61 | result = result + ", at line %d" % self.lineno |
| 62 | if self.offset is not None: |
| 63 | result = result + ", column %d" % (self.offset + 1) |
| 64 | return result |
| 65 | |
| 66 | |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 67 | class HTMLParser(markupbase.ParserBase): |
Fred Drake | 1d4601d | 2001-08-03 19:50:59 +0000 | [diff] [blame] | 68 | """Find tags and other markup and call handler functions. |
| 69 | |
| 70 | Usage: |
| 71 | p = HTMLParser() |
| 72 | p.feed(data) |
| 73 | ... |
| 74 | p.close() |
| 75 | |
| 76 | Start tags are handled by calling self.handle_starttag() or |
| 77 | self.handle_startendtag(); end tags by self.handle_endtag(). The |
| 78 | data between tags is passed from the parser to the derived class |
| 79 | by calling self.handle_data() with the data as argument (the data |
| 80 | may be split up in arbitrary chunks). Entity references are |
| 81 | passed by calling self.handle_entityref() with the entity |
| 82 | reference as the argument. Numeric character references are |
| 83 | passed to self.handle_charref() with the string containing the |
| 84 | reference as the argument. |
| 85 | """ |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 86 | |
| 87 | CDATA_CONTENT_ELEMENTS = ("script", "style") |
| 88 | |
| 89 | |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 90 | def __init__(self): |
Fred Drake | 1d4601d | 2001-08-03 19:50:59 +0000 | [diff] [blame] | 91 | """Initialize and reset this instance.""" |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 92 | self.reset() |
| 93 | |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 94 | def reset(self): |
Fred Drake | 1d4601d | 2001-08-03 19:50:59 +0000 | [diff] [blame] | 95 | """Reset this instance. Loses all unprocessed data.""" |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 96 | self.rawdata = '' |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 97 | self.lasttag = '???' |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 98 | self.interesting = interesting_normal |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 99 | markupbase.ParserBase.reset(self) |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 101 | def feed(self, data): |
Fred Drake | 1d4601d | 2001-08-03 19:50:59 +0000 | [diff] [blame] | 102 | """Feed data to the parser. |
| 103 | |
| 104 | Call this as often as you want, with as little or as much text |
| 105 | as you want (may include '\n'). |
| 106 | """ |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 107 | self.rawdata = self.rawdata + data |
| 108 | self.goahead(0) |
| 109 | |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 110 | def close(self): |
Fred Drake | 1d4601d | 2001-08-03 19:50:59 +0000 | [diff] [blame] | 111 | """Handle any buffered data.""" |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 112 | self.goahead(1) |
| 113 | |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 114 | def error(self, message): |
| 115 | raise HTMLParseError(message, self.getpos()) |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 116 | |
| 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 | |
| 123 | def set_cdata_mode(self): |
| 124 | self.interesting = interesting_cdata |
| 125 | |
| 126 | def clear_cdata_mode(self): |
| 127 | self.interesting = interesting_normal |
| 128 | |
| 129 | # Internal -- handle data as far as reasonable. May leave state |
| 130 | # and data to be processed by a subsequent call. If 'end' is |
| 131 | # true, force handling all data as if followed by EOF marker. |
| 132 | def goahead(self, end): |
| 133 | rawdata = self.rawdata |
| 134 | i = 0 |
| 135 | n = len(rawdata) |
| 136 | while i < n: |
| 137 | match = self.interesting.search(rawdata, i) # < or & |
| 138 | if match: |
| 139 | j = match.start() |
| 140 | else: |
| 141 | j = n |
| 142 | if i < j: self.handle_data(rawdata[i:j]) |
| 143 | i = self.updatepos(i, j) |
| 144 | if i == n: break |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 145 | startswith = rawdata.startswith |
| 146 | if startswith('<', i): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 147 | if starttagopen.match(rawdata, i): # < + letter |
| 148 | k = self.parse_starttag(i) |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 149 | elif startswith("</", i): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 150 | k = self.parse_endtag(i) |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 151 | elif startswith("<!--", i): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 152 | k = self.parse_comment(i) |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 153 | elif startswith("<?", i): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 154 | k = self.parse_pi(i) |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 155 | elif startswith("<!", i): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 156 | k = self.parse_declaration(i) |
Fred Drake | 68eac2b | 2001-09-04 15:10:16 +0000 | [diff] [blame] | 157 | elif (i + 1) < n: |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 158 | self.handle_data("<") |
| 159 | k = i + 1 |
Fred Drake | 68eac2b | 2001-09-04 15:10:16 +0000 | [diff] [blame] | 160 | else: |
| 161 | break |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 162 | if k < 0: |
| 163 | if end: |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 164 | self.error("EOF in middle of construct") |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 165 | break |
| 166 | i = self.updatepos(i, k) |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 167 | elif startswith("&#", i): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 168 | match = charref.match(rawdata, i) |
| 169 | if match: |
Fred Drake | 1d4601d | 2001-08-03 19:50:59 +0000 | [diff] [blame] | 170 | name = match.group()[2:-1] |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 171 | self.handle_charref(name) |
| 172 | k = match.end() |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 173 | if not startswith(';', k-1): |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 174 | k = k - 1 |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 175 | i = self.updatepos(i, k) |
| 176 | continue |
Fred Drake | 68eac2b | 2001-09-04 15:10:16 +0000 | [diff] [blame] | 177 | else: |
| 178 | break |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 179 | elif startswith('&', i): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 180 | match = entityref.match(rawdata, i) |
| 181 | if match: |
| 182 | name = match.group(1) |
| 183 | self.handle_entityref(name) |
| 184 | k = match.end() |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 185 | if not startswith(';', k-1): |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 186 | k = k - 1 |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 187 | i = self.updatepos(i, k) |
| 188 | continue |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 189 | match = incomplete.match(rawdata, i) |
| 190 | if match: |
Fred Drake | 68eac2b | 2001-09-04 15:10:16 +0000 | [diff] [blame] | 191 | # match.group() will contain at least 2 chars |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 192 | if end and match.group() == rawdata[i:]: |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 193 | self.error("EOF in middle of entity or char ref") |
Fred Drake | 68eac2b | 2001-09-04 15:10:16 +0000 | [diff] [blame] | 194 | # incomplete |
| 195 | break |
| 196 | elif (i + 1) < n: |
| 197 | # not the end of the buffer, and can't be confused |
| 198 | # with some other construct |
| 199 | self.handle_data("&") |
| 200 | i = self.updatepos(i, i + 1) |
| 201 | else: |
| 202 | break |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 203 | else: |
| 204 | assert 0, "interesting.search() lied" |
| 205 | # end while |
| 206 | if end and i < n: |
| 207 | self.handle_data(rawdata[i:n]) |
| 208 | i = self.updatepos(i, n) |
| 209 | self.rawdata = rawdata[i:] |
| 210 | |
| 211 | # Internal -- parse comment, return end or -1 if not terminated |
Fred Drake | 68eac2b | 2001-09-04 15:10:16 +0000 | [diff] [blame] | 212 | def parse_comment(self, i, report=1): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 213 | rawdata = self.rawdata |
| 214 | assert rawdata[i:i+4] == '<!--', 'unexpected call to parse_comment()' |
| 215 | match = commentclose.search(rawdata, i+4) |
| 216 | if not match: |
| 217 | return -1 |
Fred Drake | 68eac2b | 2001-09-04 15:10:16 +0000 | [diff] [blame] | 218 | if report: |
| 219 | j = match.start() |
| 220 | self.handle_comment(rawdata[i+4: j]) |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 221 | j = match.end() |
| 222 | return j |
| 223 | |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 224 | # Internal -- parse processing instr, return end or -1 if not terminated |
| 225 | def parse_pi(self, i): |
| 226 | rawdata = self.rawdata |
| 227 | assert rawdata[i:i+2] == '<?', 'unexpected call to parse_pi()' |
| 228 | match = piclose.search(rawdata, i+2) # > |
| 229 | if not match: |
| 230 | return -1 |
| 231 | j = match.start() |
| 232 | self.handle_pi(rawdata[i+2: j]) |
| 233 | j = match.end() |
| 234 | return j |
| 235 | |
| 236 | # Internal -- handle starttag, return end or -1 if not terminated |
| 237 | def parse_starttag(self, i): |
| 238 | self.__starttag_text = None |
| 239 | endpos = self.check_for_whole_start_tag(i) |
| 240 | if endpos < 0: |
| 241 | return endpos |
| 242 | rawdata = self.rawdata |
| 243 | self.__starttag_text = rawdata[i:endpos] |
| 244 | |
| 245 | # Now parse the data between i+1 and j into a tag and attrs |
| 246 | attrs = [] |
| 247 | match = tagfind.match(rawdata, i+1) |
| 248 | assert match, 'unexpected call to parse_starttag()' |
| 249 | k = match.end() |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 250 | self.lasttag = tag = rawdata[i+1:k].lower() |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 251 | |
| 252 | while k < endpos: |
| 253 | m = attrfind.match(rawdata, k) |
| 254 | if not m: |
| 255 | break |
| 256 | attrname, rest, attrvalue = m.group(1, 2, 3) |
| 257 | if not rest: |
| 258 | attrvalue = None |
| 259 | elif attrvalue[:1] == '\'' == attrvalue[-1:] or \ |
| 260 | attrvalue[:1] == '"' == attrvalue[-1:]: |
| 261 | attrvalue = attrvalue[1:-1] |
| 262 | attrvalue = self.unescape(attrvalue) |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 263 | attrs.append((attrname.lower(), attrvalue)) |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 264 | k = m.end() |
| 265 | |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 266 | end = rawdata[k:endpos].strip() |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 267 | if end not in (">", "/>"): |
| 268 | lineno, offset = self.getpos() |
| 269 | if "\n" in self.__starttag_text: |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 270 | lineno = lineno + self.__starttag_text.count("\n") |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 271 | offset = len(self.__starttag_text) \ |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 272 | - self.__starttag_text.rfind("\n") |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 273 | else: |
| 274 | offset = offset + len(self.__starttag_text) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 275 | self.error("junk characters in start tag: %r" |
| 276 | % (rawdata[k:endpos][:20],)) |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 277 | if end.endswith('/>'): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 278 | # XHTML-style empty tag: <span attr="value" /> |
| 279 | self.handle_startendtag(tag, attrs) |
| 280 | else: |
| 281 | self.handle_starttag(tag, attrs) |
| 282 | if tag in self.CDATA_CONTENT_ELEMENTS: |
| 283 | self.set_cdata_mode() |
| 284 | return endpos |
| 285 | |
| 286 | # Internal -- check to see if we have a complete starttag; return end |
| 287 | # or -1 if incomplete. |
| 288 | def check_for_whole_start_tag(self, i): |
| 289 | rawdata = self.rawdata |
| 290 | m = locatestarttagend.match(rawdata, i) |
| 291 | if m: |
| 292 | j = m.end() |
| 293 | next = rawdata[j:j+1] |
| 294 | if next == ">": |
| 295 | return j + 1 |
| 296 | if next == "/": |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 297 | if rawdata.startswith("/>", j): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 298 | return j + 2 |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 299 | if rawdata.startswith("/", j): |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 300 | # buffer boundary |
| 301 | return -1 |
| 302 | # else bogus input |
| 303 | self.updatepos(i, j + 1) |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 304 | self.error("malformed empty start tag") |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 305 | if next == "": |
| 306 | # end of input |
| 307 | return -1 |
| 308 | if next in ("abcdefghijklmnopqrstuvwxyz=/" |
| 309 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ"): |
| 310 | # end of input in or before attribute value, or we have the |
| 311 | # '/' from a '/>' ending |
| 312 | return -1 |
| 313 | self.updatepos(i, j) |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 314 | self.error("malformed start tag") |
| 315 | raise AssertionError("we should not get here!") |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 316 | |
| 317 | # Internal -- parse endtag, return end or -1 if incomplete |
| 318 | def parse_endtag(self, i): |
| 319 | rawdata = self.rawdata |
| 320 | assert rawdata[i:i+2] == "</", "unexpected call to parse_endtag" |
| 321 | match = endendtag.search(rawdata, i+1) # > |
| 322 | if not match: |
| 323 | return -1 |
| 324 | j = match.end() |
| 325 | match = endtagfind.match(rawdata, i) # </ + tag + > |
| 326 | if not match: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 327 | self.error("bad end tag: %r" % (rawdata[i:j],)) |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 328 | tag = match.group(1) |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 329 | self.handle_endtag(tag.lower()) |
Fred Drake | 30d59ba | 2002-05-14 15:50:11 +0000 | [diff] [blame] | 330 | self.clear_cdata_mode() |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 331 | return j |
| 332 | |
| 333 | # Overridable -- finish processing of start+end tag: <tag.../> |
| 334 | def handle_startendtag(self, tag, attrs): |
| 335 | self.handle_starttag(tag, attrs) |
| 336 | self.handle_endtag(tag) |
| 337 | |
| 338 | # Overridable -- handle start tag |
| 339 | def handle_starttag(self, tag, attrs): |
| 340 | pass |
| 341 | |
| 342 | # Overridable -- handle end tag |
| 343 | def handle_endtag(self, tag): |
| 344 | pass |
| 345 | |
| 346 | # Overridable -- handle character reference |
| 347 | def handle_charref(self, name): |
| 348 | pass |
| 349 | |
| 350 | # Overridable -- handle entity reference |
| 351 | def handle_entityref(self, name): |
| 352 | pass |
| 353 | |
| 354 | # Overridable -- handle data |
| 355 | def handle_data(self, data): |
| 356 | pass |
| 357 | |
| 358 | # Overridable -- handle comment |
| 359 | def handle_comment(self, data): |
| 360 | pass |
| 361 | |
| 362 | # Overridable -- handle declaration |
| 363 | def handle_decl(self, decl): |
| 364 | pass |
| 365 | |
| 366 | # Overridable -- handle processing instruction |
| 367 | def handle_pi(self, data): |
| 368 | pass |
| 369 | |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 370 | def unknown_decl(self, data): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 371 | self.error("unknown declaration: %r" % (data,)) |
Fred Drake | bfc8fea | 2001-09-24 20:10:28 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 373 | # Internal -- helper to remove special character quoting |
| 374 | def unescape(self, s): |
| 375 | if '&' not in s: |
| 376 | return s |
Fred Drake | 248b043 | 2001-12-03 17:09:50 +0000 | [diff] [blame] | 377 | s = s.replace("<", "<") |
| 378 | s = s.replace(">", ">") |
| 379 | s = s.replace("'", "'") |
| 380 | s = s.replace(""", '"') |
| 381 | s = s.replace("&", "&") # Must be last |
Guido van Rossum | 8846d71 | 2001-05-18 14:50:52 +0000 | [diff] [blame] | 382 | return s |