diff --git a/src/client/core/utils/position.ts b/src/client/core/utils/position.ts index 717092d4aa..d7f33838a0 100644 --- a/src/client/core/utils/position.ts +++ b/src/client/core/utils/position.ts @@ -118,12 +118,35 @@ export function getIframeClientCoordinates (iframe: HTMLIFrameElement): Boundary iframeRectangleLeft); } -export function containsOffset (el: HTMLElement, offsetX?: number, offsetY?: number): boolean { +function isImageMapArea (el: HTMLElement): boolean { + if (domUtils.getTagName(el) !== 'area') + return false; + + const map = domUtils.closest(el, 'map'); + const container = map && domUtils.getMapContainer(map); + + return !!container && domUtils.isImgElement(container); +} + +function getMaxOffset (el: HTMLElement): AxisValuesData { + if (hammerhead.utils.browser.isFirefox && isImageMapArea(el)) { + const { width, height } = getElementRectangle(el); + + return { x: width, y: height }; + } + const dimensions = getClientDimensions(el); const width = Math.max(el.scrollWidth, dimensions.width); const height = Math.max(el.scrollHeight, dimensions.height); - const maxX = dimensions.scrollbar.right + dimensions.border.left + dimensions.border.right + width; - const maxY = dimensions.scrollbar.bottom + dimensions.border.top + dimensions.border.bottom + height; + + return { + x: dimensions.scrollbar.right + dimensions.border.left + dimensions.border.right + width, + y: dimensions.scrollbar.bottom + dimensions.border.top + dimensions.border.bottom + height, + }; +} + +export function containsOffset (el: HTMLElement, offsetX?: number, offsetY?: number): boolean { + const { x: maxX, y: maxY } = getMaxOffset(el); return (typeof offsetX === 'undefined' || offsetX >= 0 && maxX >= offsetX) && (typeof offsetY === 'undefined' || offsetY >= 0 && maxY >= offsetY); diff --git a/test/client/fixtures/automation/click-test.js b/test/client/fixtures/automation/click-test.js index dbbc96f4e7..7ebc846142 100644 --- a/test/client/fixtures/automation/click-test.js +++ b/test/client/fixtures/automation/click-test.js @@ -3,8 +3,9 @@ const browserUtils = hammerhead.utils.browser; const featureDetection = hammerhead.utils.featureDetection; const Promise = hammerhead.Promise; -const testCafeCore = window.getTestCafeModule('testCafeCore'); -const styleUtils = testCafeCore.styleUtils; +const testCafeCore = window.getTestCafeModule('testCafeCore'); +const styleUtils = testCafeCore.styleUtils; +const positionUtils = testCafeCore.positionUtils; const testCafeAutomation = window.getTestCafeModule('testCafeAutomation'); const getOffsetOptions = testCafeAutomation.getOffsetOptions; @@ -1183,6 +1184,27 @@ $(document).ready(function () { }, TEST_RESULT_TIMEOUT + 200); }); + (browserUtils.isFirefox ? test : QUnit.skip)('Offsets of an image map area should be checked against its coords', function () { + const $map = $('') + .appendTo('body') + .addClass(TEST_ELEMENT_CLASS); + + const $area = $('').appendTo($map); + + $('') + .css({ + width: '200px', + height: '200px', + }) + .appendTo('body') + .addClass(TEST_ELEMENT_CLASS); + + ok(positionUtils.containsOffset($area[0], 10, 10)); + ok(positionUtils.containsOffset($area[0], 50, 100)); + notOk(positionUtils.containsOffset($area[0], 60, 10)); + notOk(positionUtils.containsOffset($area[0], 10, 110)); + }); + module('touch devices test'); if (featureDetection.isTouchDevice) { asyncTest('touch event on click', function () {