Skip to content

Commit fda95c3

Browse files
committed
Fix segfaults when accessing attributes. Resolves #135
1 parent 9dc1437 commit fda95c3

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Add `.inner_html` property. Allows to get and set inner HTML of a node.
1010
- Update various docstrings.
1111
- Optimize performance for`css_first` in lexbor backend
12+
- Fix segfaults when accessing attributes. Resolves #135.
1213

1314
## Version 0.3.34
1415

selectolax/lexbor/node.pxi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ cdef class LexborNode:
343343
cdef size_t str_len = 0
344344
attributes = dict()
345345

346+
if self.node.type != LXB_DOM_NODE_TYPE_ELEMENT:
347+
return attributes
348+
346349
while attr != NULL:
347350
key = lxb_dom_attr_local_name_noi(attr, &str_len)
348351
value = lxb_dom_attr_value_noi(attr, &str_len)

tests/test_lexbor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ def test_sets_inner_html():
1818
parser.css_first("#main").inner_html = "<span>Test</span>"
1919
actual = parser.css_first("#main").inner_html
2020
assert actual == expected
21+
22+
23+
def test_checking_attributes_does_not_segfault():
24+
parser = LexborHTMLParser("")
25+
root_node = parser.root
26+
assert root_node is not None
27+
for node in root_node.traverse():
28+
print(node.parent)
29+
parent = node.parent
30+
assert parent is not None
31+
parent = parent.attributes.get("anything")

0 commit comments

Comments
 (0)