forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter-html.js
More file actions
145 lines (133 loc) · 4.35 KB
/
printer-html.js
File metadata and controls
145 lines (133 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @typedef {import("../document/builders.js").Doc} Doc
*/
import { fill, group, hardline } from "../document/builders.js";
import { cleanDoc, replaceEndOfLine } from "../document/utils.js";
import getPreferredQuote from "../utils/get-preferred-quote.js";
import htmlWhitespaceUtils from "../utils/html-whitespace-utils.js";
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
import clean from "./clean.js";
import embed from "./embed.js";
import getVisitorKeys from "./get-visitor-keys.js";
import { locEnd, locStart } from "./loc.js";
import { insertPragma } from "./pragma.js";
import {
printAngularControlFlowBlock,
printAngularControlFlowBlockParameters,
} from "./print/angular-control-flow-block.js";
import {
printAngularIcuCase,
printAngularIcuExpression,
} from "./print/angular-icu-expression.js";
import { printChildren } from "./print/children.js";
import { printElement } from "./print/element.js";
import {
printClosingTagEnd,
printClosingTagSuffix,
printOpeningTagPrefix,
printOpeningTagStart,
} from "./print/tag.js";
import preprocess from "./print-preprocess.js";
import { getTextValueParts, unescapeQuoteEntities } from "./utils/index.js";
function genericPrint(path, options, print) {
const { node } = path;
switch (node.type) {
case "front-matter":
return replaceEndOfLine(node.raw);
case "root":
if (options.__onHtmlRoot) {
options.__onHtmlRoot(node);
}
return [group(printChildren(path, options, print)), hardline];
case "element":
case "ieConditionalComment":
return printElement(path, options, print);
case "angularControlFlowBlock":
return printAngularControlFlowBlock(path, options, print);
case "angularControlFlowBlockParameters":
return printAngularControlFlowBlockParameters(path, options, print);
case "angularControlFlowBlockParameter":
return htmlWhitespaceUtils.trim(node.expression);
case "angularIcuExpression":
return printAngularIcuExpression(path, options, print);
case "angularIcuCase":
return printAngularIcuCase(path, options, print);
case "ieConditionalStartComment":
case "ieConditionalEndComment":
return [printOpeningTagStart(node), printClosingTagEnd(node)];
case "interpolation":
return [
printOpeningTagStart(node, options),
...path.map(print, "children"),
printClosingTagEnd(node, options),
];
case "text": {
if (node.parent.type === "interpolation") {
// replace the trailing literalline with hardline for better readability
const trailingNewlineRegex = /\n[^\S\n]*$/u;
const hasTrailingNewline = trailingNewlineRegex.test(node.value);
const value = hasTrailingNewline
? node.value.replace(trailingNewlineRegex, "")
: node.value;
return [replaceEndOfLine(value), hasTrailingNewline ? hardline : ""];
}
const printed = cleanDoc([
printOpeningTagPrefix(node, options),
...getTextValueParts(node),
printClosingTagSuffix(node, options),
]);
if (Array.isArray(printed)) {
return fill(printed);
}
return printed;
}
case "docType":
return [
group([
printOpeningTagStart(node, options),
" ",
node.value.replace(/^html\b/iu, "html").replaceAll(/\s+/gu, " "),
]),
printClosingTagEnd(node, options),
];
case "comment":
return [
printOpeningTagPrefix(node, options),
replaceEndOfLine(
options.originalText.slice(locStart(node), locEnd(node)),
),
printClosingTagSuffix(node, options),
];
case "attribute": {
if (node.value === null) {
return node.rawName;
}
const value = unescapeQuoteEntities(node.value);
const quote = getPreferredQuote(value, '"');
return [
node.rawName,
"=",
quote,
replaceEndOfLine(
quote === '"'
? value.replaceAll('"', """)
: value.replaceAll("'", "'"),
),
quote,
];
}
case "cdata": // Transformed into `text`
default:
/* c8 ignore next */
throw new UnexpectedNodeError(node, "HTML");
}
}
const printer = {
preprocess,
print: genericPrint,
insertPragma,
massageAstNode: clean,
embed,
getVisitorKeys,
};
export default printer;