@jsenv/ast 6.2.12 → 6.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/html/html_parse.js +46 -26
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/ast",
|
|
3
|
-
"version": "6.2.
|
|
3
|
+
"version": "6.2.14",
|
|
4
4
|
"description": "Parse and transform HTML, CSS and JS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@babel/parser": "7.24.8",
|
|
36
|
-
"@jsenv/humanize": "1.2.
|
|
37
|
-
"@jsenv/urls": "2.5.
|
|
36
|
+
"@jsenv/humanize": "1.2.8",
|
|
37
|
+
"@jsenv/urls": "2.5.2",
|
|
38
38
|
"acorn": "8.12.1",
|
|
39
39
|
"acorn-import-attributes": "1.9.5",
|
|
40
40
|
"acorn-walk": "8.3.3",
|
package/src/html/html_parse.js
CHANGED
|
@@ -137,45 +137,64 @@ export const stringifyHtmlAst = (
|
|
|
137
137
|
};
|
|
138
138
|
|
|
139
139
|
const ensureLineBreaksBetweenHtmlNodes = (rootNode) => {
|
|
140
|
-
const
|
|
141
|
-
|
|
140
|
+
const mutationCallbackSet = new Set();
|
|
142
141
|
const documentType = rootNode.childNodes[0];
|
|
142
|
+
const ensureChildrenSurroundedByLinebreaks = (headOrBody) => {
|
|
143
|
+
const { childNodes } = headOrBody;
|
|
144
|
+
const firstChild = childNodes[0];
|
|
145
|
+
if (firstChild.nodeName !== "#text") {
|
|
146
|
+
mutationCallbackSet.add(() => {
|
|
147
|
+
insertHtmlNodeBefore(
|
|
148
|
+
{ nodeName: "#text", value: "\n " },
|
|
149
|
+
firstChild,
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
const lastChild = childNodes[childNodes.length - 1];
|
|
154
|
+
if (lastChild.nodeName === "#text") {
|
|
155
|
+
if (headOrBody.nodeName === "head" && lastChild.value === "\n ") {
|
|
156
|
+
lastChild.value = "\n ";
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
mutationCallbackSet.add(() => {
|
|
160
|
+
insertHtmlNodeAfter({ nodeName: "#text", value: "\n " }, lastChild);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
if (
|
|
164
|
+
lastChild.nodeName === "#text" &&
|
|
165
|
+
lastChild.value[lastChild.value.length - 1] === "\n"
|
|
166
|
+
) {
|
|
167
|
+
lastChild.value = lastChild.value.slice(0, -1);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
143
171
|
if (documentType.nodeName === "#documentType") {
|
|
144
172
|
const html = rootNode.childNodes[1];
|
|
145
173
|
if (html.nodeName === "html") {
|
|
146
|
-
|
|
174
|
+
mutationCallbackSet.add(() => {
|
|
147
175
|
insertHtmlNodeAfter({ nodeName: "#text", value: "\n" }, documentType);
|
|
148
176
|
});
|
|
149
|
-
|
|
150
|
-
const head =
|
|
151
|
-
if (head
|
|
152
|
-
|
|
177
|
+
const htmlChildNodes = html.childNodes;
|
|
178
|
+
const head = htmlChildNodes.find((node) => node.nodeName === "head");
|
|
179
|
+
if (head) {
|
|
180
|
+
mutationCallbackSet.add(() => {
|
|
153
181
|
insertHtmlNodeBefore({ nodeName: "#text", value: "\n " }, head);
|
|
154
182
|
});
|
|
183
|
+
ensureChildrenSurroundedByLinebreaks(head);
|
|
155
184
|
}
|
|
156
|
-
|
|
157
|
-
const body = html.childNodes.find((node) => node.nodeName === "body");
|
|
185
|
+
const body = htmlChildNodes.find((node) => node.nodeName === "body");
|
|
158
186
|
if (body) {
|
|
159
|
-
const
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
{ nodeName: "#text", value: "\n " },
|
|
164
|
-
bodyLastChild,
|
|
165
|
-
);
|
|
187
|
+
const nodeBeforeBody = htmlChildNodes[htmlChildNodes.indexOf(body) - 1];
|
|
188
|
+
if (nodeBeforeBody && nodeBeforeBody.nodeName !== "#text") {
|
|
189
|
+
mutationCallbackSet.add(() => {
|
|
190
|
+
insertHtmlNodeBefore({ nodeName: "#text", value: "\n " }, body);
|
|
166
191
|
});
|
|
167
192
|
}
|
|
168
|
-
|
|
169
|
-
bodyLastChild.nodeName === "#text" &&
|
|
170
|
-
bodyLastChild.value[bodyLastChild.value.length - 1] === "\n"
|
|
171
|
-
) {
|
|
172
|
-
bodyLastChild.value = bodyLastChild.value.slice(0, -1);
|
|
173
|
-
}
|
|
193
|
+
ensureChildrenSurroundedByLinebreaks(body);
|
|
174
194
|
}
|
|
175
|
-
|
|
176
195
|
const htmlLastChild = html.childNodes[html.childNodes.length - 1];
|
|
177
196
|
if (htmlLastChild.nodeName !== "#text") {
|
|
178
|
-
|
|
197
|
+
mutationCallbackSet.add(() => {
|
|
179
198
|
insertHtmlNodeAfter(
|
|
180
199
|
{ nodeName: "#text", value: "\n" },
|
|
181
200
|
htmlLastChild,
|
|
@@ -184,8 +203,9 @@ const ensureLineBreaksBetweenHtmlNodes = (rootNode) => {
|
|
|
184
203
|
}
|
|
185
204
|
}
|
|
186
205
|
}
|
|
187
|
-
|
|
188
|
-
|
|
206
|
+
for (const mutationCallback of mutationCallbackSet) {
|
|
207
|
+
mutationCallback();
|
|
208
|
+
}
|
|
189
209
|
};
|
|
190
210
|
|
|
191
211
|
export const parseSvgString = (svgString) => {
|