Skip to content

Conversation

@choi2601
Copy link

@choi2601 choi2601 commented Jan 24, 2026

Summary

Fixed the issue where unclosed tags (e.g., <hello, <hello attr) resulted in an empty string when disallowedTagsMode is set to 'escape' or 'recursiveEscape'.

htmlparser2 drops malformed/unclosed tags at the end of the input stream without firing events. Added logic to check parser.endIndex after parsing completes to capture and escape any remaining unparsed content.

Closes #706

What are the specific steps to test this change?

  1. Run the test suite: npm test
  2. Verify the following cases manually or via tests:
# Test Input Mode Expected Output
1 Escape unclosed tag <hello escape &lt;hello
2 Escape unclosed tag <hello recursiveEscape &lt;hello
3 Escape unclosed tag with attributes <hello you escape &lt;hello you
4 Escape unclosed tag with attributes <hello you recursiveEscape &lt;hello you
5 Discard unclosed tag <hello discard '' (empty)
6 Escape text + unclosed tag Hello <world escape Hello &lt;world
7 Escape text + unclosed tag Hello <world recursiveEscape Hello &lt;world

Result: All 190 tests passing (183 existing + 7 new)

What kind of change does this PR introduce?

(Check at least one)

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Build-related changes
  • Other

Make sure the PR fulfills these requirements:

  • It includes a) the existing issue ID being resolved, b) a convincing reason for adding this feature, or c) a clear description of the bug it resolves
  • The changelog is updated
  • Related documentation has been updated
  • Related tests have been updated

Other information:

  • Added 7 new test cases covering unclosed tags in escape, recursiveEscape, and discard modes
  • Uses parser.endIndex (public API) from htmlparser2 to detect unparsed trailing content

@BoDonkey
Copy link
Contributor

Thanks for this contribution. We will try to take a look and get back to you soon. However, could you update the CHANGELOG, please? Don't add a version number, just ## UNRELEASED and you change. Thanks!

@choi2601
Copy link
Author

@BoDonkey
Thanks for the review! I've updated the CHANGELOG.md as requested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<hello return `` when escaping content

2 participants