Skip to content

Commit

Permalink
add hash and mem_id (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadRaziei committed Jul 27, 2023
1 parent 080a650 commit 57bbb8c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions selectolax/lexbor.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class LexborCSSSelector:
def any_matches(self, query: str, node: "LexborNode") -> bool: ...

class LexborNode:
@property
def mem_id(self) -> int: ...
@property
def child(self) -> None | "LexborNode": ...
@property
Expand All @@ -51,6 +53,7 @@ class LexborNode:
def last_child(self) -> None | "LexborNode": ...
@property
def html(self) -> str | None: ...
def __hash__(self) -> int: ...
def text_lexbor(self) -> str: ...
def text(
self, deep: bool = True, separator: str = "", strip: bool = False
Expand Down
7 changes: 7 additions & 0 deletions selectolax/lexbor/node.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ cdef class LexborNode:
self.node = node
return self

@property
def mem_id(self):
return <size_t> self.node

@property
def child(self):
"""Alias for the `first_child` property."""
Expand Down Expand Up @@ -101,6 +105,9 @@ cdef class LexborNode:
return html
return None

def __hash__(self):
return self.mem_id

def text_lexbor(self):
"""Returns the text of the node including text of all its child nodes.
Expand Down
13 changes: 13 additions & 0 deletions selectolax/modest/node.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ cdef class Node:
cdef _Attributes attributes = _Attributes.create(self.node, self.parser.decode_errors)
return attributes

@property
def mem_id(self):
"""Get the mem_id attribute of the node.
Returns
-------
text : int
"""
return <size_t> self.node

@property
def id(self):
"""Get the id attribute of the node.
Expand All @@ -229,6 +239,9 @@ cdef class Node:
attr = myhtml_attribute_by_key(self.node, key, 2)
return None if attr == NULL else attr.value.data.decode(_ENCODING, self.parser.decode_errors)

def __hash__(self):
return self.mem_id

def text(self, bool deep=True, str separator='', bool strip=False):
"""Returns the text of the node including text of all its child nodes.
Expand Down
12 changes: 12 additions & 0 deletions selectolax/parser.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ class Node:
Returns None if id does not set."""
...

def mem_id(self) -> int:
"""Get the mem_id of the node.
Returns 0 if mem_id does not set."""
...

def __hash__(self) -> int:
""" Get the hash of this node
:return: int
"""
...
def text(self, deep: bool = True, separator: str = "", strip: bool = False) -> str:
"""Returns the text of the node including text of all its child nodes."""
...
Expand Down
8 changes: 8 additions & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ def test_script_contain(parser):
""")
assert html_parser.scripts_contain('super_value')

@pytest.mark.parametrize(*_PARSERS_PARAMETRIZER)
def test_hash_nodes(parser):
tree = parser("""<div><p><strong>J</strong>ohn</p><p>Doe</p></div>""")
node = tree.css_first("div")
node_dict = {"node": node}
assert node.mem_id == hash(node)


@pytest.mark.parametrize(*_PARSERS_PARAMETRIZER)
def test_srcs_contain(parser):
Expand Down Expand Up @@ -552,3 +559,4 @@ def test_merge_text_nodes(parser):
assert node.html == "<div><p>John</p><p>Doe</p></div>"
text = tree.text(deep=True, separator=" ", strip=True)
assert text == "John Doe"

0 comments on commit 57bbb8c

Please sign in to comment.