1 |
function dumptree (code) { |
2 |
var resultRoot = document.getElementById ('test-result'); |
3 |
var node; |
4 |
try { |
5 |
node = code (); |
6 |
dumpnode (node, resultRoot); |
7 |
} catch (e) { |
8 |
resultRoot.appendChild (document.createTextNode (e)); |
9 |
} |
10 |
|
11 |
var testCode = document.getElementById ('test-code'); |
12 |
var testCodeText = testCode.textContent ? testCode.textContent |
13 |
: testCode.innerText; |
14 |
resultRoot.appendChild (document.createElement ('pre')) |
15 |
.appendChild (document.createTextNode (testCodeText)); |
16 |
} /* dumptree */ |
17 |
|
18 |
function dumpnode (node, parent) { |
19 |
parent.appendChild (document.createElement ('strong')) |
20 |
.appendChild (document.createTextNode (node.nodeName)); |
21 |
var docl = parent.appendChild (document.createElement ('dl')); |
22 |
addinfo (docl, 'nodeType', document.createTextNode (node.nodeType)); |
23 |
addinfo (docl, 'nodeValue', node.nodeValue == null |
24 |
? document.createTextNode ('(null)') |
25 |
: document.createTextNode (node.nodeValue)); |
26 |
if (node.childNodes.length > 0) { |
27 |
addinfo (docl, 'childNodes'); |
28 |
for (var i = 0; i < node.childNodes.length; i++) { |
29 |
var ddel = docl.appendChild (document.createElement ('dd')); |
30 |
dumpnode (node.childNodes[i], ddel); |
31 |
} |
32 |
} |
33 |
} /* dumpnode */ |
34 |
|
35 |
function addinfo (parent, dttext, dd) { |
36 |
parent.appendChild (document.createElement ('dt')) |
37 |
.appendChild (document.createTextNode (dttext)); |
38 |
if (dd) { |
39 |
var ddel = parent.appendChild (document.createElement ('dd')); |
40 |
ddel.appendChild (dd); |
41 |
} |
42 |
} /* addinfo */ |
43 |
|