Skip to content

Commit

Permalink
Fix codeSyntax and BlockParser to respect encodeHtml flag (dart-lang/…
Browse files Browse the repository at this point in the history
…markdown#239)

Fix codeSyntax and BlockParser to respect encodeHtml flag
  • Loading branch information
sldsrg authored and srawlins committed Mar 26, 2019
1 parent 6c6786c commit 473aeb1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
10 changes: 6 additions & 4 deletions pkgs/markdown/lib/src/block_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,12 @@ class FencedCodeBlockSyntax extends BlockSyntax {
// The Markdown tests expect a trailing newline.
childLines.add('');

// Escape the code.
var escaped = escapeHtml(childLines.join('\n'));

var code = Element.text('code', escaped);
var text = childLines.join('\n');
if (parser.document.encodeHtml) {
// Escape the code.
text = escapeHtml(text);
}
var code = Element.text('code', text);

// the info-string should be trimmed
// http://spec.commonmark.org/0.22/#example-100
Expand Down
5 changes: 4 additions & 1 deletion pkgs/markdown/lib/src/inline_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,10 @@ class CodeSyntax extends InlineSyntax {
}

bool onMatch(InlineParser parser, Match match) {
parser.addNode(Element.text('code', escapeHtml(match[2].trim())));
var code = match[2].trim();
if (parser.document.encodeHtml) code = escapeHtml(code);
parser.addNode(Element.text('code', code));

return true;
}
}
Expand Down
48 changes: 48 additions & 0 deletions pkgs/markdown/test/document_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,53 @@ void main() {
const TypeMatcher<Text>()
.having((e) => e.text, 'text', equals('< &')));
});

test('encodeHtml true allow code block escaping', () {
var document = Document(encodeHtml: true);
var result = document.parseInline("```<p>Hello <em>Markdown</em></p>```");
expect(result, hasLength(1));
expect(
result[0],
const TypeMatcher<Element>().having(
(e) => e.textContent,
'text',
equals(
"&lt;p&gt;Hello &lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;")));
});

test('encodeHtml false prevents code block escaping', () {
var document = Document(encodeHtml: false);
var result = document.parseInline("```<p>Hello <em>Markdown</em></p>```");
expect(result, hasLength(1));
expect(
result[0],
const TypeMatcher<Element>().having((e) => e.textContent, 'text',
equals("<p>Hello <em>Markdown</em></p>")));
});

test('encodeHtml true allow code block escaping (BlockParser)', () {
var document = Document(encodeHtml: true);
var lines = "```\n<p>Hello <em>Markdown</em></p>\n```\n".split('\n');
var result = document.parseLines(lines);
expect(result, hasLength(1));
expect(
result[0],
const TypeMatcher<Element>().having(
(e) => e.textContent,
'text',
equals(
"&lt;p&gt;Hello &lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;\n")));
});

test('encodeHtml false prevents code block escaping (BlockParser)', () {
var document = Document(encodeHtml: false);
var lines = "```\n<p>Hello <em>Markdown</em></p>\n```\n".split('\n');
var result = document.parseLines(lines);
expect(result, hasLength(1));
expect(
result[0],
const TypeMatcher<Element>().having((e) => e.textContent, 'text',
equals("<p>Hello <em>Markdown</em></p>\n")));
});
});
}

0 comments on commit 473aeb1

Please sign in to comment.