Repository: html5lib/html5lib-python

Issue:
AttributeError: 'TreeWalker' object has no attribute 'fragmentChildren'
For some reason my `tree` (`lxml.etree._Element`) object has no attribute getroot and is not a list so there is no `fragmentChildren` attribute on `TreeWalker`: 270a2ca14fafc989f8f1bd4f79db2f4bd9f4d1fc

lxml 3.5.0
html5lib-python f796cca5f9ddaaf1e1a8b872f68455551cd3ae2d

Discussion:
gsnedders: fixes #228 
landscape-bot: [![Code Health](https://landscape.io/badge/381520/landscape.svg?style=flat)](https://landscape.io/diff/354583)
Code quality remained the same when pulling **[2f1d040](https://github.com/gsnedders/html5lib-python/commit/2f1d04073f63e7633b8fa2204e7dac221051c7fe) on gsnedders:lxml_treewalker_lxml_tree** into **[5288737](https://github.com/html5lib/html5lib-python/commit/5288737aebcae1fcf25a640c79241f6fb14475a2) on html5lib:master**.
codecov-io: ## [Current coverage][cc-pull] is **90.83%**
> Merging [#256][cc-pull] into [master][cc-base-branch] will increase coverage by **<.01%**

```diff
@@             master       #256   diff @@
==========================================
  Files            51         51          
  Lines          6821       6835    +14   
  Methods           0          0          
  Messages          0          0          
  Branches       1313       1312     -1   
==========================================
+ Hits           6193       6208    +15   
  Misses          468        468          
+ Partials        160        159     -1   
```

> Powered by [Codecov](https://codecov.io?src=pr). Last updated by [5288737...2f1d040][cc-compare]
[cc-base-branch]: https://codecov.io/gh/html5lib/html5lib-python/branch/master?src=pr
[cc-compare]: https://codecov.io/gh/html5lib/html5lib-python/compare/5288737aebcae1fcf25a640c79241f6fb14475a2...2f1d04073f63e7633b8fa2204e7dac221051c7fe
[cc-pull]: https://codecov.io/gh/html5lib/html5lib-python/pull/256?src=pr
landscape-bot: [![Code Health](https://landscape.io/badge/385132/landscape.svg?style=flat)](https://landscape.io/diff/358204)
Code quality remained the same when pulling **[a2ee6e6](https://github.com/gsnedders/html5lib-python/commit/a2ee6e6b0ec015a3a777546192d8af80074d60b0) on gsnedders:lxml_treewalker_lxml_tree** into **[66a2f77](https://github.com/html5lib/html5lib-python/commit/66a2f7763f2dda24d3d2681c22bf799c94ee049c) on html5lib:master**.

Patch:
diff --git a/html5lib/treewalkers/lxmletree.py b/html5lib/treewalkers/lxmletree.py
index 7d99adc..ff31a44 100644
--- a/html5lib/treewalkers/lxmletree.py
+++ b/html5lib/treewalkers/lxmletree.py
@@ -22,13 +22,20 @@ class Root(object):
     def __init__(self, et):
         self.elementtree = et
         self.children = []
-        if et.docinfo.internalDTD:
-            self.children.append(Doctype(self,
-                                         ensure_str(et.docinfo.root_name),
-                                         ensure_str(et.docinfo.public_id),
-                                         ensure_str(et.docinfo.system_url)))
-        root = et.getroot()
-        node = root
+
+        try:
+            if et.docinfo.internalDTD:
+                self.children.append(Doctype(self,
+                                             ensure_str(et.docinfo.root_name),
+                                             ensure_str(et.docinfo.public_id),
+                                             ensure_str(et.docinfo.system_url)))
+        except AttributeError:
+            pass
+
+        try:
+            node = et.getroot()
+        except AttributeError:
+            node = et
 
         while node.getprevious() is not None:
             node = node.getprevious()
@@ -118,12 +125,12 @@ class FragmentWrapper(object):
 class TreeWalker(_base.NonRecursiveTreeWalker):
     def __init__(self, tree):
         # pylint:disable=redefined-variable-type
-        if hasattr(tree, "getroot"):
-            self.fragmentChildren = set()
-            tree = Root(tree)
-        elif isinstance(tree, list):
+        if isinstance(tree, list):
             self.fragmentChildren = set(tree)
             tree = FragmentRoot(tree)
+        else:
+            self.fragmentChildren = set()
+            tree = Root(tree)
         _base.NonRecursiveTreeWalker.__init__(self, tree)
         self.filter = ihatexml.InfosetFilter()
 


Test patch:
diff --git a/html5lib/tests/test_treewalkers.py b/html5lib/tests/test_treewalkers.py
index 332027a..81ed277 100644
--- a/html5lib/tests/test_treewalkers.py
+++ b/html5lib/tests/test_treewalkers.py
@@ -2,6 +2,11 @@ from __future__ import absolute_import, division, unicode_literals
 
 import pytest
 
+try:
+    import lxml.etree
+except ImportError:
+    pass
+
 from .support import treeTypes
 
 from html5lib import html5parser, treewalkers
@@ -93,3 +98,19 @@ def test_treewalker_six_mix():
     for tree in sorted(treeTypes.items()):
         for intext, attrs, expected in sm_tests:
             yield runTreewalkerEditTest, intext, expected, attrs, tree
+
+
+@pytest.mark.skipif(treeTypes["lxml"] is None, reason="lxml not importable")
+def test_lxml_xml():
+    expected = [
+        {'data': {}, 'name': 'div', 'namespace': None, 'type': 'StartTag'},
+        {'data': {}, 'name': 'div', 'namespace': None, 'type': 'StartTag'},
+        {'name': 'div', 'namespace': None, 'type': 'EndTag'},
+        {'name': 'div', 'namespace': None, 'type': 'EndTag'}
+    ]
+
+    lxmltree = lxml.etree.fromstring('<div><div></div></div>')
+    walker = treewalkers.getTreeWalker('lxml')
+    output = Lint(walker(lxmltree))
+
+    assert list(output) == expected