annotate Lib/_markupbase.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents 8ef68ebec0e1
children 4ddbb756b602
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35386
38824b6e3600 add note about "markupbase" not being intended for direct use
Fred Drake <fdrake@acm.org>
parents: 32603
diff changeset
1 """Shared support for scanning document type declarations in HTML and XHTML.
38824b6e3600 add note about "markupbase" not being intended for direct use
Fred Drake <fdrake@acm.org>
parents: 32603
diff changeset
2
47434
f150177ff1ff Remove the htmllib and sgmllib modules as per PEP 3108.
Georg Brandl <georg@python.org>
parents: 44212
diff changeset
3 This module is used as a foundation for the html.parser module. It has no
f150177ff1ff Remove the htmllib and sgmllib modules as per PEP 3108.
Georg Brandl <georg@python.org>
parents: 44212
diff changeset
4 documented public API and should not be used directly.
35386
38824b6e3600 add note about "markupbase" not being intended for direct use
Fred Drake <fdrake@acm.org>
parents: 32603
diff changeset
5
38824b6e3600 add note about "markupbase" not being intended for direct use
Fred Drake <fdrake@acm.org>
parents: 32603
diff changeset
6 """
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8 import re
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 _declname_match = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11 _declstringlit_match = re.compile(r'(\'[^\']*\'|"[^"]*")\s*').match
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
12 _commentclose = re.compile(r'--\s*>')
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
13 _markedsectionclose = re.compile(r']\s*]\s*>')
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
14
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
15 # An analysis of the MS-Word extensions is available at
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
16 # http://www.planetpublish.com/xmlarena/xap/Thursday/WordtoXML.pdf
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
17
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
18 _msmarkedsectionclose = re.compile(r']\s*>')
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20 del re
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 class ParserBase:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 """Parser base class which provides some common support methods used
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 by the SGML/HTML and XHTML parsers."""
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26
20699
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
27 def __init__(self):
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
28 if self.__class__ is ParserBase:
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
29 raise RuntimeError(
44212
c6fb393b6c81 rename markupbase to _markupbase
Fred Drake <fdrake@acm.org>
parents: 38673
diff changeset
30 "_markupbase.ParserBase must be subclassed")
20699
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
31
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
32 def error(self, message):
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
33 raise NotImplementedError(
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
34 "subclasses of ParserBase must override error()")
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
35
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 def reset(self):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 self.lineno = 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 self.offset = 0
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 def getpos(self):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 """Return current line number and offset."""
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 return self.lineno, self.offset
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 # Internal -- update line number and offset. This should be
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 # called for each piece of data exactly once, in order -- in other
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 # words the concatenation of all the input strings to this
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 # function should be exactly the entire input.
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 def updatepos(self, i, j):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 if i >= j:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 rawdata = self.rawdata
23590
a4bb18212b48 Use string methods where possible, and remove import string
Neal Norwitz <nnorwitz@gmail.com>
parents: 20699
diff changeset
52 nlines = rawdata.count("\n", i, j)
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 if nlines:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 self.lineno = self.lineno + nlines
23590
a4bb18212b48 Use string methods where possible, and remove import string
Neal Norwitz <nnorwitz@gmail.com>
parents: 20699
diff changeset
55 pos = rawdata.rindex("\n", i, j) # Should not fail
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 self.offset = j-(pos+1)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 self.offset = self.offset + j-i
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 _decl_otherchars = ''
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 # Internal -- parse declaration (for use by subclasses).
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 def parse_declaration(self, i):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 # This is some sort of declaration; in "HTML as
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 # deployed," this should only be the document type
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 # declaration ("<!DOCTYPE html...>").
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28446
diff changeset
68 # ISO 8879:1986, however, has more complex
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
69 # declaration syntax for elements in <!...>, including:
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
70 # --comment--
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
71 # [marked section]
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28446
diff changeset
72 # name in the following list: ENTITY, DOCTYPE, ELEMENT,
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28446
diff changeset
73 # ATTLIST, NOTATION, SHORTREF, USEMAP,
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
74 # LINKTYPE, LINK, IDLINK, USELINK, SYSTEM
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 rawdata = self.rawdata
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 j = i + 2
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 assert rawdata[i:j] == "<!", "unexpected call to parse_declaration"
36914
f1eaae7f962d Bug #1442874: handle "<!>", the empty SGML comment
Georg Brandl <georg@python.org>
parents: 35386
diff changeset
78 if rawdata[j:j+1] == ">":
f1eaae7f962d Bug #1442874: handle "<!>", the empty SGML comment
Georg Brandl <georg@python.org>
parents: 35386
diff changeset
79 # the empty comment <!>
f1eaae7f962d Bug #1442874: handle "<!>", the empty SGML comment
Georg Brandl <georg@python.org>
parents: 35386
diff changeset
80 return j + 1
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 if rawdata[j:j+1] in ("-", ""):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 # Start of comment followed by buffer boundary,
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 # or just a buffer boundary.
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84 return -1
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
85 # A simple, practical version could look like: ((name|stringlit) S*) + '>'
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 n = len(rawdata)
36914
f1eaae7f962d Bug #1442874: handle "<!>", the empty SGML comment
Georg Brandl <georg@python.org>
parents: 35386
diff changeset
87 if rawdata[j:j+2] == '--': #comment
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
88 # Locate --.*-- as the body of the comment
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
89 return self.parse_comment(i)
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
90 elif rawdata[j] == '[': #marked section
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
91 # Locate [statusWord [...arbitrary SGML...]] as the body of the marked section
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
92 # Where statusWord is one of TEMP, CDATA, IGNORE, INCLUDE, RCDATA
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
93 # Note that this is extended by Microsoft Office "Save as Web" function
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
94 # to include [if...] and [endif].
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
95 return self.parse_marked_section(i)
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
96 else: #all other declaration elements
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
97 decltype, j = self._scan_name(j, i)
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
99 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100 if decltype == "doctype":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101 self._decl_otherchars = ''
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 while j < n:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 c = rawdata[j]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 if c == ">":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 # end of declaration syntax
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 data = rawdata[i+2:j]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107 if decltype == "doctype":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 self.handle_decl(data)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 self.unknown_decl(data)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111 return j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 if c in "\"'":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 m = _declstringlit_match(rawdata, j)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114 if not m:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 return -1 # incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 j = m.end()
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117 elif c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 name, j = self._scan_name(j, i)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 elif c in self._decl_otherchars:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 j = j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 elif c == "[":
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
122 # this could be handled in a separate doctype parser
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 if decltype == "doctype":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124 j = self._parse_doctype_subset(j + 1, i)
64479
8ef68ebec0e1 Take advantage of the frozenset constant optimization.
Raymond Hettinger <python@rcn.com>
parents: 47434
diff changeset
125 elif decltype in {"attlist", "linktype", "link", "element"}:
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
126 # must tolerate []'d groups in a content model in an element declaration
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
127 # also in data attribute specifications of attlist declaration
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
128 # also link type declaration subsets in linktype declarations
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
129 # also link attribute specification lists in link declarations
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
130 self.error("unsupported '[' char in %s declaration" % decltype)
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
132 self.error("unexpected '[' char in declaration")
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
133 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 self.error(
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28774
diff changeset
135 "unexpected %r char in declaration" % rawdata[j])
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138 return -1 # incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
140 # Internal -- parse a marked section
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
141 # Override this to handle MS-word extension syntax <![if word]>content<![endif]>
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 36914
diff changeset
142 def parse_marked_section(self, i, report=1):
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
143 rawdata= self.rawdata
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
144 assert rawdata[i:i+3] == '<![', "unexpected call to parse_marked_section()"
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
145 sectName, j = self._scan_name( i+3, i )
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
146 if j < 0:
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
147 return j
64479
8ef68ebec0e1 Take advantage of the frozenset constant optimization.
Raymond Hettinger <python@rcn.com>
parents: 47434
diff changeset
148 if sectName in {"temp", "cdata", "ignore", "include", "rcdata"}:
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
149 # look for standard ]]> ending
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
150 match= _markedsectionclose.search(rawdata, i+3)
64479
8ef68ebec0e1 Take advantage of the frozenset constant optimization.
Raymond Hettinger <python@rcn.com>
parents: 47434
diff changeset
151 elif sectName in {"if", "else", "endif"}:
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
152 # look for MS Office ]> ending
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
153 match= _msmarkedsectionclose.search(rawdata, i+3)
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
154 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28774
diff changeset
155 self.error('unknown status keyword %r in marked section' % rawdata[i+3:j])
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
156 if not match:
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
157 return -1
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
158 if report:
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
159 j = match.start(0)
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
160 self.unknown_decl(rawdata[i+3: j])
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
161 return match.end(0)
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28446
diff changeset
162
28446
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
163 # Internal -- parse comment, return length or -1 if not terminated
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
164 def parse_comment(self, i, report=1):
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
165 rawdata = self.rawdata
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
166 if rawdata[i:i+4] != '<!--':
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
167 self.error('unexpected call to parse_comment()')
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
168 match = _commentclose.search(rawdata, i+4)
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
169 if not match:
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
170 return -1
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
171 if report:
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
172 j = match.start(0)
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
173 self.handle_comment(rawdata[i+4: j])
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
174 return match.end(0)
500f545a24f0 Patch #545300: Support marked sections.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
175
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
176 # Internal -- scan past the internal subset in a <!DOCTYPE declaration,
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 # returning the index just past any whitespace following the trailing ']'.
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
178 def _parse_doctype_subset(self, i, declstartpos):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179 rawdata = self.rawdata
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
180 n = len(rawdata)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181 j = i
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
182 while j < n:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183 c = rawdata[j]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184 if c == "<":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 s = rawdata[j:j+2]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 if s == "<":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 # end of buffer; incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
189 if s != "<!":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
190 self.updatepos(declstartpos, j + 1)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28774
diff changeset
191 self.error("unexpected char in internal subset (in %r)" % s)
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
192 if (j + 2) == n:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
193 # end of buffer; incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
194 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
195 if (j + 4) > n:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196 # end of buffer; incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 if rawdata[j:j+4] == "<!--":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
199 j = self.parse_comment(j, report=0)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
200 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
201 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
202 continue
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
203 name, j = self._scan_name(j + 2, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
204 if j == -1:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
205 return -1
64479
8ef68ebec0e1 Take advantage of the frozenset constant optimization.
Raymond Hettinger <python@rcn.com>
parents: 47434
diff changeset
206 if name not in {"attlist", "element", "entity", "notation"}:
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
207 self.updatepos(declstartpos, j + 2)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
208 self.error(
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28774
diff changeset
209 "unknown declaration %r in internal subset" % name)
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
210 # handle the individual names
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
211 meth = getattr(self, "_parse_doctype_" + name)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
212 j = meth(j, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
213 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
214 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
215 elif c == "%":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
216 # parameter entity reference
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
217 if (j + 1) == n:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
218 # end of buffer; incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
219 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
220 s, j = self._scan_name(j + 1, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
221 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
222 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
223 if rawdata[j] == ";":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
224 j = j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
225 elif c == "]":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
226 j = j + 1
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23590
diff changeset
227 while j < n and rawdata[j].isspace():
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
228 j = j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
229 if j < n:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
230 if rawdata[j] == ">":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
231 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
232 self.updatepos(declstartpos, j)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
233 self.error("unexpected char after internal subset")
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
234 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
235 return -1
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23590
diff changeset
236 elif c.isspace():
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
237 j = j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
238 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
239 self.updatepos(declstartpos, j)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28774
diff changeset
240 self.error("unexpected char %r in internal subset" % c)
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
241 # end of buffer reached
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
242 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
243
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
244 # Internal -- scan past <!ELEMENT declarations
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
245 def _parse_doctype_element(self, i, declstartpos):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
246 name, j = self._scan_name(i, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
247 if j == -1:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
248 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
249 # style content model; just skip until '>'
20699
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
250 rawdata = self.rawdata
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
251 if '>' in rawdata[j:]:
23590
a4bb18212b48 Use string methods where possible, and remove import string
Neal Norwitz <nnorwitz@gmail.com>
parents: 20699
diff changeset
252 return rawdata.find(">", j) + 1
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
253 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
254
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
255 # Internal -- scan past <!ATTLIST declarations
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
256 def _parse_doctype_attlist(self, i, declstartpos):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
257 rawdata = self.rawdata
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
258 name, j = self._scan_name(i, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
259 c = rawdata[j:j+1]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
260 if c == "":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
261 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
262 if c == ">":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 return j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
264 while 1:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
265 # scan a series of attribute descriptions; simplified:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266 # name type [value] [#constraint]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
267 name, j = self._scan_name(j, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
268 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
269 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
270 c = rawdata[j:j+1]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
271 if c == "":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
272 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
273 if c == "(":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
274 # an enumerated type; look for ')'
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
275 if ")" in rawdata[j:]:
23590
a4bb18212b48 Use string methods where possible, and remove import string
Neal Norwitz <nnorwitz@gmail.com>
parents: 20699
diff changeset
276 j = rawdata.find(")", j) + 1
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
277 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
278 return -1
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23590
diff changeset
279 while rawdata[j:j+1].isspace():
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
280 j = j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
281 if not rawdata[j:]:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
282 # end of buffer, incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
283 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
284 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
285 name, j = self._scan_name(j, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
286 c = rawdata[j:j+1]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
287 if not c:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
288 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
289 if c in "'\"":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
290 m = _declstringlit_match(rawdata, j)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
291 if m:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
292 j = m.end()
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
293 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
294 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
295 c = rawdata[j:j+1]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
296 if not c:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
297 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
298 if c == "#":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
299 if rawdata[j:] == "#":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
300 # end of buffer
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
301 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
302 name, j = self._scan_name(j + 1, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
303 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
304 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
305 c = rawdata[j:j+1]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
306 if not c:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
307 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
308 if c == '>':
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
309 # all done
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
310 return j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
311
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
312 # Internal -- scan past <!NOTATION declarations
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
313 def _parse_doctype_notation(self, i, declstartpos):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
314 name, j = self._scan_name(i, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
315 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
316 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
317 rawdata = self.rawdata
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
318 while 1:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
319 c = rawdata[j:j+1]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
320 if not c:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
321 # end of buffer; incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
322 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
323 if c == '>':
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
324 return j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
325 if c in "'\"":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
326 m = _declstringlit_match(rawdata, j)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
327 if not m:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
328 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
329 j = m.end()
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
330 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
331 name, j = self._scan_name(j, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
332 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
333 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
334
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
335 # Internal -- scan past <!ENTITY declarations
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
336 def _parse_doctype_entity(self, i, declstartpos):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
337 rawdata = self.rawdata
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
338 if rawdata[i:i+1] == "%":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
339 j = i + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
340 while 1:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
341 c = rawdata[j:j+1]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
342 if not c:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
343 return -1
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23590
diff changeset
344 if c.isspace():
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
345 j = j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
346 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
347 break
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
348 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
349 j = i
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
350 name, j = self._scan_name(j, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
351 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
352 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
353 while 1:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
354 c = self.rawdata[j:j+1]
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
355 if not c:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
356 return -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
357 if c in "'\"":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
358 m = _declstringlit_match(rawdata, j)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
359 if m:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
360 j = m.end()
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
361 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
362 return -1 # incomplete
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
363 elif c == ">":
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
364 return j + 1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
365 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
366 name, j = self._scan_name(j, declstartpos)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
367 if j < 0:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
368 return j
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
369
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
370 # Internal -- scan a name token and the new position and the token, or
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
371 # return -1 if we've reached the end of the buffer.
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
372 def _scan_name(self, i, declstartpos):
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
373 rawdata = self.rawdata
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
374 n = len(rawdata)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
375 if i == n:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
376 return None, -1
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
377 m = _declname_match(rawdata, i)
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
378 if m:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
379 s = m.group()
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
380 name = s.strip()
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
381 if (i + len(s)) == n:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
382 return None, -1 # end of buffer
23590
a4bb18212b48 Use string methods where possible, and remove import string
Neal Norwitz <nnorwitz@gmail.com>
parents: 20699
diff changeset
383 return name.lower(), m.end()
20065
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
384 else:
f84691b11402 New base class for the SGMLParser and HTMLParser classes from the sgmllib
Fred Drake <fdrake@acm.org>
parents:
diff changeset
385 self.updatepos(declstartpos, i)
32603
799bcdff2cdc [Patch #965175] Incorporate a suggestion for a better error message
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
386 self.error("expected name token at %r"
799bcdff2cdc [Patch #965175] Incorporate a suggestion for a better error message
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
387 % rawdata[declstartpos:declstartpos+20])
20699
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
388
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
389 # To be overridden -- handlers for unknown objects
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
390 def unknown_decl(self, data):
fa0bf6c79e04 Re-arrange things and remove some unused variables/imports to keep pychecker
Fred Drake <fdrake@acm.org>
parents: 20416
diff changeset
391 pass