annotate Lib/HTMLParser.py @ 61885:ebda6803f8d5 legacy-trunk

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