Skip to content

Escape text and attribute values in to_html - #203

Merged
satoryu merged 1 commit into
ruby-docx:masterfrom
jayhemnani9910:fix/to-html-escape-text
Nov 19, 2026
Merged

satoryu merged 1 commit into
ruby-docx:masterfrom
jayhemnani9910:fix/to-html-escape-text

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

to_html writes a run's text and a link's href into the HTML without escaping. A paragraph with the text Terms: <name> & R&D renders as Terms: & R&D in a browser, because <name> is read as a tag. If the document contains markup as text, that markup ends up in the page as real HTML.

TextRun#to_html now starts from CGI.escapeHTML(@text), and html_tag escapes attribute values, which covers link hrefs from the relationships part.

Added a spec to document_spec.rb (escapes the document text); it fails without the change. All specs pass (296 examples).

TextRun#to_html and html_tag wrote document text and attribute values (link
hrefs) into the HTML as is, so text such as "<name>" disappeared from the
output and markup in a document came out as markup.
@satoryu
satoryu merged commit b206e73 into ruby-docx:master Nov 19, 2026
6 checks passed
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.

2 participants