als-document 1.0.3-alpha → 1.0.5-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.3-alpha",
3
+ "version": "1.0.5-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/node/node.js CHANGED
@@ -1,3 +1,9 @@
1
+ function insertBefore(arr, index, newItem) {
2
+ const existingIndex = arr.indexOf(newItem);
3
+ if (existingIndex !== -1) arr.splice(existingIndex, 1);
4
+ arr.splice(index, 0, newItem);
5
+ }
6
+
1
7
  class Node {
2
8
  constructor(tagName, attributes = {}, parent = null) {
3
9
  this.isSingle = false;
@@ -11,10 +17,12 @@ class Node {
11
17
  this._dataset = null
12
18
  }
13
19
 
14
- get id() { return this.attributes.id || null; }
20
+ get id() { return this.attributes.id ? this.attributes.id : null; }
21
+ set id(newValue) { this.attributes.id = newValue; }
15
22
  get className() {return this.attributes.class || null}
16
23
  get parentNode() { return this.parent }
17
24
  get ancestors() {
25
+ if(!this.parent) return []
18
26
  const ancestors = []
19
27
  let element = this.parent
20
28
  while (element.tagName !== 'ROOT') {
@@ -24,17 +32,25 @@ class Node {
24
32
  return ancestors.reverse()
25
33
  }
26
34
 
27
- get childIndex() { return this.parent.childNodes ? this.parent.childNodes.indexOf(this) : null }
35
+ get childNodeIndex() {
36
+ if(!this.parent) return null
37
+ return this.parent.childNodes ? this.parent.childNodes.indexOf(this) : null
38
+ }
39
+
40
+ get childIndex() {
41
+ if(!this.parent) return null
42
+ return this.parent.children ? this.parent.children.indexOf(this) : null
43
+ }
28
44
  get previousElementSibling() { return this.prev }
29
45
  get prev() {
30
46
  if (!this.childIndex) return null // if no index or index == 0
31
- return this.parent.childNodes[this.childIndex - 1]
47
+ return this.parent.children[this.childIndex - 1]
32
48
  }
33
49
 
34
50
  get nextElementSibling() { return this.next }
35
51
  get next() {
36
52
  if (!this.childIndex) return null
37
- return this.parent.childNodes[this.childIndex + 1] || null
53
+ return this.parent.children[this.childIndex + 1] || null
38
54
  }
39
55
 
40
56
  get dataset() {
@@ -54,7 +70,7 @@ class Node {
54
70
 
55
71
  get outerHTML() {
56
72
  const attrs = Object.entries(this.attributes).map(([key, val]) => `${key}="${val}"`).join(" ");
57
- return `<${this.tagName} ${attrs}>${this.innerHTML}</${this.tagName}>`;
73
+ return `<${this.tagName}${attrs ? ' '+attrs : ''}>${this.innerHTML}</${this.tagName}>`;
58
74
  }
59
75
 
60
76
  getAttribute(attrName) { return this.attributes[attrName] || null }
@@ -103,9 +119,11 @@ class Node {
103
119
  const pos = position.toLowerCase();
104
120
  if (pos === "afterbegin") this.childNodes.unshift(newElement);
105
121
  else if (pos === "beforeend") this.childNodes.push(newElement);
122
+ newElement.parent = this
106
123
  if (!this.parent) return newElement
107
- if (pos === "beforebegin") this.parent.childNodes.unshift(newElement);
108
- else if (pos === "afterend") this.parent.childNodes.splice(this.childIndex + 1, 0, newElement);
124
+ if (pos === "beforebegin") insertBefore(this.parent.childNodes, this.childNodeIndex, newElement)
125
+ else if (pos === "afterend") this.parent.childNodes.splice(this.childNodeIndex + 1, 0, newElement);
126
+ newElement.parent = this.parent
109
127
  return newElement
110
128
  }
111
129
 
@@ -51,8 +51,8 @@ function parseHTML(html) {
51
51
  }
52
52
 
53
53
  if (html.startsWith("<", i)) {
54
- if (currentText.trim()) {
55
- stack[stack.length - 1].childNodes.push(new TextNode(currentText.trim()));
54
+ if (currentText && stack[stack.length - 1]) {
55
+ stack[stack.length - 1].childNodes.push(new TextNode(currentText));
56
56
  currentText = "";
57
57
  }
58
58
 
@@ -91,6 +91,6 @@ function parseHTML(html) {
91
91
  i++;
92
92
  }
93
93
  }
94
- if (currentText.trim()) stack[stack.length - 1].childNodes.push(new TextNode(currentText.trim()));
94
+ if (currentText.trim() && stack[stack.length - 1]) stack[stack.length - 1].childNodes.push(new TextNode(currentText));
95
95
  return root;
96
96
  }
@@ -1,7 +1,7 @@
1
1
  function checkElement(el,selector) {
2
2
  if(selector == undefined) return true
3
3
  if(el == null) return false
4
- let {tag,classList,attributes,id,prev,ancestors,parents,prevAny} = selector
4
+ let {tag,classList,attribs:attributes,id,prev,ancestors,parents,prevAny} = selector
5
5
  if(typeof el === 'string') return false
6
6
  if(id !== undefined && el.id === null) return false
7
7
  if(id && id !== el.id) return false
package/tests/index.html CHANGED
@@ -5,7 +5,7 @@
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <title>Document</title>
7
7
  <script src="/node_modules/als-simple-test/test.js"></script>
8
- <script src="/document.js"></script>
8
+ <script src="../document.js"></script>
9
9
  <script src="./data/html1.js"></script>
10
10
  <!-- <script src="./data/html2.js"></script> -->
11
11
  <script src="./data/svg.js"></script>
@@ -14,8 +14,8 @@
14
14
  let {describe,it,beforeEach,runTests,expect,delay,assert,beforeAll} = SimpleTest
15
15
  SimpleTest.showFullError = true
16
16
  </script>
17
- <script src="./query.js"></script>
18
17
  <script src="utils.js"></script>
18
+ <script src="./query.js"></script>
19
19
  <script src="parser.js"></script>
20
20
  <script src="node.js"></script>
21
21
  </head>
package/tests/node.js CHANGED
@@ -130,7 +130,7 @@ describe('Content Manipulation', () => {
130
130
  expect(rootNode.childNodes[2].tagName).equalTo('a');
131
131
 
132
132
  childNode.insertAdjacentHTML('beforebegin', '<strong></strong>');
133
- expect(rootNode.childNodes[0].tagName).equalTo('strong');
133
+ expect(rootNode.childNodes[1].tagName).equalTo('strong');
134
134
 
135
135
  childNode.insertAdjacentText('afterend', 'Some text');
136
136
  expect(typeof rootNode.childNodes[3].nodeValue).equalTo('string');
package/tests/parser.js CHANGED
@@ -225,11 +225,11 @@ describe('signle tags, script and style', () => {
225
225
  <link rel="stylesheet" href="styles.css">
226
226
  `;
227
227
  const rootMetaLink = parseHTML(testMetaLink);
228
- assert(rootMetaLink.childNodes[0].tagName === "meta", "Test Meta/Link 1: Meta tag not created");
229
- assert(rootMetaLink.childNodes[0].getAttribute("charset") === "UTF-8", "Test Meta/Link 1: Meta content not correct");
230
- assert(rootMetaLink.childNodes[1].tagName === "link", "Test Meta/Link 2: Link tag not created");
231
- assert(rootMetaLink.childNodes[1].getAttribute("rel") === "stylesheet", "Test Meta/Link 2: Link rel attribute not correct");
232
- assert(rootMetaLink.childNodes[1].getAttribute("href") === "styles.css", "Test Meta/Link 2: Link href attribute not correct");
228
+ assert(rootMetaLink.children[0].tagName === "meta", "Test Meta/Link 1: Meta tag not created");
229
+ assert(rootMetaLink.children[0].getAttribute("charset") === "UTF-8", "Test Meta/Link 1: Meta content not correct");
230
+ assert(rootMetaLink.children[1].tagName === "link", "Test Meta/Link 2: Link tag not created");
231
+ assert(rootMetaLink.children[1].getAttribute("rel") === "stylesheet", "Test Meta/Link 2: Link rel attribute not correct");
232
+ assert(rootMetaLink.children[1].getAttribute("href") === "styles.css", "Test Meta/Link 2: Link href attribute not correct");
233
233
  })
234
234
 
235
235
  it('broken html structure', () => {