dom/tests/js/DumpTree.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/js/DumpTree.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,44 @@
     1.4 +/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +//
    1.11 +// travers the html tree and dump out the type of element
    1.12 +//
    1.13 +function traverse(node, indent)
    1.14 +{
    1.15 +    dump("\n")
    1.16 +    indent += "  "
    1.17 +    var type = node.nodeType;
    1.18 +
    1.19 +    // if it's an element dump the tag and recurse the children
    1.20 +    if (type == Node.ELEMENT_NODE) {
    1.21 +
    1.22 +        dump(indent + node.tagName)
    1.23 +
    1.24 +        // go through the children
    1.25 +        if (node.hasChildNodes()) {
    1.26 +            var children = node.childNodes;
    1.27 +            var length = children.length;
    1.28 +            var count = 0;
    1.29 +            while(count < length) {
    1.30 +                child = children[count]
    1.31 +                traverse(child, indent)
    1.32 +                count++
    1.33 +            }
    1.34 +        }
    1.35 +    }
    1.36 +    // it's just text, no tag, dump "Text"
    1.37 +    else if (type == Node.TEXT_NODE) {
    1.38 +        dump(indent + "Text")
    1.39 +    }
    1.40 +}
    1.41 +
    1.42 +var node = document.documentElement
    1.43 +
    1.44 +traverse(node, "")
    1.45 +dump("\n")
    1.46 +
    1.47 +  
    1.48 \ No newline at end of file

mercurial