annotate Lib/HTMLParser.py @ 19682:8dfb13a4e175 legacy-trunk

Added reasonable parsing of the DOCTYPE declaration, fixed edge cases regarding bare ampersands in content.
author Fred Drake <fdrake@acm.org>
date Tue, 04 Sep 2001 15:10:16 +0000
parents c403af003884
children 3731ad1d9033
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)
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
272 decltype = None
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
273 extrachars = ""
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 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
275 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
276 if c == ">":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 # 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
278 data = rawdata[i+2:j]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
279 if decltype == "doctype":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
280 self.handle_decl(data)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
281 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
282 self.unknown_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
283 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
284 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
285 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
286 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
287 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
288 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
289 elif c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 m = declname.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
291 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
292 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
293 j = m.end()
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
294 if decltype is None:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
295 decltype = m.group(0).rstrip().lower()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
296 if decltype != "doctype":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
297 extrachars = "="
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
298 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
299 j = self.parse_doctype_subset(j + 1, i)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
300 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
301 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
302 elif c in extrachars:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
303 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
304 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
305 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
306 if j == n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
307 # end of buffer while in declaration
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
308 return -1
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 raise HTMLParseError(
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 "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
312 self.getpos())
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
313 decltype = decltype or ''
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 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
315
19682
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
316 # 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
317 # 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
318 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
319 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
320 n = len(rawdata)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
321 j = i
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
322 while j < n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
323 c = rawdata[j]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
324 if c == "<":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
325 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
326 if s == "<":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
327 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
328 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
329 if s != "<!":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
330 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
331 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
332 self.getpos())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
333 if (j + 2) == n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
334 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
335 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
336 if (j + 4) > n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
337 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
338 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
339 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
340 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
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 continue
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
344 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
345 if j == -1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
346 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
347 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
348 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
349 raise HTMLParseError(
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
350 "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
351 self.getpos())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
352 # handle the individual names
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
353 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
354 j = meth(j, declstartpos)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
355 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
356 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
357 elif c == "%":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
358 # parameter entity reference
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
359 if (j + 1) == n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
360 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
361 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
362 m = declname.match(rawdata, j + 1)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
363 s = m.group()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
364 if s == rawdata[j+1:]:
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 j = j + 1 + len(s.rstrip())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
367 if rawdata[j] == ";":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
368 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
369 elif c == "]":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
370 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
371 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
372 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
373 if j < n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
374 if rawdata[j] == ">":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
375 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
376 self.updatepos(declstartpos, j)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
377 raise HTMLParseError(
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
378 "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
379 self.getpos())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
380 else:
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 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
383 j = j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
384 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
385 self.updatepos(declstartpos, j)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
386 raise HTMLParseError("unexpected char in internal subset",
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
387 self.getpos())
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
388 # end of buffer reached
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
389 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
390
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
391 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
392 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
393 n = len(rawdata)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
394 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
395 if j == -1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
396 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
397 # 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
398 if '>' in rawdata[j:]:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
399 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
400 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
401
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
402 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
403 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
404 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
405 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
406 if c == "":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
407 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
408 if c == ">":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
409 return j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
410 while 1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
411 # 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
412 # name type [value] [#constraint]
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
413 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
414 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
415 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
416 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
417 if c == "":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
418 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
419 if c == "(":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
420 # 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
421 if ")" in rawdata[j:]:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
422 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
423 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
424 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
425 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
426 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 rawdata[j:]:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
428 # end of buffer, incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
429 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
430 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
431 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
432 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
433 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
434 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
435 if c in "'\"":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
436 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
437 if m:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
438 j = m.end()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
439 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
440 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
441 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
442 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
443 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
444 if c == "#":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
445 if rawdata[j:] == "#":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
446 # end of buffer
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
447 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
448 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
449 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
450 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
451 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
452 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
453 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
454 if c == '>':
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
455 # all done
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
456 return j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
457
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
458 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
459 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
460 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
461 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
462 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
463 while 1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
464 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
465 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
466 # end of buffer; incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
467 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
468 if c == '>':
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
469 return j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
470 if c in "'\"":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
471 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
472 if not m:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
473 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
474 j = m.end()
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 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
477 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
478 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
479
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
480 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
481 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
482 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
483 j = i + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
484 while 1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
485 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
486 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
487 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
488 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
489 j = j + 1
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 break
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
492 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
493 j = i
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
494 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
495 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
496 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
497 while 1:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
498 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
499 if not c:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
500 return -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
501 if c in "'\"":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
502 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
503 if m:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
504 j = m.end()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
505 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
506 return -1 # incomplete
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
507 elif c == ">":
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
508 return j + 1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
509 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
510 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
511 if j < 0:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
512 return j
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
513
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
514 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
515 rawdata = self.rawdata
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
516 n = len(rawdata)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
517 if i == n:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
518 return None, -1
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
519 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
520 if m:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
521 s = m.group()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
522 name = s.strip()
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
523 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
524 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
525 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
526 else:
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
527 self.updatepos(declstartpos, i)
8dfb13a4e175 Added reasonable parsing of the DOCTYPE declaration, fixed edge cases
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
528 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
529
17853
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 # 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
531 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
532 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
533 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
534 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
535 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
536 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 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
538 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
539 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
540 return j
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
541
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 # 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
543 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
544 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
545 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
546 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
547 return endpos
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 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
549 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
550
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 # 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
552 attrs = []
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 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
554 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
555 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
556 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
557
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 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
559 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
560 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
561 break
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 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
563 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
564 attrvalue = None
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 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
566 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
567 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
568 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
569 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
570 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
571
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 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
573 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
574 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
575 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
576 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
577 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
578 - 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
579 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 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
581 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
582 % `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
583 (lineno, offset))
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 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
585 # 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
586 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
587 else:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 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
589 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
590 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
591 return endpos
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
592
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 # 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
594 # 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
595 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
596 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
597 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
598 if m:
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 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
600 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
601 if next == ">":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 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
603 if next == "/":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 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
605 if s == "/>":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 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
607 if s == "/":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 # buffer boundary
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 # 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
611 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
612 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
613 self.getpos())
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 if next == "":
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 # 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
616 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 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
618 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 # 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
620 # '/' 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
621 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 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
623 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
624 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
625
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 # 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
627 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
628 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
629 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
630 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
631 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
632 return -1
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 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
634 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
635 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
636 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
637 self.getpos())
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 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
639 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
640 return j
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
641
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 # 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
643 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
644 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
645 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
646
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 # 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
648 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
649 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
650
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 # 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
652 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
653 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
654
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 # 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
656 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
657 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
658
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 # 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
660 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
661 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
662
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 # 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
664 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
665 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
666
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 # 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
668 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
669 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
670
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 # 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
672 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
673 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
674
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 # 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
676 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
677 pass
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
678
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 # 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
680 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
681 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
682 return s
055fc046e080 A much improved HTML parser -- a replacement for sgmllib. The API is
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 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
684 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
685 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
686 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
687 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
688 return s