/* 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/. */ const { dom } = ChromeUtils.importESModule( "chrome://remote/content/shared/DOM.sys.mjs" ); const { NodeCache } = ChromeUtils.importESModule( "chrome://remote/content/shared/webdriver/NodeCache.sys.mjs" ); class MockElement { constructor(tagName, attrs = {}) { this.tagName = tagName; this.localName = tagName; this.isConnected = false; this.ownerGlobal = { document: { isActive() { return true; }, }, }; for (let attr in attrs) { this[attr] = attrs[attr]; } } get nodeType() { return 1; } get ELEMENT_NODE() { return 1; } // this is a severely limited CSS selector // that only supports lists of tag names matches(selector) { let tags = selector.split(","); return tags.includes(this.localName); } } class MockXULElement extends MockElement { constructor(tagName, attrs = {}) { super(tagName, attrs); this.namespaceURI = XUL_NS; if (typeof this.ownerDocument == "undefined") { this.ownerDocument = {}; } if (typeof this.ownerDocument.documentElement == "undefined") { this.ownerDocument.documentElement = { namespaceURI: XUL_NS }; } } } const xulEl = new MockXULElement("text"); const domElInPrivilegedDocument = new MockElement("input", { nodePrincipal: { isSystemPrincipal: true }, }); const xulElInPrivilegedDocument = new MockXULElement("text", { nodePrincipal: { isSystemPrincipal: true }, }); function setupTest() { const browser = Services.appShell.createWindowlessBrowser(false); browser.document.body.innerHTML = `
`; const divEl = browser.document.querySelector("div"); const svgEl = browser.document.querySelector("svg"); const videoEl = browser.document.querySelector("video"); const shadowRoot = videoEl.openOrClosedShadowRoot; const buttonEl = browser.document.querySelector("button"); const fieldsetEl = browser.document.querySelector("fieldset"); const inputEl = browser.document.querySelector("input"); const optgroupEl = browser.document.querySelector("optgroup"); const optionInGroupEl = browser.document.querySelector("option#in-group"); const optionNoGroupEl = browser.document.querySelector("option#no-group"); const selectEl = browser.document.querySelector("select"); const textareaEl = browser.document.querySelector("textarea"); const iframeEl = browser.document.querySelector("iframe"); const childEl = iframeEl.contentDocument.createElement("div"); iframeEl.contentDocument.body.appendChild(childEl); return { browser, buttonEl, childEl, divEl, inputEl, fieldsetEl, iframeEl, nodeCache: new NodeCache(), optgroupEl, optionInGroupEl, optionNoGroupEl, selectEl, shadowRoot, svgEl, textareaEl, videoEl, }; } add_task(function test_findClosest() { const { divEl, videoEl } = setupTest(); equal(dom.findClosest(divEl, "foo"), null); equal(dom.findClosest(videoEl, "div"), divEl); }); add_task(function test_isSelected() { const { browser, divEl } = setupTest(); const checkbox = browser.document.createElement("input"); checkbox.setAttribute("type", "checkbox"); ok(!dom.isSelected(checkbox)); checkbox.checked = true; ok(dom.isSelected(checkbox)); // selected is not a property of checkbox.selected = true; checkbox.checked = false; ok(!dom.isSelected(checkbox)); const option = browser.document.createElement("option"); ok(!dom.isSelected(option)); option.selected = true; ok(dom.isSelected(option)); // checked is not a property of