annotate Lib/HTMLParser.py @ 19687:3731ad1d9033 legacy-trunk

HTMLParser is allowed to be more strict than sgmllib, so let's not change their basic behavior: When parsing something that cannot possibly be valid in either HTML or XHTML, raise an exception.
author Fred Drake <fdrake@acm.org>
date Tue, 04 Sep 2001 16:26:03 +0000
parents 8dfb13a4e175
children 0f9df563b65d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
1 """A parser for HTML and XHTML."""
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # This file is based on sgmllib.py, but the API is slightly different.
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # XXX There should be a way to distinguish between PCDATA (parsed
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # character data -- the normal case), RCDATA (replaceable character
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # data -- only char and entity references and end tags are special)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # and CDATA (character data -- only end tags are special).
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 import re
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 import string
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # Regular expressions used for parsing
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 interesting_normal = re.compile('[&<]')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 interesting_cdata = re.compile(r'<(/|\Z)')
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
18 incomplete = re.compile('&[a-zA-Z#]')
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
21 charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 starttagopen = re.compile('<[a-zA-Z]')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 piopen = re.compile(r'<\?')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 piclose = re.compile('>')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 endtagopen = re.compile('</')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 declopen = re.compile('<!')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 special = re.compile('<![^<>]*>')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 commentopen = re.compile('<!--')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 commentclose = re.compile(r'--\s*>')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 attrfind = re.compile(
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./:;+*%?!&$\(\)_#=~]*))?')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 locatestarttagend = re.compile(r"""
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 <[a-zA-Z][-.a-zA-Z0-9:_]* # tag name
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 (?:\s+ # whitespace before attribute name
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 (?:[a-zA-Z_][-.:a-zA-Z0-9_]* # attribute name
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 (?:\s*=\s* # value indicator
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 (?:'[^']*' # LITA-enclosed value
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 |\"[^\"]*\" # LIT-enclosed value
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 |[^'\">\s]+ # bare value
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 )
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 )?
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 )
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 )*
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 \s* # trailing whitespace
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 """, re.VERBOSE)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 endstarttag = re.compile(r"\s*/?>")
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 endendtag = re.compile('>')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 declname = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 declstringlit = re.compile(r'(\'[^\']*\'|"[^"]*")\s*')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 class HTMLParseError(Exception):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 """Exception raised for all parse errors."""
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 def __init__(self, msg, position=(None, None)):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 assert msg
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 self.msg = msg
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 self.lineno = position[0]
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 self.offset = position[1]
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 def __str__(self):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 result = self.msg
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 if self.lineno is not None:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 result = result + ", at line %d" % self.lineno
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 if self.offset is not None:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 result = result + ", column %d" % (self.offset + 1)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 return result
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
76 class HTMLParser:
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
77 """Find tags and other markup and call handler functions.
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
78
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
79 Usage:
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
80 p = HTMLParser()
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
81 p.feed(data)
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
82 ...
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
83 p.close()
17932
ce7f0e380830 Removed incorrect comment left over from sgmllib.py.
Guido van Rossum <guido@python.org>
parents: 17853
diff changeset
84
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
85 Start tags are handled by calling self.handle_starttag() or
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
86 self.handle_startendtag(); end tags by self.handle_endtag(). The
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
87 data between tags is passed from the parser to the derived class
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
88 by calling self.handle_data() with the data as argument (the data
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
89 may be split up in arbitrary chunks). Entity references are
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
90 passed by calling self.handle_entityref() with the entity
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
91 reference as the argument. Numeric character references are
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
92 passed to self.handle_charref() with the string containing the
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
93 reference as the argument.
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
94 """
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 CDATA_CONTENT_ELEMENTS = ("script", "style")
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 def __init__(self):
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
100 """Initialize and reset this instance."""
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 self.reset()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def reset(self):
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
104 """Reset this instance. Loses all unprocessed data."""
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 self.rawdata = ''
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 self.stack = []
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 self.lasttag = '???'
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 self.lineno = 1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 self.offset = 0
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self.interesting = interesting_normal
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 def feed(self, data):
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
113 """Feed data to the parser.
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
114
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
115 Call this as often as you want, with as little or as much text
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
116 as you want (may include '\n').
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
117 """
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 self.rawdata = self.rawdata + data
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 self.goahead(0)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def close(self):
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
122 """Handle any buffered data."""
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 self.goahead(1)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 # Internal -- update line number and offset. This should be
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 # called for each piece of data exactly once, in order -- in other
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 # words the concatenation of all the input strings to this
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 # function should be exactly the entire input.
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 def updatepos(self, i, j):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 if i >= j:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 return j
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 rawdata = self.rawdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 nlines = string.count(rawdata, "\n", i, j)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 if nlines:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 self.lineno = self.lineno + nlines
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 pos = string.rindex(rawdata, "\n", i, j) # Should not fail
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 self.offset = j-(pos+1)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 self.offset = self.offset + j-i
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return j
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 def getpos(self):
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
143 """Return current line number and offset."""
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 return self.lineno, self.offset
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 __starttag_text = None
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 def get_starttag_text(self):
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
149 """Return full source of start tag: '<...>'."""
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 return self.__starttag_text
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 def set_cdata_mode(self):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 self.interesting = interesting_cdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 def clear_cdata_mode(self):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 self.interesting = interesting_normal
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 # Internal -- handle data as far as reasonable. May leave state
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 # and data to be processed by a subsequent call. If 'end' is
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 # true, force handling all data as if followed by EOF marker.
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 def goahead(self, end):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 rawdata = self.rawdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 i = 0
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 n = len(rawdata)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 while i < n:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 match = self.interesting.search(rawdata, i) # < or &
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 if match:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 j = match.start()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 j = n
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 if i < j: self.handle_data(rawdata[i:j])
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 i = self.updatepos(i, j)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 if i == n: break
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 if rawdata[i] == '<':
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 if starttagopen.match(rawdata, i): # < + letter
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 k = self.parse_starttag(i)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 elif endtagopen.match(rawdata, i): # </
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 k = self.parse_endtag(i)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 if k >= 0:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 self.clear_cdata_mode()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 elif commentopen.match(rawdata, i): # <!--
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 k = self.parse_comment(i)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 elif piopen.match(rawdata, i): # <?
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 k = self.parse_pi(i)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 elif declopen.match(rawdata, i): # <!
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 k = self.parse_declaration(i)
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
187 elif (i + 1) < n:
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18984
diff changeset
188 self.handle_data("<")
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18984
diff changeset
189 k = i + 1
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
190 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
191 break
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 if k < 0:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 if end:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 raise HTMLParseError("EOF in middle of construct",
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 self.getpos())
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 break
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 i = self.updatepos(i, k)
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
198 elif rawdata[i:i+2] == "&#":
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 match = charref.match(rawdata, i)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 if match:
18984
00935031b373 Change some comments into docstrings.
Fred Drake <fdrake@acm.org>
parents: 17934
diff changeset
201 name = match.group()[2:-1]
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 self.handle_charref(name)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 k = match.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 if rawdata[k-1] != ';':
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18984
diff changeset
205 k = k - 1
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 i = self.updatepos(i, k)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 continue
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
208 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
209 break
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
210 elif rawdata[i] == '&':
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 match = entityref.match(rawdata, i)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 if match:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 name = match.group(1)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 self.handle_entityref(name)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 k = match.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 if rawdata[k-1] != ';':
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18984
diff changeset
217 k = k - 1
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 i = self.updatepos(i, k)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 continue
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18984
diff changeset
220 match = incomplete.match(rawdata, i)
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18984
diff changeset
221 if match:
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
222 # match.group() will contain at least 2 chars
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18984
diff changeset
223 rest = rawdata[i:]
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
224 if end and match.group() == rest:
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 raise HTMLParseError(
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 "EOF in middle of entity or char ref",
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 self.getpos())
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
228 # incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
229 break
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
230 elif (i + 1) < n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
231 # not the end of the buffer, and can't be confused
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
232 # with some other construct
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
233 self.handle_data("&")
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
234 i = self.updatepos(i, i + 1)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
235 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
236 break
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 assert 0, "interesting.search() lied"
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 # end while
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 if end and i < n:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 self.handle_data(rawdata[i:n])
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 i = self.updatepos(i, n)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 self.rawdata = rawdata[i:]
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 # Internal -- parse comment, return end or -1 if not terminated
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
246 def parse_comment(self, i, report=1):
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 rawdata = self.rawdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 assert rawdata[i:i+4] == '<!--', 'unexpected call to parse_comment()'
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 match = commentclose.search(rawdata, i+4)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 if not match:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 return -1
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
252 if report:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
253 j = match.start()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
254 self.handle_comment(rawdata[i+4: j])
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 j = match.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 return j
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 # Internal -- parse declaration.
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 def parse_declaration(self, i):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 # This is some sort of declaration; in "HTML as
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 # deployed," this should only be the document type
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 # declaration ("<!DOCTYPE html...>").
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 rawdata = self.rawdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 j = i + 2
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 assert rawdata[i:j] == "<!", "unexpected call to parse_declaration"
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 if rawdata[j:j+1] in ("-", ""):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 # Start of comment followed by buffer boundary,
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 # or just a buffer boundary.
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 # in practice, this should look like: ((name|stringlit) S*)+ '>'
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 n = len(rawdata)
19687
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
272 decltype, j = self.scan_name(j, i)
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
273 if j < 0:
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
274 return j
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
275 if decltype.lower() != "doctype":
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
276 raise HTMLParseError("unknown declaration: '%s'" % decltype,
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
277 self.getpos())
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 while j < n:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 c = rawdata[j]
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 if c == ">":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 # end of declaration syntax
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
282 data = rawdata[i+2:j]
19687
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
283 self.handle_decl(data)
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 return j + 1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 if c in "\"'":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 m = declstringlit.match(rawdata, j)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 if not m:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 return -1 # incomplete
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 j = m.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 elif c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
19687
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
291 name, j = self.scan_name(j, i)
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
292 elif c == "[" and decltype == "doctype":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
293 j = self.parse_doctype_subset(j + 1, i)
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 raise HTMLParseError(
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 "unexpected char in declaration: %s" % `rawdata[j]`,
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 self.getpos())
19687
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
298 if j < 0:
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
299 return j
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 return -1 # incomplete
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
302 # Internal -- scan past the internal subset in a <!DOCTYPE declaration,
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
303 # returning the index just past any whitespace following the trailing ']'.
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
304 def parse_doctype_subset(self, i, declstartpos):
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
305 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
306 n = len(rawdata)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
307 j = i
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
308 while j < n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
309 c = rawdata[j]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
310 if c == "<":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
311 s = rawdata[j:j+2]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
312 if s == "<":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
313 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
314 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
315 if s != "<!":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
316 self.updatepos(declstartpos, j + 1)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
317 raise HTMLParseError("unexpect char in internal subset",
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
318 self.getpos())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
319 if (j + 2) == n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
320 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
321 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
322 if (j + 4) > n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
323 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
324 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
325 if rawdata[j:j+4] == "<!--":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
326 j = self.parse_comment(j, report=0)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
327 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
328 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
329 continue
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
330 name, j = self.scan_name(j + 2, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
331 if j == -1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
332 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
333 if name not in ("attlist", "element", "entity", "notation"):
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
334 self.updatepos(declstartpos, j + 2)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
335 raise HTMLParseError(
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
336 "unknown declaration %s in internal subset" % `name`,
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
337 self.getpos())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
338 # handle the individual names
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
339 meth = getattr(self, "parse_doctype_" + name)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
340 j = meth(j, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
341 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
342 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
343 elif c == "%":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
344 # parameter entity reference
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
345 if (j + 1) == n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
346 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
347 return -1
19687
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
348 s, j = self.scan_name(j + 1, declstartpos)
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
349 if j < 0:
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
350 return j
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
351 if rawdata[j] == ";":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
352 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
353 elif c == "]":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
354 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
355 while j < n and rawdata[j] in string.whitespace:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
356 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
357 if j < n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
358 if rawdata[j] == ">":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
359 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
360 self.updatepos(declstartpos, j)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
361 raise HTMLParseError(
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
362 "unexpected char after internal subset",
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
363 self.getpos())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
364 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
365 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
366 elif c in string.whitespace:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
367 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
368 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
369 self.updatepos(declstartpos, j)
19687
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
370 raise HTMLParseError(
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
371 "unexpected char %s in internal subset" % `c`,
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19682
diff changeset
372 self.getpos())
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
373 # end of buffer reached
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
374 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
375
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
376 def parse_doctype_element(self, i, declstartpos):
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
377 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
378 n = len(rawdata)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
379 name, j = self.scan_name(i, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
380 if j == -1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
381 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
382 # style content model; just skip until '>'
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
383 if '>' in rawdata[j:]:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
384 return string.find(rawdata, ">", j) + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
385 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
386
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
387 def parse_doctype_attlist(self, i, declstartpos):
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
388 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
389 name, j = self.scan_name(i, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
390 c = rawdata[j:j+1]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
391 if c == "":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
392 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
393 if c == ">":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
394 return j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
395 while 1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
396 # scan a series of attribute descriptions; simplified:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
397 # name type [value] [#constraint]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
398 name, j = self.scan_name(j, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
399 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
400 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
401 c = rawdata[j:j+1]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
402 if c == "":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
403 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
404 if c == "(":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
405 # an enumerated type; look for ')'
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
406 if ")" in rawdata[j:]:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
407 j = string.find(rawdata, ")", j) + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
408 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
409 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
410 while rawdata[j:j+1] in string.whitespace:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
411 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
412 if not rawdata[j:]:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
413 # end of buffer, incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
414 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
415 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
416 name, j = self.scan_name(j, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
417 c = rawdata[j:j+1]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
418 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
419 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
420 if c in "'\"":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
421 m = declstringlit.match(rawdata, j)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
422 if m:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
423 j = m.end()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
424 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
425 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
426 c = rawdata[j:j+1]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
427 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
428 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
429 if c == "#":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
430 if rawdata[j:] == "#":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
431 # end of buffer
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
432 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
433 name, j = self.scan_name(j + 1, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
434 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
435 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
436 c = rawdata[j:j+1]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
437 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
438 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
439 if c == '>':
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
440 # all done
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
441 return j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
442
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
443 def parse_doctype_notation(self, i, declstartpos):
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
444 name, j = self.scan_name(i, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
445 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
446 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
447 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
448 while 1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
449 c = rawdata[j:j+1]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
450 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
451 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
452 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
453 if c == '>':
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
454 return j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
455 if c in "'\"":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
456 m = declstringlit.match(rawdata, j)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
457 if not m:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
458 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
459 j = m.end()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
460 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
461 name, j = self.scan_name(j, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
462 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
463 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
464
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
465 def parse_doctype_entity(self, i, declstartpos):
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
466 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
467 if rawdata[i:i+1] == "%":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
468 j = i + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
469 while 1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
470 c = rawdata[j:j+1]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
471 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
472 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
473 if c in string.whitespace:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
474 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
475 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
476 break
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
477 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
478 j = i
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
479 name, j = self.scan_name(j, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
480 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
481 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
482 while 1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
483 c = self.rawdata[j:j+1]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
484 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
485 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
486 if c in "'\"":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
487 m = declstringlit.match(rawdata, j)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
488 if m:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
489 j = m.end()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
490 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
491 return -1 # incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
492 elif c == ">":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
493 return j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
494 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
495 name, j = self.scan_name(j, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
496 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
497 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
498
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
499 def scan_name(self, i, declstartpos):
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
500 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
501 n = len(rawdata)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
502 if i == n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
503 return None, -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
504 m = declname.match(rawdata, i)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
505 if m:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
506 s = m.group()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
507 name = s.strip()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
508 if (i + len(s)) == n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
509 return None, -1 # end of buffer
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
510 return name.lower(), m.end()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
511 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
512 self.updatepos(declstartpos, i)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
513 raise HTMLParseError("expected name token", self.getpos())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
514
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 # Internal -- parse processing instr, return end or -1 if not terminated
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 def parse_pi(self, i):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 rawdata = self.rawdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 assert rawdata[i:i+2] == '<?', 'unexpected call to parse_pi()'
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 match = piclose.search(rawdata, i+2) # >
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 if not match:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 j = match.start()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 self.handle_pi(rawdata[i+2: j])
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 j = match.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 return j
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
526
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 # Internal -- handle starttag, return end or -1 if not terminated
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 def parse_starttag(self, i):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 self.__starttag_text = None
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 endpos = self.check_for_whole_start_tag(i)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 if endpos < 0:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 return endpos
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 rawdata = self.rawdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 self.__starttag_text = rawdata[i:endpos]
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
535
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 # Now parse the data between i+1 and j into a tag and attrs
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 attrs = []
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 match = tagfind.match(rawdata, i+1)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 assert match, 'unexpected call to parse_starttag()'
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 k = match.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 self.lasttag = tag = string.lower(rawdata[i+1:k])
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
542
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 while k < endpos:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 m = attrfind.match(rawdata, k)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 if not m:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 break
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 attrname, rest, attrvalue = m.group(1, 2, 3)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 if not rest:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 attrvalue = None
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 attrvalue[:1] == '"' == attrvalue[-1:]:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 attrvalue = attrvalue[1:-1]
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 attrvalue = self.unescape(attrvalue)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 attrs.append((string.lower(attrname), attrvalue))
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 k = m.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
556
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 end = string.strip(rawdata[k:endpos])
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 if end not in (">", "/>"):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 lineno, offset = self.getpos()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 if "\n" in self.__starttag_text:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 lineno = lineno + string.count(self.__starttag_text, "\n")
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 offset = len(self.__starttag_text) \
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 - string.rfind(self.__starttag_text, "\n")
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 offset = offset + len(self.__starttag_text)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 raise HTMLParseError("junk characters in start tag: %s"
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 % `rawdata[k:endpos][:20]`,
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 (lineno, offset))
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 if end[-2:] == '/>':
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 # XHTML-style empty tag: <span attr="value" />
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 self.handle_startendtag(tag, attrs)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 self.handle_starttag(tag, attrs)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 if tag in self.CDATA_CONTENT_ELEMENTS:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 self.set_cdata_mode()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 return endpos
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
577
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 # Internal -- check to see if we have a complete starttag; return end
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 # or -1 if incomplete.
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 def check_for_whole_start_tag(self, i):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 rawdata = self.rawdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 m = locatestarttagend.match(rawdata, i)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 if m:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 j = m.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 next = rawdata[j:j+1]
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 if next == ">":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 return j + 1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 if next == "/":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 s = rawdata[j:j+2]
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 if s == "/>":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 return j + 2
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 if s == "/":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 # buffer boundary
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 # else bogus input
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 self.updatepos(i, j + 1)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 raise HTMLParseError("malformed empty start tag",
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 self.getpos())
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 if next == "":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 # end of input
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 if next in ("abcdefghijklmnopqrstuvwxyz=/"
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 # end of input in or before attribute value, or we have the
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 # '/' from a '/>' ending
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 self.updatepos(i, j)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 raise HTMLParseError("malformed start tag", self.getpos())
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 raise AssertionError("we should not gt here!")
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
610
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 # Internal -- parse endtag, return end or -1 if incomplete
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 def parse_endtag(self, i):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 rawdata = self.rawdata
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 assert rawdata[i:i+2] == "</", "unexpected call to parse_endtag"
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 match = endendtag.search(rawdata, i+1) # >
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 if not match:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 j = match.end()
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 match = endtagfind.match(rawdata, i) # </ + tag + >
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 if not match:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 raise HTMLParseError("bad end tag: %s" % `rawdata[i:j]`,
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 self.getpos())
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 tag = match.group(1)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 self.handle_endtag(string.lower(tag))
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 return j
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
626
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 # Overridable -- finish processing of start+end tag: <tag.../>
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 def handle_startendtag(self, tag, attrs):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 self.handle_starttag(tag, attrs)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 self.handle_endtag(tag)
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
631
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 # Overridable -- handle start tag
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 def handle_starttag(self, tag, attrs):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
635
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 # Overridable -- handle end tag
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 def handle_endtag(self, tag):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
639
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 # Overridable -- handle character reference
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 def handle_charref(self, name):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
643
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 # Overridable -- handle entity reference
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 def handle_entityref(self, name):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
647
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 # Overridable -- handle data
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 def handle_data(self, data):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
651
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 # Overridable -- handle comment
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 def handle_comment(self, data):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
655
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 # Overridable -- handle declaration
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 def handle_decl(self, decl):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
659
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 # Overridable -- handle processing instruction
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 def handle_pi(self, data):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
663
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 # Internal -- helper to remove special character quoting
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 def unescape(self, s):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 if '&' not in s:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 return s
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 s = string.replace(s, "&lt;", "<")
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 s = string.replace(s, "&gt;", ">")
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 s = string.replace(s, "&apos;", "'")
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 s = string.replace(s, "&quot;", '"')
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 s = string.replace(s, "&amp;", "&") # Must be last
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 return s