Skip to content

Commit

Permalink
Fix root node property. Resolves #32
Browse files Browse the repository at this point in the history
  • Loading branch information
rushter committed Dec 19, 2020
1 parent 9332e6b commit fe1a1fb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
selectolax Changelog
====================

Version 0.2.10
--------------

Released

- Fix root node property (`#32`_ ). The `root` property now points to the html tag.

.. _#32: https://github.com/rushter/selectolax/issues/32

Version 0.2.9
-------------

Expand Down
2 changes: 1 addition & 1 deletion selectolax/node.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ cdef class Node:
def unwrap_tags(self, list tags):
"""Unwraps specified tags from the HTML tree.
Works the same as th ``unwrap`` method, but applied to a list of tags.
Works the same as the ``unwrap`` method, but applied to a list of tags.
Parameters
----------
Expand Down
8 changes: 2 additions & 6 deletions selectolax/parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,10 @@ cdef class HTMLParser:
@property
def root(self):
"""Returns root node."""
cdef myhtml_tree_node_t* root
root = myhtml_tree_get_document(self.html_tree)

if root != NULL:
if self.html_tree and self.html_tree.node_html:
node = Node()
node._init(root, self)
node._init(self.html_tree.node_html, self)
return node

return None

@property
Expand Down
4 changes: 2 additions & 2 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def test_traverse():
)
html_parser = HTMLParser(html)
actual = [node.tag for node in html_parser.root.traverse()]
expected = ['-undef', 'html', 'head', 'body', 'div', 'div', 'div', 'h1', 'div', 'img', 'div']
expected = ['html', 'head', 'body', 'div', 'div', 'div', 'h1', 'div', 'img', 'div']
assert actual == expected


Expand All @@ -297,7 +297,7 @@ def test_traverse_with_text():
)
html_parser = HTMLParser(html)
actual = [node.tag for node in html_parser.root.traverse(include_text=True)]
expected = ['-undef', 'html', 'head', 'body', 'div', 'div', 'div', 'h1', '-text', 'div', '-text', 'img', 'div']
expected = ['html', 'head', 'body', 'div', 'div', 'div', 'h1', '-text', 'div', '-text', 'img', 'div']
assert actual == expected


Expand Down
5 changes: 5 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ def test_nodes():
html_output = htmlp.html
assert len(html_output) >= len(html)
assert SequenceMatcher(None, html, html_output).ratio() > 0.8


def test_root_css():
tree = HTMLParser('test')
assert len(tree.root.css('data')) == 0

0 comments on commit fe1a1fb

Please sign in to comment.