als-document 1.0.0-beta → 1.0.0
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 +2 -2
- package/index.js +2 -2
- package/index.mjs +2 -2
- package/package.json +1 -1
- package/src/node/node.js +7 -4
- package/src/query/query.js +12 -6
- package/tests/index.html +1 -1
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class Query{
|
|
2
1
|
static get(query){
|
|
3
2
|
let q=new Query(query)
|
|
4
3
|
return q.selectors
|
|
5
4
|
}
|
|
6
5
|
constructor(query){
|
|
7
6
|
this.query=query
|
|
8
7
|
this.selectors=[]
|
|
9
8
|
this.stringValues=[];
|
|
10
9
|
this.parseSelectors(query.split(','))
|
|
11
10
|
}
|
|
12
11
|
parseSelectors(selectors){
|
|
13
12
|
selectors.forEach(selector=>{
|
|
14
13
|
let originalSelector=selector.trim()
|
|
15
14
|
selector=this.removeSpaces(selector)
|
|
16
15
|
this.stringValues=[]
|
|
17
16
|
selector=selector.replace(/\[.*?\]/g,(value)=>{
|
|
18
17
|
this.stringValues.push(value)
|
|
19
18
|
return `[${this.stringValues.length-1}]`
|
|
20
19
|
})
|
|
21
20
|
let [element,ancestors]=this.splitAndCutLast(selector,' ')
|
|
22
21
|
element=this.getFamily(element)
|
|
23
22
|
if (ancestors.length>0)
|
|
24
23
|
element.ancestors=ancestors.map(ancestor=>this.getFamily(ancestor))
|
|
25
24
|
element.group=originalSelector
|
|
26
25
|
this.selectors.push(element)
|
|
27
26
|
});
|
|
28
27
|
}
|
|
29
28
|
splitAndCutLast(string,splitBy){
|
|
30
29
|
const array=string.split(splitBy);
|
|
31
30
|
const last=array.pop();
|
|
32
31
|
return [last,array];
|
|
33
32
|
}
|
|
34
33
|
getFamily(group,element,prev,prevAny,sign){
|
|
35
34
|
if (group.match(/\~|\+/)!==null){
|
|
36
35
|
let [last,prevBrothers]=this.splitAndCutLast(group,/\~|\+/)
|
|
37
36
|
let signs=group.replace(last,'')
|
|
38
37
|
prevBrothers.forEach(el=>signs=signs.replace(el,''))
|
|
39
38
|
signs=signs.match(/\~|\+/g)
|
|
40
39
|
if (signs.length==1){
|
|
41
40
|
sign=signs[0]
|
|
42
41
|
} else if (signs.length>1){
|
|
43
42
|
sign=signs.splice(signs.length-1,signs.length-1)[0]
|
|
44
43
|
prevBrothers[0]=prevBrothers.map((b,i)=>{
|
|
45
44
|
if (i< prevBrothers.length-1) b+=signs[i]
|
|
46
45
|
return b
|
|
47
46
|
}).join('')
|
|
48
47
|
prevBrothers[0]=this.getFamily(prevBrothers[0])
|
|
49
48
|
}
|
|
50
49
|
if (sign=='~') prevAny=prevBrothers[0]
|
|
51
50
|
else if (sign=='+') prev=prevBrothers[0]
|
|
52
51
|
element=last
|
|
53
52
|
} else element=group
|
|
54
53
|
let family
|
|
55
54
|
if (prev || prevAny){
|
|
56
55
|
family=this.getParents(element)
|
|
57
56
|
if (prev) family.prev=this.getParents(prev)
|
|
58
57
|
if (prevAny) family.prevAny=this.getParents(prevAny)
|
|
59
58
|
} else family=this.getParents(element)
|
|
60
59
|
if (family.query!==group) family.group=group
|
|
61
60
|
return family
|
|
62
61
|
}
|
|
63
62
|
getParents(selector){
|
|
64
63
|
if (typeof selector=='string'){
|
|
65
64
|
let [element,parents]=this.splitAndCutLast(selector,'>')
|
|
66
65
|
element=this.buildElement(element)
|
|
67
66
|
parents=parents.map(parent=>this.buildElement(parent))
|
|
68
67
|
if (parents.length>0) element.parents=parents
|
|
69
68
|
return element
|
|
70
69
|
} else return selector
|
|
71
70
|
}
|
|
72
71
|
buildElement(element,id=null,tag=null,classList=[]){
|
|
73
72
|
let query=element
|
|
74
73
|
element=element.replace(/\#(\w-?)*/,$id=>{
|
|
75
74
|
id=$id.replace(/^\#/,''); return ''
|
|
76
75
|
})
|
|
77
76
|
element=element.replace(/\.(\w-?)*/,$class=>{
|
|
78
77
|
classList.push($class.replace(/^\./,'')); return ''
|
|
79
78
|
})
|
|
80
79
|
element=element.replace(/(\w\:?-?)*/,$tag=>{
|
|
81
80
|
tag=$tag=='' ? null : $tag; return ''
|
|
82
81
|
})
|
|
83
82
|
let attribs=this.getAttributes(element)
|
|
84
83
|
element={ query }
|
|
85
84
|
if (id) element.id=id
|
|
86
85
|
if (tag) element.tag=tag
|
|
87
86
|
if (classList.length>0) element.classList=classList
|
|
88
87
|
if (attribs.length>0) element.attribs=attribs
|
|
89
88
|
return element
|
|
90
89
|
}
|
|
91
90
|
getAttributes(element){
|
|
92
91
|
let attribs=this.stringValues.filter((value,index)=>{
|
|
93
92
|
let searchValue=`[${index}]`
|
|
94
93
|
if (element.match(searchValue)) return true
|
|
95
94
|
else return false
|
|
96
95
|
})
|
|
97
96
|
attribs=attribs.map(attrib=>{
|
|
98
97
|
let query=attrib
|
|
99
98
|
attrib=attrib.replace('[','').replace(']','')
|
|
100
99
|
let [name,value]=attrib.split(/[\~\|\^\$\*]?\=/)
|
|
101
100
|
let sign=attrib.replace(name,'').replace(value,'')
|
|
102
101
|
attrib={ query }
|
|
103
102
|
if (name) attrib.name=name
|
|
104
103
|
if (value) attrib.value=value.trim().replace(/^\"/,'').replace(/\"$/,'')
|
|
105
104
|
if (sign){
|
|
106
105
|
attrib.sign=sign
|
|
107
106
|
attrib.check=this.getAttribFn(sign).bind(attrib)
|
|
108
107
|
}
|
|
109
108
|
return attrib
|
|
110
109
|
});
|
|
111
110
|
return attribs
|
|
112
111
|
}
|
|
113
112
|
getAttribFn(sign){
|
|
114
113
|
if (sign=='=') return function (value){ return value===this.value }
|
|
115
114
|
if (sign=='*=') return function (value){ return value.includes(this.value) }
|
|
116
115
|
if (sign=='^=') return function (value){ return value.startsWith(this.value) }
|
|
117
116
|
if (sign=='$=') return function (value){ return value.endsWith(this.value) }
|
|
118
117
|
if (sign=='|=') return function (value){
|
|
119
118
|
return value.trim().split(' ').length==1
|
|
120
119
|
&& (value.startsWith(this.value) || value.startsWith(this.value+'-'))
|
|
121
120
|
? true : false
|
|
122
121
|
}
|
|
123
122
|
if (sign=='~=') return function (value){
|
|
124
123
|
return this.value.trim().split(' ').length==1 && value.includes(this.value) ? true : false
|
|
125
124
|
}
|
|
126
125
|
}
|
|
127
126
|
removeSpaces(selector){
|
|
128
127
|
selector=selector.replace(/\s{2}/g,' ')
|
|
129
128
|
selector=selector.replace(/\s?\^?\$?\|?\~?\*?\=\s*/g,(m)=>m.trim())
|
|
130
129
|
selector=selector.replace(/\s?(\+|\~|\>)\s?/g,(m)=>m.trim())
|
|
131
130
|
return selector
|
|
132
131
|
}
|
|
132
|
+
class Query{
|
|
133
133
|
static get(query){
|
|
134
134
|
let q=new Query(query)
|
|
135
135
|
return q.selectors
|
|
136
136
|
}
|
|
137
137
|
constructor(query){
|
|
138
138
|
this.query=query
|
|
139
139
|
this.selectors=[]
|
|
140
140
|
this.stringValues=[];
|
|
141
141
|
this.parseSelectors(query.split(','))
|
|
142
142
|
}
|
|
143
143
|
parseSelectors(selectors){
|
|
144
144
|
selectors.forEach(selector=>{
|
|
145
145
|
let originalSelector=selector.trim()
|
|
146
146
|
selector=this.removeSpaces(selector)
|
|
147
147
|
this.stringValues=[]
|
|
148
148
|
selector=selector.replace(/\[.*?\]/g,(value)=>{
|
|
149
149
|
this.stringValues.push(value)
|
|
150
150
|
return `[${this.stringValues.length-1}]`
|
|
151
151
|
})
|
|
152
152
|
let [element,ancestors]=this.splitAndCutLast(selector,' ')
|
|
153
153
|
element=this.getFamily(element)
|
|
154
154
|
if (ancestors.length>0)
|
|
155
155
|
element.ancestors=ancestors.map(ancestor=>this.getFamily(ancestor))
|
|
156
156
|
element.group=originalSelector
|
|
157
157
|
this.selectors.push(element)
|
|
158
158
|
});
|
|
159
159
|
}
|
|
160
160
|
splitAndCutLast(string,splitBy){
|
|
161
161
|
const array=string.split(splitBy);
|
|
162
162
|
const last=array.pop();
|
|
163
163
|
return [last,array];
|
|
164
164
|
}
|
|
165
165
|
getFamily(group,element,prev,prevAny,sign){
|
|
166
166
|
if (group.match(/\~|\+/)!==null){
|
|
167
167
|
let [last,prevBrothers]=this.splitAndCutLast(group,/\~|\+/)
|
|
168
168
|
let signs=group.replace(last,'')
|
|
169
169
|
prevBrothers.forEach(el=>signs=signs.replace(el,''))
|
|
170
170
|
signs=signs.match(/\~|\+/g)
|
|
171
171
|
if (signs.length==1){
|
|
172
172
|
sign=signs[0]
|
|
173
173
|
} else if (signs.length>1){
|
|
174
174
|
sign=signs.splice(signs.length-1,signs.length-1)[0]
|
|
175
175
|
prevBrothers[0]=prevBrothers.map((b,i)=>{
|
|
176
176
|
if (i< prevBrothers.length-1) b+=signs[i]
|
|
177
177
|
return b
|
|
178
178
|
}).join('')
|
|
179
179
|
prevBrothers[0]=this.getFamily(prevBrothers[0])
|
|
180
180
|
}
|
|
181
181
|
if (sign=='~') prevAny=prevBrothers[0]
|
|
182
182
|
else if (sign=='+') prev=prevBrothers[0]
|
|
183
183
|
element=last
|
|
184
184
|
} else element=group
|
|
185
185
|
let family
|
|
186
186
|
if (prev || prevAny){
|
|
187
187
|
family=this.getParents(element)
|
|
188
188
|
if (prev) family.prev=this.getParents(prev)
|
|
189
189
|
if (prevAny) family.prevAny=this.getParents(prevAny)
|
|
190
190
|
} else family=this.getParents(element)
|
|
191
191
|
if (family.query!==group) family.group=group
|
|
192
192
|
return family
|
|
193
193
|
}
|
|
194
194
|
getParents(selector){
|
|
195
195
|
if (typeof selector=='string'){
|
|
196
196
|
let [element,parents]=this.splitAndCutLast(selector,'>')
|
|
197
197
|
element=this.buildElement(element)
|
|
198
198
|
parents=parents.map(parent=>this.buildElement(parent))
|
|
199
199
|
if (parents.length>0) element.parents=parents
|
|
200
200
|
return element
|
|
201
201
|
} else return selector
|
|
202
202
|
}
|
|
203
203
|
buildElement(element,id=null,tag=null,classList=[]){
|
|
204
204
|
let query=element
|
|
205
205
|
element=element.replace(/\#(\w-?)*/,$id=>{
|
|
206
206
|
id=$id.replace(/^\#/,''); return ''
|
|
207
207
|
})
|
|
208
208
|
element=element.replace(/\.(\w-?)*/,$class=>{
|
|
209
209
|
classList.push($class.replace(/^\./,'')); return ''
|
|
210
210
|
})
|
|
211
211
|
element=element.replace(/(\w\:?-?)*/,$tag=>{
|
|
212
212
|
tag=$tag=='' ? null : $tag; return ''
|
|
213
213
|
})
|
|
214
214
|
let attribs=this.getAttributes(element)
|
|
215
215
|
element={ query }
|
|
216
216
|
if (id) element.id=id
|
|
217
217
|
if (tag) element.tag=tag
|
|
218
218
|
if (classList.length>0) element.classList=classList
|
|
219
219
|
if (attribs.length>0) element.attribs=attribs
|
|
220
220
|
return element
|
|
221
221
|
}
|
|
222
222
|
getAttributes(element){
|
|
223
223
|
let attribs=this.stringValues.filter((value,index)=>{
|
|
224
224
|
let searchValue=`[${index}]`
|
|
225
225
|
if (element.match(searchValue)) return true
|
|
226
226
|
else return false
|
|
227
227
|
})
|
|
228
228
|
attribs=attribs.map(attrib=>{
|
|
229
229
|
let query=attrib
|
|
230
230
|
attrib=attrib.replace('[','').replace(']','')
|
|
231
231
|
let [name,...values]=attrib.split('=')
|
|
232
232
|
const value=values.join('=').trim().replace(/^\"/,'').replace(/\"$/,'')
|
|
233
233
|
let sign
|
|
234
234
|
if(value){
|
|
235
235
|
sign='='
|
|
236
236
|
name=name.replace(/[\~\|\^\$\*]$/,(match=>{
|
|
237
237
|
sign=match+sign
|
|
238
238
|
return ''
|
|
239
239
|
}))
|
|
240
240
|
}
|
|
241
241
|
attrib={query,name,value}
|
|
242
242
|
if (sign){
|
|
243
243
|
attrib.sign=sign
|
|
244
244
|
attrib.check=this.getAttribFn(sign).bind(attrib)
|
|
245
245
|
}
|
|
246
246
|
return attrib
|
|
247
247
|
});
|
|
248
248
|
return attribs
|
|
249
249
|
}
|
|
250
250
|
getAttribFn(sign){
|
|
251
251
|
if (sign=='=') return function (value){ return value===this.value }
|
|
252
252
|
if (sign=='*=') return function (value){ return value.includes(this.value) }
|
|
253
253
|
if (sign=='^=') return function (value){ return value.startsWith(this.value) }
|
|
254
254
|
if (sign=='$=') return function (value){ return value.endsWith(this.value) }
|
|
255
255
|
if (sign=='|=') return function (value){
|
|
256
256
|
return value.trim().split(' ').length==1
|
|
257
257
|
&& (value.startsWith(this.value) || value.startsWith(this.value+'-'))
|
|
258
258
|
? true : false
|
|
259
259
|
}
|
|
260
260
|
if (sign=='~=') return function (value){
|
|
261
261
|
return this.value.trim().split(' ').length==1 && value.includes(this.value) ? true : false
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
264
|
removeSpaces(selector){
|
|
265
265
|
selector=selector.replace(/\s{2}/g,' ')
|
|
266
266
|
selector=selector.replace(/\s?\^?\$?\|?\~?\*?\=\s*/g,(m)=>m.trim())
|
|
267
267
|
selector=selector.replace(/\s?(\+|\~|\>)\s?/g,(m)=>m.trim())
|
|
268
268
|
return selector
|
|
269
269
|
}
|
|
270
270
|
}
|
|
271
271
|
function checkElement(el,selector){
|
|
272
272
|
if(selector==undefined) return true
|
|
273
273
|
if(el==null) return false
|
|
274
274
|
let{tag,classList,attribs:attributes,id,prev,ancestors,parents,prevAny}=selector
|
|
275
275
|
if(typeof el==='string') return false
|
|
276
276
|
if(id!==undefined && el.id===null) return false
|
|
277
277
|
if(id && id!==el.id) return false
|
|
278
278
|
if(tag && el.tagName===undefined) return false
|
|
279
279
|
else if(tag && tag!==el.tagName) return false
|
|
280
280
|
const clas=el.attributes.class
|
|
281
281
|
if(classList!==undefined && (clas===undefined || clas==='')) return false
|
|
282
282
|
else if(classList!==undefined){
|
|
283
283
|
if(classList.every(e=>el.classList.contains(e))===false) return false
|
|
284
284
|
}
|
|
285
285
|
if(checkattributes(attributes,el)===false) return false
|
|
286
286
|
if(checkElement(el.prev,prev)===false) return false
|
|
287
287
|
if(checkAncestors(el.ancestors,ancestors)===false) return false
|
|
288
288
|
if(checkParents(el.ancestors,parents)===false) return false
|
|
289
289
|
if(el.parent){
|
|
290
290
|
if(checkPrevAny(el.parent.children,el.childIndex,prevAny)==false) return false
|
|
291
291
|
}
|
|
292
292
|
return true
|
|
293
293
|
}
|
|
294
294
|
function checkattributes(attributes=[],el){
|
|
295
295
|
let elattributes=el.attributes
|
|
296
296
|
let names=Object.keys(elattributes)
|
|
297
297
|
let passedTests=0
|
|
298
298
|
if(attributes) for(let i=0; i<attributes.length; i++){
|
|
299
299
|
let{name,value,check}=attributes[i]
|
|
300
300
|
if(name=='inner' && value!==undefined && check && el.inner){
|
|
301
301
|
if(check(el.inner)) passedTests++
|
|
302
302
|
}
|
|
303
303
|
if(!names.includes(name)) continue
|
|
304
304
|
else if(value==undefined) passedTests++
|
|
305
305
|
else if(value && elattributes[name]){
|
|
306
306
|
if(check(elattributes[name])==false) continue
|
|
307
307
|
else passedTests++
|
|
308
308
|
}
|
|
309
309
|
}
|
|
310
310
|
if(passedTests==attributes.length) return true
|
|
311
311
|
else return false
|
|
@@ -21,7 +21,7 @@ function buildStyle(attributes){
|
|
|
21
21
|
const styles=attributes.style || "";
|
|
22
22
|
con
|
|
23
23
|
class NodeClassList{
|
|
24
24
|
constructor(node){ this.node=node }
|
|
25
25
|
get classes(){ return (this.node.attributes.class || "").split(" ").filter(Boolean) }
|
|
26
26
|
set classes(val){ this.node.attributes.class=val.join(" ") }
|
|
27
27
|
contains(className){ return this.classes.includes(className) }
|
|
28
28
|
add(className){
|
|
29
29
|
const currentClasses=this.classes;
|
|
30
30
|
if (!currentClasses.includes(className)) this.classes=[...currentClasses,className];
|
|
31
31
|
}
|
|
32
32
|
remove(className){ this.classes=this.classes.filter(cls=>cls!==className); }
|
|
33
33
|
toggle(className){
|
|
34
34
|
if (this.classes.includes(className)) this.remove(className);
|
|
35
35
|
else this.add(className);
|
|
36
36
|
}
|
|
37
37
|
replace(oldClass,newClass){
|
|
38
38
|
if (this.classes.includes(oldClass)){
|
|
39
39
|
this.remove(oldClass);
|
|
40
40
|
this.add(newClass);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
function insertBefore(arr,index,newItem){
|
|
45
45
|
const existingIndex=arr.indexOf(newItem);
|
|
46
46
|
if (existingIndex!==-1) arr.splice(existingIndex,1);
|
|
47
47
|
arr.splice(index,0,newItem);
|
|
48
|
-
}
|
|
49
48
|
class Node{
|
|
50
49
|
constructor(tagName,attributes={},parent=null){
|
|
51
50
|
this.isSingle=false;
|
|
52
51
|
this.tagName=tagName;
|
|
53
52
|
this.attributes=attributes;
|
|
54
53
|
this.childNodes=[];
|
|
55
54
|
if (parent!==null) parent.childNodes.push(this)
|
|
56
55
|
this.parent=parent;
|
|
57
56
|
this._classList=null;
|
|
58
57
|
this.__style=null;
|
|
59
58
|
this._dataset=null
|
|
60
59
|
}
|
|
61
60
|
get id(){ return this.attributes.id ? this.attributes.id : null; }
|
|
62
61
|
set id(newValue){ this.attributes.id=newValue; }
|
|
63
62
|
get className(){return this.attributes.class || null}
|
|
64
63
|
get parentNode(){ return this.parent }
|
|
65
64
|
get ancestors(){
|
|
66
65
|
if(!this.parent) return []
|
|
67
66
|
const ancestors=[]
|
|
68
67
|
let element=this.parent
|
|
69
68
|
while (element.tagName!=='ROOT'){
|
|
70
69
|
ancestors.push(element)
|
|
71
70
|
element=element.parent
|
|
72
71
|
}
|
|
73
72
|
return ancestors.reverse()
|
|
74
73
|
}
|
|
75
74
|
get childNodeIndex(){
|
|
76
75
|
if(!this.parent) return null
|
|
77
76
|
return this.parent.childNodes ? this.parent.childNodes.indexOf(this) : null
|
|
78
77
|
}
|
|
79
78
|
get childIndex(){
|
|
80
79
|
if(!this.parent) return null
|
|
81
80
|
return this.parent.children ? this.parent.children.indexOf(this) : null
|
|
82
81
|
}
|
|
83
82
|
get previousElementSibling(){ return this.prev }
|
|
84
83
|
get prev(){
|
|
85
84
|
if (!this.childIndex) return null
|
|
86
85
|
return this.parent.children[this.childIndex-1]
|
|
87
86
|
}
|
|
88
87
|
get nextElementSibling(){ return this.next }
|
|
89
88
|
get next(){
|
|
90
89
|
if (!this.childIndex) return null
|
|
91
90
|
return this.parent.children[this.childIndex+1] || null
|
|
92
91
|
}
|
|
93
92
|
get dataset(){
|
|
94
93
|
if (!this._dataset) this._dataset=getDataset(this);
|
|
95
94
|
return this._dataset;
|
|
96
95
|
}
|
|
97
96
|
get classList(){
|
|
98
97
|
if (!this._classList) this._classList=new NodeClassList(this);
|
|
99
98
|
return this._classList;
|
|
100
99
|
}
|
|
101
100
|
get style(){
|
|
102
101
|
if (!this.__style) this.__style=buildStyle(this.attributes)
|
|
103
102
|
return this.__style
|
|
104
103
|
}
|
|
105
104
|
get outerHTML(){
|
|
106
105
|
const attrs=Object.entries(this.attributes).map(([key,val])=>val.length ? `${key}="${val}"` : key).join(" ");
|
|
107
106
|
return `<${this.tagName}${attrs ? ' '+attrs : ''}>${this.innerHTML}</${this.tagName}>`;
|
|
108
107
|
}
|
|
109
108
|
getAttribute(attrName){ return this.attributes[attrName] || null }
|
|
110
109
|
setAttribute(attrName,value){ this.attributes[attrName]=value }
|
|
111
110
|
removeAttribute(attrName){ delete this.attributes[attrName] }
|
|
112
111
|
remove(){
|
|
113
112
|
if (!this.parent) return
|
|
114
113
|
const index=this.childNodeIndex;
|
|
115
114
|
if (index!==null) this.parent.childNodes.splice(index,1);
|
|
116
115
|
}
|
|
117
116
|
get innerHTML(){
|
|
118
117
|
return this.childNodes.map(child=>{
|
|
119
118
|
if (child instanceof Node || child instanceof SingleNode) return child.outerHTML;
|
|
120
119
|
else if (child instanceof TextNode) return child.textContent;
|
|
121
120
|
else return child
|
|
122
121
|
}).join("");
|
|
123
122
|
}
|
|
124
123
|
$$(query){return this.querySelectorAll(query)}
|
|
125
124
|
querySelectorAll(query){
|
|
126
125
|
const selectors=Query.get(query)
|
|
127
126
|
return find(selectors,this,new Set())
|
|
128
127
|
}
|
|
129
128
|
$(query){return this.querySelector(query)}
|
|
130
129
|
querySelector(query){
|
|
131
130
|
const selectors=Query.get(query)
|
|
132
131
|
return find(selectors,this,new Set(),true)[0] || null
|
|
133
132
|
}
|
|
134
133
|
getElementsByClassName(query){ return this.querySelectorAll('.'+query) }
|
|
135
134
|
getElementsByTagName(query){ return this.querySelectorAll(query) }
|
|
136
135
|
getElementById(query){ return this.querySelector('#'+query) }
|
|
137
136
|
get children(){
|
|
138
137
|
return this.childNodes.filter(child=>{
|
|
139
138
|
if (!(child instanceof Node)) return false
|
|
140
139
|
if (child.tagName==='#comment') return false
|
|
141
140
|
return true
|
|
142
141
|
});
|
|
143
142
|
}
|
|
144
143
|
insertAdjacentElement(position,newElement){
|
|
145
144
|
if(newElement.tagName==='ROOT' && newElement.childNodes.length>0) newElement=newElement.childNodes[0]
|
|
146
145
|
const pos=position.toLowerCase();
|
|
147
146
|
if (pos==="afterbegin") this.childNodes.unshift(newElement);
|
|
148
147
|
else if (pos==="beforeend") this.childNodes.push(newElement);
|
|
149
148
|
newElement.parent=this
|
|
150
149
|
if (!this.parent) return newElement
|
|
151
150
|
if (pos==="beforebegin") insertBefore(this.parent.childNodes,this.childNodeIndex,newElement)
|
|
152
151
|
else if (pos==="afterend") this.parent.childNodes.splice(this.childNodeIndex+1,0,newElement);
|
|
153
152
|
newElement.parent=this.parent
|
|
154
153
|
return newElement
|
|
155
154
|
}
|
|
156
155
|
insertAdjacentHTML(position,html){
|
|
157
156
|
const newNode=parseHTML(html);
|
|
158
157
|
newNode.childNodes.reverse().forEach(node=>{
|
|
159
158
|
this.insertAdjacentElement(position,node);
|
|
160
159
|
});
|
|
161
160
|
return newNode
|
|
162
161
|
}
|
|
163
162
|
insertAdjacentText(position,text){
|
|
164
163
|
return this.insertAdjacentElement(position,new TextNode(text));
|
|
165
164
|
}
|
|
166
165
|
insert(position,element){
|
|
167
166
|
const positions=['beforebegin','afterbegin','beforeend','afterend']
|
|
168
167
|
if(positions[position]) position=positions[position]
|
|
169
168
|
if(typeof element==='string'){
|
|
170
169
|
element=element.trim()
|
|
171
170
|
if(element.startsWith('<') && element.endsWith('>')){
|
|
172
171
|
return this.insertAdjacentHTML(position,element)
|
|
173
172
|
}
|
|
174
173
|
return this.insertAdjacentText(position,element)
|
|
175
174
|
}
|
|
176
175
|
return this.insertAdjacentElement(position,element)
|
|
177
176
|
}
|
|
178
177
|
set innerHTML(html){
|
|
179
178
|
const parsed=parseHTML(html);
|
|
180
179
|
this.childNodes=parsed.childNodes;
|
|
181
180
|
}
|
|
182
181
|
set outerHTML(html){
|
|
183
182
|
const parsed=parseHTML(html);
|
|
184
183
|
if (!this.parent) return console.log('element has no parent node')
|
|
185
184
|
const index=this.childIndex
|
|
186
185
|
if (index!==null) this.parent.childNodes.splice(index,1,...parsed.childNodes);
|
|
187
186
|
}
|
|
188
187
|
appendChild(newChild){
|
|
189
188
|
if (newChild instanceof Node || newChild instanceof TextNode || newChild instanceof SingleNode){
|
|
190
189
|
if (newChild.parent) newChild.parent.childNodes=newChild.parent.childNodes.filter(child=>child!==newChild);
|
|
191
190
|
} else if(typeof newChild==='string') newChild=new TextNode(newChild)
|
|
192
191
|
else return newChild
|
|
193
192
|
this.childNodes.push(newChild);
|
|
194
193
|
newChild.parent=this;
|
|
195
194
|
return newChild;
|
|
196
195
|
}
|
|
197
196
|
get textContent(){
|
|
198
197
|
if (this.childNodes.length===0) return this.nodeName==='#text' ? this.nodeValue : '';
|
|
199
198
|
return this.childNodes.map(child=>{
|
|
200
199
|
if(child instanceof SingleNode) return ''
|
|
201
200
|
if(child instanceof TextNode) return child.nodeValue
|
|
202
201
|
if(child instanceof Node) return child.textContent;
|
|
203
202
|
else return child;
|
|
204
203
|
}).join(" ");
|
|
205
204
|
}
|
|
206
205
|
set textContent(value){
|
|
207
206
|
this.childNodes=[];
|
|
208
207
|
if (value!==null && value!==undefined){
|
|
209
208
|
this.childNodes.push(value.toString());
|
|
210
209
|
}
|
|
211
210
|
}
|
|
211
|
+
}
|
|
212
212
|
class Node{
|
|
213
213
|
constructor(tagName,attributes={},parent=null){
|
|
214
214
|
this.isSingle=false;
|
|
215
215
|
this.tagName=tagName;
|
|
216
216
|
this.attributes=attributes;
|
|
217
217
|
this.childNodes=[];
|
|
218
218
|
if (parent!==null) parent.childNodes.push(this)
|
|
219
219
|
this.parent=parent;
|
|
220
220
|
this._classList=null;
|
|
221
221
|
this.__style=null;
|
|
222
222
|
this._dataset=null
|
|
223
223
|
}
|
|
224
224
|
get id(){ return this.attributes.id ? this.attributes.id : null; }
|
|
225
225
|
set id(newValue){ this.attributes.id=newValue; }
|
|
226
226
|
get className(){return this.attributes.class || null}
|
|
227
227
|
get parentNode(){ return this.parent }
|
|
228
228
|
get ancestors(){
|
|
229
229
|
if(!this.parent) return []
|
|
230
230
|
const ancestors=[]
|
|
231
231
|
let element=this.parent
|
|
232
232
|
while (element.tagName!=='ROOT'){
|
|
233
233
|
ancestors.push(element)
|
|
234
234
|
element=element.parent
|
|
235
235
|
}
|
|
236
236
|
return ancestors.reverse()
|
|
237
237
|
}
|
|
238
238
|
get childNodeIndex(){
|
|
239
239
|
if(!this.parent) return null
|
|
240
240
|
return this.parent.childNodes ? this.parent.childNodes.indexOf(this) : null
|
|
241
241
|
}
|
|
242
242
|
get childIndex(){
|
|
243
243
|
if(!this.parent) return null
|
|
244
244
|
return this.parent.children ? this.parent.children.indexOf(this) : null
|
|
245
245
|
}
|
|
246
246
|
get previousElementSibling(){ return this.prev }
|
|
247
247
|
get prev(){
|
|
248
248
|
if (!this.childIndex) return null
|
|
249
249
|
return this.parent.children[this.childIndex-1]
|
|
250
250
|
}
|
|
251
251
|
get nextElementSibling(){ return this.next }
|
|
252
252
|
get next(){
|
|
253
253
|
if (!this.childIndex) return null
|
|
254
254
|
return this.parent.children[this.childIndex+1] || null
|
|
255
255
|
}
|
|
256
256
|
get dataset(){
|
|
257
257
|
if (!this._dataset) this._dataset=getDataset(this);
|
|
258
258
|
return this._dataset;
|
|
259
259
|
}
|
|
260
260
|
get classList(){
|
|
261
261
|
if (!this._classList) this._classList=new NodeClassList(this);
|
|
262
262
|
return this._classList;
|
|
263
263
|
}
|
|
264
264
|
get style(){
|
|
265
265
|
if (!this.__style) this.__style=buildStyle(this.attributes)
|
|
266
266
|
return this.__style
|
|
267
267
|
}
|
|
268
268
|
get outerHTML(){
|
|
269
269
|
const attrs=Object.entries(this.attributes).map(([key,val])=>val.length ? `${key}="${val}"` : key).join(" ");
|
|
270
270
|
return `<${this.tagName}${attrs ? ' '+attrs : ''}>${this.innerHTML}</${this.tagName}>`;
|
|
271
271
|
}
|
|
272
272
|
getAttribute(attrName){ return this.attributes[attrName] || null }
|
|
273
273
|
setAttribute(attrName,value){ this.attributes[attrName]=value }
|
|
274
274
|
removeAttribute(attrName){ delete this.attributes[attrName] }
|
|
275
275
|
remove(){
|
|
276
276
|
if (!this.parent) return
|
|
277
277
|
const index=this.childNodeIndex;
|
|
278
278
|
if (index!==null) this.parent.childNodes.splice(index,1);
|
|
279
279
|
}
|
|
280
280
|
get innerHTML(){
|
|
281
281
|
return this.childNodes.map(child=>{
|
|
282
282
|
if (child instanceof Node || child instanceof SingleNode) return child.outerHTML;
|
|
283
283
|
else if (child instanceof TextNode) return child.textContent;
|
|
284
284
|
else return child
|
|
285
285
|
}).join("");
|
|
286
286
|
}
|
|
287
287
|
$$(query){return this.querySelectorAll(query)}
|
|
288
288
|
querySelectorAll(query){
|
|
289
289
|
const selectors=Query.get(query)
|
|
290
290
|
return find(selectors,this,new Set())
|
|
291
291
|
}
|
|
292
292
|
$(query){return this.querySelector(query)}
|
|
293
293
|
querySelector(query){
|
|
294
294
|
const selectors=Query.get(query)
|
|
295
295
|
return find(selectors,this,new Set(),true)[0] || null
|
|
296
296
|
}
|
|
297
297
|
getElementsByClassName(query){ return this.querySelectorAll('.'+query) }
|
|
298
298
|
getElementsByTagName(query){ return this.querySelectorAll(query) }
|
|
299
299
|
getElementById(query){ return this.querySelector('#'+query) }
|
|
300
300
|
get children(){
|
|
301
301
|
return this.childNodes.filter(child=>{
|
|
302
302
|
if (!(child instanceof Node)) return false
|
|
303
303
|
if (child.tagName==='#comment') return false
|
|
304
304
|
return true
|
|
305
305
|
});
|
|
306
306
|
}
|
|
307
307
|
insertAdjacentElement(position,newElement){
|
|
308
308
|
if(newElement.tagName==='ROOT' && newElement.childNodes.length>0) newElement=newElement.childNodes[0]
|
|
309
309
|
const pos=position.toLowerCase();
|
|
310
310
|
if(pos==='afterbegin' || pos==='beforeend'){
|
|
311
311
|
if (pos==="afterbegin") this.childNodes.unshift(newElement);
|
|
312
312
|
else if (pos==="beforeend") this.childNodes.push(newElement);
|
|
313
313
|
newElement.parent=this
|
|
314
314
|
return newElement
|
|
315
315
|
}
|
|
316
316
|
if (!this.parent) throw new Error("Can't insert element to element without parent")
|
|
317
317
|
if (pos==="beforebegin") insertBefore(this.parent.childNodes,this.childNodeIndex,newElement)
|
|
318
318
|
else if (pos==="afterend") this.parent.childNodes.splice(this.childNodeIndex+1,0,newElement);
|
|
319
319
|
newElement.parent=this.parent
|
|
320
320
|
return newElement
|
|
321
321
|
}
|
|
322
322
|
insertAdjacentHTML(position,html){
|
|
323
323
|
const newNode=parseHTML(html);
|
|
324
324
|
newNode.childNodes.reverse().forEach(node=>{
|
|
325
325
|
this.insertAdjacentElement(position,node);
|
|
326
326
|
});
|
|
327
327
|
return newNode
|
|
328
328
|
}
|
|
329
329
|
insertAdjacentText(position,text){
|
|
330
330
|
return this.insertAdjacentElement(position,new TextNode(text));
|
|
331
331
|
}
|
|
332
332
|
insert(position,element){
|
|
333
333
|
const positions=['beforebegin','afterbegin','beforeend','afterend']
|
|
334
334
|
if(positions[position]) position=positions[position]
|
|
335
335
|
if(typeof element==='string'){
|
|
336
336
|
element=element.trim()
|
|
337
337
|
if(element.startsWith('<') && element.endsWith('>')){
|
|
338
338
|
return this.insertAdjacentHTML(position,element)
|
|
339
339
|
}
|
|
340
340
|
return this.insertAdjacentText(position,element)
|
|
341
341
|
}
|
|
342
342
|
return this.insertAdjacentElement(position,element)
|
|
343
343
|
}
|
|
344
344
|
set innerHTML(html){
|
|
345
345
|
const parsed=parseHTML(html);
|
|
346
346
|
this.childNodes=parsed.childNodes;
|
|
347
347
|
}
|
|
348
348
|
set outerHTML(html){
|
|
349
349
|
const parsed=parseHTML(html);
|
|
350
350
|
if (!this.parent) return console.log('element has no parent node')
|
|
351
351
|
const index=this.childIndex
|
|
352
352
|
if (index!==null) this.parent.childNodes.splice(index,1,...parsed.childNodes);
|
|
353
353
|
}
|
|
354
354
|
appendChild(newChild){
|
|
355
355
|
if (newChild instanceof Node || newChild instanceof TextNode || newChild instanceof SingleNode){
|
|
356
356
|
if (newChild.parent) newChild.parent.childNodes=newChild.parent.childNodes.filter(child=>child!==newChild);
|
|
357
357
|
} else if(typeof newChild==='string') newChild=new TextNode(newChild)
|
|
358
358
|
else return newChild
|
|
359
359
|
this.childNodes.push(newChild);
|
|
360
360
|
newChild.parent=this;
|
|
361
361
|
return newChild;
|
|
362
362
|
}
|
|
363
363
|
get textContent(){
|
|
364
364
|
if (this.childNodes.length===0) return this.nodeName==='#text' ? this.nodeValue : '';
|
|
365
365
|
return this.childNodes.map(child=>{
|
|
366
366
|
if(child instanceof SingleNode) return ''
|
|
367
367
|
if(child instanceof TextNode) return child.nodeValue
|
|
368
368
|
if(child instanceof Node) return child.textContent;
|
|
369
369
|
else return child;
|
|
370
370
|
}).join(" ");
|
|
371
371
|
}
|
|
372
372
|
set textContent(value){
|
|
373
373
|
this.childNodes=[];
|
|
374
374
|
if (value!==null && value!==undefined){
|
|
375
375
|
this.childNodes.push(value.toString());
|
|
376
376
|
}
|
|
377
377
|
}
|
|
378
378
|
}
|
|
379
379
|
class SingleNode extends Node{
|
|
380
380
|
constructor(tagName,attributes={},parent=null){
|
|
381
381
|
if(attributes['?'] && tagName==='?xml') delete attributes['?']
|
|
382
382
|
super(tagName,attributes,parent);
|
|
383
383
|
this.isSingle=true
|
|
384
384
|
}
|
|
385
385
|
get outerHTML(){
|
|
386
386
|
if (this.tagName==="#cdata-section") return `<![CDATA[${this.nodeValue}]]>`;
|
|
387
387
|
const attrs=Object.entries(this.attributes).map(([key,val])=>val.length ? `${key}="${val}"` : key).join(" ");
|
|
388
388
|
return `<${this.tagName} ${attrs}${this.tagName==='?xml' ? '?' : ''}>`;
|
|
389
389
|
}
|
|
390
390
|
get innerHTML(){ return ""; }
|
|
391
391
|
set innerHTML(_){ }
|
|
392
392
|
$(_){return null}
|
|
393
393
|
$$(_){return []}
|
|
394
394
|
querySelectorAll(_){ return []; }
|
|
395
395
|
querySelector(_){ return null; }
|
|
396
396
|
getElementsByClassName(_){ return []; }
|
|
397
397
|
getElementsByTagName(_){ return []; }
|
|
398
398
|
getElementById(_){ return null; }
|
|
399
399
|
get children(){ return []; }
|
|
400
400
|
insertAdjacentElement(_,__){ }
|
|
401
401
|
insertAdjacentHTML(_,__){ }
|
|
402
402
|
insertAdjacentText(_,__){ }
|
|
403
403
|
appendChild(_){ }
|
|
404
404
|
insert(_,__){ }
|
|
405
405
|
get textContent(){ return ""; }
|
|
406
406
|
set textContent(_){ }
|
|
407
407
|
}
|