2018-09-19 18:07:27 +03:00
|
|
|
"use strict";
|
|
|
|
|
2018-09-22 16:53:38 +03:00
|
|
|
const htmlTagNames = require("html-tag-names");
|
|
|
|
|
|
|
|
const HTML_TAGS = arrayToMap(htmlTagNames);
|
|
|
|
|
|
|
|
// NOTE: must be same as the one in htmlparser2 so that the parsing won't be inconsistent
|
|
|
|
// https://github.com/fb55/htmlparser2/blob/v3.9.2/lib/Parser.js#L59-L91
|
|
|
|
const VOID_TAGS = arrayToMap([
|
2018-09-19 18:07:27 +03:00
|
|
|
"area",
|
|
|
|
"base",
|
2018-09-22 16:53:38 +03:00
|
|
|
"basefont",
|
2018-09-19 18:07:27 +03:00
|
|
|
"br",
|
|
|
|
"col",
|
2018-09-22 16:53:38 +03:00
|
|
|
"command",
|
2018-09-19 18:07:27 +03:00
|
|
|
"embed",
|
2018-09-22 16:53:38 +03:00
|
|
|
"frame",
|
2018-09-19 18:07:27 +03:00
|
|
|
"hr",
|
|
|
|
"img",
|
|
|
|
"input",
|
2018-09-22 16:53:38 +03:00
|
|
|
"isindex",
|
|
|
|
"keygen",
|
2018-09-19 18:07:27 +03:00
|
|
|
"link",
|
|
|
|
"meta",
|
|
|
|
"param",
|
|
|
|
"source",
|
|
|
|
"track",
|
2018-09-22 16:53:38 +03:00
|
|
|
"wbr",
|
|
|
|
|
|
|
|
"path",
|
|
|
|
"circle",
|
|
|
|
"ellipse",
|
|
|
|
"line",
|
|
|
|
"rect",
|
|
|
|
"use",
|
|
|
|
"stop",
|
|
|
|
"polyline",
|
|
|
|
"polygon"
|
|
|
|
]);
|
|
|
|
|
|
|
|
function arrayToMap(array) {
|
|
|
|
const map = Object.create(null);
|
|
|
|
for (const value of array) {
|
|
|
|
map[value] = true;
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
2018-09-19 18:07:27 +03:00
|
|
|
|
|
|
|
function hasPrettierIgnore(path) {
|
|
|
|
const node = path.getValue();
|
|
|
|
|
|
|
|
if (isWhitespaceOnlyText(node) || node.type === "attribute") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentNode = path.getParentNode();
|
|
|
|
|
|
|
|
if (!parentNode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const index = path.getName();
|
|
|
|
|
|
|
|
if (typeof index !== "number" || index === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prevNode = parentNode.children[index - 1];
|
|
|
|
|
|
|
|
if (isPrettierIgnore(prevNode)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isWhitespaceOnlyText(prevNode)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prevPrevNode = parentNode.children[index - 2];
|
|
|
|
return prevPrevNode && isPrettierIgnore(prevPrevNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPrettierIgnore(node) {
|
|
|
|
return node.type === "comment" && node.data.trim() === "prettier-ignore";
|
|
|
|
}
|
|
|
|
|
|
|
|
function isWhitespaceOnlyText(node) {
|
|
|
|
return node.type === "text" && node.data.trim().length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPreTagNode(node) {
|
|
|
|
return node.type === "tag" && node.name === "pre";
|
|
|
|
}
|
|
|
|
|
|
|
|
function isTextAreaTagNode(node) {
|
|
|
|
return node.type === "tag" && node.name === "textarea";
|
|
|
|
}
|
|
|
|
|
|
|
|
function isScriptTagNode(node) {
|
|
|
|
return node.type === "script" || node.type === "style";
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-09-22 16:53:38 +03:00
|
|
|
HTML_TAGS,
|
|
|
|
VOID_TAGS,
|
2018-09-19 18:07:27 +03:00
|
|
|
hasPrettierIgnore,
|
|
|
|
isPreTagNode,
|
|
|
|
isScriptTagNode,
|
|
|
|
isTextAreaTagNode,
|
2018-09-22 16:53:38 +03:00
|
|
|
isWhitespaceOnlyText
|
2018-09-19 18:07:27 +03:00
|
|
|
};
|