1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const clone = require("clone");
|
17 | const dom5 = require("dom5/lib/index-next");
|
18 | const parse5 = require("parse5");
|
19 | const document_1 = require("../parser/document");
|
20 | class ParsedHtmlDocument extends document_1.ParsedDocument {
|
21 | constructor() {
|
22 | super(...arguments);
|
23 | this.type = 'html';
|
24 | }
|
25 | visit(visitors) {
|
26 | for (const node of dom5.depthFirst(this.ast)) {
|
27 | visitors.forEach((visitor) => visitor(node));
|
28 | }
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 | _sourceRangeForElementWithEndTag(node) {
|
34 | const location = node.__location;
|
35 | if (isElementLocationInfo(location)) {
|
36 | return {
|
37 | file: this.url,
|
38 | start: {
|
39 | line: location.startTag.line - 1,
|
40 | column: location.startTag.col - 1
|
41 | },
|
42 | end: {
|
43 | line: location.endTag.line - 1,
|
44 | column: location.endTag.col + (node.tagName || '').length + 2
|
45 | }
|
46 | };
|
47 | }
|
48 | }
|
49 |
|
50 | _sourceRangeForNode(node) {
|
51 | const location = node.__location;
|
52 | if (!node.__location) {
|
53 | return;
|
54 | }
|
55 | if (isElementLocationInfo(location)) {
|
56 | if (voidTagNames.has(node.tagName || '')) {
|
57 | return this.sourceRangeForStartTag(node);
|
58 | }
|
59 | return this._sourceRangeForElementWithEndTag(node);
|
60 | }
|
61 | return this._getSourceRangeForLocation(location);
|
62 | }
|
63 | sourceRangeForAttribute(node, attrName) {
|
64 | return this._getSourceRangeForLocation(getAttributeLocation(node, attrName));
|
65 | }
|
66 | sourceRangeForAttributeName(node, attrName) {
|
67 | const range = this.sourceRangeForAttribute(node, attrName);
|
68 | if (!range) {
|
69 | return;
|
70 | }
|
71 |
|
72 |
|
73 | return {
|
74 | file: range.file,
|
75 | start: range.start,
|
76 | end: { line: range.start.line, column: range.start.column + attrName.length }
|
77 | };
|
78 | }
|
79 | sourceRangeForAttributeValue(node, attrName, excludeQuotes) {
|
80 | const attributeRange = this.sourceRangeForAttribute(node, attrName);
|
81 | if (!attributeRange) {
|
82 | return;
|
83 | }
|
84 |
|
85 | if ((attributeRange.start.line === attributeRange.end.line) &&
|
86 | (attributeRange.end.column - attributeRange.start.column ===
|
87 | attrName.length)) {
|
88 | return undefined;
|
89 | }
|
90 | const location = getAttributeLocation(node, attrName);
|
91 |
|
92 | const fullAttribute = this.contents.substring(location.startOffset, location.endOffset);
|
93 | const equalsIndex = fullAttribute.indexOf('=');
|
94 | if (equalsIndex === -1) {
|
95 |
|
96 |
|
97 |
|
98 | return undefined;
|
99 | }
|
100 | const whitespaceAfterEquals = fullAttribute.substring(equalsIndex + 1).match(/[\s\n]*/)[0];
|
101 | let endOfTextToSkip =
|
102 |
|
103 | location.startOffset +
|
104 |
|
105 | equalsIndex +
|
106 |
|
107 | 1 +
|
108 |
|
109 | whitespaceAfterEquals.length;
|
110 | if (excludeQuotes) {
|
111 | const maybeQuote = this.contents.charAt(endOfTextToSkip);
|
112 | if (maybeQuote === '\'' || maybeQuote === '"') {
|
113 | endOfTextToSkip += 1;
|
114 | }
|
115 | }
|
116 | return this.offsetsToSourceRange(endOfTextToSkip, location.endOffset);
|
117 | }
|
118 | sourceRangeForStartTag(node) {
|
119 | return this._getSourceRangeForLocation(getStartTagLocation(node));
|
120 | }
|
121 | sourceRangeForEndTag(node) {
|
122 | return this._getSourceRangeForLocation(getEndTagLocation(node));
|
123 | }
|
124 | _getSourceRangeForLocation(location) {
|
125 | if (!location) {
|
126 | return;
|
127 | }
|
128 | return this.offsetsToSourceRange(location.startOffset, location.endOffset);
|
129 | }
|
130 | _findClonedContainingNode(clonedAst, docContainingNode) {
|
131 | const cloneIterator = dom5.depthFirst(clonedAst);
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 | for (const node of dom5.depthFirst(this.ast)) {
|
138 | const cloneNode = cloneIterator.next().value;
|
139 | if (node === docContainingNode) {
|
140 | return cloneNode;
|
141 | }
|
142 | }
|
143 | }
|
144 | stringify(options = {}) {
|
145 | const { prettyPrint = true } = options;
|
146 | |
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | const astClone = clone(this.ast);
|
156 | const inlineDocuments = options.inlineDocuments || [];
|
157 |
|
158 |
|
159 |
|
160 | const [ourInlineDocuments, otherDocuments] = partition(inlineDocuments, (d) => d.astNode != null && d.astNode.containingDocument === this);
|
161 | for (const doc of ourInlineDocuments) {
|
162 | if (doc.astNode == null || doc.astNode.language !== 'html') {
|
163 | throw new Error(`This should not happen, ` +
|
164 | `we already checked for this condition in partition()`);
|
165 | }
|
166 | const docContainingNode = doc.astNode.node;
|
167 | let expectedIndentation;
|
168 | if (prettyPrint) {
|
169 | const docContainingLocation = docContainingNode.__location;
|
170 | if (docContainingLocation &&
|
171 | !isElementLocationInfo(docContainingLocation)) {
|
172 | expectedIndentation = docContainingLocation.col;
|
173 | const parentLocation = docContainingNode.parentNode &&
|
174 | docContainingNode.parentNode.__location;
|
175 | if (parentLocation && !isElementLocationInfo(parentLocation)) {
|
176 | expectedIndentation -= parentLocation.col;
|
177 | }
|
178 | }
|
179 | if (expectedIndentation === undefined) {
|
180 | expectedIndentation = 2;
|
181 | }
|
182 | }
|
183 |
|
184 |
|
185 | const clonedDocContainingNode = this._findClonedContainingNode(astClone, docContainingNode);
|
186 | const inlineStringifyOptions = Object.assign({}, options, { indent: expectedIndentation, inlineDocuments: otherDocuments });
|
187 | const endingSpace = expectedIndentation ? ' '.repeat(expectedIndentation - 1) : '';
|
188 | dom5.setTextContent(clonedDocContainingNode, '\n' + doc.stringify(inlineStringifyOptions) + endingSpace);
|
189 | }
|
190 | removeFakeNodes(astClone);
|
191 | return parse5.serialize(astClone);
|
192 | }
|
193 | }
|
194 | exports.ParsedHtmlDocument = ParsedHtmlDocument;
|
195 | const injectedTagNames = new Set(['html', 'head', 'body']);
|
196 | function removeFakeNodes(ast) {
|
197 | const children = (ast.childNodes || []).slice();
|
198 | if (ast.parentNode && isFakeNode(ast)) {
|
199 | for (const child of children) {
|
200 | dom5.insertBefore(ast.parentNode, ast, child);
|
201 | }
|
202 | dom5.remove(ast);
|
203 | }
|
204 | for (const child of children) {
|
205 | removeFakeNodes(child);
|
206 | }
|
207 | }
|
208 | function isFakeNode(ast) {
|
209 | return !ast.__location && injectedTagNames.has(ast.nodeName);
|
210 | }
|
211 | exports.isFakeNode = isFakeNode;
|
212 | function isElementLocationInfo(location) {
|
213 | const loc = location;
|
214 | return (loc.startTag && loc.endTag) != null;
|
215 | }
|
216 | function getStartTagLocation(node) {
|
217 | if (voidTagNames.has(node.tagName || '')) {
|
218 | return node.__location;
|
219 | }
|
220 | if ('startTag' in node.__location) {
|
221 | return node.__location.startTag;
|
222 | }
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | if ('attrs' in node.__location) {
|
228 | return node.__location;
|
229 | }
|
230 | }
|
231 | function getEndTagLocation(node) {
|
232 | if ('endTag' in node.__location) {
|
233 | return node.__location.endTag;
|
234 | }
|
235 | }
|
236 | function getAttributeLocation(node, attrName) {
|
237 | if (!node || !node.__location) {
|
238 | return;
|
239 | }
|
240 | let attrs = undefined;
|
241 | const location = node.__location;
|
242 | const elemLocation = location;
|
243 | const elemStartLocation = location;
|
244 | if (elemLocation.startTag !== undefined && elemLocation.startTag.attrs) {
|
245 | attrs = elemLocation.startTag.attrs;
|
246 | }
|
247 | else if (elemStartLocation.attrs !== undefined) {
|
248 | attrs = elemStartLocation.attrs;
|
249 | }
|
250 | if (!attrs) {
|
251 | return;
|
252 | }
|
253 | return attrs[attrName];
|
254 | }
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | const voidTagNames = new Set([
|
262 | 'area',
|
263 | 'base',
|
264 | 'br',
|
265 | 'col',
|
266 | 'embed',
|
267 | 'hr',
|
268 | 'img',
|
269 | 'input',
|
270 | 'keygen',
|
271 | 'link',
|
272 | 'meta',
|
273 | 'param',
|
274 | 'source',
|
275 | 'track',
|
276 | 'wbr'
|
277 | ]);
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | function partition(items, predicate) {
|
283 | const matched = [];
|
284 | const notMatched = [];
|
285 | for (const item of items) {
|
286 | if (predicate(item)) {
|
287 | matched.push(item);
|
288 | }
|
289 | else {
|
290 | notMatched.push(item);
|
291 | }
|
292 | }
|
293 | return [matched, notMatched];
|
294 | }
|
295 |
|
\ | No newline at end of file |