dom/tests/js/HTMLString.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/js/HTMLString.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,66 @@
     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 +// return a string representing the html content in html format 
    1.12 +//
    1.13 +function htmlString(node, indent)
    1.14 +{
    1.15 +    var html = ""
    1.16 +    indent += "  "
    1.17 +
    1.18 +    var type = node.nodeType
    1.19 +    if (type == Node.ELEMENT) {
    1.20 +
    1.21 +        // open tag
    1.22 +        html += "\n" + indent + "<" + node.tagName
    1.23 +
    1.24 +        // dump the attributes if any
    1.25 +        attributes = node.attributes
    1.26 +        if (null != attributes) {
    1.27 +            var countAttrs = attributes.length
    1.28 +            var index = 0
    1.29 +            while(index < countAttrs) {
    1.30 +                att = attributes[index]
    1.31 +                if (null != att) {
    1.32 +                    html += " "
    1.33 +                    html += att.name + "=" + att.value;
    1.34 +                }
    1.35 +                index++
    1.36 +            }
    1.37 +        }
    1.38 +
    1.39 +        // end tag
    1.40 +        html += ">"
    1.41 +
    1.42 +        // recursively dump the children
    1.43 +        if (node.hasChildNodes) {
    1.44 +            // get the children
    1.45 +            var children = node.childNodes
    1.46 +            var length = children.length
    1.47 +            var count = 0;
    1.48 +            while(count < length) {
    1.49 +                child = children[count]
    1.50 +                html += htmlString(child, indent)
    1.51 +                count++
    1.52 +            }
    1.53 +        }
    1.54 +
    1.55 +        // close tag
    1.56 +        html += "\n" + indent + "</" + node.tagName + ">"
    1.57 +    }
    1.58 +    // if it's a piece of text just dump the text
    1.59 +    else if (type == Node.TEXT) {
    1.60 +        html += node.data
    1.61 +    }
    1.62 +
    1.63 +    return html;
    1.64 +}
    1.65 +
    1.66 +htmlString(document.documentElement, "") 
    1.67 +
    1.68 +
    1.69 +  
    1.70 \ No newline at end of file

mercurial