dom/tests/js/HTMLString.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 //
     8 // return a string representing the html content in html format 
     9 //
    10 function htmlString(node, indent)
    11 {
    12     var html = ""
    13     indent += "  "
    15     var type = node.nodeType
    16     if (type == Node.ELEMENT) {
    18         // open tag
    19         html += "\n" + indent + "<" + node.tagName
    21         // dump the attributes if any
    22         attributes = node.attributes
    23         if (null != attributes) {
    24             var countAttrs = attributes.length
    25             var index = 0
    26             while(index < countAttrs) {
    27                 att = attributes[index]
    28                 if (null != att) {
    29                     html += " "
    30                     html += att.name + "=" + att.value;
    31                 }
    32                 index++
    33             }
    34         }
    36         // end tag
    37         html += ">"
    39         // recursively dump the children
    40         if (node.hasChildNodes) {
    41             // get the children
    42             var children = node.childNodes
    43             var length = children.length
    44             var count = 0;
    45             while(count < length) {
    46                 child = children[count]
    47                 html += htmlString(child, indent)
    48                 count++
    49             }
    50         }
    52         // close tag
    53         html += "\n" + indent + "</" + node.tagName + ">"
    54     }
    55     // if it's a piece of text just dump the text
    56     else if (type == Node.TEXT) {
    57         html += node.data
    58     }
    60     return html;
    61 }
    63 htmlString(document.documentElement, "") 

mercurial