From b132530eca6eab76e24332eb2895ad66a013e5b9 Mon Sep 17 00:00:00 2001 From: Ika Date: Fri, 9 Nov 2018 19:56:45 +0800 Subject: [PATCH 1/3] test: add tests --- .../__snapshots__/jsfmt.spec.js.snap | 256 ++++++++++++++++++ tests/html_comments/conditional.html | 45 ++- 2 files changed, 289 insertions(+), 12 deletions(-) diff --git a/tests/html_comments/__snapshots__/jsfmt.spec.js.snap b/tests/html_comments/__snapshots__/jsfmt.spec.js.snap index 971df3a63ee8..ae9a8e9b41ad 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,33 @@ exports[`conditional.html - html-verify 1`] = ` + + + + + + + + + + + + + + + + + + + + + `; exports[`conditional.html - html-verify 2`] = ` @@ -97,6 +145,27 @@ exports[`conditional.html - html-verify 2`] = ` + + + + + + + + + + + + + + + + + + + + + ~ @@ -134,6 +203,47 @@ exports[`conditional.html - html-verify 2`] = ` + + + + + + + + + + + + + + + + + + + + + `; exports[`conditional.html - html-verify 3`] = ` @@ -149,6 +259,27 @@ exports[`conditional.html - html-verify 3`] = ` + + + + + + + + + + + + + + + + + + + + + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -161,6 +292,33 @@ exports[`conditional.html - html-verify 3`] = ` + + + + + + + + + + + + + + + + + + + + + `; exports[`conditional.html - html-verify 4`] = ` @@ -176,6 +334,27 @@ exports[`conditional.html - html-verify 4`] = ` + + + + + + + + + + + + + + + + + + + + + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -188,6 +367,33 @@ exports[`conditional.html - html-verify 4`] = ` + + + + + + + + + + + + + + + + + + + + + `; exports[`conditional.html - html-verify 5`] = ` @@ -203,6 +409,27 @@ exports[`conditional.html - html-verify 5`] = ` + + + + + + + + + + + + + + + + + + + + + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -230,6 +457,35 @@ 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 35394ad60d26..7df8fbf393dd 100644 --- a/tests/html_comments/conditional.html +++ b/tests/html_comments/conditional.html @@ -1,12 +1,33 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 7ebabc1ad7fe347cf1b43d8c1ae5ec2f61817c35 Mon Sep 17 00:00:00 2001 From: Ika Date: Fri, 9 Nov 2018 20:04:24 +0800 Subject: [PATCH 2/3] fix(html): preserve unterminated ie conditional comments --- src/language-html/printer-html.js | 13 +++- src/language-html/utils.js | 37 +++++++--- .../__snapshots__/jsfmt.spec.js.snap | 70 ++++--------------- 3 files changed, 51 insertions(+), 69 deletions(-) diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 014f5f9ea555..a38313f5bef0 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 1561a3dd2f0f..ed3fe227e918 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 ae9a8e9b41ad..d34082746ecf 100644
--- a/tests/html_comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/html_comments/__snapshots__/jsfmt.spec.js.snap
@@ -104,27 +104,21 @@ exports[`conditional.html - html-verify 1`] = `
 
 
 
-
+
 
   
   
 
 
 
-
+
 
   
   
 
 
 
-
+
 
   
   
@@ -204,11 +198,7 @@ exports[`conditional.html - html-verify 2`] = `
 
 
 
-
+
 
@@ -217,12 +207,7 @@ exports[`conditional.html - html-verify 2`] = `
 
 
 
-
+
 
@@ -231,12 +216,7 @@ exports[`conditional.html - html-verify 2`] = `
 
 
 
-
+
 
@@ -293,27 +273,21 @@ exports[`conditional.html - html-verify 3`] = `
 
 
 
-
+
 
   
   
 
 
 
-
+
 
   
   
 
 
 
-
+
 
   
   
@@ -368,27 +342,21 @@ exports[`conditional.html - html-verify 4`] = `
 
 
 
-
+
 
   
   
 
 
 
-
+
 
   
   
 
 
 
-
+
 
   
   
@@ -458,29 +426,21 @@ exports[`conditional.html - html-verify 5`] = `
 
 
 
-
+
 
   
   
 
 
 
-
+
 
   
   
 
 
 
-
+
 
   
   

From 6e4050934cfe5b8c14c14d7349ee9475977b2137 Mon Sep 17 00:00:00 2001
From: Ika 
Date: Fri, 9 Nov 2018 21:40:10 +0800
Subject: [PATCH 3/3] chore: update tests

---
 tests/html_comments/conditional.html | 66 ++++++++++++++--------------
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/tests/html_comments/conditional.html b/tests/html_comments/conditional.html
index 7df8fbf393dd..a4e18e171ab8 100644
--- a/tests/html_comments/conditional.html
+++ b/tests/html_comments/conditional.html
@@ -1,33 +1,33 @@
-
-
-  
-
-    
-    
-    
-    
-    
-
-  
-
-
-
-
-
-  
-  
-
-
-
-
-
-  
-  
-
-
-
-
-
-  
-  
-
+
+
+  
+
+    
+    
+    
+    
+    
+
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+
+
+
+
+
+  
+  
+