Skip to content

Commit d34cb64

Browse files
committed
Fix parsing bug, global DOM update
1 parent e2618a7 commit d34cb64

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

example/config.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,22 @@ class Counter extends tApp.Component {
4646
for(let i = 0; i < this.children.length; i++) {
4747
this.children[i].destroy();
4848
}
49-
return (`<div>
50-
[[
49+
return (`<div>[[
5150
CounterButton
5251
{
5352
text: "-",
5453
incrementor: -1
5554
}
56-
]]
57-
[[
55+
]][[
5856
CounterText
5957
{}
60-
]]
61-
[[
58+
]][[
6259
CounterButton
6360
{
6461
text: "+",
6562
incrementor: 1
6663
}
67-
]]
68-
</div>`);
64+
]]</div>`);
6965
}
7066
}
7167

tApp.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class tApp {
88
static database;
99
static currentHash = "/";
1010
static get version() {
11-
return "v0.10.1";
11+
return "v0.10.2";
1212
}
1313
static configure(params) {
1414
if(params == null) {
@@ -432,22 +432,32 @@ class tApp {
432432
return new DOMParser().parseFromString(html, "text/html").body.childNodes[0];
433433
}
434434
}
435+
function compareNode(before, after) {
436+
if(before.nodeName != after.nodeName) {
437+
return false;
438+
}
439+
return true;
440+
}
435441
function compareChildren(before, after) {
436442
if(before.childNodes.length != after.childNodes.length) {
437443
return false;
438444
}
439445
for(let i = 0; i < before.childNodes.length; i++) {
440-
if(before.childNodes[i].nodeName != after.childNodes[i].nodeName) {
441-
return false;
442-
}
443-
if(before.childNodes[i].getAttribute("tapp-component") != after.childNodes[i].getAttribute("tApp-component")) {
446+
if(!compareNode(before, after)) {
444447
return false;
445448
}
446449
}
447450
return true;
448451
}
449452
function convertNode(before, after) {
450-
if(after.attributes != null) {
453+
if(before.attributes != null && after.attributes != null) {
454+
for(let i = 0; i < before.attributes.length; i++) {
455+
if(before.attributes.item(i).nodeName == "value") {
456+
before.value = "";
457+
} else {
458+
before.removeAttribute(before.attributes.item(i).nodeName);
459+
}
460+
}
451461
for(let i = 0; i < after.attributes.length; i++) {
452462
if(after.attributes.item(i).nodeName == "value") {
453463
before.value = after.value;
@@ -491,8 +501,6 @@ class tApp {
491501
pointerBefore++;
492502
pointerAfter++;
493503
}
494-
//console.log("before", beforeChildren, beforeChildren.map(child => {if(child != null){ return child.data }else{ return "null"}}));
495-
//console.log("after", afterChildren, afterChildren.map(child => {if(child != null){ return child.data }else{ return "null"}}));
496504
for(let i = 0; i < beforeChildren.length; i++) {
497505
let nullBefore = beforeChildren.length == beforeChildren.filter(el => el == null || el.nodeName == "#text").length;
498506
if(beforeChildren[i] == null && afterChildren[i] == null) {
@@ -533,6 +541,9 @@ class tApp {
533541
for(let i = 0; i < els.length; i++) {
534542
convertNode(els[i], compiled);
535543
}
544+
for(let i = 0; i < component.children.length; i++) {
545+
tApp.updateComponent(component.children[i]);
546+
}
536547
}
537548
static compileComponent(component, props = {}, parent = "global") {
538549
function htmlToDOM(html) {
@@ -644,8 +655,6 @@ class tApp {
644655
let newStrList = [];
645656
let tmpLoader = "";
646657
for(let i = 0; i < str.length; i++) {
647-
if(newLineStack.length > 0 || (i > 820 && i < 880)) {
648-
}
649658
if(tmpLoader == "" && ["[", "]", "{", "}"].includes(str[i])) {
650659
newStrList.push(str[i]);
651660
tmpLoader = str[i];
@@ -658,16 +667,28 @@ class tApp {
658667
newStrList.push(str[i]);
659668
}
660669
newLineStack.push(tmpLoader);
661-
tmpLoader = "";
670+
if(["[", "]", "{", "}"].includes(str[i])) {
671+
tmpLoader = str[i];
672+
} else {
673+
tmpLoader = "";
674+
}
662675
} else if((newLineStack[newLineStack.length - 1] == "{{{" && tmpLoader == "}}}") || (newLineStack[newLineStack.length - 1] == "[[" && tmpLoader == "]]")) {
663676
newStrList.push(str[i]);
664677
newLineStack.pop();
665-
tmpLoader = "";
678+
if(["[", "]", "{", "}"].includes(str[i])) {
679+
tmpLoader = str[i];
680+
} else {
681+
tmpLoader = "";
682+
}
666683
} else if(str[i] == tmpLoader[0]) {
667684
newStrList.push(str[i]);
668685
tmpLoader += str[i];
669686
} else {
670-
tmpLoader = "";
687+
if(["[", "]", "{", "}"].includes(str[i])) {
688+
tmpLoader = str[i];
689+
} else {
690+
tmpLoader = "";
691+
}
671692
if(newLineStack[newLineStack.length - 1] == "{{{" && str[i] == "\n") {
672693
newStrList.push(";");
673694
} else if(newLineStack[newLineStack.length - 1] == "[[" && str[i] == "\n") {
@@ -780,7 +801,7 @@ class tApp {
780801
newRes = newRes.replace(next.value[0], contextEval[0]);
781802
next = it.next();
782803
}
783-
it = newRes.matchAll(new RegExp("\[\[[\\s|\\t]*(.+?(?=\]\]))[\\s|\\t]*\]\]", "g"));
804+
it = newRes.matchAll(new RegExp("\\[\\[[\\s|\\t]*(.+?(?=\\]\\]))[\\s|\\t]*\\]\\]", "g"));
784805
next = it.next();
785806
while(!next.done) {
786807
newRes = newRes.replace(next.value[0], tApp.compileComponent(next.value[1], options, componentParent));
@@ -1048,7 +1069,7 @@ tApp.Component = class {
10481069
return state;
10491070
}
10501071
this.state = recursivelySetState(key, val, this.state);
1051-
tApp.updateComponent(this);
1072+
tApp.updateComponent(tApp.GlobalComponent);
10521073
return val;
10531074
}
10541075
render(props) {

0 commit comments

Comments
 (0)