Skip to content

Escape HTML special characters in HTMLFormatter#274

Merged
d-ronnqvist merged 3 commits into
swiftlang:mainfrom
somiljain2006:html-escaping-fix
Jun 9, 2026
Merged

Escape HTML special characters in HTMLFormatter#274
d-ronnqvist merged 3 commits into
swiftlang:mainfrom
somiljain2006:html-escaping-fix

Conversation

@somiljain2006

@somiljain2006 somiljain2006 commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Bug/issue #, if applicable: Fixes #264

Summary

The HTML formatter previously emitted text, inline code, and code block contents without escaping HTML special characters. As a result, markdown containing escaped entities such as:

<key>

could be rendered as HTML that browsers interpret as an actual tag rather than displaying the text . This change escapes &, <, and > when rendering text, inline code, and code block contents in HTMLFormatter. Raw HTML nodes (HTMLBlock and InlineHTML) continue to be emitted unchanged.

Dependencies

N/A

Testing

Regression tests have been added to verify correct rendering for:

  • Regular text containing escaped HTML entities
  • Inline code containing angle brackets
  • Code blocks containing angle brackets

Steps:

  1. Build and run the test suite:
  2. Verify the new regression tests pass.

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary

@somiljain2006

Copy link
Copy Markdown
Contributor Author

@d-ronnqvist Can you review this pr?

Comment thread Sources/Markdown/Walker/Walkers/HTMLFormatter.swift Outdated
Comment thread Sources/Markdown/Walker/Walkers/HTMLFormatter.swift Outdated
@somiljain2006

somiljain2006 commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@d-ronnqvist Thanks for the review. I've updated the escaping helper to only escape &, <, and >, since it's used for text and code content rather than attribute values. I also removed the unrelated link title change to keep the PR focused on the HTML escaping fix, so there is no longer any untested behavior being introduced.

@somiljain2006
somiljain2006 requested a review from d-ronnqvist June 5, 2026 18:20

@d-ronnqvist d-ronnqvist left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this issue.

Comment thread Tests/MarkdownTests/Visitors/HTMLFormatterTests.swift
@d-ronnqvist

Copy link
Copy Markdown
Contributor

@swift-ci please test

@somiljain2006

Copy link
Copy Markdown
Contributor Author

@d-ronnqvist I have added the required comment. Can you run the CI again?

@d-ronnqvist

Copy link
Copy Markdown
Contributor

Thanks. That comment looks great

@d-ronnqvist

Copy link
Copy Markdown
Contributor

@swift-ci please test

@d-ronnqvist
d-ronnqvist merged commit 4661b55 into swiftlang:main Jun 9, 2026
2 checks passed
@somiljain2006
somiljain2006 deleted the html-escaping-fix branch June 9, 2026 13:38
jackbolen added a commit to jackbolen/swift-markdown that referenced this pull request Jul 19, 2026
Upstream swiftlang#274 (visitText/visitInlineCode/visitCodeBlock escape &<> through
String.htmlEscaped()) and swiftlang#280 (visitHeading descends into nested markup)
land byte-for-byte so the next upstream merge is clean. Fork delta on top:
attribute positions — link href, image src/title, footnote ids/hrefs, and
data-attributes — route through a quote-escaping attribute wrapper, since
upstream's 3-character text helper cannot serve a double-quoted attribute;
data-attributes drops its invalid backslash-escaped quote for &quot;. Math
text moves onto htmlEscaped() (a quote needs no escape in text position).
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.

Bug: Html formatter parses html escaped code

2 participants