als-document 1.0.2-alpha → 1.0.3-alpha
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/document.js +573 -32
- package/index.js +571 -30
- package/index.mjs +571 -30
- package/package.json +1 -1
- package/src/build.js +2 -0
- package/src/node/node.js +4 -1
- package/src/parse/parser.js +24 -2
package/package.json
CHANGED
package/src/build.js
CHANGED
|
@@ -2,7 +2,9 @@ const { readFileSync, writeFileSync, watchFile, watch } = require('fs')
|
|
|
2
2
|
const { join,basename } = require('path')
|
|
3
3
|
|
|
4
4
|
function optimizeCode(content) {
|
|
5
|
+
// content = content.replace(/(?<!\\)\/\/.*$/gm, '') // remove comments
|
|
5
6
|
content = content.replace(/^(?<!\\)\/\/.*$|(?<=\s)(?<!\\)\/\/.*$/gm, '') // remove comments
|
|
7
|
+
// content = content.replace(/\;\s*?$/gm,'') // remove ; at end of line
|
|
6
8
|
content = content.replace(/\[\s*?\n\s*/gm, '[') //
|
|
7
9
|
content = content.replace(/\s*?\]/gm, ']') //
|
|
8
10
|
content = content.replace(/\s*?$/gm, '') // remove space at end of line
|
package/src/node/node.js
CHANGED
|
@@ -111,7 +111,10 @@ class Node {
|
|
|
111
111
|
|
|
112
112
|
insertAdjacentHTML(position, html) {
|
|
113
113
|
const newNode = parseHTML(html);
|
|
114
|
-
|
|
114
|
+
newNode.childNodes.reverse().forEach(node => {
|
|
115
|
+
this.insertAdjacentElement(position, node);
|
|
116
|
+
});
|
|
117
|
+
return newNode
|
|
115
118
|
}
|
|
116
119
|
|
|
117
120
|
insertAdjacentText(position, text) {
|
package/src/parse/parser.js
CHANGED
|
@@ -2,6 +2,27 @@ function parseHTML(html) {
|
|
|
2
2
|
const root = new Node("ROOT");
|
|
3
3
|
const stack = [root];
|
|
4
4
|
let currentText = "", i = 0;
|
|
5
|
+
let max = 0
|
|
6
|
+
|
|
7
|
+
function parseScript() {
|
|
8
|
+
if (!html.startsWith("<script", i)) return false;
|
|
9
|
+
const openTagEnd = html.indexOf(">", i);
|
|
10
|
+
if (openTagEnd === -1) return false;
|
|
11
|
+
|
|
12
|
+
const attributesString = html.substring(i + 7, openTagEnd).trim(); // +7 чтобы пропустить "<script"
|
|
13
|
+
const attributes = parseAttributes(attributesString); // Извлечь атрибуты
|
|
14
|
+
|
|
15
|
+
let closeTagStart = html.indexOf("</script>", openTagEnd); // Находим закрывающий тег
|
|
16
|
+
if (closeTagStart === -1) return false; // Если нет закрывающего тега, возвращаем ошибку
|
|
17
|
+
const content = html.substring(openTagEnd + 1, closeTagStart); // Получить содержимое тега
|
|
18
|
+
|
|
19
|
+
const scriptNode = new Node('script', attributes, stack[stack.length - 1]); // Создаем узел
|
|
20
|
+
if(content.length > 0) scriptNode.childNodes.push(content);
|
|
21
|
+
|
|
22
|
+
// Переместить указатель i к концу закрывающего тега
|
|
23
|
+
i = closeTagStart + 9; // +9 чтобы пропустить "</script>"
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
5
26
|
|
|
6
27
|
function parseSpecial(startStr, endStr, n1, n2, tag) {
|
|
7
28
|
if (!html.startsWith(startStr, i)) return false
|
|
@@ -13,8 +34,10 @@ function parseHTML(html) {
|
|
|
13
34
|
}
|
|
14
35
|
|
|
15
36
|
while (i < html.length) {
|
|
37
|
+
if(i >= max) max = i;
|
|
38
|
+
else break;
|
|
39
|
+
if (parseScript()) continue
|
|
16
40
|
if (parseSpecial("<!--", "-->", 4, 3, '#comment')) continue
|
|
17
|
-
if (parseSpecial("<script", "</script>", 8, 9, 'script')) continue
|
|
18
41
|
if (parseSpecial("<style", "</style>", 7, 8, 'style')) continue
|
|
19
42
|
|
|
20
43
|
if (html.startsWith("<![CDATA[", i)) {
|
|
@@ -52,7 +75,6 @@ function parseHTML(html) {
|
|
|
52
75
|
}
|
|
53
76
|
|
|
54
77
|
const tagContent = html.substring(i + 1, tagEnd);
|
|
55
|
-
|
|
56
78
|
if (tagContent.startsWith("/")) stack.pop(); // It is close tag
|
|
57
79
|
else { // It is open tag
|
|
58
80
|
let isSelfClosing = tagContent.endsWith('/');
|