diff --git a/src/js/scriptlets/element-picker.js b/src/js/scriptlets/element-picker.js index d3d8948a3..00b706674 100644 --- a/src/js/scriptlets/element-picker.js +++ b/src/js/scriptlets/element-picker.js @@ -189,6 +189,47 @@ var safeQuerySelectorAll = function(node, selector) { /******************************************************************************/ +var getElementBoundingClientRect = function(elem) { + var prect = typeof elem.getBoundingClientRect === 'function' ? + elem.getBoundingClientRect() : + { height: 0, left: 0, top: 0, width: 0 }; + + // https://github.com/gorhill/uBlock/issues/1024 + // Try not returning an empty bounding rect. + if ( prect.width !== 0 && prect.height !== 0 ) { + return prect; + } + + var left = prect.left, + right = prect.right, + top = prect.top, + bottom = prect.bottom, + crect; + + var children = elem.children, + i = children.length; + + while ( i-- ) { + crect = getElementBoundingClientRect(children[i]); + if ( crect.width === 0 || crect.height === 0 ) { + continue; + } + if ( crect.left < left ) { left = crect.left; } + if ( crect.right > right ) { right = crect.right; } + if ( crect.top < top ) { top = crect.top; } + if ( crect.bottom > bottom ) { bottom = crect.bottom; } + } + + return { + height: bottom - top, + left: left, + top: top, + width: right - left + }; +}; + +/******************************************************************************/ + var highlightElements = function(elems, force) { // To make mouse move handler more efficient if ( !force && elems.length === targetElements.length ) { @@ -215,10 +256,7 @@ var highlightElements = function(elems, force) { if ( elem === pickerRoot ) { continue; } - if ( typeof elem.getBoundingClientRect !== 'function' ) { - continue; - } - rect = elem.getBoundingClientRect(); + rect = getElementBoundingClientRect(elem); // Ignore if it's not on the screen if ( rect.left > ow || rect.top > oh ||