Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix escaping in an indented code block #262

Merged
merged 4 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Properly encode `<`, `>`, and `"` as their respective HTML entities when
interpreted as text.
* Improve inline code parsing when using multiple backticks.
* Do not encode HTML in indented code blocks when `encodeHtml` is false (e.g.
when used in Flutter).

## 2.0.3

Expand Down
7 changes: 4 additions & 3 deletions lib/src/block_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,11 @@ class CodeBlockSyntax extends BlockSyntax {
// The Markdown tests expect a trailing newline.
childLines.add('');

// Escape the code.
var escaped = escapeHtml(childLines.join('\n'));
var content = parser.document.encodeHtml
? escapeHtml(childLines.join('\n'))
: childLines.join('\n');

return Element('pre', [Element.text('code', escaped)]);
return Element('pre', [Element.text('code', content)]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: markdown
version: 2.1.0-dev
version: 2.1.0

description: A library for converting markdown to HTML.
author: Dart Team <misc@dartlang.org>
Expand Down
87 changes: 47 additions & 40 deletions test/document_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,59 @@ void main() {
.having((e) => e.text, 'text', equals('< &')));
});

test('encodeHtml true allow code block escaping', () {
group('with encodeHtml enabled', () {
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('encodes HTML in an inline code snippet', () {
var result = document.parseInline("``<p>Hello <em>Markdown</em></p>``");
var codeSnippet = result.single as Element;
expect(codeSnippet.textContent,
equals("&lt;p&gt;Hello &lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;"));
});

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('encodes HTML in a fenced code block', () {
var lines = "```\n<p>Hello <em>Markdown</em></p>\n```\n".split('\n');
var result = document.parseLines(lines);
var codeBlock = result.single as Element;
expect(codeBlock.textContent,
equals("&lt;p&gt;Hello &lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;\n"));
});

test('encodes HTML in an indented code block', () {
var lines = " <p>Hello <em>Markdown</em></p>\n".split('\n');
var result = document.parseLines(lines);
var codeBlock = result.single as Element;
expect(codeBlock.textContent,
equals("&lt;p&gt;Hello &lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;\n"));
});
});

test('encodeHtml false prevents code block escaping (BlockParser)', () {
group('with encodeHtml disabled', () {
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")));

test('leaves HTML alone, in a code snippet', () {
var result =
document.parseInline("```<p>Hello <em>Markdown</em></p>```");
var codeSnippet = result.single as Element;
expect(
codeSnippet.textContent, equals("<p>Hello <em>Markdown</em></p>"));
});

test('leaves HTML alone, in a fenced code block', () {
var lines = "```\n<p>Hello <em>Markdown</em></p>\n```\n".split('\n');
var result = document.parseLines(lines);
var codeBlock = result.single as Element;
expect(
codeBlock.textContent, equals("<p>Hello <em>Markdown</em></p>\n"));
});

test('leaves HTML alone, in an indented code block', () {
var lines = " <p>Hello <em>Markdown</em></p>\n".split('\n');
var result = document.parseLines(lines);
var codeBlock = result.single as Element;
expect(
codeBlock.textContent, equals("<p>Hello <em>Markdown</em></p>\n"));
});
});
});
}