diff -r 000000000000 -r 6474c204b198 browser/metro/base/tests/unit/test_util_populateFragmentFromString.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/browser/metro/base/tests/unit/test_util_populateFragmentFromString.js Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,86 @@ +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +Components.utils.import("resource:///modules/ContentUtil.jsm"); +let Util = ContentUtil; + +function empty_node(node) { + let cnode; + while((cnode = node.firstChild)) + node.removeChild(cnode); + return node; +} + +function DOMSerializer() { + return Components.classes["@mozilla.org/xmlextras/xmlserializer;1"] + .createInstance(Components.interfaces.nsIDOMSerializer); +} + +function serializeContents(node) { + let str = DOMSerializer().serializeToString(node); + return str.replace(/^<[^>]+>/, '') + .replace(/<\/[^>]+>$/, ''); +} + +function run_test() { + let doc, body, str, expectedResult, frag; + + do_print("Testing Util.populateFragmentFromString"); + + do_check_true(!!Util.populateFragmentFromString); + + do_print("Loading blank document"); + doc = do_parse_document("blank.xhtml", "application/xhtml+xml"); + + // sanity check + do_check_eq(doc.nodeType, 9); + do_check_true(doc.documentElement.localName != "parsererror"); + + body = doc.documentElement.getElementsByTagName("body")[0]; + do_check_eq(1, body.nodeType); + + frag = doc.createDocumentFragment(); + + // test proper handling of leading and trailing text + str = "Before, #2, #1. After"; + Util.populateFragmentFromString( + frag, str, + { text: "One", className: "text-one"}, + { text: "Two", className: "text-two"} + ); + + empty_node(body); + body.appendChild(frag); + + expectedResult = 'Before, Two, One. After'; + do_check_eq(serializeContents(body), expectedResult); + + // test proper handling of unspecified markers + str = "#8080: #1. http://localhost/#bar"; + Util.populateFragmentFromString( + frag, str, + { text: "One" } + ); + + empty_node(body); + body.appendChild(frag); + + expectedResult = '#8080: One. http://localhost/#bar'; + do_check_eq(serializeContents(body), expectedResult); + + // test proper handling of markup in strings + str = "#1 tag. © 2000 - Some Corp\u2122"; + Util.populateFragmentFromString( + frag, str, + { text: "About the" } + ); + + empty_node(body); + body.appendChild(frag); + expectedResult = "About the <body> tag. &copy; 2000 - Some Corp™" + do_check_eq(serializeContents(body), expectedResult); +}