Repository: Polymer/vulcanize (js)

Issue:
@license comments missing from imports
Because the inlining only copies the contents of `<head>` and `<body>`, comments with `@license` after the doctype will be missing in the output.

Pull request description:
Make sure all @license comments are maintained when flattening
Fixes #308

Patch:
diff --git a/lib/vulcan.js b/lib/vulcan.js
index 44ed8c5..a5bb163 100644
--- a/lib/vulcan.js
+++ b/lib/vulcan.js
@@ -183,6 +183,13 @@ Vulcan.prototype = {
     dom5.remove(importNode);
   },
 
+  isLicenseComment: function(node) {
+    if (dom5.isCommentNode(node)) {
+      return dom5.getTextContent(node).indexOf('@license') > -1;
+    }
+    return false;
+  },
+
   flatten: function flatten(tree, bodyFragment, mainDocUrl) {
     var doc = tree.html.ast;
     var imports = tree.imports;
@@ -219,6 +226,14 @@ Vulcan.prototype = {
         // merge head and body tags for imports into main document
         var importHeadChildren = importHead.childNodes;
         var importBodyChildren = importBody.childNodes;
+        // make sure @license comments from import document make it into head
+        // check top level nodes
+        var licenseComments = importDoc.childNodes.filter(this.isLicenseComment);
+        var importHtml = importHead.parentNode;
+        // check child nodes of <html>
+        licenseComments = licenseComments.concat(importHtml.childNodes.filter(this.isLicenseComment));
+        // put license comments at the top of <head>
+        importHeadChildren = licenseComments.concat(importHeadChildren);
         // delete link
         this.removeImportAndNewline(thisImport);
         // defer head children to be ordered properly in main document


Test patch:
diff --git a/test/html/comments.html b/test/html/comments.html
index 7d72687..04654a1 100644
--- a/test/html/comments.html
+++ b/test/html/comments.html
@@ -1,5 +1,5 @@
 <!DOCTYPE html>
-<!-- @license test -->
+<!-- @license main -->
 <html lang="en">
 <head>
   <meta charset="UTF-8">
diff --git a/test/html/imports/comment-in-import.html b/test/html/imports/comment-in-import.html
index f6db123..026cba5 100644
--- a/test/html/imports/comment-in-import.html
+++ b/test/html/imports/comment-in-import.html
@@ -1,7 +1,7 @@
-<!-- @license test -->
+<!-- @license import -->
 <link rel="import" href="sub/second.html">
 <!-- comment in import -->
 <div>
-  <!-- @license test -->
+  <!-- @license import -->
   <!-- comment in import -->
 </div>
diff --git a/test/test.js b/test/test.js
index f3bb8ec..643327a 100644
--- a/test/test.js
+++ b/test/test.js
@@ -696,9 +696,15 @@ suite('Vulcan', function() {
           return done(err);
         }
         var comments = dom5.nodeWalkAll(doc, dom5.isCommentNode);
-        assert.equal(comments.length, 1);
-        var commentActual = dom5.getTextContent(comments[0]).trim();
-        assert.equal('@license test', commentActual);
+        assert.equal(comments.length, 2);
+        var commentsExpected = [
+          '@license import',
+          '@license main'
+        ];
+        var commentsActual = comments.map(function(c) {
+          return dom5.getTextContent(c).trim();
+        });
+        assert.deepEqual(commentsExpected, commentsActual);
         done();
       };
       process('test/html/comments.html', callback, options);
@@ -713,11 +719,11 @@ suite('Vulcan', function() {
           return done(err);
         }
         var comments = dom5.nodeWalkAll(doc, dom5.isCommentNode);
-        assert.equal(comments.length, 5);
         var expectedComments = [
-          '@license test',
+          '@license main',
+          '@license import',
           'comment in import',
-          '@license test',
+          '@license import',
           'comment in import',
           'comment in main'
         ];