@slyte/html-parser 2.0.0-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.
@@ -0,0 +1,986 @@
1
+ import htmlToJsObjectLyteRendered from "./htmlToJsObjectLyteRendered.js";
2
+ const MAX_DEPTH = 512;
3
+ let dNegateItems = {
4
+ '__events__':true,
5
+ 'errorInfo':true,
6
+ 'attr':true,
7
+ 'node':true,
8
+ 'text':true,
9
+ 'attrType':true,
10
+ // 'parent':true,
11
+ // 'prev':true,
12
+ // 'next':true,
13
+ 'cont':true,
14
+ '_parent':true,
15
+ 'child':true
16
+ }
17
+
18
+ // Skip keys that can pollute Object.prototype when copied via defineProperty
19
+ let dangerousCloneKeys = {
20
+ '__proto__': true,
21
+ 'constructor': true,
22
+ 'prototype': true
23
+ };
24
+
25
+ function setNodeCompile(node, compile) {
26
+ if (!node || !compile) {
27
+ return;
28
+ }
29
+ Object.defineProperty(node, 'Compile', {
30
+ value: compile,
31
+ writable: true,
32
+ configurable: true,
33
+ enumerable: false
34
+ });
35
+ }
36
+
37
+ function getNodeCompile(node) {
38
+ if (!node || !Object.prototype.hasOwnProperty.call(node, 'Compile')) {
39
+ return undefined;
40
+ }
41
+ return node.Compile;
42
+ }
43
+
44
+ /**
45
+ * Parse HTML for public DOM-like setters (innerHTML / outerHTML).
46
+ * Inherits the host node's strict policy from `_opt` (same as toObject: default true).
47
+ */
48
+ function resolveNodeStrict(node) {
49
+ return node && node._opt && node._opt.strict;
50
+ }
51
+
52
+ function parseHtmlFromSetter(hostNode, html) {
53
+ const tagName = hostNode.tagName;
54
+ return htmlToJsObjectLyteRendered.toObject(String(html == null ? "" : html), {
55
+ fromInternal: true,
56
+ enhanced: true,
57
+ withoutTableProcessing: true,
58
+ withoutTemplateConversion: true,
59
+ hasParentSvg: tagName == tagName.toLowerCase(),
60
+ fromSub: true,
61
+ Compile: getNodeCompile(hostNode),
62
+ strict: resolveNodeStrict(hostNode)
63
+ }).childNodes;
64
+ }
65
+
66
+ function inheritNodeOpt(fromNode, toNode) {
67
+ if (fromNode && toNode && fromNode._opt && Object.keys(fromNode._opt).length) {
68
+ toNode._opt = { ...fromNode._opt };
69
+ }
70
+ }
71
+
72
+ function deepCopyObject( obj ) {
73
+ let current, copies = [{source : obj, target : Object.create(Object.getPrototypeOf(obj))}], keys, propertyIndex, descriptor, nextSource, sourceReferences = [obj];
74
+ let cloneObject = copies[0].target, targetReferences = [cloneObject];
75
+ let referenceChecker = new WeakMap();
76
+ while(current = copies.shift()){
77
+ keys = Object.getOwnPropertyNames(current.source);
78
+ for(propertyIndex = 0; propertyIndex < keys.length; propertyIndex++){
79
+ if (dangerousCloneKeys[keys[propertyIndex]]) {
80
+ continue;
81
+ }
82
+ descriptor = Object.getOwnPropertyDescriptor(current.source, keys[propertyIndex]);
83
+ if(!descriptor.value || typeof descriptor.value != "object"){
84
+ Object.defineProperty(current.target, keys[propertyIndex], descriptor);
85
+ continue;
86
+ }
87
+ nextSource = descriptor.value;
88
+ descriptor.value = Array.isArray(nextSource) ? [] : Object.create(Object.getPrototypeOf(nextSource));
89
+ let indexOf = referenceChecker.get(nextSource);
90
+ if(indexOf){
91
+ descriptor.value = targetReferences[indexOf];
92
+ Object.defineProperty(current.target, keys[propertyIndex], descriptor);
93
+ continue;
94
+ }
95
+ sourceReferences.push(nextSource);
96
+ referenceChecker.set(nextSource,sourceReferences.length-1);
97
+ targetReferences.push(descriptor.value);
98
+ Object.defineProperty(current.target, keys[propertyIndex], descriptor);
99
+ let res = {source : nextSource, target : descriptor.value};
100
+ if(keys[propertyIndex] == "child"){
101
+ res._parent = current.source;
102
+ }
103
+ if(current._parent){
104
+ res.parent = current._parent;
105
+ }
106
+ if(current.parent){
107
+ current.source.parent = current.parent;
108
+ }
109
+ if (dNegateItems[keys[propertyIndex]] || !(current.source instanceof Node)) {
110
+ copies.push(res);
111
+ }
112
+ }
113
+ }
114
+ return cloneObject;
115
+ }
116
+ function getSelectorType(selector){
117
+ let index;
118
+ if(selector[0]=='.') { return {_attr:"class",_attrVal:selector.substring(1),single:1}; }
119
+ else if(selector[0]=='#') { return {_attr:"id",_attrVal:selector.substring(1),single:1}; }
120
+ else if((index = selector.search('\\[')) != -1){
121
+ let tagName;
122
+ if(index != 0){
123
+ [tagName,selector] = selector.split('[');
124
+ tagName = tagName.trim();
125
+ selector = "["+selector;
126
+ }
127
+ let res;
128
+ if(selector.search('=')!=-1){
129
+ let searchItem = selector.substring(1,selector.length-1).split("=");
130
+ let attr = searchItem[0];
131
+ let attrVal = searchItem[1].replace(/^"+|"+$/g, '');
132
+ attrVal = attrVal.replace(/^'+|'+$/g, '');
133
+ res = {_attr:attr,_attrVal:attrVal,single:1};
134
+ }
135
+ else {
136
+ let attr = selector.substring(1,selector.length-1);
137
+ res = {_attr:attr,_attrVal:null,single:2};
138
+ }
139
+ if(tagName){
140
+ res.tagName = tagName;
141
+ }
142
+ return res;
143
+ };
144
+ return {tagName:selector,_attrVal:null,single:3};
145
+ }
146
+ function deepCopyNode(obj,hasSeenSvg){
147
+ if(!obj){return}
148
+ obj = new Node(obj);
149
+ if(hasSeenSvg && obj.tag == "template" && obj.cont){
150
+ let temp = obj.content.childNodes,ls = obj.child = [];
151
+ obj.child = temp;
152
+ delete obj.cont;
153
+ }
154
+ if(!hasSeenSvg && obj.tag == "template" && obj.child ){
155
+ let temp = obj.child;
156
+ obj.content = obj.createDocumentFragment();
157
+ obj.cont.child=temp;
158
+ delete obj.child;
159
+ }
160
+ if(obj.tag == "svg"){
161
+ hasSeenSvg = hasSeenSvg || true;
162
+ }
163
+ let prev;
164
+ if(obj.content){
165
+ obj.content = new Node(obj.content);
166
+ let ls = [];
167
+ for(let i in obj.content.child){
168
+ let ref = obj.content.child[i] = deepCopyNode(obj.content.child[i],hasSeenSvg);
169
+ if(prev){
170
+ prev.next = ref;
171
+ }
172
+ ref.parent = obj.content;
173
+ ref.prev = prev;
174
+ prev = ref;
175
+ ls.push(ref);
176
+ }
177
+ obj.content.child = ls;
178
+
179
+ }
180
+ else if (obj.childNodes){
181
+ let ls = [];
182
+ for(let i in obj.child){
183
+ let ref = obj.child[i] = deepCopyNode(obj.child[i],hasSeenSvg);
184
+ if(prev){
185
+ prev.next = ref;
186
+ }
187
+ ref.parent = obj;
188
+ ref.prev = prev;
189
+ prev = ref;
190
+ ls.push(ref);
191
+ }
192
+ obj.child = ls;
193
+ }
194
+ return obj;
195
+ }
196
+ class Node{
197
+ _opt = {};
198
+ constructor(obj) {
199
+ this.node = obj.node;
200
+ (obj.hasOwnProperty("parent")) ? this.parent = obj.parent : this.parent = null;
201
+ (obj.hasOwnProperty("prev")) ? this.prev = obj.prev : this.prev = null;
202
+ (obj.hasOwnProperty("next")) ? this.next = obj.next : this.next = null;
203
+ if (obj.hasOwnProperty("tag")) { this.tag = obj.tag; }
204
+ if(obj.hasOwnProperty("text")) { this.text = obj.text; }
205
+ if(obj.hasOwnProperty("attr")) { this.attr = Object.assign({},obj.attr); }
206
+ if(obj.hasOwnProperty("child")) { this.child = obj.child; }
207
+ if(obj.hasOwnProperty("attrType")) { this.attrType = Object.assign({},obj.attrType) }
208
+ else if(obj.node == "#element"){
209
+ this.attrType = {};
210
+ }
211
+ {
212
+ const compile = getNodeCompile(obj);
213
+ if (compile) {
214
+ setNodeCompile(this, compile);
215
+ }
216
+ }
217
+ if(obj.check){
218
+ this._opt = {...obj.check};
219
+ }
220
+ if(obj.hasOwnProperty("errorInfo")){ this.errorInfo = obj.errorInfo}
221
+ if(obj.hasOwnProperty("cont")){this.cont = obj.content}
222
+ }
223
+ /**
224
+ * @param {{}} check
225
+ */
226
+ set setOpt(opt){
227
+ if(opt){
228
+ this._opt = {...opt};
229
+ }
230
+ }
231
+ refreshNode(NodeStruct = false){
232
+ let nodes = this.querySelectorAll("tbody",true);
233
+ let ds = {};
234
+ let value = null;
235
+ for(let node of nodes){
236
+ let parent = node.parent || node._parent
237
+ let index = parent.child.indexOf(node);
238
+ let temp = null;
239
+ let count = 0;
240
+ if (parent.tag != "table"){
241
+ parent.child.splice(parent.child.indexOf(node), 1);
242
+ let ptr = parent;
243
+ while(parent){
244
+ if (parent.tag == "table" && temp) {
245
+ let ls = []
246
+ index = parent.child.indexOf(temp);
247
+ if (parent.hasAttribute("haveChangesInTbody")) {
248
+ value = parent.attr.haveChangesInTbody;
249
+ }
250
+ else {
251
+ count++;
252
+ parent.setAttribute("haveChangesInTbody", count.toString());
253
+ value = count.toString();
254
+ }
255
+ if(ds[value]){
256
+ ds[value].push({ node: node, index: index});
257
+ }
258
+ else{
259
+ ds[value] = [{ node: node, index: index}]
260
+ }
261
+ if(node.next && node.next.node == "#text"){
262
+ ds[value].push({ node: node.next, index: index});
263
+ ptr.child.splice(ptr.child.indexOf(node.next), 1);
264
+ }
265
+ break;
266
+ };
267
+ temp = parent;
268
+ parent = parent.parent
269
+ }
270
+ }
271
+ }
272
+ let ls = [];
273
+ for(let i in ds){
274
+ let parent = this.querySelector("[haveChangesInTbody=" + i + "]",true);
275
+ let key = parent.attr.haveChangesInTbody;
276
+ let childToAdd = []
277
+ let index = null
278
+ for(let j of ds[i]){
279
+ childToAdd.push(j.node);
280
+ index = j.index
281
+ }
282
+ let child = parent.child
283
+ if (index == child.length - 1) {
284
+ ls = child.concat(childToAdd);
285
+ }
286
+ else{
287
+ ls = (child.slice(0,index)).concat(childToAdd).concat(child.slice(index));
288
+ }
289
+ parent.child = []
290
+ for (let j = 0; j < ls.length;j++){
291
+ parent.appendChild(ls[j]);
292
+ }
293
+ parent.removeAttribute("haveChangesInTbody");
294
+ }
295
+ htmlToJsObjectLyteRendered.processTable(this);
296
+ if (NodeStruct) {
297
+ let list = [];
298
+ list.push(this);
299
+ while (list.length > 0) {
300
+ let obj = list.splice(0, 1);
301
+ if (obj.child) {
302
+ for (let child of obj.child) {
303
+ child = new Node(child);
304
+ list.push(child);
305
+ }
306
+ }
307
+ }
308
+ }
309
+ }
310
+
311
+ get content(){
312
+ return this.cont;
313
+ }
314
+
315
+ set content(docFag){
316
+ this.cont = docFag;
317
+ docFag._parent = this;
318
+ }
319
+
320
+ get children(){
321
+ if(this.child){
322
+ let obj = this.child;
323
+ let res = [];
324
+ for(let i=0; i<obj.length; i++){
325
+ if(obj[i].node== "#element") { res.push(obj[i]) }
326
+ }
327
+ return res;
328
+ }
329
+ return [];
330
+ }
331
+
332
+ //.childNodes
333
+ get childNodes(){
334
+ return this.child;
335
+ }
336
+ //.outerObj()
337
+ get outerObj(){
338
+ return (this.node!= "#document-fragment")?/*deepCopyNode(*/this/*)*/:undefined;
339
+ }
340
+
341
+ set outerObj(obj){
342
+ if(obj){
343
+ let tagName = this.tagName;
344
+ obj = obj.childNodes;
345
+ // let obj = htmlToJsObjectLyteRendered.toObject(html, { fromInternal:true, enhanced: true,withoutTableProcessing:true,withoutTemplateConversion:true,hasParentSvg:(tagName == tagName.toLowerCase()),fromSub:true ,Compile: this.__proto__.Compile}).childNodes;
346
+ if (this.node != "#document-fragment") {
347
+ let child = this.parent.childNodes;
348
+ let index = child.indexOf(this);
349
+ // child = [child.slice(0, index+1),...obj,child.slice(index+1)];
350
+ let secondHalf = child.slice(index + 1);
351
+ this.parent.child = child.slice(0, index);
352
+ for(let i of obj){
353
+ this.parent.appendChild(deepCopyNode(i,(tagName == tagName.toLowerCase())));
354
+ }
355
+ for(let i of secondHalf){
356
+ this.parent.appendChild(i);
357
+ }
358
+ }
359
+ }
360
+ }
361
+ //.outerHTML()
362
+ get outerHTML(){
363
+ return (this.node!= "#document-fragment")?htmlToJsObjectLyteRendered.toHtml(this):undefined;
364
+ }
365
+
366
+ set outerHTML(html){
367
+ let obj = parseHtmlFromSetter(this, html);
368
+ if (this.node != "#document-fragment") {
369
+ let child = this.parent.childNodes;
370
+ let index = child.indexOf(this);
371
+ // child = [child.slice(0, index+1),...obj,child.slice(index+1)];
372
+ let secondHalf = child.slice(index + 1);
373
+ this.parent.child = child.slice(0, index);
374
+ for(let i of obj){
375
+ this.parent.appendChild(i);
376
+ }
377
+ for(let i of secondHalf){
378
+ this.parent.appendChild(i);
379
+ }
380
+ }
381
+ }
382
+
383
+ //.innerObj()
384
+ get innerObj(){
385
+ let obj = this;
386
+ obj = (!obj.content) ? obj.childNodes : obj.content.childNodes;
387
+ return obj;
388
+ }
389
+
390
+ set innerObj(obj){
391
+ let tagName = this.tagName;
392
+ if (tagName == "TEMPLATE") {
393
+ this.content.child = [];
394
+ if(obj.length != undefined){
395
+ for(let i of obj){
396
+ this.content.appendChild(deepCopyNode(i,(tagName == tagName.toLowerCase())));
397
+ }
398
+ }
399
+ else{
400
+ this.content.appendChild(deepCopyNode(obj,(tagName == tagName.toLowerCase())));
401
+ }
402
+ }
403
+ else {
404
+ this.child = []
405
+ if(obj.length != undefined){
406
+ for(let i of obj){
407
+ this.appendChild(deepCopyNode(i,(tagName == tagName.toLowerCase())));
408
+ }
409
+ }
410
+ else{
411
+ this.appendChild(deepCopyNode(obj,(tagName == tagName.toLowerCase())));
412
+ }
413
+ }
414
+ }
415
+
416
+ //.innerHTML()
417
+ get innerHTML(){
418
+ let res = "";
419
+ let obj = this;
420
+ obj = (!obj.content) ? obj.childNodes : obj.content.childNodes;
421
+ for(let i=0; i<obj.length; i++){
422
+ res += htmlToJsObjectLyteRendered.toHtml(obj[i]);
423
+ }
424
+ return res;
425
+ }
426
+
427
+ set innerHTML(html){
428
+ let tagName = this.tagName
429
+ let obj = parseHtmlFromSetter(this, html);
430
+ if (tagName == "TEMPLATE" && this.content) {
431
+ this.content.child = [];
432
+ this.content.appendChildArr(obj);
433
+ }
434
+ else {
435
+ this.child = []
436
+ this.appendChildArr(obj);
437
+ }
438
+ }
439
+ prepend(child,deleteInPreviousParent){
440
+ if (child.parent && child.parent.child && deleteInPreviousParent) {
441
+ child.remove();
442
+ }
443
+ if(child.node == "#document-fragment"){
444
+ return this.appendChildArr(child.child);
445
+ }
446
+ if (this.child && this.child.length) {
447
+ for(let i of this.child){
448
+ if(i==child){
449
+ i.remove();
450
+ break;
451
+ }
452
+ }
453
+ let next = this.child[0];
454
+ next.prev = child;
455
+ child.parent = this;
456
+ child.prev = null;
457
+ child.next = next;
458
+ }
459
+ else {
460
+ this.child = [];
461
+ child.parent = this;
462
+ child.prev = null;
463
+ child.next = null;
464
+ }
465
+ this.child.unshift(child);
466
+ if(child){
467
+ child.setOpt = this._opt;
468
+ }
469
+ return this;
470
+ }
471
+ //replaceChild()
472
+ replaceChild(newChild){
473
+ let oldChild=this;
474
+ newChild.parent=this.parent;
475
+ newChild.prev=this.prev;
476
+ newChild.next=this.next;
477
+ let index=this.parent.child.indexOf(this);
478
+ this.parent.child.splice(index,1,newChild);
479
+ if(this.next)
480
+ { this.next.prev = newChild; }
481
+ if(this.prev)
482
+ { this.prev.next = newChild; }
483
+
484
+ newChild.content.appendChild(oldChild);
485
+ return newChild;
486
+ }
487
+ //.appendChild()
488
+ appendChild(child,deleteInPreviousParent){
489
+ if (child.parent && child.parent.child && deleteInPreviousParent) {
490
+ child.remove();
491
+ }
492
+ if(child.node == "#document-fragment"){
493
+ return this.appendChildArr(child.child);
494
+ }
495
+ if (this.child && this.child.length) {
496
+ for(let i of this.child){
497
+ if(i==child){
498
+ i.remove();
499
+ break;
500
+ }
501
+ }
502
+ let prev = this.child[this.child.length-1];
503
+ child.parent = this;
504
+ child.prev = prev;
505
+ child.next = null;
506
+ prev.next = child;
507
+ }
508
+ else {
509
+ this.child = [];
510
+ child.parent = this;
511
+ child.prev = null;
512
+ child.next = null;
513
+ }
514
+ this.child.push(child);
515
+ if(child){
516
+ child.setOpt = this._opt;
517
+ }
518
+ return this;
519
+ }
520
+
521
+ appendChildArr(childArr){
522
+ for(let i=0; i<childArr.length;i++){
523
+ this.appendChild(childArr[i]);
524
+ }
525
+ return this;
526
+ }
527
+
528
+ //.hasAttribute()
529
+ hasAttribute(attr){
530
+ return (this.attr && this.attr[attr] != undefined)? true : false;
531
+ }
532
+
533
+ //.hasAttributes()
534
+ hasAttributes(){
535
+ return (this.attr && Object.keys(this.attr).length > 0) ? true : false;
536
+ }
537
+ //.getAttribute()
538
+ getAttribute(attr){
539
+ return (this.attr && this.attr[attr] != undefined) ? this.attr[attr] : null;
540
+ }
541
+
542
+ //.hasChildNodes()
543
+ hasChildNodes(){
544
+ return (this.childNodes && this.childNodes.length > 0) ? true : false;
545
+ }
546
+
547
+ get firstChild(){
548
+ return (this.child[0]) ? this.child[0] : null;
549
+ }
550
+
551
+ //.setAttribute()
552
+ setAttribute(attr,value){
553
+ if(!this.attr) { this.attr = {}; }
554
+ if(this._opt && !this._opt.fromInternal && this._opt.withoutTemplateConversion && typeof value == "string"){
555
+ let singleQuot = value.indexOf("'");
556
+ let doubleQuot = value.indexOf('"');
557
+ if (!this.attrType) {
558
+ this.attrType = { ___attrStart___: {},___nullCheck___:{} };
559
+ } else if (!this.attrType.___attrStart___) {
560
+ this.attrType.___attrStart___ = {};
561
+ }
562
+ else if(!this.attrType.___nullCheck___){
563
+ this.attrType.___nullCheck___ = {};
564
+ }
565
+ this.attrType.___attrStart___[attr] = singleQuot!=-1 && doubleQuot!=-1 ? undefined : singleQuot!=-1 ? '"' : doubleQuot!=-1 ? "'" : undefined;
566
+ }
567
+ if (typeof value == "string" || typeof value == "number") {
568
+ this.attr[attr] = value.toString();
569
+ }
570
+ else{
571
+ this.attr[attr] =JSON.stringify(value);
572
+ }
573
+ return;
574
+ }
575
+
576
+ //.tagName()
577
+ get tagName(){
578
+ if(this.tag){
579
+ let tag = this.tag;
580
+ if(tag == "svg"){
581
+ return tag.toLowerCase();
582
+ }
583
+ let obj = this;
584
+ if(!obj.replacedNode){
585
+ while(obj){
586
+ if(obj.tag == "svg" || obj.hasParentSvg) { return tag.toLowerCase(); }
587
+ if(obj.tag =="foreignObject"){
588
+ break;
589
+ }
590
+ obj = obj.parent;
591
+ }
592
+ }
593
+ return tag.toUpperCase();
594
+ }
595
+ return null;
596
+ }
597
+
598
+ //.nextSibling()
599
+ get nextSibling(){
600
+ return this.next;
601
+ }
602
+
603
+ //.nextElementSibling()
604
+ get nextElementSibling(){
605
+ let obj = this.next;
606
+ while (obj) {
607
+ if(obj.node == "#element") { return obj; }
608
+ obj = obj.next;
609
+ }
610
+ return null;
611
+ }
612
+
613
+ //.previousSibling()
614
+ get previousSibling(){
615
+ return this.prev;
616
+ }
617
+
618
+ //.previousElementSibling()
619
+ get previousElementSibling(){
620
+ let obj = this.prev;
621
+ while(obj){
622
+ if(obj.node == "#element") { return obj; }
623
+ obj = obj.prev;
624
+ }
625
+ return null;
626
+ }
627
+ updateNode(newNode){
628
+ let parent = this.parent
629
+ let ind = parent.child.indexOf(this);
630
+ newNode.prev = this.prev;
631
+ newNode.next = this.next;
632
+ newNode.parent = parent;
633
+ if(this.prev){
634
+ this.prev.next = newNode;
635
+ }
636
+ parent.child.splice(ind,1,newNode)
637
+ }
638
+ //.parentElement()
639
+ get parentElement(){
640
+ return (this.parent && this.parent.node=="#element")? this.parent : null;
641
+ }
642
+
643
+ //.parentNode()
644
+ get parentNode(){
645
+ return this.parent;
646
+ }
647
+
648
+ nodeParentHasSvg() {
649
+ let obj = this
650
+ while(obj){
651
+ if(this.tag == "svg") { return true; }
652
+ obj = obj.parent;
653
+ }
654
+ return false;
655
+ }
656
+
657
+ deepCopyObject(){
658
+ return new Node(this);
659
+ }
660
+ //.nodeName()
661
+ get nodeName() {
662
+ let obj = this;
663
+ if(this.nodeType==2 || this.nodeType == 3){
664
+ return this.node;
665
+ }
666
+ if(obj.node && obj.node!="#document-fragment" && obj.tagName){
667
+ let node = obj.tagName.toUpperCase();
668
+ while(obj){
669
+ if(obj.tag == "svg") { return node.toLowerCase(); }
670
+ obj = obj.parent;
671
+ }
672
+ return node.toUpperCase();
673
+ }
674
+ return null;
675
+ }
676
+
677
+ //.nodeType()
678
+ get nodeType(){
679
+ let check = {"#element":1,'#text':3,'#comment':8,attr:2,'#document-fragment':11};
680
+ return check[this.node] || ((this.ownerElement)? 2:undefined);
681
+ }
682
+
683
+ //.nodeValue()
684
+ get nodeValue(){
685
+ if(this.nodeType==8 || this.nodeType==3) { return this.text; }
686
+ if(this.nodeType==2){
687
+ return this.value;
688
+ }
689
+ return null;
690
+ }
691
+
692
+ set nodeValue(textValue){
693
+ if(this.nodeType == 8 || this.nodeType == 3){
694
+ this.text = textValue;
695
+ }
696
+ if(this.nodeType == 2){
697
+ this.ownerElement.setAttribute(this.nodeName, textValue);
698
+ }
699
+ }
700
+ //.remove()
701
+ remove(){
702
+ let index = this.parent.child.indexOf(this);
703
+ if(this.prev) {
704
+ this.prev.next = this.next;
705
+ }
706
+ if(this.next) {
707
+ this.next.prev = this.prev;
708
+ }
709
+ this.parent.child.splice(index,1);
710
+ }
711
+ get firstElementSibling(){
712
+ for (let i = 0; i < this.child.length;i++){
713
+ if(this.child[i].node == "#element"){
714
+ return this.child[i];
715
+ }
716
+ }
717
+ return undefined;
718
+ }
719
+ //.removeAttribute()
720
+ removeAttribute(attr){
721
+ if (this.attr) {
722
+ delete this.attr[attr];
723
+ }
724
+ if(this.attr && Object.keys(this.attr).length==0) { delete this.attr; }
725
+ }
726
+ //.replaceWith()
727
+ replaceWith(newNode) {
728
+ newNode.parent = this.parent;
729
+ newNode.prev = this.prev;
730
+ newNode.next = this.next;
731
+ this.replacedNode = true;
732
+ newNode.replacedNode = true;
733
+ let index = this.parent.child.indexOf(this);
734
+ this.parent.child.splice(index,1,newNode)
735
+ if(this.next)
736
+ { this.next.prev = newNode; }
737
+ if(this.prev)
738
+ { this.prev.next = newNode; }
739
+ }
740
+
741
+ //.attributes()
742
+ get attributes(){
743
+ let attr = this.attr;
744
+ let res = [];
745
+ for(let i in attr){
746
+ res[i] = this.createAttribute(i);
747
+ res[i].value = (attr[i]) ? attr[i] : "";
748
+ res.push(res[i]);
749
+ }
750
+ return (this.attr)?res:[];
751
+ }
752
+
753
+ //.localName()
754
+ get localName(){
755
+ return this.tag?this.tag:null;
756
+ }
757
+
758
+ //.cloneNode()
759
+ cloneNode(flag){
760
+ let res={}
761
+ res = new Node((flag)?deepCopyObject(this):this);
762
+
763
+ if(this.attr)
764
+ { res.attr = Object.assign({},this.attr )};
765
+ if(this.parent)
766
+ { res.parent = null; }
767
+ if(this.prev)
768
+ { res.prev = null; }
769
+ if(this.next)
770
+ { res.next = null; }
771
+ if(res.tagName == "TEMPLATE" && !flag){
772
+ res.content = this.createDocumentFragment();
773
+ }
774
+ return res;
775
+ }
776
+
777
+ createAttribute(nodeName){
778
+ let temp = new Node({node:nodeName});
779
+ temp.value = "";
780
+ temp.name = nodeName;
781
+ temp.ownerElement = this
782
+ return temp;
783
+ }
784
+
785
+ createDocumentFragment(){
786
+ let node = new Node({ node: "#document-fragment", child: [], cont: [] });
787
+ setNodeCompile(node, getNodeCompile(this));
788
+ inheritNodeOpt(this, node);
789
+ return node;
790
+ }
791
+
792
+ //.createElement()
793
+ createElement(tag,options = {next:null,prev:null,parent:null,attr:{}}){
794
+
795
+ let node = {};
796
+ if (options.attr)
797
+ { node = { node: "#element", tag: tag.toLowerCase(), prev: options.prev, next: options.next, parent: options.parent, attr: options.attr }; }
798
+ else
799
+ { node = { node: "#element", tag: tag.toLowerCase(), prev: options.prev, next: options.next, parent: options.parent, attr: {} }; }
800
+
801
+ if(options.attrType){
802
+ node.attrType = options.attrType
803
+ }
804
+ if (options.errorInfo) {
805
+ node.errorInfo = options.errorInfo;
806
+ }
807
+ node = new Node(node);
808
+ setNodeCompile(node, getNodeCompile(this));
809
+ inheritNodeOpt(this, node);
810
+ if(node.tag == "template"){
811
+ node.content = this.createDocumentFragment();
812
+ }else{
813
+ node.child = [];
814
+ }
815
+ return node;
816
+ }
817
+ //.createTextNode()
818
+ createTextNode(text,options = {next:null,prev:null,parent:null}){
819
+ let node = new Node({node:"#text",prev:options.prev,next:options.next,parent:options.parent,text:text});
820
+ setNodeCompile(node, getNodeCompile(this));
821
+ inheritNodeOpt(this, node);
822
+ node.attrType = options.attrType
823
+ if(options.lineIndex){
824
+ node.lineIndex = options.lineIndex;
825
+ }
826
+ if(options.colIndex){
827
+ node.colIndex = options.colIndex;
828
+ }
829
+ return node;
830
+ }
831
+
832
+ //.querySelector()
833
+ findItem(dontCheckTemplate,obj,attr,attrVal,single = 1,tag = undefined){
834
+ if (dontCheckTemplate && obj.tag == "template" && obj.tagName == "TEMPLATE") {
835
+ obj = obj.content.child;
836
+ }
837
+ else {
838
+ obj = obj.child;
839
+ }
840
+ if (!obj || (obj.attr && obj.attr.dontConsiderForDefault == "true")) { return null; }
841
+ let res = null;
842
+ for(let i=0; i<obj.length; i++){
843
+ if (obj[i].attr && obj[i].attr.dontConsiderForDefault == "true") { continue; }
844
+ if(single==1 && obj[i].attr && obj[i].attr[attr]==attrVal)
845
+ { return obj[i]; }
846
+ if(single==2 && obj[i].attr && this.find(obj[i].attr, attr))
847
+ { return obj[i]; }
848
+ if(single==3 && obj[i].tag == attr)
849
+ { return obj[i]; }
850
+ if(single==4 && obj[i].attr && obj[i].attr[attr]==attrVal && obj[i].tag == tag)
851
+ { return obj[i]; }
852
+ else {
853
+ res = this.findItem(dontCheckTemplate, obj[i], attr, attrVal, single, tag);
854
+ }
855
+ if(res) { break; }
856
+ }
857
+ return res;
858
+ }
859
+
860
+ querySelector(selector,dontCheckTemplate=false) {
861
+ if(selector[0]=='.') { return this.findItem(dontCheckTemplate,this,"class",selector.substring(1)); }
862
+ else if(selector[0]=='#') { return this.findItem(dontCheckTemplate,this,"id",selector.substring(1)); }
863
+ else if(selector[0]=='['){
864
+ if(selector.search('=')!=-1){
865
+ let searchItem = selector.substring(1,selector.length-1).split("=");
866
+ let attr = searchItem[0];
867
+ let attrVal = searchItem[1].replace(/^"+|"+$/g, '');
868
+ attrVal = attrVal.replace(/^'+|'+$/g, '');
869
+ return this.findItem(dontCheckTemplate,this,attr,attrVal);
870
+ }
871
+ else {
872
+ let attr = selector.substring(1, selector.length - 1);
873
+ return this.findItem(dontCheckTemplate,this, attr, null, 2);
874
+ }
875
+ }
876
+ else if(selector.match(/^\w+\[.+=.+]$/gm)){
877
+ selector = selector.split("[");
878
+ let tag = selector[0];
879
+ selector = selector[1].split("=");
880
+ let attr = selector[0].replace(/^"+|"+$/g, '').replace(/^'+|'+$/g, '');
881
+ let attrVal = selector[1].substring(0,selector[1].length-1).replace(/^"+|"+$/g, '').replace(/^'+|'+$/g, '');
882
+
883
+ return this.findItem(dontCheckTemplate,this, attr, attrVal, 4, tag);
884
+ }
885
+ return this.findItem(dontCheckTemplate,this,selector,null,3);
886
+ }
887
+
888
+ find(obj, find){
889
+ if (obj) {
890
+ for (let i in obj) {
891
+ if (i == find) {
892
+ return true;
893
+ }
894
+ }
895
+ }
896
+ return false;
897
+ }
898
+
899
+ //.querySelectorAll()
900
+ findAll(obj,listToCheck,dontCheckTemplate,result = [],depth = 0){
901
+ if (depth > MAX_DEPTH) {
902
+ throw new Error("Parse Error: HTML nesting depth exceeds the maximum allowed (" + MAX_DEPTH + ").");
903
+ }
904
+ obj = ((obj.tag == "template" && dontCheckTemplate && obj.tagName == "TEMPLATE") ? obj.content.child : obj.child);
905
+ if(!obj){ return result;}
906
+ for (let i=0; i<obj.length; i++){
907
+ for(let lsItem of listToCheck){
908
+ let {single,_attr,_attrVal,tagName} = lsItem;
909
+ let tagNameCheck = (tagName==undefined || obj[i].tag==tagName);
910
+ if(single == 1 && obj[i].attr && obj[i].attr[_attr] == _attrVal && tagNameCheck)
911
+ { result.push(obj[i]); break; }
912
+ else if (single == 2 && obj[i].attr && this.find(obj[i].attr, _attr) && tagNameCheck)
913
+ { result.push(obj[i]); break; }
914
+ else if(single == 3 && obj[i].tag == tagName)
915
+ { result.push(obj[i]); break; }
916
+ }
917
+ this.findAll(obj[i],listToCheck,dontCheckTemplate,result,depth + 1);
918
+ }
919
+ return result;
920
+ }
921
+ querySelectorAll(selector,dontCheckTemplate){
922
+ let multiList = [];
923
+ if((multiList = selector.split(',')).length>1){
924
+ for(let index in multiList){
925
+ multiList[index] = getSelectorType(multiList[index].trim());
926
+ }
927
+ return this.findAll(this,multiList,dontCheckTemplate);
928
+ }
929
+ return this.findAll(this,[getSelectorType(selector)],dontCheckTemplate);
930
+ }
931
+
932
+ //Cases
933
+ // let defaultCase = node.querySelectorChildren("[default]")
934
+ // let isBreak = currentCase.querySelectorChildren("template[is=break]");
935
+ // let isBreak = dCaseTemplate.querySelectorChildren("template[is=break]");
936
+
937
+ querySelectorChildren(string){
938
+ let children = this.child;
939
+ if(string.startsWith("#")){
940
+ return;
941
+ }else if(string.startsWith("[")){
942
+ let attrString = string.slice(1,string.length-1)
943
+ for(let i=0; i<children.length; i++){
944
+ if(children[i].attr && children[i].attr.hasOwnProperty(attrString)){
945
+ return children[i];
946
+ }
947
+ }
948
+ }else if(string.indexOf("[") != -1){
949
+ let arr = string.split("[");
950
+ let tagName = arr[0];
951
+ let attrString = arr[1].slice(0,arr[1].length-1);
952
+ let attrStringArr = attrString.split("=");
953
+ let attrName = attrStringArr[0];
954
+ let attrVal = attrStringArr[1];
955
+ for(let i=0; i<children.length; i++){
956
+ if(children[i].tag && children[i].tag == tagName){
957
+ if(children[i].attr[attrName] && children[i].attr[attrName] == attrVal){
958
+ return children[i];
959
+ }
960
+ }
961
+ }
962
+ }
963
+ }
964
+
965
+ // Cases
966
+ // let cases = node.querySelectorAllChildren("[case]")
967
+ querySelectorAllChildren(string){
968
+ let children = this.child,
969
+ output = [];
970
+ if(string.startsWith("#")){
971
+ return output;
972
+ }else if(string.startsWith("[")){
973
+ let attrString = string.slice(1,string.length-1)
974
+ for(let i=0; i<children.length; i++){
975
+ if(children[i].attr && children[i].attr[attrString]){
976
+ output.push(children[i])
977
+ }
978
+ }
979
+ }
980
+ return output;
981
+ }
982
+ json2html(obj) { return htmlToJsObjectLyteRendered.toHtml(obj) }
983
+ }
984
+
985
+ export { setNodeCompile, getNodeCompile };
986
+ export default Node;