Merge pull request dart-lang/markdown#87 from mehaase/linebreaks

Fixes dart-lang/markdowndart-lang/markdown#86.
diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md
index 7af705f..924535f 100644
--- a/pkgs/markdown/CHANGELOG.md
+++ b/pkgs/markdown/CHANGELOG.md
@@ -3,6 +3,10 @@
 * BREAKING: Now following the CommonMark spec for fenced code blocks.
   If a language (info string) is provided, it is added as a class to the `code`
   element with a `language-` prefix.
+* BREAKING: Now following the CommonMark spec for images. Previously,
+  `![text](img.png)` would compile too
+  `<a href="img.prg"><img src="img.prg" alt="text"></img></a>`. That same code
+  will now compile to `<img src="img.png" alt="text" />`.
 
 ## 0.9.0
 
diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart
index 52ee24e..6a70cc1 100644
--- a/pkgs/markdown/lib/src/inline_parser.dart
+++ b/pkgs/markdown/lib/src/inline_parser.dart
@@ -16,7 +16,7 @@
     new AutolinkSyntax(),
     new LineBreakSyntax(),
     new LinkSyntax(),
-    new ImageLinkSyntax(),
+    new ImageSyntax(),
     // Allow any punctuation to be escaped.
     new EscapeSyntax(),
     // "*" surrounded by spaces is left alone.
@@ -82,7 +82,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)
     ]);
   }
 
@@ -292,8 +292,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:
@@ -383,30 +383,24 @@
 
 /// Matches images like `![alternate text](url "optional title")` 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 @@
 ![](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](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
 ![](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`](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>
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 [![](/are.png)](http://foo.com) awesome
+
+<<<
+<p>links <a href="http://foo.com"><img src="/are.png" /></a> awesome</p>
+>>> image with alt inside link
+links [![my alt](/are.png)](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 [![](/are.png "my title")](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>