Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): preserve unterminated ie conditional comments #5424

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
};