Fixes dart-lang/markdowndart-lang/markdown#83. An inline image is now rendered correctly, sans anchor element, and not emitting a close tag for the image element. Unit tests have been updated to match correct rendering. An inline image may also be placed inside the content of a link, and this case is also rendered correctly. I added three new unit tests for this. This *would* boost the CommonMark test for images from 1/22 to 9/22 and the test for links from 33/79 to 53/79, except the CommonMark tests are sensitive to attribute order. (See separate pull request on dart-lang/markdowndart-lang/markdown#51.)
diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart index a008ca8..9be8704 100644 --- a/pkgs/markdown/lib/src/inline_parser.dart +++ b/pkgs/markdown/lib/src/inline_parser.dart
@@ -15,7 +15,7 @@ new List<InlineSyntax>.unmodifiable(<InlineSyntax>[ new AutolinkSyntax(), new LinkSyntax(), - new ImageLinkSyntax(), + new ImageSyntax(), // Allow any punctuation to be escaped. new EscapeSyntax(), // "*" surrounded by spaces is left alone. @@ -85,7 +85,7 @@ // Custom link resolvers go after the generic text syntax. syntaxes.insertAll(1, [ new LinkSyntax(linkResolver: document.linkResolver), - new ImageLinkSyntax(linkResolver: document.imageLinkResolver) + new ImageSyntax(linkResolver: document.imageLinkResolver) ]); } @@ -284,8 +284,8 @@ /// breaks it into pieces. static get linkPattern { var refLink = r'\[([^\]]*)\]'; // `[id]` reflink id. - var title = r'(?:\s*"([^"]+)"|)'; // Optional title in quotes. - var inlineLink = '\\((\\S*)$title\\)'; // `(url "title")` link. + var title = r'(?:\s*"([^"]+?)"|)'; // Optional title in quotes. + var inlineLink = '\\((\\S*?)$title\\)'; // `(url "title")` link. return '\](?:($refLink|$inlineLink)|)'; // The groups matched by this are: @@ -375,30 +375,24 @@ /// Matches images like `` and /// `![alternate text][url reference]`. -class ImageLinkSyntax extends LinkSyntax { - ImageLinkSyntax({Resolver linkResolver}) +class ImageSyntax extends LinkSyntax { + ImageSyntax({Resolver linkResolver}) : super(linkResolver: linkResolver, pattern: r'!\['); - /// Creates an <a> element from the given complete [match]. + /// Creates an <img> 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; + var link = getLink(parser, match, state); + var image = new Element.empty("img"); + image.attributes["src"] = escapeHtml(link.url); - var image = new Element.withTag("img"); - image.attributes["src"] = element.attributes["href"]; - - if (element.attributes.containsKey("title")) { - image.attributes["title"] = element.attributes["title"]; + if (link.title != null) { + image.attributes["title"] = escapeHtml(link.title);; } - var alt = element.children.map((e) => e is! Text ? "" : e.text).join(" "); + var alt = state.children.map((e) => e is! Text ? "" : e.text).join(" "); if (alt != "") image.attributes["alt"] = alt; - element.children - ..clear() - ..add(image); - - return element; + return image; } }
diff --git a/pkgs/markdown/test/original/inline_images.unit b/pkgs/markdown/test/original/inline_images.unit index a17a8e2..45316b6 100644 --- a/pkgs/markdown/test/original/inline_images.unit +++ b/pkgs/markdown/test/original/inline_images.unit
@@ -2,19 +2,19 @@  <<< -<p><a href="http://foo.com/foo.png"><img src="http://foo.com/foo.png"></img></a></p> +<p><img src="http://foo.com/foo.png" /></p> >>> alternate text  <<< -<p><a href="http://foo.com/foo.png"><img alt="alternate text" src="http://foo.com/foo.png"></img></a></p> +<p><img alt="alternate text" src="http://foo.com/foo.png" /></p> >>> title  <<< -<p><a href="http://foo.com/foo.png" title="optional title"><img src="http://foo.com/foo.png" title="optional title"></img></a></p> +<p><img src="http://foo.com/foo.png" title="optional title" /></p> >>> invalid alt text  <<< -<p><a href="http://foo.com/foo.png"><img src="http://foo.com/foo.png"></img></a></p> +<p><img src="http://foo.com/foo.png" /></p>
diff --git a/pkgs/markdown/test/original/inline_links.unit b/pkgs/markdown/test/original/inline_links.unit index d6e6d77..a63a26e 100644 --- a/pkgs/markdown/test/original/inline_links.unit +++ b/pkgs/markdown/test/original/inline_links.unit
@@ -13,6 +13,21 @@ <<< <p>links <a href="http://foo.com"><em>are</em></a> awesome</p> +>>> image inside link +links [](http://foo.com) awesome + +<<< +<p>links <a href="http://foo.com"><img src="/are.png" /></a> awesome</p> +>>> image with alt inside link +links [](http://foo.com) awesome + +<<< +<p>links <a href="http://foo.com"><img alt="my alt" src="/are.png" /></a> awesome</p> +>>> image with title inside link +links [](http://foo.com) awesome + +<<< +<p>links <a href="http://foo.com"><img src="/are.png" title="my title" /></a> awesome</p> >>> no URL links [are]() awesome
diff --git a/pkgs/markdown/test/original/reference_images.unit b/pkgs/markdown/test/original/reference_images.unit index 84d1820..4f00072 100644 --- a/pkgs/markdown/test/original/reference_images.unit +++ b/pkgs/markdown/test/original/reference_images.unit
@@ -3,22 +3,22 @@ [foo]: http://foo.com/foo.png <<< -<p><a href="http://foo.com/foo.png"><img src="http://foo.com/foo.png"></img></a></p> +<p><img src="http://foo.com/foo.png" /></p> >>> alternate text ![alternate text][foo] [foo]: http://foo.com/foo.png <<< -<p><a href="http://foo.com/foo.png"><img alt="alternate text" src="http://foo.com/foo.png"></img></a></p> +<p><img alt="alternate text" src="http://foo.com/foo.png" /></p> >>> title ![][foo] [foo]: http://foo.com/foo.png "optional title" <<< -<p><a href="http://foo.com/foo.png" title="optional title"><img src="http://foo.com/foo.png" title="optional title"></img></a></p> +<p><img src="http://foo.com/foo.png" title="optional title" /></p> >>> invalid alt text ![`alt`][foo] [foo]: http://foo.com/foo.png "optional title" <<< -<p><a href="http://foo.com/foo.png" title="optional title"><img src="http://foo.com/foo.png" title="optional title"></img></a></p> +<p><img src="http://foo.com/foo.png" title="optional title" /></p>