Markdown target: escape a < that would open markup - #273
Merged
Conversation
MarkdownRenderer::escapeText() escaped the Markdown metacharacters and left `<` alone, so djot text went out as Markdown that a CommonMark reader takes as raw HTML: `a<b>c` emitted `a<b>c`, and `<b>` opens a tag. The authored text is gone and markup appears in its place. A `<` is now escaped with a backslash when the next character is an ASCII letter, `/`, `!` or `?` - the four things that open raw HTML - and left alone otherwise. `>` takes nothing: it is inert mid-line, and at line start it is a block quote marker the line-level handling already covers. A backslash rather than an entity, because the operation is to protect the character so it reads back as itself; an entity replaces it. So `a < b and 3 > 2` survives unchanged and `a<b>c` writes `a\<b>c`, which a CommonMark reader gives back as the text that was written. The pass runs after the metacharacter pass so the backslash it inserts is not escaped a second time. Code spans, autolinks and raw nodes never reach this function and are unaffected.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #273 +/- ##
=========================================
Coverage 92.42% 92.42%
Complexity 3682 3682
=========================================
Files 109 109
Lines 10440 10441 +1
=========================================
+ Hits 9649 9650 +1
Misses 791 791 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found while surveying recent sibling-project fixes for things that apply here. Mirrors markup-carve/carve-php#1241.
MarkdownRenderer::escapeText()escapes the Markdown metacharacters (\ ` * _ [ ] #) and leaves<alone. So djot text is emitted as Markdown that a CommonMark reader parses as raw HTML.On master, this djot:
renders to this Markdown:
and every CommonMark reader takes
<b>as an HTML tag. The authored text is gone and markup appears in its place.The rule
A
<is escaped with a backslash when the next character is an ASCII letter,/,!or?- the four things that open raw HTML. Every other<is left alone, and>takes nothing: inert mid-line, and at line start it is a block quote marker the line-level handling already covers.A backslash rather than an entity. The job is to protect the character so it reads back as itself; an entity replaces it with something else.
a<b>ca<b>ca\<b>ca < b and 3 > 2x<!DOCTYPE yx<!DOCTYPE yx\<!DOCTYPE yOne line, appended after the metacharacter pass so the backslash it inserts is not escaped a second time.
Scope
escapeText()only, which servesTextnodes and a div's title. Code spans, code blocks, autolinks, raw blocks and raw inlines never reach it - a test pins that a code span holding<b>keeps its content verbatim, and that a<https://example.com>autolink still renders as a Markdown link.