als-document 1.0.1-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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "als-document",
3
- "version": "1.0.1-alpha",
3
+ "version": "1.0.3-alpha",
4
4
  "description": "A powerful HTML parser & DOM manipulation library for both backend and frontend.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
package/src/build.js CHANGED
@@ -2,7 +2,8 @@ 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
+ // content = content.replace(/(?<!\\)\/\/.*$/gm, '') // remove comments
6
+ content = content.replace(/^(?<!\\)\/\/.*$|(?<=\s)(?<!\\)\/\/.*$/gm, '') // remove comments
6
7
  // content = content.replace(/\;\s*?$/gm,'') // remove ; at end of line
7
8
  content = content.replace(/\[\s*?\n\s*/gm, '[') //
8
9
  content = content.replace(/\s*?\]/gm, ']') //
package/src/node/node.js CHANGED
@@ -83,7 +83,7 @@ class Node {
83
83
  $(query) {return this.querySelector(query)}
84
84
  querySelector(query) {
85
85
  const selectors = Query.get(query)
86
- return find(selectors, this, new Set(), true)[0]
86
+ return find(selectors, this, new Set(), true)[0] || null
87
87
  }
88
88
 
89
89
  getElementsByClassName(query) { return this.querySelectorAll('.' + query) }
@@ -111,7 +111,10 @@ class Node {
111
111
 
112
112
  insertAdjacentHTML(position, html) {
113
113
  const newNode = parseHTML(html);
114
- return this.insertAdjacentElement(position, newNode);
114
+ newNode.childNodes.reverse().forEach(node => {
115
+ this.insertAdjacentElement(position, node);
116
+ });
117
+ return newNode
115
118
  }
116
119
 
117
120
  insertAdjacentText(position, text) {
@@ -6,7 +6,7 @@ class SingleNode extends Node {
6
6
  }
7
7
 
8
8
  get outerHTML() { // Переопределение outerHTML для одиночного узла
9
- if (this.tagName === "#cdata-section") return `<![CDATA[${this.textContent}]]>`;
9
+ if (this.tagName === "#cdata-section") return `<![CDATA[${this.nodeValue}]]>`;
10
10
  const attrs = Object.entries(this.attributes).map(([key, val]) => `${key}="${val}"`).join(" ");
11
11
  return `<${this.tagName} ${attrs}${this.tagName === '?xml' ? '?' : ''}>`;
12
12
  }
@@ -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('/');
@@ -1,9 +1,8 @@
1
1
  function checkElement(el,selector) {
2
2
  if(selector == undefined) return true
3
3
  if(el == null) return false
4
- let {tag,classList,attribs,id,prev,ancestors,parents,prevAny} = selector
4
+ let {tag,classList,attributes,id,prev,ancestors,parents,prevAny} = selector
5
5
  if(typeof el === 'string') return false
6
- if(el.isSpecial) return false
7
6
  if(id !== undefined && el.id === null) return false
8
7
  if(id && id !== el.id) return false
9
8
  if(tag && el.tagName === undefined) return false
@@ -13,7 +12,7 @@ function checkElement(el,selector) {
13
12
  else if(classList !== undefined) {
14
13
  if(classList.every(e => el.classList.contains(e)) === false) return false
15
14
  }
16
- if(checkattributes(attribs,el) === false) return false
15
+ if(checkattributes(attributes,el) === false) return false
17
16
  if(checkElement(el.prev,prev) === false) return false
18
17
  if(checkAncestors(el.ancestors,ancestors) === false) return false
19
18
  if(checkParents(el.ancestors,parents) === false) return false