2012-03-22 19:19:57 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2016-02-03 21:54:23 +03:00
|
|
|
"use strict";
|
2017-06-28 21:04:14 +03:00
|
|
|
/* global XPCNativeWrapper */
|
2015-04-15 14:18:00 +03:00
|
|
|
|
2017-05-08 19:05:20 +03:00
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
2016-02-03 21:54:23 +03:00
|
|
|
|
2017-02-07 22:00:28 +03:00
|
|
|
Cu.import("chrome://marionette/content/assert.js");
|
2016-02-03 22:00:46 +03:00
|
|
|
Cu.import("chrome://marionette/content/atom.js");
|
2017-06-28 21:01:49 +03:00
|
|
|
const {
|
|
|
|
InvalidSelectorError,
|
|
|
|
NoSuchElementError,
|
|
|
|
StaleElementReferenceError,
|
|
|
|
} = Cu.import("chrome://marionette/content/error.js", {});
|
2017-10-13 19:59:30 +03:00
|
|
|
const {pprint} = Cu.import("chrome://marionette/content/format.js", {});
|
2017-10-02 18:50:11 +03:00
|
|
|
const {PollPromise} = Cu.import("chrome://marionette/content/sync.js", {});
|
2015-04-15 14:18:00 +03:00
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["element"];
|
|
|
|
|
|
|
|
const XMLNS = "http://www.w3.org/1999/xhtml";
|
2017-09-29 13:54:03 +03:00
|
|
|
const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
2017-07-26 15:11:53 +03:00
|
|
|
|
2017-10-10 16:51:27 +03:00
|
|
|
/** XUL elements that support checked property. */
|
|
|
|
const XUL_CHECKED_ELS = new Set([
|
|
|
|
"button",
|
|
|
|
"checkbox",
|
|
|
|
"listitem",
|
|
|
|
"toolbarbutton",
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** XUL elements that support selected property. */
|
|
|
|
const XUL_SELECTED_ELS = new Set([
|
|
|
|
"listitem",
|
|
|
|
"menu",
|
|
|
|
"menuitem",
|
|
|
|
"menuseparator",
|
|
|
|
"radio",
|
|
|
|
"richlistitem",
|
|
|
|
"tab",
|
|
|
|
]);
|
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
const uuidGen = Cc["@mozilla.org/uuid-generator;1"]
|
|
|
|
.getService(Ci.nsIUUIDGenerator);
|
|
|
|
|
2012-03-22 19:19:57 +04:00
|
|
|
/**
|
2016-05-20 18:52:01 +03:00
|
|
|
* This module provides shared functionality for dealing with DOM-
|
|
|
|
* and web elements in Marionette.
|
2015-09-17 14:31:39 +03:00
|
|
|
*
|
|
|
|
* A web element is an abstraction used to identify an element when it
|
|
|
|
* is transported across the protocol, between remote- and local ends.
|
|
|
|
*
|
|
|
|
* Each element has an associated web element reference (a UUID) that
|
|
|
|
* uniquely identifies the the element across all browsing contexts. The
|
|
|
|
* web element reference for every element representing the same element
|
|
|
|
* is the same.
|
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* The {@link element.Store} provides a mapping between web element
|
2016-05-20 18:52:01 +03:00
|
|
|
* references and DOM elements for each browsing context. It also provides
|
2015-09-17 14:31:39 +03:00
|
|
|
* functionality for looking up and retrieving elements.
|
2017-07-26 15:11:53 +03:00
|
|
|
*
|
|
|
|
* @namespace
|
2012-03-22 19:19:57 +04:00
|
|
|
*/
|
2016-03-04 15:01:09 +03:00
|
|
|
this.element = {};
|
|
|
|
|
|
|
|
element.Key = "element-6066-11e4-a52e-4f735466cecf";
|
2016-05-20 18:52:01 +03:00
|
|
|
element.LegacyKey = "ELEMENT";
|
2016-03-04 15:01:09 +03:00
|
|
|
|
|
|
|
element.Strategy = {
|
|
|
|
ClassName: "class name",
|
|
|
|
Selector: "css selector",
|
|
|
|
ID: "id",
|
|
|
|
Name: "name",
|
|
|
|
LinkText: "link text",
|
|
|
|
PartialLinkText: "partial link text",
|
|
|
|
TagName: "tag name",
|
|
|
|
XPath: "xpath",
|
|
|
|
Anon: "anon",
|
|
|
|
AnonAttribute: "anon attribute",
|
|
|
|
};
|
|
|
|
|
2016-05-20 17:07:21 +03:00
|
|
|
/**
|
|
|
|
* Stores known/seen elements and their associated web element
|
|
|
|
* references.
|
|
|
|
*
|
|
|
|
* Elements are added by calling |add(el)| or |addAll(elements)|, and
|
|
|
|
* may be queried by their web element reference using |get(element)|.
|
2017-07-26 15:11:53 +03:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @memberof element
|
2016-05-20 17:07:21 +03:00
|
|
|
*/
|
|
|
|
element.Store = class {
|
2016-05-20 15:32:52 +03:00
|
|
|
constructor() {
|
2016-05-20 17:07:21 +03:00
|
|
|
this.els = {};
|
2016-05-20 15:32:52 +03:00
|
|
|
this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
}
|
2012-03-22 19:19:57 +04:00
|
|
|
|
2016-05-20 18:54:42 +03:00
|
|
|
clear() {
|
2016-05-20 17:07:21 +03:00
|
|
|
this.els = {};
|
2016-05-20 15:32:52 +03:00
|
|
|
}
|
2012-03-22 19:19:57 +04:00
|
|
|
|
|
|
|
/**
|
2016-05-20 15:28:27 +03:00
|
|
|
* Make a collection of elements seen.
|
|
|
|
*
|
|
|
|
* The oder of the returned web element references is guaranteed to
|
|
|
|
* match that of the collection passed in.
|
|
|
|
*
|
|
|
|
* @param {NodeList} els
|
|
|
|
* Sequence of elements to add to set of seen elements.
|
|
|
|
*
|
|
|
|
* @return {Array.<WebElement>}
|
|
|
|
* List of the web element references associated with each element
|
|
|
|
* from |els|.
|
|
|
|
*/
|
2016-05-20 15:32:52 +03:00
|
|
|
addAll(els) {
|
2016-05-20 15:28:27 +03:00
|
|
|
let add = this.add.bind(this);
|
|
|
|
return [...els].map(add);
|
2016-05-20 15:32:52 +03:00
|
|
|
}
|
2016-05-20 15:28:27 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-20 15:32:52 +03:00
|
|
|
* Make an element seen.
|
|
|
|
*
|
2017-08-21 20:56:19 +03:00
|
|
|
* @param {Element} el
|
2016-05-20 15:32:52 +03:00
|
|
|
* Element to add to set of seen elements.
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
* Web element reference associated with element.
|
|
|
|
*/
|
|
|
|
add(el) {
|
2016-05-20 17:07:21 +03:00
|
|
|
for (let i in this.els) {
|
2016-05-20 15:28:27 +03:00
|
|
|
let foundEl;
|
2013-04-30 22:22:03 +04:00
|
|
|
try {
|
2016-05-20 17:07:21 +03:00
|
|
|
foundEl = this.els[i].get();
|
2015-04-15 14:18:00 +03:00
|
|
|
} catch (e) {}
|
2016-05-20 15:28:27 +03:00
|
|
|
|
2013-04-17 20:06:39 +04:00
|
|
|
if (foundEl) {
|
2016-05-20 17:07:21 +03:00
|
|
|
if (new XPCNativeWrapper(foundEl) == new XPCNativeWrapper(el)) {
|
2013-04-17 20:06:39 +04:00
|
|
|
return i;
|
|
|
|
}
|
2016-05-20 17:07:21 +03:00
|
|
|
|
|
|
|
// cleanup reference to gc'd element
|
2015-04-15 14:18:00 +03:00
|
|
|
} else {
|
2016-05-20 17:07:21 +03:00
|
|
|
delete this.els[i];
|
2012-03-22 19:19:57 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-20 15:28:27 +03:00
|
|
|
|
2016-02-23 18:01:12 +03:00
|
|
|
let id = element.generateUUID();
|
2016-05-20 17:07:21 +03:00
|
|
|
this.els[id] = Cu.getWeakReference(el);
|
2012-03-22 19:19:57 +04:00
|
|
|
return id;
|
2016-05-20 15:32:52 +03:00
|
|
|
}
|
2014-02-06 02:04:42 +04:00
|
|
|
|
2012-03-22 19:19:57 +04:00
|
|
|
/**
|
2016-05-20 17:07:21 +03:00
|
|
|
* Determine if the provided web element reference has been seen
|
|
|
|
* before/is in the element store.
|
2012-03-22 19:19:57 +04:00
|
|
|
*
|
2017-10-02 21:09:26 +03:00
|
|
|
* Unlike when getting the element, a staleness check is not
|
|
|
|
* performed.
|
|
|
|
*
|
2016-05-20 17:07:21 +03:00
|
|
|
* @param {string} uuid
|
|
|
|
* Element's associated web element reference.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if element is in the store, false otherwise.
|
|
|
|
*/
|
|
|
|
has(uuid) {
|
|
|
|
return Object.keys(this.els).includes(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a DOM element by its unique web element reference/UUID.
|
|
|
|
*
|
2017-10-02 21:09:26 +03:00
|
|
|
* Upon retrieving the element, an element staleness check is
|
|
|
|
* performed.
|
|
|
|
*
|
2016-05-20 17:07:21 +03:00
|
|
|
* @param {string} uuid
|
|
|
|
* Web element reference, or UUID.
|
2017-10-02 21:09:26 +03:00
|
|
|
* @param {WindowProxy} window
|
|
|
|
* Current browsing context, which may differ from the associate
|
|
|
|
* browsing context of <var>el</var>.
|
2016-05-20 17:07:21 +03:00
|
|
|
*
|
2017-08-21 20:56:19 +03:00
|
|
|
* @returns {Element}
|
2016-05-20 17:07:21 +03:00
|
|
|
* Element associated with reference.
|
2012-03-22 19:19:57 +04:00
|
|
|
*
|
2017-08-05 02:50:46 +03:00
|
|
|
* @throws {NoSuchElementError}
|
2017-09-12 15:18:52 +03:00
|
|
|
* If the web element reference <var>uuid</var> has not been
|
|
|
|
* seen before.
|
2016-05-20 17:07:21 +03:00
|
|
|
* @throws {StaleElementReferenceError}
|
2017-09-12 15:18:52 +03:00
|
|
|
* If the element has gone stale, indicating it is no longer
|
|
|
|
* attached to the DOM, or its node document is no longer the
|
|
|
|
* active document.
|
2012-03-22 19:19:57 +04:00
|
|
|
*/
|
2017-10-02 21:09:26 +03:00
|
|
|
get(uuid, window) {
|
2017-09-12 15:18:52 +03:00
|
|
|
if (!this.has(uuid)) {
|
|
|
|
throw new NoSuchElementError(
|
|
|
|
"Web element reference not seen before: " + uuid);
|
2012-03-22 19:19:57 +04:00
|
|
|
}
|
2016-05-20 17:07:21 +03:00
|
|
|
|
2017-09-12 15:18:52 +03:00
|
|
|
let el;
|
|
|
|
let ref = this.els[uuid];
|
2013-04-30 22:22:03 +04:00
|
|
|
try {
|
2017-09-12 15:18:52 +03:00
|
|
|
el = ref.get();
|
2016-05-20 17:07:21 +03:00
|
|
|
} catch (e) {
|
2017-06-28 21:15:00 +03:00
|
|
|
delete this.els[uuid];
|
2013-04-30 22:22:03 +04:00
|
|
|
}
|
2016-05-20 17:07:21 +03:00
|
|
|
|
2017-10-02 21:09:26 +03:00
|
|
|
if (element.isStale(el, window)) {
|
2015-04-15 14:18:00 +03:00
|
|
|
throw new StaleElementReferenceError(
|
2017-09-12 15:18:52 +03:00
|
|
|
pprint`The element reference of ${el || uuid} stale; ` +
|
2017-10-02 21:09:26 +03:00
|
|
|
"either the element is no longer attached to the DOM, " +
|
|
|
|
"it is not in the current frame context, " +
|
2017-08-21 21:09:15 +03:00
|
|
|
"or the document has been refreshed");
|
2012-03-22 19:19:57 +04:00
|
|
|
}
|
2016-05-20 17:07:21 +03:00
|
|
|
|
2012-03-22 19:19:57 +04:00
|
|
|
return el;
|
2016-05-20 15:32:52 +03:00
|
|
|
}
|
2016-05-20 15:28:27 +03:00
|
|
|
};
|
2014-02-06 02:04:42 +04:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
/**
|
|
|
|
* Find a single element or a collection of elements starting at the
|
|
|
|
* document root or a given node.
|
|
|
|
*
|
|
|
|
* If |timeout| is above 0, an implicit search technique is used.
|
|
|
|
* This will wait for the duration of |timeout| for the element
|
|
|
|
* to appear in the DOM.
|
|
|
|
*
|
|
|
|
* See the |element.Strategy| enum for a full list of supported
|
|
|
|
* search strategies that can be passed to |strategy|.
|
|
|
|
*
|
2017-08-21 20:56:19 +03:00
|
|
|
* Available flags for <var>opts</var>:
|
2016-05-20 15:28:27 +03:00
|
|
|
*
|
2017-08-21 20:56:19 +03:00
|
|
|
* <dl>
|
|
|
|
* <dt><code>all</code>
|
|
|
|
* <dd>
|
|
|
|
* If true, a multi-element search selector is used and a sequence
|
|
|
|
* of elements will be returned. Otherwise a single element.
|
2016-05-20 15:28:27 +03:00
|
|
|
*
|
2017-08-21 20:56:19 +03:00
|
|
|
* <dt><code>timeout</code>
|
|
|
|
* <dd>
|
|
|
|
* Duration to wait before timing out the search. If <code>all</code>
|
|
|
|
* is false, a {@link NoSuchElementError} is thrown if unable to
|
|
|
|
* find the element within the timeout duration.
|
2016-05-20 15:28:27 +03:00
|
|
|
*
|
2017-08-21 20:56:19 +03:00
|
|
|
* <dt><code>startNode</code>
|
|
|
|
* <dd>Element to use as the root of the search.
|
2016-05-20 15:28:27 +03:00
|
|
|
*
|
2017-08-21 20:56:19 +03:00
|
|
|
* @param {Object.<string, WindowProxy>} container
|
2016-05-20 15:28:27 +03:00
|
|
|
* Window object and an optional shadow root that contains the
|
|
|
|
* root shadow DOM element.
|
|
|
|
* @param {string} strategy
|
|
|
|
* Search strategy whereby to locate the element(s).
|
|
|
|
* @param {string} selector
|
|
|
|
* Selector search pattern. The selector must be compatible with
|
2017-08-21 20:56:19 +03:00
|
|
|
* the chosen search <var>strategy</var>.
|
2016-05-20 15:28:27 +03:00
|
|
|
* @param {Object.<string, ?>} opts
|
|
|
|
* Options.
|
|
|
|
*
|
2017-08-21 20:56:19 +03:00
|
|
|
* @return {Promise.<(Element|Array.<Element>)>}
|
2016-05-20 15:28:27 +03:00
|
|
|
* Single element or a sequence of elements.
|
|
|
|
*
|
|
|
|
* @throws InvalidSelectorError
|
2017-08-21 20:56:19 +03:00
|
|
|
* If <var>strategy</var> is unknown.
|
2016-05-20 15:28:27 +03:00
|
|
|
* @throws InvalidSelectorError
|
2017-08-21 20:56:19 +03:00
|
|
|
* If <var>selector</var> is malformed.
|
2016-05-20 15:28:27 +03:00
|
|
|
* @throws NoSuchElementError
|
|
|
|
* If a single element is requested, this error will throw if the
|
|
|
|
* element is not found.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.find = function(container, strategy, selector, opts = {}) {
|
2017-10-02 19:13:57 +03:00
|
|
|
let all = !!opts.all;
|
|
|
|
let timeout = opts.timeout || 0;
|
|
|
|
let startNode = opts.startNode;
|
2016-05-20 15:28:27 +03:00
|
|
|
|
|
|
|
let searchFn;
|
|
|
|
if (opts.all) {
|
|
|
|
searchFn = findElements.bind(this);
|
|
|
|
} else {
|
|
|
|
searchFn = findElement.bind(this);
|
|
|
|
}
|
2016-02-23 18:18:55 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-02 18:50:11 +03:00
|
|
|
let findElements = new PollPromise((resolve, reject) => {
|
2017-10-02 19:13:57 +03:00
|
|
|
let res = find_(container, strategy, selector, searchFn,
|
|
|
|
{all, startNode});
|
2016-11-22 01:41:20 +03:00
|
|
|
if (res.length > 0) {
|
|
|
|
resolve(Array.from(res));
|
|
|
|
} else {
|
|
|
|
reject([]);
|
|
|
|
}
|
2017-10-02 19:13:57 +03:00
|
|
|
}, {timeout});
|
2016-05-20 15:28:27 +03:00
|
|
|
|
|
|
|
findElements.then(foundEls => {
|
|
|
|
// the following code ought to be moved into findElement
|
|
|
|
// and findElements when bug 1254486 is addressed
|
|
|
|
if (!opts.all && (!foundEls || foundEls.length == 0)) {
|
|
|
|
let msg;
|
|
|
|
switch (strategy) {
|
|
|
|
case element.Strategy.AnonAttribute:
|
2017-06-30 02:40:24 +03:00
|
|
|
msg = "Unable to locate anonymous element: " +
|
|
|
|
JSON.stringify(selector);
|
2016-05-20 15:28:27 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
msg = "Unable to locate element: " + selector;
|
2016-02-23 18:18:55 +03:00
|
|
|
}
|
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
reject(new NoSuchElementError(msg));
|
|
|
|
}
|
2016-02-23 18:18:55 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
if (opts.all) {
|
|
|
|
resolve(foundEls);
|
|
|
|
}
|
|
|
|
resolve(foundEls[0]);
|
|
|
|
}, reject);
|
|
|
|
});
|
|
|
|
};
|
2016-01-22 00:07:36 +03:00
|
|
|
|
2017-10-02 19:13:57 +03:00
|
|
|
function find_(container, strategy, selector, searchFn,
|
|
|
|
{startNode = null, all = false} = {}) {
|
2016-05-20 15:28:27 +03:00
|
|
|
let rootNode = container.shadowRoot || container.frame.document;
|
2016-12-02 19:36:52 +03:00
|
|
|
|
2017-10-02 19:13:57 +03:00
|
|
|
if (!startNode) {
|
2016-12-02 19:36:52 +03:00
|
|
|
switch (strategy) {
|
2017-06-30 02:40:24 +03:00
|
|
|
// For anonymous nodes the start node needs to be of type
|
|
|
|
// DOMElement, which will refer to :root in case of a DOMDocument.
|
2016-12-02 19:36:52 +03:00
|
|
|
case element.Strategy.Anon:
|
|
|
|
case element.Strategy.AnonAttribute:
|
|
|
|
if (rootNode instanceof Ci.nsIDOMDocument) {
|
|
|
|
startNode = rootNode.documentElement;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
startNode = rootNode;
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 18:18:55 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
let res;
|
|
|
|
try {
|
|
|
|
res = searchFn(strategy, selector, rootNode, startNode);
|
|
|
|
} catch (e) {
|
|
|
|
throw new InvalidSelectorError(
|
|
|
|
`Given ${strategy} expression "${selector}" is invalid: ${e}`);
|
|
|
|
}
|
2016-02-23 18:18:55 +03:00
|
|
|
|
2016-07-04 19:34:06 +03:00
|
|
|
if (res) {
|
2017-10-02 19:13:57 +03:00
|
|
|
if (all) {
|
2016-07-04 19:34:06 +03:00
|
|
|
return res;
|
|
|
|
}
|
2016-05-20 15:28:27 +03:00
|
|
|
return [res];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
2012-04-03 22:08:49 +04:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
/**
|
2016-07-02 23:51:21 +03:00
|
|
|
* Find a single element by XPath expression.
|
2016-05-20 15:28:27 +03:00
|
|
|
*
|
2016-07-02 23:51:21 +03:00
|
|
|
* @param {DOMElement} root
|
|
|
|
* Document root
|
|
|
|
* @param {DOMElement} startNode
|
|
|
|
* Where in the DOM hiearchy to begin searching.
|
|
|
|
* @param {string} expr
|
|
|
|
* XPath search expression.
|
2016-05-20 15:28:27 +03:00
|
|
|
*
|
2016-07-02 23:51:21 +03:00
|
|
|
* @return {DOMElement}
|
|
|
|
* First element matching expression.
|
2016-05-20 15:28:27 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.findByXPath = function(root, startNode, expr) {
|
2016-07-02 23:51:21 +03:00
|
|
|
let iter = root.evaluate(expr, startNode, null,
|
2017-02-07 22:18:40 +03:00
|
|
|
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null);
|
2016-07-02 23:51:21 +03:00
|
|
|
return iter.singleNodeValue;
|
|
|
|
};
|
2012-04-03 22:08:49 +04:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
/**
|
2016-07-02 23:51:21 +03:00
|
|
|
* Find elements by XPath expression.
|
2016-05-20 15:28:27 +03:00
|
|
|
*
|
2016-07-02 23:51:21 +03:00
|
|
|
* @param {DOMElement} root
|
|
|
|
* Document root.
|
|
|
|
* @param {DOMElement} startNode
|
|
|
|
* Where in the DOM hierarchy to begin searching.
|
|
|
|
* @param {string} expr
|
|
|
|
* XPath search expression.
|
2016-05-20 15:28:27 +03:00
|
|
|
*
|
2016-07-02 23:51:21 +03:00
|
|
|
* @return {Array.<DOMElement>}
|
|
|
|
* Sequence of found elements matching expression.
|
2016-05-20 15:28:27 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.findByXPathAll = function(root, startNode, expr) {
|
2016-07-02 23:51:21 +03:00
|
|
|
let rv = [];
|
|
|
|
let iter = root.evaluate(expr, startNode, null,
|
|
|
|
Ci.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
|
|
|
|
let el = iter.iterateNext();
|
|
|
|
while (el) {
|
|
|
|
rv.push(el);
|
|
|
|
el = iter.iterateNext();
|
2016-05-20 15:28:27 +03:00
|
|
|
}
|
2016-07-02 23:51:21 +03:00
|
|
|
return rv;
|
|
|
|
};
|
2014-02-06 02:04:42 +04:00
|
|
|
|
2016-07-02 23:38:48 +03:00
|
|
|
/**
|
2017-10-05 14:56:28 +03:00
|
|
|
* Find all hyperlinks descendant of <var>node</var> which link text
|
|
|
|
* is <var>s</var>.
|
2016-07-02 23:38:48 +03:00
|
|
|
*
|
|
|
|
* @param {DOMElement} node
|
2017-10-05 14:56:28 +03:00
|
|
|
* Where in the DOM hierarchy to begin searching.
|
2016-07-02 23:38:48 +03:00
|
|
|
* @param {string} s
|
|
|
|
* Link text to search for.
|
|
|
|
*
|
|
|
|
* @return {Array.<DOMAnchorElement>}
|
2017-10-05 14:56:28 +03:00
|
|
|
* Sequence of link elements which text is <var>s</var>.
|
2016-07-02 23:38:48 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.findByLinkText = function(node, s) {
|
2016-07-08 01:06:17 +03:00
|
|
|
return filterLinks(node, link => link.text.trim() === s);
|
2016-07-02 23:38:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all hyperlinks descendant of |node| which link text contains |s|.
|
|
|
|
*
|
|
|
|
* @param {DOMElement} node
|
|
|
|
* Where in the DOM hierachy to begin searching.
|
|
|
|
* @param {string} s
|
|
|
|
* Link text to search for.
|
|
|
|
*
|
|
|
|
* @return {Array.<DOMAnchorElement>}
|
|
|
|
* Sequence of link elements which text containins |s|.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.findByPartialLinkText = function(node, s) {
|
2016-07-02 23:38:48 +03:00
|
|
|
return filterLinks(node, link => link.text.indexOf(s) != -1);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters all hyperlinks that are descendant of |node| by |predicate|.
|
|
|
|
*
|
|
|
|
* @param {DOMElement} node
|
|
|
|
* Where in the DOM hierarchy to begin searching.
|
|
|
|
* @param {function(DOMAnchorElement): boolean} predicate
|
|
|
|
* Function that determines if given link should be included in
|
|
|
|
* return value or filtered away.
|
|
|
|
*
|
|
|
|
* @return {Array.<DOMAnchorElement>}
|
|
|
|
* Sequence of link elements matching |predicate|.
|
|
|
|
*/
|
|
|
|
function filterLinks(node, predicate) {
|
|
|
|
let rv = [];
|
|
|
|
for (let link of node.getElementsByTagName("a")) {
|
|
|
|
if (predicate(link)) {
|
|
|
|
rv.push(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
/**
|
|
|
|
* Finds a single element.
|
|
|
|
*
|
|
|
|
* @param {element.Strategy} using
|
|
|
|
* Selector strategy to use.
|
|
|
|
* @param {string} value
|
|
|
|
* Selector expression.
|
|
|
|
* @param {DOMElement} rootNode
|
|
|
|
* Document root.
|
|
|
|
* @param {DOMElement=} startNode
|
|
|
|
* Optional node from which to start searching.
|
|
|
|
*
|
|
|
|
* @return {DOMElement}
|
|
|
|
* Found elements.
|
|
|
|
*
|
|
|
|
* @throws {InvalidSelectorError}
|
|
|
|
* If strategy |using| is not recognised.
|
|
|
|
* @throws {Error}
|
|
|
|
* If selector expression |value| is malformed.
|
|
|
|
*/
|
|
|
|
function findElement(using, value, rootNode, startNode) {
|
|
|
|
switch (using) {
|
|
|
|
case element.Strategy.ID:
|
2017-06-28 21:16:34 +03:00
|
|
|
{
|
|
|
|
if (startNode.getElementById) {
|
|
|
|
return startNode.getElementById(value);
|
|
|
|
}
|
|
|
|
let expr = `.//*[@id="${value}"]`;
|
|
|
|
return element.findByXPath( rootNode, startNode, expr);
|
2016-05-20 15:28:27 +03:00
|
|
|
}
|
2015-09-17 14:31:39 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.Name:
|
2017-06-28 21:16:34 +03:00
|
|
|
{
|
|
|
|
if (startNode.getElementsByName) {
|
|
|
|
return startNode.getElementsByName(value)[0];
|
|
|
|
}
|
|
|
|
let expr = `.//*[@name="${value}"]`;
|
|
|
|
return element.findByXPath(rootNode, startNode, expr);
|
2016-05-20 15:28:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case element.Strategy.ClassName:
|
|
|
|
// works for >= Firefox 3
|
2017-06-28 21:16:34 +03:00
|
|
|
return startNode.getElementsByClassName(value)[0];
|
2016-05-20 15:28:27 +03:00
|
|
|
|
|
|
|
case element.Strategy.TagName:
|
|
|
|
// works for all elements
|
|
|
|
return startNode.getElementsByTagName(value)[0];
|
|
|
|
|
|
|
|
case element.Strategy.XPath:
|
2017-06-28 21:16:34 +03:00
|
|
|
return element.findByXPath(rootNode, startNode, value);
|
2016-05-20 15:28:27 +03:00
|
|
|
|
|
|
|
case element.Strategy.LinkText:
|
2016-07-02 23:38:48 +03:00
|
|
|
for (let link of startNode.getElementsByTagName("a")) {
|
2016-07-08 01:06:17 +03:00
|
|
|
if (link.text.trim() === value) {
|
2016-07-02 23:38:48 +03:00
|
|
|
return link;
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 21:16:34 +03:00
|
|
|
return undefined;
|
2016-07-02 23:38:48 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.PartialLinkText:
|
2016-07-02 23:38:48 +03:00
|
|
|
for (let link of startNode.getElementsByTagName("a")) {
|
|
|
|
if (link.text.indexOf(value) != -1) {
|
|
|
|
return link;
|
2012-03-22 19:19:57 +04:00
|
|
|
}
|
2016-05-20 15:28:27 +03:00
|
|
|
}
|
2017-06-28 21:16:34 +03:00
|
|
|
return undefined;
|
2016-03-04 15:01:09 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.Selector:
|
|
|
|
try {
|
|
|
|
return startNode.querySelector(value);
|
|
|
|
} catch (e) {
|
|
|
|
throw new InvalidSelectorError(`${e.message}: "${value}"`);
|
|
|
|
}
|
2015-09-17 14:31:39 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.Anon:
|
|
|
|
return rootNode.getAnonymousNodes(startNode);
|
2015-09-17 14:31:39 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.AnonAttribute:
|
|
|
|
let attr = Object.keys(value)[0];
|
2017-06-28 21:16:34 +03:00
|
|
|
return rootNode.getAnonymousElementByAttribute(
|
|
|
|
startNode, attr, value[attr]);
|
2016-05-20 15:28:27 +03:00
|
|
|
}
|
2017-06-28 21:16:34 +03:00
|
|
|
|
|
|
|
throw new InvalidSelectorError(`No such strategy: ${using}`);
|
2017-02-07 22:18:40 +03:00
|
|
|
}
|
2012-03-22 19:19:57 +04:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
/**
|
|
|
|
* Find multiple elements.
|
|
|
|
*
|
|
|
|
* @param {element.Strategy} using
|
|
|
|
* Selector strategy to use.
|
|
|
|
* @param {string} value
|
|
|
|
* Selector expression.
|
|
|
|
* @param {DOMElement} rootNode
|
|
|
|
* Document root.
|
|
|
|
* @param {DOMElement=} startNode
|
|
|
|
* Optional node from which to start searching.
|
|
|
|
*
|
|
|
|
* @return {DOMElement}
|
|
|
|
* Found elements.
|
|
|
|
*
|
|
|
|
* @throws {InvalidSelectorError}
|
|
|
|
* If strategy |using| is not recognised.
|
|
|
|
* @throws {Error}
|
|
|
|
* If selector expression |value| is malformed.
|
|
|
|
*/
|
|
|
|
function findElements(using, value, rootNode, startNode) {
|
|
|
|
switch (using) {
|
|
|
|
case element.Strategy.ID:
|
|
|
|
value = `.//*[@id="${value}"]`;
|
|
|
|
|
|
|
|
// fall through
|
|
|
|
case element.Strategy.XPath:
|
2016-07-02 23:51:21 +03:00
|
|
|
return element.findByXPathAll(rootNode, startNode, value);
|
2016-05-20 15:28:27 +03:00
|
|
|
|
|
|
|
case element.Strategy.Name:
|
|
|
|
if (startNode.getElementsByName) {
|
|
|
|
return startNode.getElementsByName(value);
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
return element.findByXPathAll(
|
|
|
|
rootNode, startNode, `.//*[@name="${value}"]`);
|
2016-05-20 15:28:27 +03:00
|
|
|
|
|
|
|
case element.Strategy.ClassName:
|
|
|
|
return startNode.getElementsByClassName(value);
|
|
|
|
|
|
|
|
case element.Strategy.TagName:
|
|
|
|
return startNode.getElementsByTagName(value);
|
|
|
|
|
|
|
|
case element.Strategy.LinkText:
|
2016-07-02 23:38:48 +03:00
|
|
|
return element.findByLinkText(startNode, value);
|
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.PartialLinkText:
|
2016-07-02 23:38:48 +03:00
|
|
|
return element.findByPartialLinkText(startNode, value);
|
2016-03-04 15:01:09 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.Selector:
|
|
|
|
return startNode.querySelectorAll(value);
|
2016-03-04 15:01:09 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.Anon:
|
|
|
|
return rootNode.getAnonymousNodes(startNode);
|
2016-03-04 15:01:09 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
case element.Strategy.AnonAttribute:
|
|
|
|
let attr = Object.keys(value)[0];
|
2017-06-30 02:40:24 +03:00
|
|
|
let el = rootNode.getAnonymousElementByAttribute(
|
|
|
|
startNode, attr, value[attr]);
|
2016-05-20 15:28:27 +03:00
|
|
|
if (el) {
|
|
|
|
return [el];
|
|
|
|
}
|
|
|
|
return [];
|
2016-03-04 15:01:09 +03:00
|
|
|
|
2016-05-20 15:28:27 +03:00
|
|
|
default:
|
|
|
|
throw new InvalidSelectorError(`No such strategy: ${using}`);
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 17:25:46 +03:00
|
|
|
|
2017-09-16 16:35:41 +03:00
|
|
|
/**
|
|
|
|
* Determines if <var>obj<var> is an HTML or JS collection.
|
|
|
|
*
|
|
|
|
* @param {*} seq
|
|
|
|
* Type to determine.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if <var>seq</va> is collection.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.isCollection = function(seq) {
|
2016-07-04 19:34:06 +03:00
|
|
|
switch (Object.prototype.toString.call(seq)) {
|
|
|
|
case "[object Arguments]":
|
|
|
|
case "[object Array]":
|
|
|
|
case "[object FileList]":
|
|
|
|
case "[object HTMLAllCollection]":
|
|
|
|
case "[object HTMLCollection]":
|
|
|
|
case "[object HTMLFormControlsCollection]":
|
|
|
|
case "[object HTMLOptionsCollection]":
|
|
|
|
case "[object NodeList]":
|
|
|
|
return true;
|
2016-02-23 18:18:55 +03:00
|
|
|
|
2016-07-04 19:34:06 +03:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-23 18:18:55 +03:00
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
element.makeWebElement = function(uuid) {
|
2016-02-23 18:18:55 +03:00
|
|
|
return {
|
|
|
|
[element.Key]: uuid,
|
|
|
|
[element.LegacyKey]: uuid,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-10-26 20:04:48 +03:00
|
|
|
/**
|
2017-06-30 02:40:24 +03:00
|
|
|
* Checks if |ref| has either |element.Key| or |element.LegacyKey|
|
|
|
|
* as properties.
|
2016-10-26 20:04:48 +03:00
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {Object.<string, string>} ref
|
2016-10-26 20:04:48 +03:00
|
|
|
* Object that represents a web element reference.
|
|
|
|
* @return {boolean}
|
|
|
|
* True if |ref| has either expected property.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.isWebElementReference = function(ref) {
|
2016-10-26 20:04:48 +03:00
|
|
|
let properties = Object.getOwnPropertyNames(ref);
|
2017-06-30 02:40:24 +03:00
|
|
|
return properties.includes(element.Key) ||
|
|
|
|
properties.includes(element.LegacyKey);
|
2016-10-26 20:04:48 +03:00
|
|
|
};
|
|
|
|
|
2016-02-23 18:01:12 +03:00
|
|
|
element.generateUUID = function() {
|
2016-01-14 17:25:46 +03:00
|
|
|
let uuid = uuidGen.generateUUID().toString();
|
|
|
|
return uuid.substring(1, uuid.length - 1);
|
|
|
|
};
|
2016-02-03 21:54:23 +03:00
|
|
|
|
2017-08-21 21:09:15 +03:00
|
|
|
/**
|
|
|
|
* Determines if <var>el</var> is stale.
|
|
|
|
*
|
2017-08-30 16:22:39 +03:00
|
|
|
* A stale element is an element no longer attached to the DOM or which
|
2017-10-02 21:09:26 +03:00
|
|
|
* node document is not the active document of the current browsing
|
|
|
|
* context.
|
|
|
|
*
|
|
|
|
* The currently selected browsing context, specified through
|
|
|
|
* <var>window<var>, is a WebDriver concept defining the target
|
|
|
|
* against which commands will run. As the current browsing context
|
|
|
|
* may differ from <var>el</var>'s associated context, an element is
|
|
|
|
* considered stale even if it is connected to a living (not discarded)
|
|
|
|
* browsing context such as an <tt><iframe></tt>.
|
|
|
|
*
|
|
|
|
* @param {Element=} el
|
|
|
|
* DOM element to check for staleness. If null, which may be
|
|
|
|
* the case if the element has been unwrapped from a weak
|
|
|
|
* reference, it is always considered stale.
|
|
|
|
* @param {WindowProxy=} window
|
|
|
|
* Current browsing context, which may differ from the associate
|
|
|
|
* browsing context of <var>el</var>. When retrieving XUL
|
|
|
|
* elements, this is optional.
|
2017-08-21 21:09:15 +03:00
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if <var>el</var> is stale, false otherwise.
|
|
|
|
*/
|
2017-10-02 21:09:26 +03:00
|
|
|
element.isStale = function(el, window = undefined) {
|
|
|
|
if (typeof window == "undefined") {
|
|
|
|
window = el.ownerGlobal;
|
2017-09-12 15:18:52 +03:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:09:26 +03:00
|
|
|
if (el === null ||
|
|
|
|
!el.ownerGlobal ||
|
|
|
|
el.ownerDocument !== window.document) {
|
2017-08-31 15:25:52 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-12 15:18:52 +03:00
|
|
|
|
2017-08-31 15:25:52 +03:00
|
|
|
return !el.isConnected;
|
2016-05-20 17:11:37 +03:00
|
|
|
};
|
|
|
|
|
2017-10-10 16:51:27 +03:00
|
|
|
/**
|
|
|
|
* Determine if <var>el</var> is selected or not.
|
|
|
|
*
|
|
|
|
* This operation only makes sense on
|
|
|
|
* <tt><input type=checkbox></tt>,
|
|
|
|
* <tt><input type=radio></tt>,
|
|
|
|
* and <tt>>option></tt> elements.
|
|
|
|
*
|
|
|
|
* @param {(DOMElement|XULElement)} el
|
|
|
|
* Element to test if selected.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if element is selected, false otherwise.
|
|
|
|
*/
|
|
|
|
element.isSelected = function(el) {
|
|
|
|
if (!el) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element.isXULElement(el)) {
|
|
|
|
if (XUL_CHECKED_ELS.has(el.tagName)) {
|
|
|
|
return el.checked;
|
|
|
|
} else if (XUL_SELECTED_ELS.has(el.tagName)) {
|
|
|
|
return el.selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ato): Use element.isDOMElement when bug 1400256 lands
|
|
|
|
} else if (typeof el == "object" &&
|
|
|
|
"nodeType" in el &&
|
|
|
|
el.nodeType == el.ELEMENT_NODE) {
|
|
|
|
if (el.localName == "input" && ["checkbox", "radio"].includes(el.type)) {
|
|
|
|
return el.checked;
|
|
|
|
} else if (el.localName == "option") {
|
|
|
|
return el.selected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-02-03 21:54:23 +03:00
|
|
|
/**
|
|
|
|
* This function generates a pair of coordinates relative to the viewport
|
|
|
|
* given a target element and coordinates relative to that element's
|
|
|
|
* top-left corner.
|
|
|
|
*
|
|
|
|
* @param {Node} node
|
|
|
|
* Target node.
|
2016-03-04 18:34:28 +03:00
|
|
|
* @param {number=} xOffset
|
|
|
|
* Horizontal offset relative to target's top-left corner.
|
|
|
|
* Defaults to the centre of the target's bounding box.
|
|
|
|
* @param {number=} yOffset
|
|
|
|
* Vertical offset relative to target's top-left corner. Defaults to
|
|
|
|
* the centre of the target's bounding box.
|
|
|
|
*
|
|
|
|
* @return {Object.<string, number>}
|
|
|
|
* X- and Y coordinates.
|
|
|
|
*
|
|
|
|
* @throws TypeError
|
|
|
|
* If |xOffset| or |yOffset| are not numbers.
|
2016-02-03 21:54:23 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.coordinates = function(
|
2016-03-04 18:34:28 +03:00
|
|
|
node, xOffset = undefined, yOffset = undefined) {
|
|
|
|
|
2016-02-03 21:54:23 +03:00
|
|
|
let box = node.getBoundingClientRect();
|
2016-03-04 18:34:28 +03:00
|
|
|
|
|
|
|
if (typeof xOffset == "undefined" || xOffset === null) {
|
|
|
|
xOffset = box.width / 2.0;
|
2016-02-03 21:54:23 +03:00
|
|
|
}
|
2016-03-04 18:34:28 +03:00
|
|
|
if (typeof yOffset == "undefined" || yOffset === null) {
|
|
|
|
yOffset = box.height / 2.0;
|
2016-02-03 21:54:23 +03:00
|
|
|
}
|
2016-03-04 18:34:28 +03:00
|
|
|
|
|
|
|
if (typeof yOffset != "number" || typeof xOffset != "number") {
|
|
|
|
throw new TypeError("Offset must be a number");
|
|
|
|
}
|
|
|
|
|
2016-02-03 21:54:23 +03:00
|
|
|
return {
|
2016-03-04 18:34:28 +03:00
|
|
|
x: box.left + xOffset,
|
|
|
|
y: box.top + yOffset,
|
2016-02-03 21:54:23 +03:00
|
|
|
};
|
2016-02-23 18:03:05 +03:00
|
|
|
};
|
2016-02-03 21:54:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns true if the node is in the viewport.
|
|
|
|
*
|
2016-03-08 16:53:00 +03:00
|
|
|
* @param {Element} el
|
2016-02-03 21:54:23 +03:00
|
|
|
* Target element.
|
|
|
|
* @param {number=} x
|
|
|
|
* Horizontal offset relative to target. Defaults to the centre of
|
|
|
|
* the target's bounding box.
|
|
|
|
* @param {number=} y
|
|
|
|
* Vertical offset relative to target. Defaults to the centre of
|
|
|
|
* the target's bounding box.
|
2016-03-08 16:53:00 +03:00
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if if |el| is in viewport, false otherwise.
|
2016-02-03 21:54:23 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.inViewport = function(el, x = undefined, y = undefined) {
|
2017-01-27 12:51:03 +03:00
|
|
|
let win = el.ownerGlobal;
|
2016-02-23 18:01:12 +03:00
|
|
|
let c = element.coordinates(el, x, y);
|
2016-02-03 21:54:23 +03:00
|
|
|
let vp = {
|
|
|
|
top: win.pageYOffset,
|
|
|
|
left: win.pageXOffset,
|
|
|
|
bottom: (win.pageYOffset + win.innerHeight),
|
2017-06-30 02:40:24 +03:00
|
|
|
right: (win.pageXOffset + win.innerWidth),
|
2016-02-03 21:54:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return (vp.left <= c.x + win.pageXOffset &&
|
|
|
|
c.x + win.pageXOffset <= vp.right &&
|
|
|
|
vp.top <= c.y + win.pageYOffset &&
|
|
|
|
c.y + win.pageYOffset <= vp.bottom);
|
|
|
|
};
|
|
|
|
|
2017-02-03 22:52:34 +03:00
|
|
|
/**
|
|
|
|
* Gets the element's container element.
|
|
|
|
*
|
|
|
|
* An element container is defined by the WebDriver
|
|
|
|
* specification to be an <option> element in a valid element context
|
|
|
|
* (https://html.spec.whatwg.org/#concept-element-contexts), meaning
|
|
|
|
* that it has an ancestral element that is either <datalist> or <select>.
|
|
|
|
*
|
|
|
|
* If the element does not have a valid context, its container element
|
|
|
|
* is itself.
|
|
|
|
*
|
|
|
|
* @param {Element} el
|
|
|
|
* Element to get the container of.
|
|
|
|
*
|
|
|
|
* @return {Element}
|
|
|
|
* Container element of |el|.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.getContainer = function(el) {
|
2017-02-03 22:52:34 +03:00
|
|
|
if (el.localName != "option") {
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
|
|
|
function validContext(ctx) {
|
|
|
|
return ctx.localName == "datalist" || ctx.localName == "select";
|
|
|
|
}
|
|
|
|
|
|
|
|
// does <option> have a valid context,
|
|
|
|
// meaning is it a child of <datalist> or <select>?
|
|
|
|
let parent = el;
|
|
|
|
while (parent.parentNode && !validContext(parent)) {
|
|
|
|
parent = parent.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validContext(parent)) {
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An element is in view if it is a member of its own pointer-interactable
|
|
|
|
* paint tree.
|
|
|
|
*
|
|
|
|
* This means an element is considered to be in view, but not necessarily
|
|
|
|
* pointer-interactable, if it is found somewhere in the
|
|
|
|
* |elementsFromPoint| list at |el|'s in-view centre coordinates.
|
|
|
|
*
|
2017-04-17 21:34:44 +03:00
|
|
|
* Before running the check, we change |el|'s pointerEvents style property
|
|
|
|
* to "auto", since elements without pointer events enabled do not turn
|
|
|
|
* up in the paint tree we get from document.elementsFromPoint. This is
|
|
|
|
* a specialisation that is only relevant when checking if the element is
|
|
|
|
* in view.
|
|
|
|
*
|
2017-02-03 22:52:34 +03:00
|
|
|
* @param {Element} el
|
|
|
|
* Element to check if is in view.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if |el| is inside the viewport, or false otherwise.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.isInView = function(el) {
|
2017-04-17 21:34:44 +03:00
|
|
|
let originalPointerEvents = el.style.pointerEvents;
|
|
|
|
try {
|
|
|
|
el.style.pointerEvents = "auto";
|
|
|
|
const tree = element.getPointerInteractablePaintTree(el);
|
|
|
|
return tree.includes(el);
|
|
|
|
} finally {
|
|
|
|
el.style.pointerEvents = originalPointerEvents;
|
|
|
|
}
|
2017-02-03 22:52:34 +03:00
|
|
|
};
|
|
|
|
|
2016-02-03 21:54:23 +03:00
|
|
|
/**
|
|
|
|
* This function throws the visibility of the element error if the element is
|
|
|
|
* not displayed or the given coordinates are not within the viewport.
|
|
|
|
*
|
2017-02-03 22:52:34 +03:00
|
|
|
* @param {Element} el
|
2016-02-03 21:54:23 +03:00
|
|
|
* Element to check if visible.
|
|
|
|
* @param {number=} x
|
|
|
|
* Horizontal offset relative to target. Defaults to the centre of
|
|
|
|
* the target's bounding box.
|
|
|
|
* @param {number=} y
|
|
|
|
* Vertical offset relative to target. Defaults to the centre of
|
|
|
|
* the target's bounding box.
|
2016-03-08 16:52:38 +03:00
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if visible, false otherwise.
|
2016-02-03 21:54:23 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.isVisible = function(el, x = undefined, y = undefined) {
|
2017-01-27 12:51:03 +03:00
|
|
|
let win = el.ownerGlobal;
|
2016-03-03 17:20:39 +03:00
|
|
|
|
2016-03-03 17:21:45 +03:00
|
|
|
// Bug 1094246: webdriver's isShown doesn't work with content xul
|
|
|
|
if (!element.isXULElement(el) && !atom.isElementDisplayed(el, win)) {
|
2016-02-03 21:54:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (el.tagName.toLowerCase() == "body") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-23 18:01:12 +03:00
|
|
|
if (!element.inViewport(el, x, y)) {
|
2016-11-15 00:06:50 +03:00
|
|
|
element.scrollIntoView(el);
|
|
|
|
if (!element.inViewport(el)) {
|
2016-02-03 21:54:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2016-03-04 19:44:38 +03:00
|
|
|
/**
|
|
|
|
* A pointer-interactable element is defined to be the first
|
|
|
|
* non-transparent element, defined by the paint order found at the centre
|
|
|
|
* point of its rectangle that is inside the viewport, excluding the size
|
|
|
|
* of any rendered scrollbars.
|
|
|
|
*
|
2017-04-17 19:24:19 +03:00
|
|
|
* An element is obscured if the pointer-interactable paint tree at its
|
|
|
|
* centre point is empty, or the first element in this tree is not an
|
|
|
|
* inclusive descendant of itself.
|
|
|
|
*
|
2016-03-04 19:44:38 +03:00
|
|
|
* @param {DOMElement} el
|
|
|
|
* Element determine if is pointer-interactable.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
2017-04-17 19:24:19 +03:00
|
|
|
* True if element is obscured, false otherwise.
|
2016-03-04 19:44:38 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.isObscured = function(el) {
|
2017-02-03 22:52:34 +03:00
|
|
|
let tree = element.getPointerInteractablePaintTree(el);
|
2017-04-17 19:24:19 +03:00
|
|
|
return !el.contains(tree[0]);
|
2016-03-04 19:44:38 +03:00
|
|
|
};
|
|
|
|
|
2017-08-04 17:51:19 +03:00
|
|
|
// TODO(ato): Only used by deprecated action API
|
|
|
|
// https://bugzil.la/1354578
|
2016-11-15 00:10:08 +03:00
|
|
|
/**
|
|
|
|
* Calculate the in-view centre point of the area of the given DOM client
|
|
|
|
* rectangle that is inside the viewport.
|
|
|
|
*
|
|
|
|
* @param {DOMRect} rect
|
2017-08-21 20:52:18 +03:00
|
|
|
* Element off a DOMRect sequence produced by calling
|
|
|
|
* <code>getClientRects</code> on an {@link Element}.
|
|
|
|
* @param {WindowProxy} window
|
|
|
|
* Current window global.
|
2016-11-15 00:10:08 +03:00
|
|
|
*
|
|
|
|
* @return {Map.<string, number>}
|
2017-08-21 20:52:18 +03:00
|
|
|
* X and Y coordinates that denotes the in-view centre point of
|
|
|
|
* <var>rect</var>.
|
2016-11-15 00:10:08 +03:00
|
|
|
*/
|
2017-08-21 20:52:18 +03:00
|
|
|
element.getInViewCentrePoint = function(rect, window) {
|
2016-11-15 00:10:08 +03:00
|
|
|
const {max, min} = Math;
|
|
|
|
|
|
|
|
let x = {
|
|
|
|
left: max(0, min(rect.x, rect.x + rect.width)),
|
2017-08-21 20:52:18 +03:00
|
|
|
right: min(window.innerWidth, max(rect.x, rect.x + rect.width)),
|
2016-11-15 00:10:08 +03:00
|
|
|
};
|
|
|
|
let y = {
|
|
|
|
top: max(0, min(rect.y, rect.y + rect.height)),
|
2017-08-21 20:52:18 +03:00
|
|
|
bottom: min(window.innerHeight, max(rect.y, rect.y + rect.height)),
|
2016-11-15 00:10:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: (x.left + x.right) / 2,
|
|
|
|
y: (y.top + y.bottom) / 2,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-03-04 19:44:38 +03:00
|
|
|
/**
|
|
|
|
* Produces a pointer-interactable elements tree from a given element.
|
|
|
|
*
|
|
|
|
* The tree is defined by the paint order found at the centre point of
|
|
|
|
* the element's rectangle that is inside the viewport, excluding the size
|
|
|
|
* of any rendered scrollbars.
|
|
|
|
*
|
|
|
|
* @param {DOMElement} el
|
|
|
|
* Element to determine if is pointer-interactable.
|
|
|
|
*
|
|
|
|
* @return {Array.<DOMElement>}
|
2017-02-03 22:52:34 +03:00
|
|
|
* Sequence of elements in paint order.
|
2016-03-04 19:44:38 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.getPointerInteractablePaintTree = function(el) {
|
2017-02-03 22:52:34 +03:00
|
|
|
const doc = el.ownerDocument;
|
|
|
|
const win = doc.defaultView;
|
2017-04-17 19:51:13 +03:00
|
|
|
const container = {frame: win};
|
|
|
|
const rootNode = el.getRootNode();
|
|
|
|
|
|
|
|
// Include shadow DOM host only if the element's root node is not the
|
|
|
|
// owner document.
|
|
|
|
if (rootNode !== doc) {
|
|
|
|
container.shadowRoot = rootNode;
|
|
|
|
}
|
2016-03-04 19:44:38 +03:00
|
|
|
|
2016-11-15 00:10:08 +03:00
|
|
|
// pointer-interactable elements tree, step 1
|
2017-08-30 16:22:39 +03:00
|
|
|
if (!el.isConnected) {
|
2016-11-15 00:10:08 +03:00
|
|
|
return [];
|
|
|
|
}
|
2016-03-04 19:44:38 +03:00
|
|
|
|
|
|
|
// steps 2-3
|
2016-11-15 00:10:08 +03:00
|
|
|
let rects = el.getClientRects();
|
|
|
|
if (rects.length == 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-03-04 19:44:38 +03:00
|
|
|
|
2016-11-15 00:10:08 +03:00
|
|
|
// step 4
|
|
|
|
let centre = element.getInViewCentrePoint(rects[0], win);
|
2016-03-04 19:44:38 +03:00
|
|
|
|
2016-11-15 00:10:08 +03:00
|
|
|
// step 5
|
2017-02-03 22:52:34 +03:00
|
|
|
return doc.elementsFromPoint(centre.x, centre.y);
|
2016-03-04 19:44:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(ato): Not implemented.
|
|
|
|
// In fact, it's not defined in the spec.
|
2017-10-03 16:35:47 +03:00
|
|
|
element.isKeyboardInteractable = () => true;
|
2016-03-04 19:44:38 +03:00
|
|
|
|
2016-11-15 00:06:50 +03:00
|
|
|
/**
|
|
|
|
* Attempts to scroll into view |el|.
|
|
|
|
*
|
|
|
|
* @param {DOMElement} el
|
|
|
|
* Element to scroll into view.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.scrollIntoView = function(el) {
|
2016-11-15 00:06:50 +03:00
|
|
|
if (el.scrollIntoView) {
|
|
|
|
el.scrollIntoView({block: "end", inline: "nearest", behavior: "instant"});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
element.isXULElement = function(el) {
|
2017-09-29 13:54:03 +03:00
|
|
|
return el.namespaceURI === XULNS;
|
2016-02-03 21:54:23 +03:00
|
|
|
};
|
2016-05-16 23:24:57 +03:00
|
|
|
|
|
|
|
const boolEls = {
|
|
|
|
audio: ["autoplay", "controls", "loop", "muted"],
|
|
|
|
button: ["autofocus", "disabled", "formnovalidate"],
|
|
|
|
details: ["open"],
|
|
|
|
dialog: ["open"],
|
|
|
|
fieldset: ["disabled"],
|
|
|
|
form: ["novalidate"],
|
|
|
|
iframe: ["allowfullscreen"],
|
|
|
|
img: ["ismap"],
|
2017-06-30 02:40:24 +03:00
|
|
|
input: [
|
|
|
|
"autofocus",
|
|
|
|
"checked",
|
|
|
|
"disabled",
|
|
|
|
"formnovalidate",
|
|
|
|
"multiple",
|
|
|
|
"readonly",
|
|
|
|
"required",
|
|
|
|
],
|
2016-05-16 23:24:57 +03:00
|
|
|
keygen: ["autofocus", "disabled"],
|
|
|
|
menuitem: ["checked", "default", "disabled"],
|
|
|
|
object: ["typemustmatch"],
|
|
|
|
ol: ["reversed"],
|
|
|
|
optgroup: ["disabled"],
|
|
|
|
option: ["disabled", "selected"],
|
|
|
|
script: ["async", "defer"],
|
|
|
|
select: ["autofocus", "disabled", "multiple", "required"],
|
|
|
|
textarea: ["autofocus", "disabled", "readonly", "required"],
|
|
|
|
track: ["default"],
|
|
|
|
video: ["autoplay", "controls", "loop", "muted"],
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if the attribute is a boolean attribute on element.
|
|
|
|
*
|
|
|
|
* @param {DOMElement} el
|
|
|
|
* Element to test if |attr| is a boolean attribute on.
|
|
|
|
* @param {string} attr
|
|
|
|
* Attribute to test is a boolean attribute.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if the attribute is boolean, false otherwise.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
element.isBooleanAttribute = function(el, attr) {
|
2016-05-16 23:24:57 +03:00
|
|
|
if (el.namespaceURI !== XMLNS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// global boolean attributes that apply to all HTML elements,
|
|
|
|
// except for custom elements
|
2017-06-30 02:40:24 +03:00
|
|
|
const customElement = !el.localName.includes("-");
|
|
|
|
if ((attr == "hidden" || attr == "itemscope") && customElement) {
|
2016-05-16 23:24:57 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!boolEls.hasOwnProperty(el.localName)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-29 12:03:00 +03:00
|
|
|
return boolEls[el.localName].includes(attr);
|
2016-05-16 23:24:57 +03:00
|
|
|
};
|