Add Document option to prevent HTML escaping. (#218)
Add Document option to prevent HTML escaping.
diff --git a/AUTHORS b/AUTHORS
index 213eca2..733332d 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -8,3 +8,4 @@
David Peek <ninjascript@gmail.com>
Daniel Schubert <daniel.schubert+github.com@gmail.com>
Jirka Daněk <dnk@mail.muni.cz>
+Seth Westphal <westy92@gmail.com>
diff --git a/lib/src/document.dart b/lib/src/document.dart
index 7aee5da..f9c47be 100644
--- a/lib/src/document.dart
+++ b/lib/src/document.dart
@@ -13,6 +13,7 @@
final ExtensionSet extensionSet;
final Resolver linkResolver;
final Resolver imageLinkResolver;
+ final bool encodeHtml;
final _blockSyntaxes = new Set<BlockSyntax>();
final _inlineSyntaxes = new Set<InlineSyntax>();
@@ -24,7 +25,8 @@
Iterable<InlineSyntax> inlineSyntaxes,
ExtensionSet extensionSet,
this.linkResolver,
- this.imageLinkResolver})
+ this.imageLinkResolver,
+ this.encodeHtml = true})
: this.extensionSet = extensionSet ?? ExtensionSet.commonMark {
this._blockSyntaxes
..addAll(blockSyntaxes ?? [])
diff --git a/lib/src/inline_parser.dart b/lib/src/inline_parser.dart
index 29c8a5d..8220694 100644
--- a/lib/src/inline_parser.dart
+++ b/lib/src/inline_parser.dart
@@ -25,6 +25,16 @@
new TextSyntax(r' \* '),
// "_" surrounded by spaces is left alone.
new TextSyntax(r' _ '),
+ // Parse "**strong**" and "*emphasis*" tags.
+ new TagSyntax(r'\*+', requiresDelimiterRun: true),
+ // Parse "__strong__" and "_emphasis_" tags.
+ new TagSyntax(r'_+', requiresDelimiterRun: true),
+ new CodeSyntax(),
+ // We will add the LinkSyntax once we know about the specific link resolver.
+ ]);
+
+ static final List<InlineSyntax> _htmlSyntaxes =
+ new List<InlineSyntax>.unmodifiable(<InlineSyntax>[
// Leave already-encoded HTML entities alone. Ensures we don't turn
// "&" into "&amp;"
new TextSyntax(r'&[#a-zA-Z0-9]*;'),
@@ -32,11 +42,6 @@
new TextSyntax(r'&', sub: '&'),
// Encode "<". (Why not encode ">" too? Gruber is toying with us.)
new TextSyntax(r'<', sub: '<'),
- // Parse "**strong**" and "*emphasis*" tags.
- new TagSyntax(r'\*+', requiresDelimiterRun: true),
- // Parse "__strong__" and "_emphasis_" tags.
- new TagSyntax(r'_+', requiresDelimiterRun: true),
- new CodeSyntax(),
// We will add the LinkSyntax once we know about the specific link resolver.
]);
@@ -77,6 +82,10 @@
syntaxes.addAll(_defaultSyntaxes);
+ if (this.document.encodeHtml) {
+ syntaxes.addAll(_htmlSyntaxes);
+ }
+
// Custom link resolvers go after the generic text syntax.
syntaxes.insertAll(1, [
new LinkSyntax(linkResolver: document.linkResolver),
diff --git a/test/document_test.dart b/test/document_test.dart
new file mode 100644
index 0000000..88659d0
--- /dev/null
+++ b/test/document_test.dart
@@ -0,0 +1,18 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:markdown/markdown.dart';
+import 'package:test/test.dart';
+
+void main() {
+ group('Document', () {
+ test('encodeHtml prevents less than and ampersand escaping', () {
+ var document = new Document(encodeHtml: false);
+ var result = document.parseInline('< &');
+ expect(result, hasLength(1));
+ expect(result[0], new isInstanceOf<Text>());
+ expect((result[0] as Text).text, equals('< &'));
+ });
+ });
+}