- assigned_to: nobody --> balls
Using the plain Tcl implementation, normalize runs into an error:
can't read "parent(node:childNodes)": no such variable
The reason is, that local variables ("node", "child") are passed to the Normalize procedure, which are not known at level #0 and thus the first line in Normalize (upvar #0 $pVar parent) will not associate a known variable.
The fix is just to remove the "#0" there.
Fixing this, more errors appear in the following code lines: The child node merged into its previous sibling is accessed after beeing unset.
I've managed to pass this by unsetting $n two lines later (and, hopefully, at the right level).
Finally, the first text node has to be updated with the data of the removed nodes. As a node is an array, the update statement has to be
array set $textNode [array get text]
I'm not sure that the last two changes are correct as a fix, as some event binding and internal node updating is involved. At least, normalize modifies the dom tree to an expected result.
The patch for dom.tcl is
-------------------------------------
@@ -2297,7 +2297,7 @@
# Adjacent text nodes are coalesced
proc dom::tcl::Element:Normalize {pVar nodes} {
- upvar #0 $pVar parent
+ upvar $pVar parent
set textNode {}
@@ -2320,11 +2320,11 @@
upvar #0 $parent(node:childNodes) childNodes
set idx [lsearch $childNodes $n]
set childNodes [lreplace $childNodes $idx $idx]
- unset $n
set cleanup [list event postMutationEvent [node parent $n] DOMSubtreeModified]
event postMutationEvent $n DOMNodeRemoved
+ uplevel #0 [list unset $n]
- set $textNode [array get text]
+ array set $textNode [array get text]
} else {
set textNode $n
catch {unset text}
---------------------------------------
A test for normalize, that initally runs into an error, is
package require dom
set docNode [::dom::parse {<outer>more<inner/>text</outer>}]
::dom::destroy [set [::dom::document getElementsByTagName $docNode inner]]
# just check that the node has disappeared
::dom::serialize $docNode
set outerNode [set [::dom::document getElementsByTagName $docNode outer]]
set childrenNodeVar [dom::node cget $outerNode -childNodes]
# still two text nodes -> 2
llength [set $childrenNodeVar]
# error: can't read "parent(node:childNodes)": no such variable
::dom::element normalize $outerNode
# normalize has done it's job: -> 1
llength [set $childrenNodeVar]