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