1 """Shared support for scanning document type declarations in HTML and XHTML."""
5 _declname_match
= re
.compile(r
'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match
6 _declstringlit_match
= re
.compile(r
'(\'[^
\']*\'|
"[^"]*")\s*').match
12 """Parser base class which provides some common support methods used
13 by the SGML/HTML and XHTML parsers."""
16 if self.__class__ is ParserBase:
18 "markupbase
.ParserBase must be subclassed
")
20 def error(self, message):
21 raise NotImplementedError(
22 "subclasses of ParserBase must override
error()")
29 """Return current line number and offset."""
30 return self.lineno, self.offset
32 # Internal -- update line number and offset. This should be
33 # called for each piece of data exactly once, in order -- in other
34 # words the concatenation of all the input strings to this
35 # function should be exactly the entire input.
36 def updatepos(self, i, j):
39 rawdata = self.rawdata
40 nlines = rawdata.count("\n", i, j)
42 self.lineno = self.lineno + nlines
43 pos = rawdata.rindex("\n", i, j) # Should not fail
44 self.offset = j-(pos+1)
46 self.offset = self.offset + j-i
51 # Internal -- parse declaration (for use by subclasses).
52 def parse_declaration(self, i):
53 # This is some sort of declaration; in "HTML
as
54 # deployed," this should only be the document type
55 # declaration ("<!DOCTYPE html...>").
56 rawdata
= self
.rawdata
58 assert rawdata
[i
:j
] == "<!", "unexpected call to parse_declaration"
59 if rawdata
[j
:j
+1] in ("-", ""):
60 # Start of comment followed by buffer boundary,
61 # or just a buffer boundary.
63 # in practice, this should look like: ((name|stringlit) S*)+ '>'
65 decltype
, j
= self
._scan
_name
(j
, i
)
68 if decltype
== "doctype":
69 self
._decl
_otherchars
= ''
73 # end of declaration syntax
75 if decltype
== "doctype":
76 self
.handle_decl(data
)
78 self
.unknown_decl(data
)
81 m
= _declstringlit_match(rawdata
, j
)
83 return -1 # incomplete
85 elif c
in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
86 name
, j
= self
._scan
_name
(j
, i
)
87 elif c
in self
._decl
_otherchars
:
90 if decltype
== "doctype":
91 j
= self
._parse
_doctype
_subset
(j
+ 1, i
)
93 self
.error("unexpected '[' char in declaration")
96 "unexpected %s char in declaration" % `rawdata
[j
]`
)
99 return -1 # incomplete
101 # Internal -- scan past the internal subset in a <!DOCTYPE declaration,
102 # returning the index just past any whitespace following the trailing ']'.
103 def _parse_doctype_subset(self
, i
, declstartpos
):
104 rawdata
= self
.rawdata
112 # end of buffer; incomplete
115 self
.updatepos(declstartpos
, j
+ 1)
116 self
.error("unexpected char in internal subset (in %s)"
119 # end of buffer; incomplete
122 # end of buffer; incomplete
124 if rawdata
[j
:j
+4] == "<!--":
125 j
= self
.parse_comment(j
, report
=0)
129 name
, j
= self
._scan
_name
(j
+ 2, declstartpos
)
132 if name
not in ("attlist", "element", "entity", "notation"):
133 self
.updatepos(declstartpos
, j
+ 2)
135 "unknown declaration %s in internal subset" % `name`
)
136 # handle the individual names
137 meth
= getattr(self
, "_parse_doctype_" + name
)
138 j
= meth(j
, declstartpos
)
142 # parameter entity reference
144 # end of buffer; incomplete
146 s
, j
= self
._scan
_name
(j
+ 1, declstartpos
)
149 if rawdata
[j
] == ";":
153 while j
< n
and rawdata
[j
].isspace():
156 if rawdata
[j
] == ">":
158 self
.updatepos(declstartpos
, j
)
159 self
.error("unexpected char after internal subset")
165 self
.updatepos(declstartpos
, j
)
166 self
.error("unexpected char %s in internal subset" % `c`
)
167 # end of buffer reached
170 # Internal -- scan past <!ELEMENT declarations
171 def _parse_doctype_element(self
, i
, declstartpos
):
172 name
, j
= self
._scan
_name
(i
, declstartpos
)
175 # style content model; just skip until '>'
176 rawdata
= self
.rawdata
177 if '>' in rawdata
[j
:]:
178 return rawdata
.find(">", j
) + 1
181 # Internal -- scan past <!ATTLIST declarations
182 def _parse_doctype_attlist(self
, i
, declstartpos
):
183 rawdata
= self
.rawdata
184 name
, j
= self
._scan
_name
(i
, declstartpos
)
191 # scan a series of attribute descriptions; simplified:
192 # name type [value] [#constraint]
193 name
, j
= self
._scan
_name
(j
, declstartpos
)
200 # an enumerated type; look for ')'
201 if ")" in rawdata
[j
:]:
202 j
= rawdata
.find(")", j
) + 1
205 while rawdata
[j
:j
+1].isspace():
208 # end of buffer, incomplete
211 name
, j
= self
._scan
_name
(j
, declstartpos
)
216 m
= _declstringlit_match(rawdata
, j
)
225 if rawdata
[j
:] == "#":
228 name
, j
= self
._scan
_name
(j
+ 1, declstartpos
)
238 # Internal -- scan past <!NOTATION declarations
239 def _parse_doctype_notation(self
, i
, declstartpos
):
240 name
, j
= self
._scan
_name
(i
, declstartpos
)
243 rawdata
= self
.rawdata
247 # end of buffer; incomplete
252 m
= _declstringlit_match(rawdata
, j
)
257 name
, j
= self
._scan
_name
(j
, declstartpos
)
261 # Internal -- scan past <!ENTITY declarations
262 def _parse_doctype_entity(self
, i
, declstartpos
):
263 rawdata
= self
.rawdata
264 if rawdata
[i
:i
+1] == "%":
276 name
, j
= self
._scan
_name
(j
, declstartpos
)
280 c
= self
.rawdata
[j
:j
+1]
284 m
= _declstringlit_match(rawdata
, j
)
288 return -1 # incomplete
292 name
, j
= self
._scan
_name
(j
, declstartpos
)
296 # Internal -- scan a name token and the new position and the token, or
297 # return -1 if we've reached the end of the buffer.
298 def _scan_name(self
, i
, declstartpos
):
299 rawdata
= self
.rawdata
303 m
= _declname_match(rawdata
, i
)
307 if (i
+ len(s
)) == n
:
308 return None, -1 # end of buffer
309 return name
.lower(), m
.end()
311 self
.updatepos(declstartpos
, i
)
312 self
.error("expected name token")
314 # To be overridden -- handlers for unknown objects
315 def unknown_decl(self
, data
):