Skip to content

Commit 65dbe9a

Browse files
committed
Firefox: fixing click to multirect elements broken by the previous PR
1 parent f3dc828 commit 65dbe9a

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

javascript/firefox-driver/js/utils.js

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -684,37 +684,10 @@ Utils.getClickablePoint = function(element) {
684684
var rect = bot.dom.getClientRect(element);
685685

686686
var rects = goog.array.filter(element.getClientRects(), function(r) {
687-
return r.width !=0 && r.height != 0;
687+
return r.width != 0 && r.height != 0;
688688
});
689689

690-
if (rects.length > 0) {
691-
for (var i = 0; i < rects.length; i++) {
692-
var candidate = rects[i];
693-
if (clickable_point = findClickablePoint(candidate)){
694-
return clickable_point;
695-
}
696-
}
697-
rect = rects[0];
698-
}
699-
700-
// Fallback to the main rect - expected to return a point so if no clickable point return middle
701-
return findClickablePoint(rect) || { x: Math.floor(rect.width/2), y: Math.floor(rect.height/2) };
702-
703-
function findClickablePoint(rect){
704-
var offsets = [
705-
{ x: Math.floor(rect.width/2), y: Math.floor(rect.height/2) },
706-
{ x: 0, y: 0 },
707-
{ x: rect.width, y: 0 },
708-
{ x: 0, y: rect.height },
709-
{ x: rect.width, y: rect.height}
710-
]
711-
712-
return goog.array.find(offsets, function(offset){
713-
return isClickableAt( { x: rect.left + offset.x, y: rect.top + offset.y } );
714-
})
715-
}
716-
717-
function isClickableAt(coord) {
690+
var isClickableAt = function(coord) {
718691
// get the outermost ancestor of the element. This will be either the document
719692
// or a shadow root.
720693
var owner = element;
@@ -741,7 +714,49 @@ Utils.getClickablePoint = function(element) {
741714
}
742715
parentElemIter = parentElemIter.parentNode;
743716
}
717+
};
718+
719+
var rectPointRelativeToView = function(x, y, r) {
720+
return { x: r.left + x, y: r.top + y }
721+
};
722+
723+
var rectPointRelativeToMainRect = function(x, y, r) {
724+
return { x: r.left - rect.left + x, y: r.top - rect.top + y }
725+
};
726+
727+
var findClickablePoint = function(r) {
728+
var offsets = [
729+
{ x: Math.floor(r.width / 2), y: Math.floor(r.height / 2) },
730+
{ x: 0, y: 0 },
731+
{ x: r.width, y: 0 },
732+
{ x: 0, y: r.height },
733+
{ x: r.width, y: r.height}
734+
]
735+
736+
return goog.array.find(offsets, function(offset){
737+
return isClickableAt(rectPointRelativeToView(offset.x, offset.y, r));
738+
})
739+
};
740+
741+
if (rects.length > 1) {
742+
goog.log.warning(Utils.LOG_, 'Multirect element ', rects.length);
743+
for (var i = 0; i < rects.length; i++) {
744+
var p = findClickablePoint(rects[i]);
745+
if (p){
746+
goog.log.warning(Utils.LOG_, 'Found clickable point in rect ' + rects[i]);
747+
return rectPointRelativeToMainRect(p.x, p.y, rects[i]);
748+
}
749+
}
750+
}
751+
752+
// Fallback to the main rect
753+
var p = findClickablePoint(rect);
754+
if (p) {
755+
return p;
744756
}
757+
758+
// Expected to return a point so if there is no clickable point return middle
759+
return { x: Math.floor(rect.width / 2), y: Math.floor(rect.height / 2) };
745760
};
746761

747762

0 commit comments

Comments
 (0)