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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/tests/unit/test_util_populateFragmentFromString.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +"use strict";
    1.10 +
    1.11 +Components.utils.import("resource:///modules/ContentUtil.jsm");
    1.12 +let Util = ContentUtil;
    1.13 +
    1.14 +function empty_node(node) {
    1.15 +  let cnode;
    1.16 +  while((cnode = node.firstChild))
    1.17 +      node.removeChild(cnode);
    1.18 +  return node;
    1.19 +}
    1.20 +
    1.21 +function DOMSerializer() {
    1.22 +  return Components.classes["@mozilla.org/xmlextras/xmlserializer;1"]
    1.23 +          .createInstance(Components.interfaces.nsIDOMSerializer);
    1.24 +}
    1.25 +
    1.26 +function serializeContents(node) {
    1.27 +  let str = DOMSerializer().serializeToString(node);
    1.28 +  return str.replace(/^<[^>]+>/, '')
    1.29 +     .replace(/<\/[^>]+>$/, '');
    1.30 +}
    1.31 +
    1.32 +function run_test() {
    1.33 +  let doc, body, str, expectedResult, frag;
    1.34 +
    1.35 +  do_print("Testing Util.populateFragmentFromString");
    1.36 +
    1.37 +  do_check_true(!!Util.populateFragmentFromString);
    1.38 +
    1.39 +  do_print("Loading blank document");
    1.40 +  doc = do_parse_document("blank.xhtml", "application/xhtml+xml");
    1.41 +
    1.42 +  // sanity check
    1.43 +  do_check_eq(doc.nodeType, 9);
    1.44 +  do_check_true(doc.documentElement.localName != "parsererror");
    1.45 +
    1.46 +  body = doc.documentElement.getElementsByTagName("body")[0];
    1.47 +  do_check_eq(1, body.nodeType);
    1.48 +
    1.49 +  frag = doc.createDocumentFragment();
    1.50 +
    1.51 +  // test proper handling of leading and trailing text
    1.52 +  str = "Before, #2, #1. After";
    1.53 +  Util.populateFragmentFromString(
    1.54 +                                    frag, str,
    1.55 +                                    { text: "One", className: "text-one"},
    1.56 +                                    { text: "Two", className: "text-two"}
    1.57 +                                  );
    1.58 +
    1.59 +  empty_node(body);
    1.60 +  body.appendChild(frag);
    1.61 +
    1.62 +  expectedResult = 'Before, <span class="text-two">Two</span>, <span class="text-one">One</span>. After';
    1.63 +  do_check_eq(serializeContents(body), expectedResult);
    1.64 +
    1.65 +  // test proper handling of unspecified markers
    1.66 +  str = "#8080: #1. http://localhost/#bar";
    1.67 +  Util.populateFragmentFromString(
    1.68 +                                    frag, str,
    1.69 +                                    { text: "One" }
    1.70 +                                  );
    1.71 +
    1.72 +  empty_node(body);
    1.73 +  body.appendChild(frag);
    1.74 +
    1.75 +  expectedResult = '#8080: <span>One</span>. http://localhost/#bar';
    1.76 +  do_check_eq(serializeContents(body), expectedResult);
    1.77 +
    1.78 +  // test proper handling of markup in strings
    1.79 +  str = "#1 <body> tag. &copy; 2000 - Some Corp\u2122";
    1.80 +  Util.populateFragmentFromString(
    1.81 +                                    frag, str,
    1.82 +                                    { text: "About the" }
    1.83 +                                  );
    1.84 +
    1.85 +  empty_node(body);
    1.86 +  body.appendChild(frag);
    1.87 +  expectedResult = "<span>About the</span> &lt;body&gt; tag. &amp;copy; 2000 - Some Corp™"
    1.88 +  do_check_eq(serializeContents(body), expectedResult);
    1.89 +}

mercurial