Commit e403208c authored by Guido van Rossum's avatar Guido van Rossum

Change in HTMLParser API: attributes without a value (e.g. <img

ismap>) pass None for the value instead of the attribute name.  This
is then used to generate the same output, rather than <img
ismap="ismap">).  Includes tests.
parent 3fde826a
......@@ -275,7 +275,7 @@ class HTMLParser:
break
attrname, rest, attrvalue = m.group(1, 2, 3)
if not rest:
attrvalue = attrname
attrvalue = None
elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
attrvalue[:1] == '"' == attrvalue[-1:]:
attrvalue = attrvalue[1:-1]
......@@ -315,7 +315,7 @@ class HTMLParser:
raise HTMLParseError("bad end tag: %s" % `rawdata[i:j]`,
self.getpos())
tag = match.group(1)
self.handle_endtag(tag)
self.handle_endtag(string.lower(tag))
return j
# Overridable -- finish processing of start+end tag: <tag.../>
......
......@@ -152,7 +152,10 @@ class TALGenerator:
for item in attrlist:
if len(item) > 2:
return 0
new.append(" %s=%s" % (item[0], quote(item[1])))
if item[1] is None:
new.append(" %s" % item[0])
else:
new.append(" %s=%s" % (item[0], quote(item[1])))
new.append(end)
collect.extend(new)
return 1
......
......@@ -209,6 +209,8 @@ class TALInterpreter:
if (self.html and not value and
string.lower(name) in BOOLEAN_HTML_ATTRS):
s = name
elif value is None:
s = name
else:
s = "%s=%s" % (name, quote(value))
if (self.wrap and
......
......@@ -10,7 +10,7 @@
<ol>
<li>second list, first item</li>
<li>second list, second item
<dl compact="compact">
<dl compact>
<dt>term 1</dt>
<dt>term 2</dt>
<dd>definition</dd>
......
......@@ -99,14 +99,14 @@ class HTMLParserTestCase(unittest.TestCase):
def check_simple_html(self):
self._run_check("""
<!DOCTYPE html PUBLIC 'foo'>
<html>&entity;&#32;
<HTML>&entity;&#32;
<!--comment1a
-></foo><bar>&lt;<?pi?></foo<bar
comment1b-->
<img src='bar' ismap>sample
<Img sRc='Bar' isMAP>sample
text
<!--comment2a-- --comment2b-->
</html>
</Html>
""", [
("data", "\n"),
("decl", "DOCTYPE html PUBLIC 'foo'"),
......@@ -117,7 +117,7 @@ text
("data", "\n"),
("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
("data", "\n"),
("starttag", "img", [("src", "bar"), ("ismap", "ismap")]),
("starttag", "img", [("src", "Bar"), ("ismap", None)]),
("data", "sample\ntext\n"),
("comment", "comment2a-- --comment2b"),
("data", "\n"),
......@@ -135,8 +135,8 @@ text
def check_attr_syntax(self):
output = [
("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", "e")])
]
("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
]
self._run_check("""<a b='v' c="v" d=v e>""", output)
self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
......
......@@ -114,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase):
def check_code_attr_syntax(self):
output = [
('setPosition', (1, 0)),
('rawtext', '<a b="v" c="v" d="v" e="e"></a>'),
('rawtext', '<a b="v" c="v" d="v" e></a>'),
]
self._run_check("""<a b='v' c="v" d=v e>""", output)
self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
......
......@@ -10,7 +10,7 @@
<ol>
<li>second list, first item</li>
<li>second list, second item
<dl compact="compact">
<dl compact>
<dt>term 1</dt>
<dt>term 2</dt>
<dd>definition</dd>
......
......@@ -99,14 +99,14 @@ class HTMLParserTestCase(unittest.TestCase):
def check_simple_html(self):
self._run_check("""
<!DOCTYPE html PUBLIC 'foo'>
<html>&entity;&#32;
<HTML>&entity;&#32;
<!--comment1a
-></foo><bar>&lt;<?pi?></foo<bar
comment1b-->
<img src='bar' ismap>sample
<Img sRc='Bar' isMAP>sample
text
<!--comment2a-- --comment2b-->
</html>
</Html>
""", [
("data", "\n"),
("decl", "DOCTYPE html PUBLIC 'foo'"),
......@@ -117,7 +117,7 @@ text
("data", "\n"),
("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
("data", "\n"),
("starttag", "img", [("src", "bar"), ("ismap", "ismap")]),
("starttag", "img", [("src", "Bar"), ("ismap", None)]),
("data", "sample\ntext\n"),
("comment", "comment2a-- --comment2b"),
("data", "\n"),
......@@ -135,8 +135,8 @@ text
def check_attr_syntax(self):
output = [
("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", "e")])
]
("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
]
self._run_check("""<a b='v' c="v" d=v e>""", output)
self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
......
......@@ -114,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase):
def check_code_attr_syntax(self):
output = [
('setPosition', (1, 0)),
('rawtext', '<a b="v" c="v" d="v" e="e"></a>'),
('rawtext', '<a b="v" c="v" d="v" e></a>'),
]
self._run_check("""<a b='v' c="v" d=v e>""", output)
self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment