Skip to content

Commit d87ca9b

Browse files
committed
Lint serializer test input; fix broken examples
1 parent f3ea8ac commit d87ca9b

File tree

4 files changed

+11
-23
lines changed

4 files changed

+11
-23
lines changed

html5lib/filters/lint.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111

1212
class Filter(_base.Filter):
13+
def __init__(self, source, require_matching_tags=True):
14+
super(Filter, self).__init__(source)
15+
self.require_matching_tags = require_matching_tags
16+
1317
def __iter__(self):
1418
open_elements = []
1519
for token in _base.Filter.__iter__(self):
@@ -26,7 +30,7 @@ def __iter__(self):
2630
assert type == "EmptyTag"
2731
else:
2832
assert type == "StartTag"
29-
if type == "StartTag":
33+
if type == "StartTag" and self.require_matching_tags:
3034
open_elements.append((namespace, name))
3135
for (namespace, name), value in token["data"].items():
3236
assert namespace is None or isinstance(namespace, text_type)
@@ -44,7 +48,7 @@ def __iter__(self):
4448
assert name != ""
4549
if (not namespace or namespace == namespaces["html"]) and name in voidElements:
4650
assert False, "Void element reported as EndTag token: %(tag)s" % {"tag": name}
47-
else:
51+
elif self.require_matching_tags:
4852
start = open_elements.pop()
4953
assert start == (namespace, name)
5054

html5lib/tests/serializer-testdata/core.test

-14
Original file line numberDiff line numberDiff line change
@@ -293,20 +293,6 @@
293293
],
294294
"description": "void element (as EmptyTag token)"
295295
},
296-
{
297-
"expected": [
298-
"<img>"
299-
],
300-
"input": [
301-
[
302-
"StartTag",
303-
"http://www.w3.org/1999/xhtml",
304-
"img",
305-
{}
306-
]
307-
],
308-
"description": "void element (as StartTag token)"
309-
},
310296
{
311297
"expected": [
312298
"<!DOCTYPE foo>"

html5lib/tests/serializer-testdata/optionaltags.test

+3-3
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
},
348348
{
349349
"expected": [
350-
"<foo>"
350+
"<meta>"
351351
],
352352
"input": [
353353
[
@@ -358,7 +358,7 @@
358358
],
359359
[
360360
"EmptyTag",
361-
"foo",
361+
"meta",
362362
{}
363363
]
364364
],
@@ -2094,7 +2094,7 @@
20942094
{}
20952095
],
20962096
[
2097-
"StartTag",
2097+
"EmptyTag",
20982098
"http://www.w3.org/1999/xhtml",
20992099
"col",
21002100
{}

html5lib/tests/test_serializer.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import html5lib
1515
from html5lib import constants
16+
from html5lib.filters.lint import Filter as Lint
1617
from html5lib.serializer import HTMLSerializer, serialize
1718
from html5lib.treewalkers._base import TreeWalker
1819

@@ -83,7 +84,7 @@ def _convertAttrib(self, attribs):
8384

8485
def serialize_html(input, options):
8586
options = dict([(str(k), v) for k, v in options.items()])
86-
stream = JsonWalker(input)
87+
stream = Lint(JsonWalker(input), False)
8788
serializer = HTMLSerializer(alphabetical_attributes=True, **options)
8889
return serializer.render(stream, options.get("encoding", None))
8990

@@ -125,9 +126,6 @@ def testCharacters(self):
125126
def testStartTagName(self):
126127
self.throwsWithLatin1([["StartTag", "http://www.w3.org/1999/xhtml", "\u0101", []]])
127128

128-
def testEmptyTagName(self):
129-
self.throwsWithLatin1([["EmptyTag", "http://www.w3.org/1999/xhtml", "\u0101", []]])
130-
131129
def testAttributeName(self):
132130
self.throwsWithLatin1([["StartTag", "http://www.w3.org/1999/xhtml", "span", [{"namespace": None, "name": "\u0101", "value": "potato"}]]])
133131

0 commit comments

Comments
 (0)