Merge pull request dart-lang/markdown#85 from mehaase/images

Fixes dart-lang/markdowndart-lang/markdown#83.
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 `![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>