/* 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"; const { getLazyTemplate, getTemplate } = ChromeUtils.importESModule( "moz-src:///comm/mail/base/content/modules/TemplateLoader.mjs", { global: "current" } ); const testElementTagName = "template-loader-test-element"; add_setup(function () { // Repeated runs reuse this window's custom element registry. if (!customElements.get(testElementTagName)) { customElements.define(testElementTagName, class extends HTMLElement {}); } }); add_task(function test_getTemplate() { const fragment = getTemplate( '' ); Assert.equal( fragment.nodeType, Node.DOCUMENT_FRAGMENT_NODE, "The loader should return a document fragment" ); const input = fragment.querySelector("input"); const label = fragment.querySelector("label"); Assert.ok(input, "The fragment should contain the input"); Assert.ok(label, "The fragment should contain the label"); Assert.equal( input.namespaceURI, "http://www.w3.org/1999/xhtml", "The input should use the HTML namespace" ); Assert.ok(input.disabled, "HTML boolean attributes should be preserved"); Assert.equal(label.control, input, "The label should refer to the input"); }); add_task(function test_independentCopies() { const template = getTemplate(""); const first = document.importNode(template, true); const second = document.importNode(template, true); first.firstElementChild.textContent = "Changed"; Assert.notEqual(first, second, "Each import should create a new fragment"); Assert.equal( second.textContent, "Original", "Changing one copy should leave another copy unchanged" ); const host = document.createElement("div"); host.append(first); Assert.equal( template.textContent, "Original", "Changing and consuming a copy should leave the cached template unchanged" ); }); add_task(function test_targetDocument() { const targetDocument = document.implementation.createHTMLDocument(); const template = getTemplate(""); const fragment = targetDocument.importNode(template, true); Assert.equal( fragment.ownerDocument, targetDocument, "The fragment should belong to the supplied document" ); Assert.equal( fragment.firstElementChild.ownerDocument, targetDocument, "The child nodes should belong to the supplied document" ); }); add_task(function test_mixedXHTMLTemplate() { const template = getTemplate(` `); const fragment = document.importNode(template, true); const form = fragment.querySelector("form"); const input = fragment.querySelector("input"); const label = fragment.querySelector("label"); Assert.ok(form, "The XML template should preserve its form"); Assert.ok(input, "The XML template should preserve its input"); Assert.ok(label, "The XML template should contain its XUL label"); Assert.ok( HTMLInputElement.isInstance(input), "The template's default namespace should create an HTML input" ); Assert.ok(input.disabled, "The XML input should preserve its disabled state"); Assert.ok( XULElement.isInstance(label), "The xul prefix should create a real XUL element" ); Assert.equal( label.namespaceURI, "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "The imported label should keep the XUL namespace" ); }); add_task(function test_xulTemplate() { const parser = new DOMParser(); parser.forceEnableXULXBL(); const targetDocument = parser.parseFromSafeString( '', "application/xml" ); const template = getTemplate( `` ); const first = targetDocument.importNode(template, true); const second = targetDocument.importNode(template, true); const box = first.querySelector("vbox"); const input = first.querySelector("input"); Assert.ok(box, "The XUL template should contain its box"); Assert.ok(input, "The XUL template should contain its HTML input"); Assert.ok( XULElement.isInstance(box), "The template's xul prefix should create a real XUL element" ); Assert.equal( box.ownerDocument, targetDocument, "The imported XUL element should belong to the destination document" ); Assert.ok( HTMLInputElement.isInstance(input), "The template's HTML input should keep its type in a XUL document" ); first.querySelector("label").setAttribute("value", "Changed"); Assert.equal( second.querySelector("label").getAttribute("value"), "Original", "Changing one XUL copy should leave the other copy unchanged" ); Assert.equal( template.querySelector("label").getAttribute("value"), "Original", "Changing a XUL copy should leave the cached template unchanged" ); }); add_task(function test_xulTemplateWithoutHTMLNamespace() { Assert.throws( () => getTemplate(``), /HTML