Files
test-repo/src/language-html/utilities/index.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

663 lines
16 KiB
JavaScript
Raw Normal View History

/**
2024-10-01 19:45:37 +08:00
* @import AstPath from "../../common/ast-path.js"
*/
2025-11-03 01:52:26 +08:00
import {
hardline,
join,
line,
replaceEndOfLine,
} from "../../document/index.js";
2025-10-30 22:16:54 +08:00
import { isFrontMatter } from "../../main/front-matter/index.js";
2025-12-01 07:25:25 +08:00
import htmlWhitespace from "../../utilities/html-whitespace.js";
import inferParser from "../../utilities/infer-parser.js";
import {
CSS_DISPLAY_DEFAULT,
CSS_DISPLAY_TAGS,
CSS_WHITE_SPACE_DEFAULT,
CSS_WHITE_SPACE_TAGS,
} from "../constants.evaluate.js";
2022-03-13 17:04:06 +08:00
import isUnknownNamespace from "./is-unknown-namespace.js";
import { hasOrInNgNonBindable } from "./ng-non-bindable.js";
2022-02-21 16:44:56 +08:00
const htmlTrimLeadingBlankLines = (string) =>
string.replaceAll(/^[\t\f\r ]*\n/g, "");
const htmlTrimPreserveIndentation = (string) =>
2025-12-01 07:25:25 +08:00
htmlTrimLeadingBlankLines(htmlWhitespace.trimEnd(string));
const getLeadingAndTrailingHtmlWhitespace = (string) => {
2023-02-24 15:41:20 +08:00
let text = string;
2025-12-01 07:25:25 +08:00
const leadingWhitespace = htmlWhitespace.getLeadingWhitespace(text);
2023-02-24 15:41:20 +08:00
if (leadingWhitespace) {
text = text.slice(leadingWhitespace.length);
}
2025-12-01 07:25:25 +08:00
const trailingWhitespace = htmlWhitespace.getTrailingWhitespace(text);
2023-02-24 15:41:20 +08:00
if (trailingWhitespace) {
text = text.slice(0, -trailingWhitespace.length);
}
return {
leadingWhitespace,
trailingWhitespace,
text,
};
};
2018-11-23 12:33:48 +08:00
function shouldPreserveContent(node, options) {
// unterminated node in ie conditional comment
// e.g. <!--[if lt IE 9]><html><![endif]-->
if (
2025-10-11 23:23:34 +08:00
node.kind === "ieConditionalComment" &&
node.lastChild &&
!node.lastChild.isSelfClosing &&
!node.lastChild.endSourceSpan
) {
return true;
}
2026-06-16 02:09:45 +08:00
// incomplete HTML in ie conditional comment
// e.g. <!--[if lt IE 9]></div><![endif]-->
2025-10-11 23:23:34 +08:00
if (node.kind === "ieConditionalComment" && !node.complete) {
return true;
}
2018-11-04 23:03:07 +08:00
// TODO: handle non-text children in <pre>
if (
isPreLikeNode(node) &&
node.children.some(
2025-10-11 23:23:34 +08:00
(child) => child.kind !== "text" && child.kind !== "interpolation",
)
2018-11-04 23:03:07 +08:00
) {
return true;
}
2020-04-17 07:54:24 +09:00
if (
2020-06-17 19:18:29 +08:00
isVueNonHtmlBlock(node, options) &&
!isScriptLikeTag(node, options) &&
2025-10-11 23:23:34 +08:00
node.kind !== "interpolation"
2020-04-17 07:54:24 +09:00
) {
return true;
}
return false;
}
function hasPrettierIgnore(node) {
2022-10-22 23:33:08 +08:00
/* c8 ignore next 3 */
2025-10-11 23:23:34 +08:00
if (node.kind === "attribute") {
return false;
}
2022-10-22 23:33:08 +08:00
/* c8 ignore next 3 */
if (!node.parent) {
return false;
}
if (!node.prev) {
return false;
}
return isPrettierIgnore(node.prev);
}
function isPrettierIgnore(node) {
2025-10-11 23:23:34 +08:00
return node.kind === "comment" && node.value.trim() === "prettier-ignore";
}
2018-11-23 21:25:58 +08:00
/** there's no opening/closing tag or it's considered not breakable */
function isTextLikeNode(node) {
2025-10-11 23:23:34 +08:00
return node.kind === "text" || node.kind === "comment";
2018-11-23 21:25:58 +08:00
}
function isScriptLikeTag(node, options) {
2018-11-04 23:03:07 +08:00
return (
2025-10-11 23:23:34 +08:00
node.kind === "element" &&
2018-11-04 23:03:07 +08:00
(node.fullName === "script" ||
node.fullName === "style" ||
node.fullName === "svg:style" ||
2023-04-10 10:43:48 +08:00
node.fullName === "svg:script" ||
(node.fullName === "mj-style" && options.parser === "mjml") ||
(isUnknownNamespace(node) &&
(node.name === "script" || node.name === "style")))
2018-11-04 23:03:07 +08:00
);
}
function canHaveInterpolation(node, options) {
if (!node.children || isScriptLikeTag(node, options)) {
return false;
}
if (hasOrInNgNonBindable(node, options)) {
return false;
}
return true;
2018-11-04 23:03:07 +08:00
}
function isWhitespaceSensitiveNode(node, options) {
2018-11-04 23:03:07 +08:00
return (
isScriptLikeTag(node, options) ||
2025-10-11 23:23:34 +08:00
node.kind === "interpolation" ||
2018-11-04 23:03:07 +08:00
isIndentationSensitiveNode(node)
);
}
function isIndentationSensitiveNode(node) {
return getNodeCssStyleWhiteSpace(node).startsWith("pre");
}
2020-04-17 07:54:24 +09:00
function isLeadingSpaceSensitiveNode(node, options) {
const isLeadingSpaceSensitive = _isLeadingSpaceSensitiveNode();
if (
isLeadingSpaceSensitive &&
2018-11-04 23:03:07 +08:00
!node.prev &&
node.parent?.tagDefinition?.ignoreFirstLf
) {
2025-10-11 23:23:34 +08:00
return node.kind === "interpolation";
}
return isLeadingSpaceSensitive;
function _isLeadingSpaceSensitiveNode() {
2025-10-11 23:23:34 +08:00
if (isFrontMatter(node) || node.kind === "angularControlFlowBlock") {
return false;
}
if (
2025-10-11 23:23:34 +08:00
(node.kind === "text" || node.kind === "interpolation") &&
node.prev &&
2025-10-11 23:23:34 +08:00
(node.prev.kind === "text" || node.prev.kind === "interpolation")
) {
return true;
}
if (!node.parent || node.parent.cssDisplay === "none") {
return false;
}
if (isPreLikeNode(node.parent)) {
return true;
}
if (
!node.prev &&
2025-10-11 23:23:34 +08:00
(node.parent.kind === "root" ||
(isPreLikeNode(node) && node.parent) ||
isScriptLikeTag(node.parent, options) ||
2020-04-17 07:54:24 +09:00
isVueCustomBlock(node.parent, options) ||
!isFirstChildLeadingSpaceSensitiveCssDisplay(node.parent.cssDisplay))
) {
return false;
}
if (
node.prev &&
!isNextLeadingSpaceSensitiveCssDisplay(node.prev.cssDisplay)
) {
return false;
}
2018-11-04 23:03:07 +08:00
return true;
}
}
2020-04-17 07:54:24 +09:00
function isTrailingSpaceSensitiveNode(node, options) {
2025-10-11 23:23:34 +08:00
if (isFrontMatter(node) || node.kind === "angularControlFlowBlock") {
return false;
}
if (
2025-10-11 23:23:34 +08:00
(node.kind === "text" || node.kind === "interpolation") &&
node.next &&
2025-10-11 23:23:34 +08:00
(node.next.kind === "text" || node.next.kind === "interpolation")
) {
return true;
}
2018-11-04 23:03:07 +08:00
if (!node.parent || node.parent.cssDisplay === "none") {
return false;
}
2018-11-04 23:03:07 +08:00
if (isPreLikeNode(node.parent)) {
return true;
}
if (
2018-11-04 23:03:07 +08:00
!node.next &&
2025-10-11 23:23:34 +08:00
(node.parent.kind === "root" ||
(isPreLikeNode(node) && node.parent) ||
isScriptLikeTag(node.parent, options) ||
2020-04-17 07:54:24 +09:00
isVueCustomBlock(node.parent, options) ||
2018-11-04 23:03:07 +08:00
!isLastChildTrailingSpaceSensitiveCssDisplay(node.parent.cssDisplay))
) {
return false;
}
2018-11-04 23:03:07 +08:00
if (
node.next &&
!isPrevTrailingSpaceSensitiveCssDisplay(node.next.cssDisplay)
) {
return false;
}
return true;
}
function isDanglingSpaceSensitiveNode(node, options) {
2018-11-04 23:03:07 +08:00
return (
isDanglingSpaceSensitiveCssDisplay(node.cssDisplay) &&
!isScriptLikeTag(node, options)
2018-11-04 23:03:07 +08:00
);
}
function forceNextEmptyLine(node) {
2018-11-04 23:03:07 +08:00
return (
2023-02-13 10:17:39 +08:00
isFrontMatter(node) ||
2018-11-04 23:03:07 +08:00
(node.next &&
2020-07-14 22:02:32 +08:00
node.sourceSpan.end &&
2018-11-04 23:03:07 +08:00
node.sourceSpan.end.line + 1 < node.next.sourceSpan.start.line)
);
}
/** firstChild leadingSpaces and lastChild trailingSpaces */
function forceBreakContent(node) {
return (
forceBreakChildren(node) ||
2025-10-11 23:23:34 +08:00
(node.kind === "element" &&
node.children.length > 0 &&
2019-11-17 18:06:14 +08:00
(["body", "script", "style"].includes(node.name) ||
node.children.some((child) => hasNonTextChild(child)))) ||
(node.firstChild &&
node.firstChild === node.lastChild &&
2025-10-11 23:23:34 +08:00
node.firstChild.kind !== "text" &&
2019-11-09 01:14:31 +01:00
hasLeadingLineBreak(node.firstChild) &&
(!node.lastChild.isTrailingSpaceSensitive ||
hasTrailingLineBreak(node.lastChild)))
);
}
/** spaces between children */
function forceBreakChildren(node) {
return (
2025-10-11 23:23:34 +08:00
node.kind === "element" &&
node.children.length > 0 &&
2019-11-17 18:06:14 +08:00
(["html", "head", "ul", "ol", "select"].includes(node.name) ||
(node.cssDisplay.startsWith("table") && node.cssDisplay !== "table-cell"))
);
}
function preferHardlineAsLeadingSpaces(node) {
return (
preferHardlineAsSurroundingSpaces(node) ||
2018-11-04 23:03:07 +08:00
(node.prev && preferHardlineAsTrailingSpaces(node.prev)) ||
hasSurroundingLineBreak(node)
);
}
function preferHardlineAsTrailingSpaces(node) {
return (
preferHardlineAsSurroundingSpaces(node) ||
2025-10-11 23:23:34 +08:00
(node.kind === "element" && node.fullName === "br") ||
hasSurroundingLineBreak(node)
);
2018-11-04 23:03:07 +08:00
}
function hasSurroundingLineBreak(node) {
return hasLeadingLineBreak(node) && hasTrailingLineBreak(node);
}
function hasLeadingLineBreak(node) {
return (
node.hasLeadingSpaces &&
(node.prev
? node.prev.sourceSpan.end.line < node.sourceSpan.start.line
2025-10-11 23:23:34 +08:00
: node.parent.kind === "root" ||
2018-11-04 23:03:07 +08:00
node.parent.startSourceSpan.end.line < node.sourceSpan.start.line)
);
}
function hasTrailingLineBreak(node) {
return (
node.hasTrailingSpaces &&
(node.next
? node.next.sourceSpan.start.line > node.sourceSpan.end.line
2025-10-11 23:23:34 +08:00
: node.parent.kind === "root" ||
2020-01-13 22:31:03 +08:00
(node.parent.endSourceSpan &&
node.parent.endSourceSpan.start.line > node.sourceSpan.end.line))
);
}
function preferHardlineAsSurroundingSpaces(node) {
2025-10-11 23:23:34 +08:00
switch (node.kind) {
case "ieConditionalComment":
case "comment":
case "directive":
return true;
2018-11-04 23:03:07 +08:00
case "element":
2019-11-17 18:06:14 +08:00
return ["script", "select"].includes(node.name);
}
return false;
}
function getLastDescendant(node) {
return node.lastChild ? getLastDescendant(node.lastChild) : node;
}
function hasNonTextChild(node) {
2025-10-11 23:23:34 +08:00
return node.children?.some((child) => child.kind !== "text");
}
function inferParserByTypeAttribute(type) {
if (!type) {
return;
2020-04-17 07:54:24 +09:00
}
switch (type) {
case "module":
case "text/javascript":
case "text/babel":
case "text/jsx":
case "application/javascript":
return "babel";
2020-04-17 07:54:24 +09:00
case "application/x-typescript":
return "typescript";
2020-04-17 07:54:24 +09:00
case "text/markdown":
return "markdown";
case "text/html":
return "html";
2020-04-17 07:54:24 +09:00
case "text/x-handlebars-template":
return "glimmer";
2020-04-17 07:54:24 +09:00
default:
if (
type.endsWith("json") ||
type.endsWith("importmap") ||
type === "speculationrules"
) {
return "json";
}
2022-05-01 23:01:24 +08:00
}
2020-04-17 07:54:24 +09:00
}
function inferScriptParser(node, options) {
const { name, attrMap } = node;
if (name !== "script" || Object.hasOwn(attrMap, "src")) {
return;
}
const { type, lang } = node.attrMap;
if (!lang && !type) {
return "babel";
2020-04-17 07:54:24 +09:00
}
2018-11-04 23:03:07 +08:00
return (
inferParser(options, { language: lang }) ?? inferParserByTypeAttribute(type)
);
}
function inferVueSfcBlockParser(node, options) {
if (!isVueNonHtmlBlock(node, options)) {
return;
}
const { attrMap } = node;
if (Object.hasOwn(attrMap, "src")) {
return;
}
const { type, lang } = attrMap;
return (
inferParser(options, { language: lang }) ?? inferParserByTypeAttribute(type)
);
}
function inferStyleParser(node, options) {
if (node.name === "style") {
const { lang } = node.attrMap;
return lang ? inferParser(options, { language: lang }) : "css";
}
if (node.name === "mj-style" && options.parser === "mjml") {
return "css";
}
}
function inferElementParser(node, options) {
return (
inferScriptParser(node, options) ??
inferStyleParser(node, options) ??
inferVueSfcBlockParser(node, options)
);
}
function isBlockLikeCssDisplay(cssDisplay) {
2018-11-04 23:03:07 +08:00
return (
cssDisplay === "block" ||
cssDisplay === "list-item" ||
cssDisplay.startsWith("table")
);
}
2018-11-04 23:03:07 +08:00
function isFirstChildLeadingSpaceSensitiveCssDisplay(cssDisplay) {
return !isBlockLikeCssDisplay(cssDisplay) && cssDisplay !== "inline-block";
}
function isLastChildTrailingSpaceSensitiveCssDisplay(cssDisplay) {
return !isBlockLikeCssDisplay(cssDisplay) && cssDisplay !== "inline-block";
}
function isPrevTrailingSpaceSensitiveCssDisplay(cssDisplay) {
return !isBlockLikeCssDisplay(cssDisplay);
}
function isNextLeadingSpaceSensitiveCssDisplay(cssDisplay) {
return !isBlockLikeCssDisplay(cssDisplay);
}
function isDanglingSpaceSensitiveCssDisplay(cssDisplay) {
return !isBlockLikeCssDisplay(cssDisplay) && cssDisplay !== "inline-block";
}
function isPreLikeNode(node) {
return getNodeCssStyleWhiteSpace(node).startsWith("pre");
}
function hasParent(node, fn) {
let current = node;
while (current) {
if (fn(current)) {
return true;
}
current = current.parent;
}
2018-11-04 23:03:07 +08:00
return false;
}
function getNodeCssStyleDisplay(node, options) {
2026-06-16 02:09:45 +08:00
// Every root block in Vue.js SFC is a block
if (isVueSfcBlock(node, options)) {
return "block";
}
2025-10-11 23:23:34 +08:00
if (node.prev?.kind === "comment") {
// <!-- display: block -->
const match = node.prev.value.match(/^\s*display:\s*([a-z]+)\s*$/);
if (match) {
return match[1];
}
}
2018-11-04 23:03:07 +08:00
let isInSvgForeignObject = false;
2025-10-11 23:23:34 +08:00
if (node.kind === "element" && node.namespace === "svg") {
2018-11-04 23:03:07 +08:00
if (hasParent(node, (parent) => parent.fullName === "svg:foreignObject")) {
isInSvgForeignObject = true;
} else {
return node.name === "svg" ? "inline-block" : "block";
}
}
switch (options.htmlWhitespaceSensitivity) {
case "strict":
return "inline";
case "ignore":
return "block";
default:
if (
2025-10-11 23:23:34 +08:00
node.kind === "element" &&
(!node.namespace || isInSvgForeignObject || isUnknownNamespace(node)) &&
Object.hasOwn(CSS_DISPLAY_TAGS, node.name)
) {
return CSS_DISPLAY_TAGS[node.name];
}
}
return CSS_DISPLAY_DEFAULT;
}
function getNodeCssStyleWhiteSpace(node) {
if (
2025-10-11 23:23:34 +08:00
node.kind === "element" &&
(!node.namespace || isUnknownNamespace(node)) &&
Object.hasOwn(CSS_WHITE_SPACE_TAGS, node.name)
) {
return CSS_WHITE_SPACE_TAGS[node.name];
}
return CSS_WHITE_SPACE_DEFAULT;
}
function unescapeQuoteEntities(text) {
return text.replaceAll("&apos;", "'").replaceAll("&quot;", '"');
}
function getUnescapedAttributeValue(node) {
return unescapeQuoteEntities(node.value);
}
2026-06-16 02:09:45 +08:00
// top-level elements (excluding <template>, <style> and <script>) in Vue.js SFC are considered custom block
// See https://vue-loader.vuejs.org/spec.html for detail
const vueRootElementsSet = new Set(["template", "style", "script"]);
2020-04-17 07:54:24 +09:00
function isVueCustomBlock(node, options) {
2020-06-17 19:18:29 +08:00
return isVueSfcBlock(node, options) && !vueRootElementsSet.has(node.fullName);
}
function isVueSfcBlock(node, options) {
2020-04-17 07:54:24 +09:00
return (
options.parser === "vue" &&
2025-10-11 23:23:34 +08:00
node.kind === "element" &&
node.parent.kind === "root" &&
node.fullName.toLowerCase() !== "html"
2020-04-17 07:54:24 +09:00
);
}
2020-06-17 19:18:29 +08:00
function isVueNonHtmlBlock(node, options) {
return (
isVueSfcBlock(node, options) &&
(isVueCustomBlock(node, options) ||
(node.attrMap.lang && node.attrMap.lang !== "html"))
);
}
2020-11-13 09:05:30 +08:00
function isVueSlotAttribute(attribute) {
const attributeName = attribute.fullName;
return (
attributeName.charAt(0) === "#" ||
attributeName === "slot-scope" ||
attributeName === "v-slot" ||
attributeName.startsWith("v-slot:")
);
}
function isVueSfcBindingsAttribute(attribute, options) {
const element = attribute.parent;
if (!isVueSfcBlock(element, options)) {
return false;
}
const tagName = element.fullName;
const attributeName = attribute.fullName;
return (
// https://github.com/vuejs/rfcs/blob/sfc-improvements/active-rfcs/0000-sfc-script-setup.md
(tagName === "script" && attributeName === "setup") ||
// https://github.com/vuejs/rfcs/blob/sfc-improvements/active-rfcs/0000-sfc-style-variables.md
(tagName === "style" && attributeName === "vars")
);
}
2021-09-08 16:32:20 +09:00
function getTextValueParts(node, value = node.value) {
return node.parent.isWhitespaceSensitive
? node.parent.isIndentationSensitive
? replaceEndOfLine(value)
: replaceEndOfLine(
2025-12-01 07:25:25 +08:00
htmlWhitespace.dedentString(htmlTrimPreserveIndentation(value)),
2021-09-08 16:32:20 +09:00
hardline,
)
2025-12-01 07:25:25 +08:00
: join(line, htmlWhitespace.split(value));
2021-09-08 16:32:20 +09:00
}
function isVueScriptTag(node, options) {
return isVueSfcBlock(node, options) && node.name === "script";
}
function isAttributeValueQuoted(node) {
const { valueSpan, value } = node;
return valueSpan.end.offset - valueSpan.start.offset === value.length + 2;
}
function shouldUnquoteAttributeValue(node, options) {
if (isAttributeValueQuoted(node)) {
return false;
}
const { value } = node;
return (
// Embedded HTML in JS: `` /* HTML */ `<my-element data-for={value}></my-element>` ``
/^PRETTIER_HTML_PLACEHOLDER_\d+_\d+_IN_JS$/.test(value) ||
// LWC `<div foo={value}></div>`
(options.parser === "lwc" && value.startsWith("{") && value.endsWith("}"))
);
}
2022-02-21 16:44:56 +08:00
export {
2018-11-04 23:03:07 +08:00
canHaveInterpolation,
forceBreakChildren,
forceBreakContent,
forceNextEmptyLine,
getLastDescendant,
getLeadingAndTrailingHtmlWhitespace,
getNodeCssStyleDisplay,
getTextValueParts,
getUnescapedAttributeValue,
hasPrettierIgnore,
htmlTrimPreserveIndentation,
inferElementParser,
isDanglingSpaceSensitiveNode,
2018-11-04 23:03:07 +08:00
isIndentationSensitiveNode,
isLeadingSpaceSensitiveNode,
2018-11-04 23:03:07 +08:00
isPreLikeNode,
isScriptLikeTag,
2018-11-23 21:25:58 +08:00
isTextLikeNode,
isTrailingSpaceSensitiveNode,
isVueCustomBlock,
isVueNonHtmlBlock,
isVueScriptTag,
isVueSfcBindingsAttribute,
isVueSfcBlock,
isVueSlotAttribute,
isWhitespaceSensitiveNode,
preferHardlineAsLeadingSpaces,
shouldPreserveContent,
shouldUnquoteAttributeValue,
unescapeQuoteEntities,
};