DOM diff problem: delete useless comments
This commit is contained in:
parent
686caaaf77
commit
ff8bbf1676
1 changed files with 2 additions and 53 deletions
55
karax.nim
55
karax.nim
|
|
@ -110,10 +110,6 @@ proc equals(a, b: VNode): bool =
|
||||||
return true
|
return true
|
||||||
|
|
||||||
proc equalsTree(a, b : VNode): bool =
|
proc equalsTree(a, b : VNode): bool =
|
||||||
# kout cstring("equalsTree: (" & $a.kind & " = " & a.id & ") -> (" &
|
|
||||||
# $b.kind & " = " & b.id & ")")
|
|
||||||
# assert a != nil and b != nil
|
|
||||||
# kout cstring("equalsTree")
|
|
||||||
if not equals(a, b):
|
if not equals(a, b):
|
||||||
return false
|
return false
|
||||||
else:
|
else:
|
||||||
|
|
@ -125,16 +121,12 @@ proc equalsTree(a, b : VNode): bool =
|
||||||
return true
|
return true
|
||||||
|
|
||||||
proc updateElement(parent, current: Node, newNode, oldNode: VNode) =
|
proc updateElement(parent, current: Node, newNode, oldNode: VNode) =
|
||||||
#kout cstring("updateElement: (" & current.nodeName & " = " & current.id & ")")
|
|
||||||
if not equals(newNode, oldNode):
|
if not equals(newNode, oldNode):
|
||||||
let n = vnodeToDom(newNode)
|
let n = vnodeToDom(newNode)
|
||||||
if parent == nil:
|
if parent == nil:
|
||||||
replaceById("ROOT", n)
|
replaceById("ROOT", n)
|
||||||
else:
|
else:
|
||||||
parent.replaceChild(n, current)
|
parent.replaceChild(n, current)
|
||||||
#kout cstring("---- replaceChild")
|
|
||||||
#kout cstring("ReplaceChild: (" & current.nodeName & " = " & newNode.id & ") -> (" &
|
|
||||||
# n.nodeName & " = " & oldNode.id & ")")
|
|
||||||
elif newNode.kind != VNodeKind.text:
|
elif newNode.kind != VNodeKind.text:
|
||||||
let newLength = newNode.len
|
let newLength = newNode.len
|
||||||
var oldLength = oldNode.len
|
var oldLength = oldNode.len
|
||||||
|
|
@ -157,68 +149,26 @@ proc updateElement(parent, current: Node, newNode, oldNode: VNode) =
|
||||||
dec newPos
|
dec newPos
|
||||||
|
|
||||||
var pos = min(oldPos, newPos) + 1
|
var pos = min(oldPos, newPos) + 1
|
||||||
|
|
||||||
# kout cstring("commonPrefix = " & $commonPrefix)
|
|
||||||
# kout cstring("oldPos = " & $oldPos)
|
|
||||||
# kout cstring("newPos = " & $newPos)
|
|
||||||
# kout cstring("newLength = " & $newLength)
|
|
||||||
# kout cstring("oldLength = " & $oldLength)
|
|
||||||
# kout cstring("-----------")
|
|
||||||
# for i in items(newNode):
|
|
||||||
# kout cstring(i.id)
|
|
||||||
# kout cstring("-----")
|
|
||||||
# for i in items(oldNode):
|
|
||||||
# kout cstring(i.id)
|
|
||||||
|
|
||||||
for i in commonPrefix..pos-1:
|
for i in commonPrefix..pos-1:
|
||||||
updateElement(current, current.childNodes[i],
|
updateElement(current, current.childNodes[i],
|
||||||
newNode[i],
|
newNode[i],
|
||||||
oldNode[i])
|
oldNode[i])
|
||||||
#kout cstring(newNode[i].id & " " & oldNode[i].id)
|
|
||||||
#kout cstring("----------")
|
|
||||||
|
|
||||||
var nextChildPos = oldPos + 1
|
var nextChildPos = oldPos + 1
|
||||||
# kout cstring("pos = " & $pos)
|
|
||||||
# kout cstring("nextChildPos = " & $nextChildPos)
|
|
||||||
while pos <= newPos:
|
while pos <= newPos:
|
||||||
if nextChildPos == oldLength:
|
if nextChildPos == oldLength:
|
||||||
current.appendChild(vnodeToDom(newNode[pos]))
|
current.appendChild(vnodeToDom(newNode[pos]))
|
||||||
#kout cstring"---- appendChild"
|
|
||||||
# kout cstring("commonPrefix = " & $commonPrefix)
|
|
||||||
# kout cstring("oldPos = " & $oldPos)
|
|
||||||
# kout cstring("newPos = " & $newPos)
|
|
||||||
# kout cstring("newLength = " & $newLength)
|
|
||||||
# kout cstring("oldLength = " & $oldLength)
|
|
||||||
|
|
||||||
# kout cstring("pos = " & $pos)
|
|
||||||
# kout cstring("nextChildPos = " & $nextChildPos)
|
|
||||||
# kout cstring("-----------")
|
|
||||||
#kout cstring("pos = " & $pos)
|
|
||||||
#kout cstring("newLength = " & $newLength)
|
|
||||||
else:
|
else:
|
||||||
#kout cstring"---- insertBefore"
|
|
||||||
current.insertBefore(vnodeToDom(newNode[pos]), current.childNodes[nextChildPos])
|
current.insertBefore(vnodeToDom(newNode[pos]), current.childNodes[nextChildPos])
|
||||||
|
# added new Node, so old state of VDOM have one more Node
|
||||||
inc oldLength
|
inc oldLength
|
||||||
inc pos
|
inc pos
|
||||||
inc nextChildPos
|
inc nextChildPos
|
||||||
|
|
||||||
for i in 0..oldPos-pos:
|
for i in 0..oldPos-pos:
|
||||||
#kout cstring"---- removeChild"
|
|
||||||
current.removeChild(current.childNodes[pos])
|
current.removeChild(current.childNodes[pos])
|
||||||
|
|
||||||
# var itNew = newPos + 1
|
# previous version of DOM diff algorithm
|
||||||
# var itOld = oldPos + 1
|
|
||||||
# while itNew < newLength and itOld < oldLength:
|
|
||||||
# updateElement(current, current.childNodes[itNew],
|
|
||||||
# newNode[itNew],
|
|
||||||
# oldNode[itOld])
|
|
||||||
|
|
||||||
# kout cstring("commonPrefix = " & $commonPrefix)
|
|
||||||
# kout cstring("oldPos = " & $oldPos)
|
|
||||||
# kout cstring("newPos = " & $newPos)
|
|
||||||
# kout cstring("newLength = " & $newLength)
|
|
||||||
# kout cstring("oldLength = " & $oldLength)
|
|
||||||
|
|
||||||
# for i in 0..min(newLength, oldLength)-1:
|
# for i in 0..min(newLength, oldLength)-1:
|
||||||
# updateElement(current, current.childNodes[i],
|
# updateElement(current, current.childNodes[i],
|
||||||
# newNode[i],
|
# newNode[i],
|
||||||
|
|
@ -232,7 +182,6 @@ proc updateElement(parent, current: Node, newNode, oldNode: VNode) =
|
||||||
# kout cstring"removeChild"
|
# kout cstring"removeChild"
|
||||||
# current.removeChild(current.lastChild)
|
# current.removeChild(current.lastChild)
|
||||||
|
|
||||||
|
|
||||||
proc dodraw() =
|
proc dodraw() =
|
||||||
let newtree = dorender()
|
let newtree = dorender()
|
||||||
newtree.id = "ROOT"
|
newtree.id = "ROOT"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue