Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Fixed
-----

* Close tags more correctly, even if the end tag has trailing text.

For example, ``</tag bonus>`` now closes ``<tag>`` correctly.
The trailing content is not available to parsers.
2 changes: 2 additions & 0 deletions src/feedparser_sgmllib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ def finish_starttag(

# Internal -- finish processing of end tag
def finish_endtag(self, tag: str) -> None:
if (match := tagfind.match(tag)) is not None:
tag = match.group(0)
if not tag:
found = len(self.stack) - 1
if found < 0:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_sgmllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,45 @@ def test_endtag_closes_innermost_matching_tag(nested_tag_collector):
assert nested_tag_collector.stack == ["a"]


@pytest.mark.parametrize(
"trailing_text",
(
" bonus",
"/",
"=x",
";",
",",
),
)
def test_endtag_with_trailing_content_still_closes_tag(
cdata_event_collector, trailing_text
):
"""Verify that a closing tag with trailing content still closes the tag."""

cdata_event_collector.check_events(
f"<cdata>content</cdata{trailing_text}>after",
[
("starttag", "cdata", []),
("data", "content"),
("endtag", "cdata"),
("data", "after"),
],
)
assert cdata_event_collector.stack == []


def test_endtag_with_no_valid_tag_name_does_not_close_open_tag(nested_tag_collector):
"""Verify invalid tag names do not close any open tags."""

nested_tag_collector.feed("<a>content</123>after")

assert nested_tag_collector.events == [
("start_a", []),
("unknown_endtag", "123"),
]
assert nested_tag_collector.stack == ["a"]


# XXX These tests have been disabled by prefixing their names with
# an underscore. The first two exercise outstanding bugs in the
# sgmllib module, and the third exhibits questionable behavior
Expand Down