browser/metro/base/tests/unit/test_util_populateFragmentFromString.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
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 "use strict";
michael@0 7
michael@0 8 Components.utils.import("resource:///modules/ContentUtil.jsm");
michael@0 9 let Util = ContentUtil;
michael@0 10
michael@0 11 function empty_node(node) {
michael@0 12 let cnode;
michael@0 13 while((cnode = node.firstChild))
michael@0 14 node.removeChild(cnode);
michael@0 15 return node;
michael@0 16 }
michael@0 17
michael@0 18 function DOMSerializer() {
michael@0 19 return Components.classes["@mozilla.org/xmlextras/xmlserializer;1"]
michael@0 20 .createInstance(Components.interfaces.nsIDOMSerializer);
michael@0 21 }
michael@0 22
michael@0 23 function serializeContents(node) {
michael@0 24 let str = DOMSerializer().serializeToString(node);
michael@0 25 return str.replace(/^<[^>]+>/, '')
michael@0 26 .replace(/<\/[^>]+>$/, '');
michael@0 27 }
michael@0 28
michael@0 29 function run_test() {
michael@0 30 let doc, body, str, expectedResult, frag;
michael@0 31
michael@0 32 do_print("Testing Util.populateFragmentFromString");
michael@0 33
michael@0 34 do_check_true(!!Util.populateFragmentFromString);
michael@0 35
michael@0 36 do_print("Loading blank document");
michael@0 37 doc = do_parse_document("blank.xhtml", "application/xhtml+xml");
michael@0 38
michael@0 39 // sanity check
michael@0 40 do_check_eq(doc.nodeType, 9);
michael@0 41 do_check_true(doc.documentElement.localName != "parsererror");
michael@0 42
michael@0 43 body = doc.documentElement.getElementsByTagName("body")[0];
michael@0 44 do_check_eq(1, body.nodeType);
michael@0 45
michael@0 46 frag = doc.createDocumentFragment();
michael@0 47
michael@0 48 // test proper handling of leading and trailing text
michael@0 49 str = "Before, #2, #1. After";
michael@0 50 Util.populateFragmentFromString(
michael@0 51 frag, str,
michael@0 52 { text: "One", className: "text-one"},
michael@0 53 { text: "Two", className: "text-two"}
michael@0 54 );
michael@0 55
michael@0 56 empty_node(body);
michael@0 57 body.appendChild(frag);
michael@0 58
michael@0 59 expectedResult = 'Before, <span class="text-two">Two</span>, <span class="text-one">One</span>. After';
michael@0 60 do_check_eq(serializeContents(body), expectedResult);
michael@0 61
michael@0 62 // test proper handling of unspecified markers
michael@0 63 str = "#8080: #1. http://localhost/#bar";
michael@0 64 Util.populateFragmentFromString(
michael@0 65 frag, str,
michael@0 66 { text: "One" }
michael@0 67 );
michael@0 68
michael@0 69 empty_node(body);
michael@0 70 body.appendChild(frag);
michael@0 71
michael@0 72 expectedResult = '#8080: <span>One</span>. http://localhost/#bar';
michael@0 73 do_check_eq(serializeContents(body), expectedResult);
michael@0 74
michael@0 75 // test proper handling of markup in strings
michael@0 76 str = "#1 <body> tag. &copy; 2000 - Some Corp\u2122";
michael@0 77 Util.populateFragmentFromString(
michael@0 78 frag, str,
michael@0 79 { text: "About the" }
michael@0 80 );
michael@0 81
michael@0 82 empty_node(body);
michael@0 83 body.appendChild(frag);
michael@0 84 expectedResult = "<span>About the</span> &lt;body&gt; tag. &amp;copy; 2000 - Some Corp™"
michael@0 85 do_check_eq(serializeContents(body), expectedResult);
michael@0 86 }

mercurial