Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ Patches and suggestions
- Ville Skyttä
- Hugo van Kemenade
- Mark Vasilkov
- shkyyy18

2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Features:

Bug fixes:

* Allow the lxml treewalker to serialize empty fragments without raising
``IndexError``. (#343)
* The sanitizer now permits ``<summary>`` tags. It used to allow ``<details>``
already. (#423)

Expand Down
23 changes: 23 additions & 0 deletions html5lib/tests/test_treewalkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from html5lib import html5parser, treewalkers
from html5lib.filters.lint import Filter as Lint
from html5lib.serializer import HTMLSerializer

import re
attrlist = re.compile(r"^(\s+)\w+=.*(\n\1\w+=.*)+", re.M)
Expand Down Expand Up @@ -123,6 +124,28 @@ def test_fragment_single_char(tree, char):
assert list(output) == expected


@pytest.mark.parametrize("tree,intext",
itertools.product(sorted(treeTypes.items()),
["", "<!doctype html>"]))
def test_fragment_empty(tree, intext):
treeName, treeClass = tree
if treeClass is None:
pytest.skip("Treebuilder not loaded")

parser = html5parser.HTMLParser(tree=treeClass["builder"])
document = parser.parseFragment(intext)
document = treeClass.get("adapter", lambda x: x)(document)

assert list(Lint(treeClass["walker"](document))) == []
assert "".join(HTMLSerializer().serialize(treeClass["walker"](document))) == ""


@pytest.mark.skipif(treeTypes["lxml"] is None, reason="lxml not importable")
def test_lxml_empty_list():
walker = treewalkers.getTreeWalker('lxml')
assert list(Lint(walker([]))) == []


@pytest.mark.skipif(treeTypes["lxml"] is None, reason="lxml not importable")
def test_lxml_xml():
expected = [
Expand Down
7 changes: 4 additions & 3 deletions html5lib/treewalkers/etree_lxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def getnext(self):
return None

def __len__(self):
return 1
return len(self.children)


class Doctype(object):
Expand Down Expand Up @@ -180,11 +180,12 @@ def getNodeDetails(self, node):
def getFirstChild(self, node):
assert not isinstance(node, tuple), "Text nodes have no children"

assert len(node) or node.text, "Node has no children"
if node.text:
return (node, "text")
else:
elif len(node):
return node[0]
else:
return None

def getNextSibling(self, node):
if isinstance(node, tuple): # Text node
Expand Down