Make markdown error-free in strong mode. Fix dart-lang/markdown#40. R=nweiz@google.com Review URL: https://codereview.chromium.org//1325903002 .
diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md index bc370d8..fdbb0d6 100644 --- a/pkgs/markdown/CHANGELOG.md +++ b/pkgs/markdown/CHANGELOG.md
@@ -1,6 +1,7 @@ -## 0.7.3 +## 0.8.0 * Switch tests to use [test][] instead of [unittest][]. +* Remove (probably unused) `resolved` field from `LinkSyntax`. [test]: https://pub.dartlang.org/packages/test [unittest]: https://pub.dartlang.org/packages/unittest
diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart index 5c11d7f..fe7c4dc 100644 --- a/pkgs/markdown/lib/src/inline_parser.dart +++ b/pkgs/markdown/lib/src/inline_parser.dart
@@ -138,7 +138,8 @@ // If the previous node is text too, just append. if (nodes.length > 0 && nodes.last is Text) { - nodes[nodes.length - 1] = new Text('${nodes.last.text}$text'); + var textNode = nodes.last as Text; + nodes[nodes.length - 1] = new Text('${textNode.text}$text'); } else { nodes.add(new Text(text)); } @@ -192,8 +193,8 @@ final String substitute; TextSyntax(String pattern, {String sub}) - : super(pattern), - substitute = sub; + : substitute = sub, + super(pattern); bool onMatch(InlineParser parser, Match match) { if (substitute == null) { @@ -230,8 +231,8 @@ final String tag; TagSyntax(String pattern, {this.tag, String end}) - : super(pattern), - endPattern = new RegExp((end != null) ? end : pattern, multiLine: true); + : endPattern = new RegExp((end != null) ? end : pattern, multiLine: true), + super(pattern); bool onMatch(InlineParser parser, Match match) { parser._stack @@ -249,9 +250,6 @@ class LinkSyntax extends TagSyntax { final Resolver linkResolver; - /// Weather or not this link was resolved by a [Resolver] - bool resolved = false; - /// The regex for the end of a link needs to handle both reference style and /// inline styles as well as optional titles for inline links. To make that /// a bit more palatable, this breaks it into pieces. @@ -287,21 +285,28 @@ var textToResolve = parser.source.substring(state.endPos, parser.pos); // See if we have a resolver that will generate a link for us. - resolved = true; return linkResolver(textToResolve); } else { - var link = getLink(parser, match, state); - if (link == null) return null; - - var node = new Element('a', state.children); - - node.attributes["href"] = escapeHtml(link.url); - if (link.title != null) node.attributes['title'] = escapeHtml(link.title); - - return node; + return _createElement(parser, match, state); } } + /// Given that [match] has matched both a title and URL, creates an `<a>` + /// [Element] for it. + Element _createElement(InlineParser parser, Match match, TagState state) { + var link = getLink(parser, match, state); + if (link == null) return null; + + var element = new Element('a', state.children); + + element.attributes["href"] = escapeHtml(link.url); + if (link.title != null) { + element.attributes['title'] = escapeHtml(link.title); + } + + return element; + } + Link getLink(InlineParser parser, Match match, TagState state) { if (match[3] != null && match[3] != '') { // Inline link like [foo](url). @@ -342,31 +347,29 @@ /// Matches images like `` and /// `![alternate text][url reference]`. class ImageLinkSyntax extends LinkSyntax { - final Resolver linkResolver; + ImageLinkSyntax({Resolver linkResolver}) + : super(linkResolver: linkResolver, pattern: r'!\['); - ImageLinkSyntax({this.linkResolver}) : super(pattern: r'!\['); + /// Creates an <a> element from the given complete [match]. + Element _createElement(InlineParser parser, Match match, TagState state) { + var element = super._createElement(parser, match, state); + if (element == null) return null; - Node createNode(InlineParser parser, Match match, TagState state) { - var node = super.createNode(parser, match, state); + var image = new Element.withTag("img"); + image.attributes["src"] = element.attributes["href"]; - if (resolved) return node; - if (node == null) return null; - - var imageElement = new Element.withTag("img"); - imageElement.attributes["src"] = node.attributes["href"]; - - if (node.attributes.containsKey("title")) { - imageElement.attributes["title"] = node.attributes["title"]; + if (element.attributes.containsKey("title")) { + image.attributes["title"] = element.attributes["title"]; } - var alt = node.children.map((e) => e is! Text ? '' : e.text).join(" "); - if (alt != "") imageElement.attributes["alt"] = alt; + var alt = element.children.map((e) => e is! Text ? "" : e.text).join(" "); + if (alt != "") image.attributes["alt"] = alt; - node.children + element.children ..clear() - ..add(imageElement); + ..add(image); - return node; + return element; } }
diff --git a/pkgs/markdown/pubspec.yaml b/pkgs/markdown/pubspec.yaml index d50c16c..5861600 100644 --- a/pkgs/markdown/pubspec.yaml +++ b/pkgs/markdown/pubspec.yaml
@@ -1,5 +1,5 @@ name: markdown -version: 0.7.3-dev +version: 0.8.0-dev author: Dart Team <misc@dartlang.org> description: A library for converting markdown to HTML. homepage: https://github.com/dart-lang/markdown
diff --git a/pkgs/markdown/test/markdown_test.dart b/pkgs/markdown/test/markdown_test.dart index 7e270a1..4facb3b 100644 --- a/pkgs/markdown/test/markdown_test.dart +++ b/pkgs/markdown/test/markdown_test.dart
@@ -1282,7 +1282,7 @@ }); group('Resolver', () { - nyanResolver(text) => new Text('~=[,,_${text}_,,]:3'); + Node nyanResolver(String text) => new Text('~=[,,_${text}_,,]:3'); validate( 'simple link resolver', @@ -1315,8 +1315,6 @@ }); group('Custom inline syntax', () { - var nyanSyntax = [new TextSyntax('nyan', sub: '~=[,,_,,]:3')]; - validate( 'simple inline syntax', ''' @@ -1325,7 +1323,7 @@ ''' <p>~=[,,_,,]:3</p> ''', - inlineSyntaxes: nyanSyntax); + inlineSyntaxes: [new TextSyntax('nyan', sub: '~=[,,_,,]:3')]); validate('dart custom links', 'links [are<foo>] awesome', '<p>links <a>are<foo></a> awesome</p>',