Skip to content

Commit

Permalink
Fix some html entities (#256)
Browse files Browse the repository at this point in the history
Fix the HTML-ification of ", <, >
  • Loading branch information
srawlins authored Jul 16, 2019
1 parent 38b0fa5 commit d4b0401
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Improve strict spec compliance for `blockquote` by always putting the closing
tag on a new line.
* Improve strict spec compliance for `code` elements defined with "\`".
* Properly encode `<`, `>`, and `"` as their respective HTML entities when
interpreted as text.

## 2.0.3

Expand Down
16 changes: 14 additions & 2 deletions lib/src/inline_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,20 @@ class EscapeSyntax extends InlineSyntax {
EscapeSyntax() : super(r'''\\[!"#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~]''');

bool onMatch(InlineParser parser, Match match) {
// Insert the substitution.
parser.addNode(Text(match[0][1]));
final char = match[0].codeUnitAt(1);
// Insert the substitution. Why these three charactes are replaced with
// their equivalent HTML entity referenced appears to be missing from the
// CommonMark spec, but is very present in all of the examples.
// https://talk.commonmark.org/t/entity-ification-of-quotes-and-brackets-missing-from-spec/3207
if (char == $double_quote) {
parser.addNode(Text('&quot;'));
} else if (char == $lt) {
parser.addNode(Text('&lt;'));
} else if (char == $gt) {
parser.addNode(Text('&gt;'));
} else {
parser.addNode(Text(match[0][1]));
}
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/common_mark/backslash_escapes.unit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
>>> Backslash escapes - 298
\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
<<<
<p>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</p>
<p>!&quot;#$%&'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
>>> Backslash escapes - 299
\ \A\a\ \3\φ\«
<<<
Expand All @@ -18,7 +18,7 @@
\&ouml; not a character entity
<<<
<p>*not emphasized*
<br/> not a tag
&lt;br/> not a tag
[not a link](/foo)
`not code`
1. not a list
Expand Down
2 changes: 1 addition & 1 deletion test/common_mark/link_reference_definitions.unit
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ with blank line'

[foo]
<<<
<p>[foo]: /url\bar*baz "foo"bar\baz"</p>
<p>[foo]: /url\bar*baz "foo&quot;bar\baz"</p>
<p>[foo]</p>
>>> Link reference definitions - 172
[foo]
Expand Down
2 changes: 1 addition & 1 deletion test/common_mark/links.unit
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bar>)</p>
>>> Links - 490
[link](<foo\>)
<<<
<p>[link](&lt;foo>)</p>
<p>[link](&lt;foo&gt;)</p>
>>> Links - 491
[a](<b)c
[a](<b)c>
Expand Down
2 changes: 1 addition & 1 deletion test/common_mark/setext_headings.unit
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Baz
\> foo
------
<<<
<h2>> foo</h2>
<h2>&gt; foo</h2>
>>> Setext headings - 73
Foo

Expand Down
4 changes: 2 additions & 2 deletions test/gfm/backslash_escapes.unit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
>>> Backslash escapes - 308
\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
<<<
<p>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</p>
<p>!&quot;#$%&'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
>>> Backslash escapes - 309
\ \A\a\ \3\φ\«
<<<
Expand All @@ -18,7 +18,7 @@
\&ouml; not a character entity
<<<
<p>*not emphasized*
<br/> not a tag
&lt;br/> not a tag
[not a link](/foo)
`not code`
1. not a list
Expand Down
2 changes: 1 addition & 1 deletion test/gfm/link_reference_definitions.unit
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ with blank line'

[foo]
<<<
<p>[foo]: /url\bar*baz "foo"bar\baz"</p>
<p>[foo]: /url\bar*baz "foo&quot;bar\baz"</p>
<p>[foo]</p>
>>> Link reference definitions - 172
[foo]
Expand Down
2 changes: 1 addition & 1 deletion test/gfm/links.unit
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bar>)</p>
>>> Links - 502
[link](<foo\>)
<<<
<p>[link](&lt;foo>)</p>
<p>[link](&lt;foo&gt;)</p>
>>> Links - 503
[a](<b)c
[a](<b)c>
Expand Down
2 changes: 1 addition & 1 deletion test/gfm/setext_headings.unit
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Baz
\> foo
------
<<<
<h2>> foo</h2>
<h2>&gt; foo</h2>
>>> Setext headings - 73
Foo

Expand Down
4 changes: 2 additions & 2 deletions test/original/backslash_escapes.unit
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ and \? and \@ and \[ and \\ and \] and \^ and \_ and \` and \{ and \| and \}
and \~.

<<<
<p>Punctuations like ! and " and # and $ and % and & and ' and ( and )
and * and + and , and - and . and / and : and ; and < and = and >
<p>Punctuations like ! and &quot; and # and $ and % and & and ' and ( and )
and * and + and , and - and . and / and : and ; and &lt; and = and &gt;
and ? and @ and [ and \ and ] and ^ and _ and ` and { and | and }
and ~.</p>
>>> Inline code blocks can be escaped.
Expand Down
6 changes: 3 additions & 3 deletions tool/common_mark_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"Backslash escapes": {
"298": "loose",
"299": "strict",
"300": "fail",
"300": "loose",
"301": "strict",
"302": "strict",
"303": "strict",
Expand Down Expand Up @@ -435,7 +435,7 @@
"487": "strict",
"488": "strict",
"489": "strict",
"490": "loose",
"490": "strict",
"491": "strict",
"492": "strict",
"493": "strict",
Expand Down Expand Up @@ -651,7 +651,7 @@
"69": "strict",
"70": "strict",
"71": "strict",
"72": "loose",
"72": "strict",
"73": "strict",
"74": "strict",
"75": "strict",
Expand Down
6 changes: 3 additions & 3 deletions tool/common_mark_stats.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
17 of 18 – 94.4% ATX headings
19 of 19 – 100.0% Autolinks
11 of 13 – 84.6% Backslash escapes
12 of 13 – 92.3% Backslash escapes
1 of 1 – 100.0% Blank lines
22 of 25 – 88.0% Block quotes
21 of 22 – 95.5% Code spans
Expand All @@ -24,5 +24,5 @@
11 of 11 – 100.0% Tabs
3 of 3 – 100.0% Textual content
19 of 19 – 100.0% Thematic breaks
597 of 649 – 92.0% TOTAL
523 of 597 – 87.6% TOTAL Strict
598 of 649 – 92.1% TOTAL
525 of 598 – 87.8% TOTAL Strict
6 changes: 3 additions & 3 deletions tool/gfm_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"Backslash escapes": {
"308": "loose",
"309": "strict",
"310": "fail",
"310": "loose",
"311": "strict",
"312": "strict",
"313": "strict",
Expand Down Expand Up @@ -451,7 +451,7 @@
"499": "strict",
"500": "strict",
"501": "strict",
"502": "loose",
"502": "strict",
"503": "strict",
"504": "strict",
"505": "strict",
Expand Down Expand Up @@ -667,7 +667,7 @@
"69": "strict",
"70": "strict",
"71": "strict",
"72": "loose",
"72": "strict",
"73": "strict",
"74": "strict",
"75": "strict",
Expand Down
6 changes: 3 additions & 3 deletions tool/gfm_stats.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
17 of 18 – 94.4% ATX headings
17 of 19 – 89.5% Autolinks
8 of 11 – 72.7% Autolinks (extension)
11 of 13 – 84.6% Backslash escapes
12 of 13 – 92.3% Backslash escapes
1 of 1 – 100.0% Blank lines
22 of 25 – 88.0% Block quotes
21 of 22 – 95.5% Code spans
Expand All @@ -28,5 +28,5 @@
11 of 11 – 100.0% Tabs
3 of 3 – 100.0% Textual content
19 of 19 – 100.0% Thematic breaks
612 of 671 – 91.2% TOTAL
529 of 612 – 86.4% TOTAL Strict
613 of 671 – 91.4% TOTAL
531 of 613 – 86.6% TOTAL Strict

0 comments on commit d4b0401

Please sign in to comment.