fix: acknowledge self-closing flag on foreign integration points
Problem
In HTML mode the self-closing flag is honored for foreign (SVG/MathML) elements — <svg><rect/>x</svg> closes rect and keeps x as a sibling. But HTML integration points (<foreignObject>, <title>/<desc> inside <svg>, <mi>/<annotation-xml> inside <math>) are themselves foreign elements while parsing their children as HTML. Opening one pushes an HTML child context, so isInForeignContext() returns false and onselfclosingtag ignores the slash, nesting the following content inside the element:
parseDocument("<svg><foreignObject/>x</svg>")
// before: <foreignObject> contains "x" (wrong)
// after: <foreignObject/> empty, "x" sibling (matches parse5)
Same for <svg><title/>, <svg><desc/>, <math><mi/>, <math><annotation-xml/>, etc.
Cause
onselfclosingtag decided acknowledgement from the child context the element establishes instead of from whether the element itself is foreign.
Fix
Add currentTagIsForeign(), which also treats an integration point whose enclosing context is foreign as foreign. <svg><rect/>x</svg>, <svg/>x, and plain-HTML <title/> are unchanged.
Tests + snapshots added; npm test (190 tests) and lint pass.
Summary by CodeRabbit
- Bug Fixes
- Improved parsing of self-closing SVG and MathML elements.
- Ensured self-closing behavior is handled correctly across foreign-content boundaries.
- Corrected interpretation of content following SVG integration points, titles, descriptions, MathML integration points, and HTML content inside
foreignObject. - Expanded coverage for these scenarios to help prevent parsing regressions.