Fix remove_tags with greater-than in quoted attributes - #294
Open
deepakganesh78 wants to merge 1 commit into
Open
Fix remove_tags with greater-than in quoted attributes#294deepakganesh78 wants to merge 1 commit into
deepakganesh78 wants to merge 1 commit into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #294 +/- ##
==========================================
- Coverage 98.47% 98.11% -0.37%
==========================================
Files 9 9
Lines 853 902 +49
Branches 177 193 +16
==========================================
+ Hits 840 885 +45
- Misses 5 7 +2
- Partials 8 10 +2
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #284.
Reproduction
On current
master,remove_tags('<a title="x>y">texty</a>')returnsy">textybecause tag matching stops at the>inside the quoted attribute value. The same happens for single-quoted attribute values.Root cause
remove_tagsused a regular expression that treated every raw>as the end of a tag. That cuts valid tags in the middle when quoted attributes contain>, leaking the tail of the opening tag into the output.Fix
Replace the
remove_tagstag regex with a small linear scanner that reads the tag name and ignores>while inside a quoted attribute value that starts after=. Quotes in unquoted attribute values keep the previous behavior, so malformed inputs such as<a b=c"d>keep</a>are still handled as before.Compatibility notes
This changes
remove_tagsbehavior only for valid tags whose quoted attribute values contain>. Existingwhich_onesandkeepfiltering is preserved.Validation
python -m pytest tests\test_html.py::TestRemoveTags::test_remove_tags_with_gt_in_quoted_attribute tests\test_html.py::TestRemoveTags::test_remove_tags_with_unquoted_quote_in_attribute -q-> 1 failed, 1 passed (expected failure:y">textyvstexty).python -m pytest tests\test_html.py::TestRemoveTags -q-> 11 passed.python -m pytest-> 409 passed, 4 skipped, 74 xfailed.python -m ruff check w3lib\html.py tests\test_html.py-> all checks passed.python -m ruff format --check w3lib\html.py tests\test_html.py-> 2 files already formatted.python -m mypy w3lib tests-> success, no issues in 20 source files.