"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isFocusable = isFocusable; exports.isClickableInput = isClickableInput; exports.getMouseEventOptions = getMouseEventOptions; exports.isLabelWithInternallyDisabledControl = isLabelWithInternallyDisabledControl; exports.getActiveElement = getActiveElement; exports.calculateNewValue = calculateNewValue; exports.setSelectionRangeIfNecessary = setSelectionRangeIfNecessary; exports.eventWrapper = eventWrapper; exports.isValidDateValue = isValidDateValue; exports.isValidInputTimeValue = isValidInputTimeValue; exports.buildTimeValue = buildTimeValue; exports.getValue = getValue; exports.getSelectionRange = getSelectionRange; exports.isContentEditable = isContentEditable; exports.isInstanceOfElement = isInstanceOfElement; exports.isVisible = isVisible; exports.FOCUSABLE_SELECTOR = void 0; var _dom = require("@testing-library/dom"); var _helpers = require("@testing-library/dom/dist/helpers"); // isInstanceOfElement can be removed once the peerDependency for @testing-library/dom is bumped to a version that includes https://github.com/testing-library/dom-testing-library/pull/885 /** * Check if an element is of a given type. * * @param {Element} element The element to test * @param {string} elementType Constructor name. E.g. 'HTMLSelectElement' */ function isInstanceOfElement(element, elementType) { try { const window = (0, _helpers.getWindowFromNode)(element); // Window usually has the element constructors as properties but is not required to do so per specs if (typeof window[elementType] === 'function') { return element instanceof window[elementType]; } } catch (e) {// The document might not be associated with a window } // Fall back to the constructor name as workaround for test environments that // a) not associate the document with a window // b) not provide the constructor as property of window if (/^HTML(\w+)Element$/.test(element.constructor.name)) { return element.constructor.name === elementType; } // The user passed some node that is not created in a browser-like environment throw new Error(`Unable to verify if element is instance of ${elementType}. Please file an issue describing your test environment: https://github.com/testing-library/dom-testing-library/issues/new`); } function isMousePressEvent(event) { return event === 'mousedown' || event === 'mouseup' || event === 'click' || event === 'dblclick'; } function invert(map) { const res = {}; for (const key of Object.keys(map)) { res[map[key]] = key; } return res; } // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons const BUTTONS_TO_NAMES = { 0: 'none', 1: 'primary', 2: 'secondary', 4: 'auxiliary' }; const NAMES_TO_BUTTONS = invert(BUTTONS_TO_NAMES); // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button const BUTTON_TO_NAMES = { 0: 'primary', 1: 'auxiliary', 2: 'secondary' }; const NAMES_TO_BUTTON = invert(BUTTON_TO_NAMES); function convertMouseButtons(event, init, property, mapping) { if (!isMousePressEvent(event)) { return 0; } if (init[property] != null) { return init[property]; } if (init.buttons != null) { // not sure how to test this. Feel free to try and add a test if you want. // istanbul ignore next return mapping[BUTTONS_TO_NAMES[init.buttons]] || 0; } if (init.button != null) { // not sure how to test this. Feel free to try and add a test if you want. // istanbul ignore next return mapping[BUTTON_TO_NAMES[init.button]] || 0; } return property != 'button' && isMousePressEvent(event) ? 1 : 0; } function getMouseEventOptions(event, init, clickCount = 0) { init = init || {}; return { ...init, // https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail detail: event === 'mousedown' || event === 'mouseup' || event === 'click' ? 1 + clickCount : clickCount, buttons: convertMouseButtons(event, init, 'buttons', NAMES_TO_BUTTONS), button: convertMouseButtons(event, init, 'button', NAMES_TO_BUTTON) }; } // Absolutely NO events fire on label elements that contain their control // if that control is disabled. NUTS! // no joke. There are NO events for: