Bug 1354211 - Use element.findClosest in element.getContainer. r?automatedtester draft
authorAndreas Tolfsen <ato@sny.no>
Sun, 31 Dec 2017 14:41:56 +0000
changeset 719572 f48ce49bc3e39c9d08657c3f4601a2ab67044550
parent 719571 328157a76ddd6a3ea61ba139f6c45a93bee4f70b
child 719573 1f60473180a0de1c360fc6b6efbf62af3d8ec6bc
push id95293
push userbmo:ato@sny.no
push dateFri, 12 Jan 2018 10:32:36 +0000
reviewersautomatedtester
bugs1354211
milestone59.0a1
Bug 1354211 - Use element.findClosest in element.getContainer. r?automatedtester element.findClosest is a more generalised version of findAncestralElement, which is limited to a list of element tag names. By using element.findClosest and CSS matching we also avoid a redundant iteration. MozReview-Commit-ID: 32QwmkwiGf5
testing/marionette/element.js
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -869,36 +869,25 @@ element.inViewport = function(el, x = un
  *
  * @param {Element} el
  *     Element to get the container of.
  *
  * @return {Element}
  *     Container element of <var>el</var>.
  */
 element.getContainer = function(el) {
-  function findAncestralElement(startNode, validAncestors) {
-    let node = startNode;
-    while (node.parentNode) {
-      node = node.parentNode;
-      if (validAncestors.includes(node.localName)) {
-        return node;
-      }
-    }
-    return startNode;
-  }
-
   // Does <option> or <optgroup> have a valid context,
   // meaning is it a child of <datalist> or <select>?
   if (["option", "optgroup"].includes(el.localName)) {
-    return findAncestralElement(el, ["datalist", "select"]);
+    return element.findClosest(el, "datalist,select") || el;
   }
 
   // Child nodes of button will not be part of the element tree for
   // elementsFromPoint until bug 1089326 is fixed.
-  return findAncestralElement(el, ["button"]);
+  return element.findClosest(el, "button") || el;
 };
 
 /**
  * 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