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

master
Ika 2018-11-09 23:26:59 +08:00 committed by GitHub
parent 6f6b634948
commit de11f69889
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 273 additions and 14 deletions

View File

@ -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 "<!";
@ -800,6 +804,9 @@ function printClosingTagStartMarker(node) {
}
function printClosingTagEndMarker(node) {
if (shouldNotPrintClosingTag(node)) {
return "";
}
switch (node.type) {
case "comment":
return "-->";

View File

@ -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 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) &&
@ -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
};

View File

@ -70,6 +70,27 @@ exports[`conditional.html - html-verify 1`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE html>
<html>
@ -82,6 +103,27 @@ exports[`conditional.html - html-verify 1`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
`;
exports[`conditional.html - html-verify 2`] = `
@ -97,6 +139,27 @@ exports[`conditional.html - html-verify 2`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
~
<!DOCTYPE html>
<html>
@ -134,6 +197,33 @@ exports[`conditional.html - html-verify 2`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html
lang="zh-CN"
>
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html
lang="zh-CN"
>
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html
lang="zh-CN"
>
<head></head>
<body></body>
</html>
`;
exports[`conditional.html - html-verify 3`] = `
@ -149,6 +239,27 @@ exports[`conditional.html - html-verify 3`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~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CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
`;
exports[`conditional.html - html-verify 4`] = `
@ -176,6 +308,27 @@ exports[`conditional.html - html-verify 4`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE html>
<html>
@ -188,6 +341,27 @@ exports[`conditional.html - html-verify 4`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
`;
exports[`conditional.html - html-verify 5`] = `
@ -203,6 +377,27 @@ exports[`conditional.html - html-verify 5`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE html>
<html>
@ -230,6 +425,27 @@ exports[`conditional.html - html-verify 5`] = `
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
`;
exports[`for_debugging.html - html-verify 1`] = `

View File

@ -10,3 +10,24 @@
</body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>
<!DOCTYPE html>
<!--[if lt IE 9]><html lang="zh-CN"><div></div><![endif]-->
<html lang="zh-CN">
<head></head>
<body></body>
</html>