Repository: Polymer/vulcanize (js)

Issue:
Imports inside of templates are incorrectly processed
Given a construction of
```html
<template>
<link rel="import" href="foo.html">
</template>
```
Vulcanize will incorrectly inline or remove `foo.html`.

Pull request description:
Do not inline imports in templates
Fixes #353

Patch:
diff --git a/lib/vulcan.js b/lib/vulcan.js
index e5e74b7..d335032 100644
--- a/lib/vulcan.js
+++ b/lib/vulcan.js
@@ -196,6 +196,16 @@ Vulcan.prototype = {
     return false;
   },
 
+  isTemplated: function(node) {
+    while(node) {
+      if (dom5.isDocumentFragment(node)) {
+        return true;
+      }
+      node = node.parentNode;
+    }
+    return false;
+  },
+
   flatten: function flatten(tree, isMainDoc) {
     var doc = tree.html.ast;
     var imports = tree.imports;
@@ -233,6 +243,9 @@ Vulcan.prototype = {
         if (this.isExcludedImport(im)) {
           continue;
         }
+        if (this.isTemplated(thisImport)) {
+          continue;
+        }
         var bodyFragment = dom5.constructors.fragment();
         var importDoc = this.flatten(im);
         // rewrite urls


Test patch:
diff --git a/test/html/inside-template.html b/test/html/inside-template.html
new file mode 100644
index 0000000..9462a8a
--- /dev/null
+++ b/test/html/inside-template.html
@@ -0,0 +1,13 @@
+<!doctype html>
+<!--
+    @license
+    Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
+    This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
+    The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
+    The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
+    Code distributed by Google as part of the polymer project is also
+    subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
+-->
+<template>
+  <link rel="import" href="external.html">
+</template>
diff --git a/test/test.js b/test/test.js
index 3d09858..073c552 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1065,5 +1065,28 @@ suite('Vulcan', function() {
         done();
       }, {inlineScripts: true});
     });
+
+    test('Imports in templates should not inline', function(done) {
+      process('test/html/inside-template.html', function(err, doc) {
+        var importMatcher = preds.AND(
+          preds.hasTagName('link'),
+          preds.hasAttrValue('rel', 'import'),
+          preds.hasAttr('href')
+        );
+        var externalScriptMatcher = preds.AND(
+          preds.hasTagName('script'),
+          preds.hasAttrValue('src', 'external/external.js')
+        );
+        if (err) {
+          return done(err);
+        }
+        assert(doc);
+        var imports = dom5.queryAll(doc, importMatcher);
+        assert.equal(imports.length, 1, 'import in template was inlined');
+        var unexpectedScript = dom5.query(doc, externalScriptMatcher);
+        assert.equal(unexpectedScript, null, 'script in external.html should not be present');
+        done();
+      });
+    });
   });
 });