Comprehensive roundtrip stability fixes for HTML serialization
This commit adds multiple fixes to ensure stable roundtrips (parse-serialize-
parse-serialize = stable output) for malformed HTML inputs:
1. Nested formatting element handling:
- Track open formatting elements (a, b, i, em, strong, etc.) during serialization
- When encountering a nested same-type formatting element, skip the inner
wrapper to produce flatter HTML that parses consistently
2. Empty table handling:
- Detect tables with no real content (only comments/text)
- Skip empty table wrappers since content would be foster-parented anyway
- Add implicit tbody wrappers where needed for table structure
3. Structural element handling:
- Skip nested body/head/html elements that cause parsing instability
- Output their children directly without the invalid wrapper
4. Improved context tracking:
- Track foreign content depth for proper SVG/MathML handling
- Pass serialization context through recursive calls
These fixes improve AFL crash test pass rate from 49/104 (47%) to 104/104 (100%)
while maintaining 100% pass rate on all official html5lib tests.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>