From de11f69889048d9a8168d290aee5874855183ff9 Mon Sep 17 00:00:00 2001 From: Ika Date: Fri, 9 Nov 2018 23:26:59 +0800 Subject: [PATCH] fix(html): preserve unterminated ie conditional comments (#5424) --- src/language-html/printer-html.js | 13 +- src/language-html/utils.js | 37 ++- .../__snapshots__/jsfmt.spec.js.snap | 216 ++++++++++++++++++ tests/html_comments/conditional.html | 21 ++ 4 files changed, 273 insertions(+), 14 deletions(-) diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 014f5f9e..a38313f5 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -35,7 +35,8 @@ const { preferHardlineAsLeadingSpaces, replaceDocNewlines, replaceNewlines, - shouldPreserveElementContent + shouldNotPrintClosingTag, + shouldPreserveContent } = require("./utils"); const preprocess = require("./preprocess"); const assert = require("assert"); @@ -445,7 +446,7 @@ function printChildren(path, options, print) { function printChild(childPath) { const child = childPath.getValue(); - if (hasPrettierIgnore(childPath)) { + if (hasPrettierIgnore(child)) { return concat( [].concat( printOpeningTagPrefix(child), @@ -468,7 +469,7 @@ function printChildren(path, options, print) { ); } - if (shouldPreserveElementContent(childPath)) { + if (shouldPreserveContent(child)) { return concat( [].concat( printOpeningTagPrefix(child), @@ -791,6 +792,9 @@ function printOpeningTagEndMarker(node) { function printClosingTagStartMarker(node) { assert(!node.isSelfClosing); + if (shouldNotPrintClosingTag(node)) { + return ""; + } switch (node.type) { case "ieConditionalComment": return ""; diff --git a/src/language-html/utils.js b/src/language-html/utils.js index 1561a3dd..ed3fe227 100644 --- a/src/language-html/utils.js +++ b/src/language-html/utils.js @@ -34,9 +34,7 @@ function mapObject(object, fn) { return newObject; } -function shouldPreserveElementContent(path) { - const node = path.getValue(); - +function shouldPreserveContent(node) { if ( node.type === "element" && node.fullName === "template" && @@ -46,6 +44,17 @@ function shouldPreserveElementContent(path) { return true; } + // unterminated node in ie conditional comment + // e.g. + if ( + node.type === "ieConditionalComment" && + node.lastChild && + !node.lastChild.isSelfClosing && + !node.lastChild.endSourceSpan + ) { + return true; + } + // TODO: handle non-text children in
   if (
     isPreLikeNode(node) &&
@@ -59,23 +68,20 @@ function shouldPreserveElementContent(path) {
   return false;
 }
 
-function hasPrettierIgnore(path) {
-  const node = path.getValue();
+function hasPrettierIgnore(node) {
   if (node.type === "attribute" || node.type === "text") {
     return false;
   }
 
-  const parentNode = path.getParentNode();
-  if (!parentNode) {
+  if (!node.parent) {
     return false;
   }
 
-  const index = path.getName();
-  if (typeof index !== "number" || index === 0) {
+  if (typeof node.index !== "number" || node.index === 0) {
     return false;
   }
 
-  const prevNode = parentNode.children[index - 1];
+  const prevNode = node.parent.children[node.index - 1];
   return isPrettierIgnore(prevNode);
 }
 
@@ -600,6 +606,14 @@ function identity(x) {
   return x;
 }
 
+function shouldNotPrintClosingTag(node) {
+  return (
+    !node.isSelfClosing &&
+    !node.endSourceSpan &&
+    (hasPrettierIgnore(node) || shouldPreserveContent(node.parent))
+  );
+}
+
 module.exports = {
   HTML_ELEMENT_ATTRIBUTES,
   HTML_TAGS,
@@ -630,5 +644,6 @@ module.exports = {
   preferHardlineAsTrailingSpaces,
   replaceDocNewlines,
   replaceNewlines,
-  shouldPreserveElementContent
+  shouldNotPrintClosingTag,
+  shouldPreserveContent
 };
diff --git a/tests/html_comments/__snapshots__/jsfmt.spec.js.snap b/tests/html_comments/__snapshots__/jsfmt.spec.js.snap
index 971df3a6..d3408274 100644
--- a/tests/html_comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/html_comments/__snapshots__/jsfmt.spec.js.snap
@@ -70,6 +70,27 @@ exports[`conditional.html - html-verify 1`] = `
 
   
 
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 
@@ -82,6 +103,27 @@ exports[`conditional.html - html-verify 1`] = `
   
 
 
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
 `;
 
 exports[`conditional.html - html-verify 2`] = `
@@ -97,6 +139,27 @@ exports[`conditional.html - html-verify 2`] = `
 
   
 
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
 ~
 
 
@@ -134,6 +197,33 @@ exports[`conditional.html - html-verify 2`] = `
   
 
 
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
 `;
 
 exports[`conditional.html - html-verify 3`] = `
@@ -149,6 +239,27 @@ exports[`conditional.html - html-verify 3`] = `
 
   
 
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 
@@ -161,6 +272,27 @@ exports[`conditional.html - html-verify 3`] = `
   
 
 
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
 `;
 
 exports[`conditional.html - html-verify 4`] = `
@@ -176,6 +308,27 @@ exports[`conditional.html - html-verify 4`] = `
 
   
 
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 
@@ -188,6 +341,27 @@ exports[`conditional.html - html-verify 4`] = `
   
 
 
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
 `;
 
 exports[`conditional.html - html-verify 5`] = `
@@ -203,6 +377,27 @@ exports[`conditional.html - html-verify 5`] = `
 
   
 
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 
@@ -230,6 +425,27 @@ exports[`conditional.html - html-verify 5`] = `
   
 
 
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
 `;
 
 exports[`for_debugging.html - html-verify 1`] = `
diff --git a/tests/html_comments/conditional.html b/tests/html_comments/conditional.html
index 35394ad6..a4e18e17 100644
--- a/tests/html_comments/conditional.html
+++ b/tests/html_comments/conditional.html
@@ -10,3 +10,24 @@
 
   
 
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+