This commit is contained in:
Andreas Rumpf 2017-07-04 08:04:04 +02:00
commit 5f7f33bb02
3 changed files with 33 additions and 8 deletions

View file

@ -282,7 +282,7 @@ proc diff(newNode, oldNode: VNode; parent, current: Node; kxi: KaraxInstance): E
echo "newNode ", newNode.kind, " oldNode ", oldNode.kind, " eq ", eq(newNode, oldNode)
if oldNode.kind == VNodeKind.text:
echo oldNode.text
return
#return
#doAssert false, "overflow!"
inc kxi.recursion
result = eq(newNode, oldNode)
@ -315,7 +315,7 @@ proc diff(newNode, oldNode: VNode; parent, current: Node; kxi: KaraxInstance): E
let r = if isSpecial:
diff(a[i], b[j], parent, current, kxi)
else:
(assert j < current.len;
(assert same(b, current); #(j < current.len, "j: " & $j & " len " & $current.len);
diff(a[i], b[j], current, current.childNodes[j], kxi))
case r
of identical, changed, similar:
@ -327,7 +327,7 @@ proc diff(newNode, oldNode: VNode; parent, current: Node; kxi: KaraxInstance): E
of different:
# undo what 'diff' would have done:
kxi.patchLen = oldLen
#if result != different: result = r
if result != different: result = r
break
# compute common prefix:
while commonPrefix < minLength:
@ -341,7 +341,6 @@ proc diff(newNode, oldNode: VNode; parent, current: Node; kxi: KaraxInstance): E
eqAndUpdate(newNode, newPos, oldNode, oldPos, cstring"suffix"):
dec oldPos
dec newPos
echo "came here"
let pos = min(oldPos, newPos) + 1
# now the different children are in commonPrefix .. pos - 1:

View file

@ -19,15 +19,18 @@ proc shortRepr(n: VNode): string =
for i in 0..<n.len:
result &= " " & shortRepr(n[i])
var err = 0
proc doDiff(a, b: VNode; expected: varargs[string]) =
discard diff(b, a, nil, vnodeToDom(a, kxi), kxi)
for i in 0..<kxi.patchLen:
let p = $kxi.patches[i].k & " " & shortRepr(kxi.patches[i].n)
echo "got ", p
if i >= expected.len:
echo "patches differ; expected nothing but got: ", p
inc err
elif p != expected[i]:
echo "patches differ; expected ", expected[i], " but got: ", p
inc err
#hasDom(kxi.currentTree)
kxi.patchLen = 0
@ -53,7 +56,24 @@ proc testInsert() =
li: text "A"
li: text "B"
li: text "C"
doDiff(a, b, "pkInsert li B")
doDiff(a, b, "pkInsertBefore li B")
proc testInsert2() =
let a = buildHtml(tdiv):
tdiv:
tdiv:
ul:
li: text "A"
li: text "D"
let b = buildHtml(tdiv):
tdiv:
tdiv:
ul:
li: text "A"
li: text "B"
li: text "C"
li: text "D"
doDiff(a, b, "pkInsertBefore li B", "pkInsertBefore li C")
proc testDelete() =
let a = buildHtml(tdiv):
@ -70,6 +90,11 @@ proc testDelete() =
kxi = KaraxInstance(rootId: cstring"ROOT", renderer: proc (): VNode = discard)
#testAppend()
testAppend()
testInsert()
#testDelete()
testInsert2()
testDelete()
if err == 0:
echo "Success"
else:
quit 1

View file

@ -13,5 +13,6 @@ proc main =
exec("nim js examples/scrollapp/scrollapp.nim")
exec("nim js examples/mediaplayer/playerapp.nim")
exec("nim js examples/carousel/carousel.nim")
exec("nim js -d:nodejs -r tests/difftest.nim")
main()