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