]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Lib/HTMLParser.py
1 """A parser for HTML and XHTML."""
3 # This file is based on sgmllib.py, but the API is slightly different.
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).
14 # Regular expressions used for parsing
16 interesting_normal
= re
.compile('[&<]')
17 incomplete
= re
.compile('&[a-zA-Z#]')
19 entityref
= re
.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
20 charref
= re
.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
22 starttagopen
= re
.compile('<[a-zA-Z]')
23 piclose
= re
.compile('>')
24 commentclose
= re
.compile(r
'--\s*>')
26 # see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
27 # and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
28 # note: if you change tagfind/attrfind remember to update locatestarttagend too
29 tagfind
= re
.compile('([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*')
30 # this regex is currently unused, but left for backward compatibility
31 tagfind_tolerant
= re
.compile('[a-zA-Z][^\t\n\r\f />\x00]*')
33 attrfind
= re
.compile(
34 r
'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
35 r'(\'[^\']*\'|"[^
"]*"|
(?
![\'"])[^>\s]*))?(?:\s|/(?!>))*')
37 locatestarttagend = re.compile(r"""
38 <[a-zA-Z][^\t\n\r\f />\x00]* # tag name
39 (?:[\s/]* # optional whitespace before attribute name
40 (?:(?<=['"\s
/])[^\s
/>][^\s
/=>]* # attribute name
41 (?
:\s
*=+\s
* # value indicator
42 (?
:'[^']*' # LITA-enclosed value
43 |"[^"]*" # LIT-enclosed value
44 |(?!['"])[^>\s]* # bare value
49 \s* # trailing whitespace
51 endendtag = re.compile('>')
52 # the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between
53 # </ and the tag name, so maybe this should be fixed
54 endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
57 class HTMLParseError(Exception):
58 """Exception raised for all parse errors."""
60 def __init__(self, msg, position=(None, None)):
63 self.lineno = position[0]
64 self.offset = position[1]
68 if self.lineno is not None:
69 result = result + ", at line
%d" % self.lineno
70 if self.offset is not None:
71 result = result + ", column
%d" % (self.offset + 1)
75 class HTMLParser(markupbase.ParserBase):
76 """Find tags and other markup and call handler functions.
84 Start tags are handled by calling self.handle_starttag() or
85 self.handle_startendtag(); end tags by self.handle_endtag(). The
86 data between tags is passed from the parser to the derived class
87 by calling self.handle_data() with the data as argument (the data
88 may be split up in arbitrary chunks). Entity references are
89 passed by calling self.handle_entityref() with the entity
90 reference as the argument. Numeric character references are
91 passed to self.handle_charref() with the string containing the
92 reference as the argument.
95 CDATA_CONTENT_ELEMENTS = ("script
", "style
")
99 """Initialize and reset this instance."""
103 """Reset this instance. Loses all unprocessed data."""
106 self.interesting = interesting_normal
107 self.cdata_elem = None
108 markupbase.ParserBase.reset(self)
110 def feed(self, data):
111 r"""Feed data to the parser.
113 Call this as often as you want, with as little or as much text
114 as you want (may include '\n').
116 self.rawdata = self.rawdata + data
120 """Handle any buffered data."""
123 def error(self, message):
124 raise HTMLParseError(message, self.getpos())
126 __starttag_text = None
128 def get_starttag_text(self):
129 """Return full source of start tag: '<...>'."""
130 return self.__starttag_text
132 def set_cdata_mode(self, elem):
133 self.cdata_elem = elem.lower()
134 self.interesting = re.compile(r'</\s*%s\s*>' % self.cdata_elem, re.I)
136 def clear_cdata_mode(self):
137 self.interesting = interesting_normal
138 self.cdata_elem = None
140 # Internal -- handle data as far as reasonable. May leave state
141 # and data to be processed by a subsequent call. If 'end' is
142 # true, force handling all data as if followed by EOF marker.
143 def goahead(self, end):
144 rawdata = self.rawdata
148 match = self.interesting.search(rawdata, i) # < or &
155 if i < j: self.handle_data(rawdata[i:j])
156 i = self.updatepos(i, j)
158 startswith = rawdata.startswith
159 if startswith('<', i):
160 if starttagopen.match(rawdata, i): # < + letter
161 k = self.parse_starttag(i)
162 elif startswith("</", i):
163 k = self.parse_endtag(i)
164 elif startswith("<!--", i):
165 k = self.parse_comment(i)
166 elif startswith("<?
", i):
168 elif startswith("<!", i):
169 k = self.parse_html_declaration(i)
171 self.handle_data("<")
178 k = rawdata.find('>', i + 1)
180 k = rawdata.find('<', i + 1)
185 self.handle_data(rawdata[i:k])
186 i = self.updatepos(i, k)
187 elif startswith("&#", i):
188 match
= charref
.match(rawdata
, i
)
190 name
= match
.group()[2:-1]
191 self
.handle_charref(name
)
193 if not startswith(';', k
-1):
195 i
= self
.updatepos(i
, k
)
198 if ";" in rawdata
[i
:]: # bail by consuming '&#'
199 self
.handle_data(rawdata
[i
:i
+2])
200 i
= self
.updatepos(i
, i
+2)
202 elif startswith('&', i
):
203 match
= entityref
.match(rawdata
, i
)
205 name
= match
.group(1)
206 self
.handle_entityref(name
)
208 if not startswith(';', k
-1):
210 i
= self
.updatepos(i
, k
)
212 match
= incomplete
.match(rawdata
, i
)
214 # match.group() will contain at least 2 chars
215 if end
and match
.group() == rawdata
[i
:]:
216 self
.error("EOF in middle of entity or char ref")
220 # not the end of the buffer, and can't be confused
221 # with some other construct
222 self
.handle_data("&")
223 i
= self
.updatepos(i
, i
+ 1)
227 assert 0, "interesting.search() lied"
229 if end
and i
< n
and not self
.cdata_elem
:
230 self
.handle_data(rawdata
[i
:n
])
231 i
= self
.updatepos(i
, n
)
232 self
.rawdata
= rawdata
[i
:]
234 # Internal -- parse html declarations, return length or -1 if not terminated
235 # See w3.org/TR/html5/tokenization.html#markup-declaration-open-state
236 # See also parse_declaration in _markupbase
237 def parse_html_declaration(self
, i
):
238 rawdata
= self
.rawdata
239 if rawdata
[i
:i
+2] != '<!':
240 self
.error('unexpected call to parse_html_declaration()')
241 if rawdata
[i
:i
+4] == '<!--':
242 # this case is actually already handled in goahead()
243 return self
.parse_comment(i
)
244 elif rawdata
[i
:i
+3] == '<![':
245 return self
.parse_marked_section(i
)
246 elif rawdata
[i
:i
+9].lower() == '<!doctype':
248 gtpos
= rawdata
.find('>', i
+9)
251 self
.handle_decl(rawdata
[i
+2:gtpos
])
254 return self
.parse_bogus_comment(i
)
256 # Internal -- parse bogus comment, return length or -1 if not terminated
257 # see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state
258 def parse_bogus_comment(self
, i
, report
=1):
259 rawdata
= self
.rawdata
260 if rawdata
[i
:i
+2] not in ('<!', '</'):
261 self
.error('unexpected call to parse_comment()')
262 pos
= rawdata
.find('>', i
+2)
266 self
.handle_comment(rawdata
[i
+2:pos
])
269 # Internal -- parse processing instr, return end or -1 if not terminated
270 def parse_pi(self
, i
):
271 rawdata
= self
.rawdata
272 assert rawdata
[i
:i
+2] == '<?', 'unexpected call to parse_pi()'
273 match
= piclose
.search(rawdata
, i
+2) # >
277 self
.handle_pi(rawdata
[i
+2: j
])
281 # Internal -- handle starttag, return end or -1 if not terminated
282 def parse_starttag(self
, i
):
283 self
.__starttag
_text
= None
284 endpos
= self
.check_for_whole_start_tag(i
)
287 rawdata
= self
.rawdata
288 self
.__starttag
_text
= rawdata
[i
:endpos
]
290 # Now parse the data between i+1 and j into a tag and attrs
292 match
= tagfind
.match(rawdata
, i
+1)
293 assert match
, 'unexpected call to parse_starttag()'
295 self
.lasttag
= tag
= match
.group(1).lower()
298 m
= attrfind
.match(rawdata
, k
)
301 attrname
, rest
, attrvalue
= m
.group(1, 2, 3)
304 elif attrvalue
[:1] == '\'' == attrvalue
[-1:] or \
305 attrvalue
[:1] == '"' == attrvalue
[-1:]:
306 attrvalue
= attrvalue
[1:-1]
308 attrvalue
= self
.unescape(attrvalue
)
309 attrs
.append((attrname
.lower(), attrvalue
))
312 end
= rawdata
[k
:endpos
].strip()
313 if end
not in (">", "/>"):
314 lineno
, offset
= self
.getpos()
315 if "\n" in self
.__starttag
_text
:
316 lineno
= lineno
+ self
.__starttag
_text
.count("\n")
317 offset
= len(self
.__starttag
_text
) \
318 - self
.__starttag
_text
.rfind("\n")
320 offset
= offset
+ len(self
.__starttag
_text
)
321 self
.handle_data(rawdata
[i
:endpos
])
323 if end
.endswith('/>'):
324 # XHTML-style empty tag: <span attr="value" />
325 self
.handle_startendtag(tag
, attrs
)
327 self
.handle_starttag(tag
, attrs
)
328 if tag
in self
.CDATA_CONTENT_ELEMENTS
:
329 self
.set_cdata_mode(tag
)
332 # Internal -- check to see if we have a complete starttag; return end
333 # or -1 if incomplete.
334 def check_for_whole_start_tag(self
, i
):
335 rawdata
= self
.rawdata
336 m
= locatestarttagend
.match(rawdata
, i
)
339 next
= rawdata
[j
:j
+1]
343 if rawdata
.startswith("/>", j
):
345 if rawdata
.startswith("/", j
):
349 self
.updatepos(i
, j
+ 1)
350 self
.error("malformed empty start tag")
354 if next
in ("abcdefghijklmnopqrstuvwxyz=/"
355 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
356 # end of input in or before attribute value, or we have the
357 # '/' from a '/>' ending
363 raise AssertionError("we should not get here!")
365 # Internal -- parse endtag, return end or -1 if incomplete
366 def parse_endtag(self
, i
):
367 rawdata
= self
.rawdata
368 assert rawdata
[i
:i
+2] == "</", "unexpected call to parse_endtag"
369 match
= endendtag
.search(rawdata
, i
+1) # >
373 match
= endtagfind
.match(rawdata
, i
) # </ + tag + >
375 if self
.cdata_elem
is not None:
376 self
.handle_data(rawdata
[i
:gtpos
])
378 # find the name: w3.org/TR/html5/tokenization.html#tag-name-state
379 namematch
= tagfind
.match(rawdata
, i
+2)
381 # w3.org/TR/html5/tokenization.html#end-tag-open-state
382 if rawdata
[i
:i
+3] == '</>':
385 return self
.parse_bogus_comment(i
)
386 tagname
= namematch
.group(1).lower()
387 # consume and ignore other stuff between the name and the >
388 # Note: this is not 100% correct, since we might have things like
389 # </tag attr=">">, but looking for > after tha name should cover
390 # most of the cases and is much simpler
391 gtpos
= rawdata
.find('>', namematch
.end())
392 self
.handle_endtag(tagname
)
395 elem
= match
.group(1).lower() # script or style
396 if self
.cdata_elem
is not None:
397 if elem
!= self
.cdata_elem
:
398 self
.handle_data(rawdata
[i
:gtpos
])
401 self
.handle_endtag(elem
)
402 self
.clear_cdata_mode()
405 # Overridable -- finish processing of start+end tag: <tag.../>
406 def handle_startendtag(self
, tag
, attrs
):
407 self
.handle_starttag(tag
, attrs
)
408 self
.handle_endtag(tag
)
410 # Overridable -- handle start tag
411 def handle_starttag(self
, tag
, attrs
):
414 # Overridable -- handle end tag
415 def handle_endtag(self
, tag
):
418 # Overridable -- handle character reference
419 def handle_charref(self
, name
):
422 # Overridable -- handle entity reference
423 def handle_entityref(self
, name
):
426 # Overridable -- handle data
427 def handle_data(self
, data
):
430 # Overridable -- handle comment
431 def handle_comment(self
, data
):
434 # Overridable -- handle declaration
435 def handle_decl(self
, decl
):
438 # Overridable -- handle processing instruction
439 def handle_pi(self
, data
):
442 def unknown_decl(self
, data
):
445 # Internal -- helper to remove special character quoting
447 def unescape(self
, s
):
450 def replaceEntities(s
):
455 if s
[0] in ['x','X']:
463 # Cannot use name2codepoint directly, because HTMLParser supports apos,
464 # which is not part of HTML 4
465 import htmlentitydefs
466 if HTMLParser
.entitydefs
is None:
467 entitydefs
= HTMLParser
.entitydefs
= {'apos':u
"'"}
468 for k
, v
in htmlentitydefs
.name2codepoint
.iteritems():
469 entitydefs
[k
] = unichr(v
)
471 return self
.entitydefs
[s
]
475 return re
.sub(r
"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities
, s
)