Skip to content

Commit

Permalink
find_html: allow list of tags to find.
Browse files Browse the repository at this point in the history
To make find_html work the same was as find_child,
allow `target_tag` (now `target_tags`) to be a list
of strings instead of a single string, and return
all HTML element children with one of those tags.
  • Loading branch information
kristian-clausal committed Oct 18, 2024
1 parent a709d4b commit 340a560
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/wikitextprocessor/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def filter_empty_str_child(self) -> Iterator[Union[str, "WikiNode"]]:
@overload
def find_html(
self,
target_tag: str,
target_tags: str | list[str],
with_index: Literal[True],
attr_name: str,
attr_value: str,
Expand All @@ -505,15 +505,15 @@ def find_html(
@overload
def find_html(
self,
target_tag: str,
target_tags: str | list[str],
with_index: Literal[False] = ...,
attr_name: str = ...,
attr_value: str = ...,
) -> Iterator["HTMLNode"]: ...

def find_html(
self,
target_tag: str,
target_tags: str | list[str],
with_index: bool = False,
attr_name: str = "",
attr_value: str = "",
Expand All @@ -523,7 +523,9 @@ def find_html(
if TYPE_CHECKING:
assert isinstance(node, HTMLNode)
# node.tag is an alias for node.sarg defined in HTMLNode
if node.tag == target_tag:
if isinstance(target_tags, str):
target_tags = [target_tags]
if node.tag == target_tags:
if len(attr_name) > 0 and attr_value not in node.attrs.get(
attr_name, {}
):
Expand Down

0 comments on commit 340a560

Please sign in to comment.