Skip to content

fix: stop double escaping in code blocks - #307

Open
faisalahammad wants to merge 1 commit into
Automattic:masterfrom
faisalahammad:fix/286-plain-text-double-escape
Open

fix: stop double escaping in code blocks#307
faisalahammad wants to merge 1 commit into
Automattic:masterfrom
faisalahammad:fix/286-plain-text-double-escape

Conversation

@faisalahammad

Copy link
Copy Markdown

Fixes #286

Changes proposed in this Pull Request

  • In render_block(), always run htmlspecialchars_decode() on the extracted code. Since 3.7.2 (PR Fix escaping when language is unknown #283) the decode was behind a language check, so Plain Text blocks (empty language) stayed escaped and got escaped a second time in the shortcode callback. A block with <foo> / bar & showed &lt;foo> / bar &amp; on the front end.
  • Accept a bare <pre> tag when extracting the code. The block saves <pre> with no attributes, and the old pattern #<pre [^>]+>([^<]+)?</pre># required an attribute and also stopped at any < inside the code.
  • Pin the code format to raw around shortcode_callback(). Block content is always raw at this point, but a post stored as encoded (code format 2) would make the callback strip tags instead of escaping once.

Testing instructions

  1. Add a SyntaxHighlighter block, set language to Plain Text, enter <foo> and bar &.
  2. Publish and view the post. Front end should show <foo> and bar & literally, not &lt;foo> / bar &amp;.
  3. Check an older post with a SyntaxHighlighter block. It should render correctly again.
  4. Check blocks with other languages and classic [sourcecode] shortcodes. Output should be unchanged.

Block content is escaped once on save, so render_block() must decode it once before passing it to the shortcode callback. The language check added in 3.7.2 skipped the decode for plain text blocks, leaving entities that got escaped a second time on output.

Also accept a bare <pre> tag when extracting the code, since the block saves one without attributes.

Fixes Automattic#286
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.

Doubly-escaped HTML special characters in Plain Text blocks, also in old posts

1 participant