Skip to content

Commit

Permalink
fix(html): preserve unterminated ie conditional comments (#5424)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 9, 2018
1 parent 6f6b634 commit de11f69
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 14 deletions.
13 changes: 10 additions & 3 deletions src/language-html/printer-html.js
Expand Up @@ -35,7 +35,8 @@ const {
preferHardlineAsLeadingSpaces,
replaceDocNewlines,
replaceNewlines,
shouldPreserveElementContent
shouldNotPrintClosingTag,
shouldPreserveContent
} = require("./utils");
const preprocess = require("./preprocess");
const assert = require("assert");
Expand Down Expand Up @@ -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),
Expand All @@ -468,7 +469,7 @@ function printChildren(path, options, print) {
);
}

if (shouldPreserveElementContent(childPath)) {
if (shouldPreserveContent(child)) {
return concat(
[].concat(
printOpeningTagPrefix(child),
Expand Down Expand Up @@ -791,6 +792,9 @@ function printOpeningTagEndMarker(node) {

function printClosingTagStartMarker(node) {
assert(!node.isSelfClosing);
if (shouldNotPrintClosingTag(node)) {
return "";
}
switch (node.type) {
case "ieConditionalComment":
return "<!";
Expand All @@ -800,6 +804,9 @@ function printClosingTagStartMarker(node) {
}

function printClosingTagEndMarker(node) {
if (shouldNotPrintClosingTag(node)) {
return "";
}
switch (node.type) {
case "comment":
return "-->";
Expand Down
37 changes: 26 additions & 11 deletions src/language-html/utils.js
Expand Up @@ -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" &&
Expand All @@ -46,6 +44,17 @@ function shouldPreserveElementContent(path) {
return true;
}

// unterminated node in ie conditional comment
// e.g. <!--[if lt IE 9]><html><![endif]-->
if (
node.type === "ieConditionalComment" &&
node.lastChild &&
!node.lastChild.isSelfClosing &&
!node.lastChild.endSourceSpan
) {
return true;
}

// TODO: handle non-text children in <pre>
if (
isPreLikeNode(node) &&
Expand All @@ -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);
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -630,5 +644,6 @@ module.exports = {
preferHardlineAsTrailingSpaces,
replaceDocNewlines,
replaceNewlines,
shouldPreserveElementContent
shouldNotPrintClosingTag,
shouldPreserveContent
};

0 comments on commit de11f69

Please sign in to comment.