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: ...@@ -275,7 +275,7 @@ class HTMLParser:
break break
attrname, rest, attrvalue = m.group(1, 2, 3) attrname, rest, attrvalue = m.group(1, 2, 3)
if not rest: if not rest:
attrvalue = attrname attrvalue = None
elif attrvalue[:1] == '\'' == attrvalue[-1:] or \ elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
attrvalue[:1] == '"' == attrvalue[-1:]: attrvalue[:1] == '"' == attrvalue[-1:]:
attrvalue = attrvalue[1:-1] attrvalue = attrvalue[1:-1]
...@@ -315,7 +315,7 @@ class HTMLParser: ...@@ -315,7 +315,7 @@ class HTMLParser:
raise HTMLParseError("bad end tag: %s" % `rawdata[i:j]`, raise HTMLParseError("bad end tag: %s" % `rawdata[i:j]`,
self.getpos()) self.getpos())
tag = match.group(1) tag = match.group(1)
self.handle_endtag(tag) self.handle_endtag(string.lower(tag))
return j return j
# Overridable -- finish processing of start+end tag: <tag.../> # Overridable -- finish processing of start+end tag: <tag.../>
......
...@@ -152,6 +152,9 @@ class TALGenerator: ...@@ -152,6 +152,9 @@ class TALGenerator:
for item in attrlist: for item in attrlist:
if len(item) > 2: if len(item) > 2:
return 0 return 0
if item[1] is None:
new.append(" %s" % item[0])
else:
new.append(" %s=%s" % (item[0], quote(item[1]))) new.append(" %s=%s" % (item[0], quote(item[1])))
new.append(end) new.append(end)
collect.extend(new) collect.extend(new)
......
...@@ -209,6 +209,8 @@ class TALInterpreter: ...@@ -209,6 +209,8 @@ class TALInterpreter:
if (self.html and not value and if (self.html and not value and
string.lower(name) in BOOLEAN_HTML_ATTRS): string.lower(name) in BOOLEAN_HTML_ATTRS):
s = name s = name
elif value is None:
s = name
else: else:
s = "%s=%s" % (name, quote(value)) s = "%s=%s" % (name, quote(value))
if (self.wrap and if (self.wrap and
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<ol> <ol>
<li>second list, first item</li> <li>second list, first item</li>
<li>second list, second item <li>second list, second item
<dl compact="compact"> <dl compact>
<dt>term 1</dt> <dt>term 1</dt>
<dt>term 2</dt> <dt>term 2</dt>
<dd>definition</dd> <dd>definition</dd>
......
...@@ -99,14 +99,14 @@ class HTMLParserTestCase(unittest.TestCase): ...@@ -99,14 +99,14 @@ class HTMLParserTestCase(unittest.TestCase):
def check_simple_html(self): def check_simple_html(self):
self._run_check(""" self._run_check("""
<!DOCTYPE html PUBLIC 'foo'> <!DOCTYPE html PUBLIC 'foo'>
<html>&entity;&#32; <HTML>&entity;&#32;
<!--comment1a <!--comment1a
-></foo><bar>&lt;<?pi?></foo<bar -></foo><bar>&lt;<?pi?></foo<bar
comment1b--> comment1b-->
<img src='bar' ismap>sample <Img sRc='Bar' isMAP>sample
text text
<!--comment2a-- --comment2b--> <!--comment2a-- --comment2b-->
</html> </Html>
""", [ """, [
("data", "\n"), ("data", "\n"),
("decl", "DOCTYPE html PUBLIC 'foo'"), ("decl", "DOCTYPE html PUBLIC 'foo'"),
...@@ -117,7 +117,7 @@ text ...@@ -117,7 +117,7 @@ text
("data", "\n"), ("data", "\n"),
("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"), ("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
("data", "\n"), ("data", "\n"),
("starttag", "img", [("src", "bar"), ("ismap", "ismap")]), ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
("data", "sample\ntext\n"), ("data", "sample\ntext\n"),
("comment", "comment2a-- --comment2b"), ("comment", "comment2a-- --comment2b"),
("data", "\n"), ("data", "\n"),
...@@ -135,7 +135,7 @@ text ...@@ -135,7 +135,7 @@ text
def check_attr_syntax(self): def check_attr_syntax(self):
output = [ 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 b = 'v' c = "v" d = v e>""", output) self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
......
...@@ -114,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -114,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase):
def check_code_attr_syntax(self): def check_code_attr_syntax(self):
output = [ output = [
('setPosition', (1, 0)), ('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)
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 @@ ...@@ -10,7 +10,7 @@
<ol> <ol>
<li>second list, first item</li> <li>second list, first item</li>
<li>second list, second item <li>second list, second item
<dl compact="compact"> <dl compact>
<dt>term 1</dt> <dt>term 1</dt>
<dt>term 2</dt> <dt>term 2</dt>
<dd>definition</dd> <dd>definition</dd>
......
...@@ -99,14 +99,14 @@ class HTMLParserTestCase(unittest.TestCase): ...@@ -99,14 +99,14 @@ class HTMLParserTestCase(unittest.TestCase):
def check_simple_html(self): def check_simple_html(self):
self._run_check(""" self._run_check("""
<!DOCTYPE html PUBLIC 'foo'> <!DOCTYPE html PUBLIC 'foo'>
<html>&entity;&#32; <HTML>&entity;&#32;
<!--comment1a <!--comment1a
-></foo><bar>&lt;<?pi?></foo<bar -></foo><bar>&lt;<?pi?></foo<bar
comment1b--> comment1b-->
<img src='bar' ismap>sample <Img sRc='Bar' isMAP>sample
text text
<!--comment2a-- --comment2b--> <!--comment2a-- --comment2b-->
</html> </Html>
""", [ """, [
("data", "\n"), ("data", "\n"),
("decl", "DOCTYPE html PUBLIC 'foo'"), ("decl", "DOCTYPE html PUBLIC 'foo'"),
...@@ -117,7 +117,7 @@ text ...@@ -117,7 +117,7 @@ text
("data", "\n"), ("data", "\n"),
("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"), ("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
("data", "\n"), ("data", "\n"),
("starttag", "img", [("src", "bar"), ("ismap", "ismap")]), ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
("data", "sample\ntext\n"), ("data", "sample\ntext\n"),
("comment", "comment2a-- --comment2b"), ("comment", "comment2a-- --comment2b"),
("data", "\n"), ("data", "\n"),
...@@ -135,7 +135,7 @@ text ...@@ -135,7 +135,7 @@ text
def check_attr_syntax(self): def check_attr_syntax(self):
output = [ 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 b = 'v' c = "v" d = v e>""", output) self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
......
...@@ -114,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -114,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase):
def check_code_attr_syntax(self): def check_code_attr_syntax(self):
output = [ output = [
('setPosition', (1, 0)), ('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)
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