xq icon indicating copy to clipboard operation
xq copied to clipboard

Fix #160: Add proper text escaping in FormatHtml

Open bukzor opened this issue 2 months ago • 1 comments

Summary

  • Fixes HTML text nodes containing &, <, > being output without proper escaping
  • Prevents data corruption when round-tripping HTML through xq | xq -j

Changes

  • Added escapeTextContent() function for minimal entity escaping (&amp;, &lt;, &gt;)
  • Modified FormatHtml() to escape text nodes properly
  • Added comprehensive tests verifying output is valid XML

Test plan

  • [x] Unit tests pass: go test ./...
  • [x] New tests verify proper escaping of &, <, > in HTML text nodes
  • [x] Tests confirm xq output can be parsed as XML (required for -j flag)
  • [x] Verified tests fail without the fix (showing they detect the bug)

Example

Before this fix:

echo '<html>1 &amp; 2</html>' | xq
# Output: <html>1 & 2</html>  (bare & causes parse error)
echo '<html>1 &amp; 2</html>' | xq | xq -j
# Error: invalid character entity & (no semicolon)

After this fix:

echo '<html>1 &amp; 2</html>' | xq
# Output: <html>1 &amp; 2</html>  (properly escaped)
echo '<html>1 &amp; 2</html>' | xq | xq -j
# Success: {"html": "1 & 2"}

🤖 Generated with Claude Code

bukzor avatar Oct 22 '25 15:10 bukzor

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 80.77%. Comparing base (ec5a59a) to head (e35e4d8). :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #161      +/-   ##
==========================================
+ Coverage   80.57%   80.77%   +0.19%     
==========================================
  Files           5        5              
  Lines         690      697       +7     
==========================================
+ Hits          556      563       +7     
  Misses         92       92              
  Partials       42       42              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Nov 14 '25 17:11 codecov-commenter