Skip to content

Commit

Permalink
refactor: removed IE leftovers from src\client\core (#8000)
Browse files Browse the repository at this point in the history
<!--
Thank you for your contribution.

Before making a PR, please read our contributing guidelines at

https://github.com/DevExpress/testcafe/blob/master/CONTRIBUTING.md#code-contribution

We recommend creating a *draft* PR, so that you can mark it as 'ready
for review' when you are done.
-->

## Purpose
_Describe the problem you want to address or the feature you want to
implement._

## Approach
_Describe how your changes address the issue or implement the desired
functionality in as much detail as possible._

## References
_Provide a link to the existing issue(s), if any._

## Pre-Merge TODO
- [ ] Write tests for your proposed changes
- [ ] Make sure that existing tests do not fail
  • Loading branch information
Aleksey28 authored Sep 12, 2023
1 parent 87d42a3 commit 056b9e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 72 deletions.
48 changes: 9 additions & 39 deletions src/client/core/prevent-real-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ import {

import scrollController from './scroll/controller';

import { get, hasDimensions } from './utils/style';
import { filter } from './utils/array';
import {
isShadowUIElement,
isWindow,
getParents,
} from './utils/dom';
import { hasDimensions } from './utils/style';
import { isShadowUIElement } from './utils/dom';

const browserUtils = utils.browser;
const listeners = eventSandbox.listeners;
const eventSimulator = eventSandbox.eventSimulator;
const browserUtils = utils.browser;
const listeners = eventSandbox.listeners;

const PREVENTED_EVENTS = [
'click', 'mousedown', 'mouseup', 'dblclick', 'contextmenu', 'mousemove', 'mouseover', 'mouseout',
Expand Down Expand Up @@ -47,35 +41,11 @@ function preventRealEventHandler (e, dispatched, preventDefault, cancelHandlers,
}

// NOTE: if an element loses focus because of becoming invisible, the blur event is
// raised. We must not prevent this blur event. In IE, an element loses focus only
// if the CSS 'display' property is set to 'none', other ways of making an element
// invisible don't lead to blurring (in MSEdge, focus/blur are sync).
if (e.type === 'blur') {
if (browserUtils.isIE && browserUtils.version < 12) {
const isWindowInstance = isWindow(target);
const isElementInvisible = !isWindowInstance && get(target, 'display') === 'none';
let elementParents = null;
let invisibleParents = false;

if (!isWindowInstance && !isElementInvisible) {
elementParents = getParents(target);
invisibleParents = filter(elementParents, parent => get(parent, 'display') === 'none');
}

if (isElementInvisible || invisibleParents.length) {
// NOTE: In IE we should prevent the event and raise it on timeout. This is a fix for
// the case when a focus event leads to the element disappearing. If we don't prevent
// the blur event it will be raised before the previous focus event is raised (see B254768)
eventSandbox.timers.deferFunction(() => {
eventSimulator.blur(target);
});
}
}
// NOTE: fix for a jQuery bug. An exception is raised when calling .is(':visible')
// for a window or document on page loading (when e.ownerDocument is null).
else if (target !== window && target !== window.document && !hasDimensions(target))
return;
}
// raised. We must not prevent this blur event.
// NOTE: fix for a jQuery bug. An exception is raised when calling .is(':visible')
// for a window or document on page loading (when e.ownerDocument is null).
if (e.type === 'blur' && target !== window && target !== window.document && !hasDimensions(target))
return;

preventDefault();
}
Expand Down
42 changes: 9 additions & 33 deletions src/client/core/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const isMapElement = hammerhead.utils.dom.isMap
export const isBodyElement = hammerhead.utils.dom.isBodyElement;
export const isHtmlElement = hammerhead.utils.dom.isHtmlElement;
export const isDocument = hammerhead.utils.dom.isDocument;
export const isWindow = hammerhead.utils.dom.isWindow;
export const isTextEditableInput = hammerhead.utils.dom.isTextEditableInput;
export const isTextEditableElement = hammerhead.utils.dom.isTextEditableElement;
export const isTextEditableElementAndEditingAllowed = hammerhead.utils.dom.isTextEditableElementAndEditingAllowed;
Expand Down Expand Up @@ -75,7 +74,7 @@ function canFocus (element, parent, tabIndex) {
if (getElementStyleProperty(element, 'display') === 'none' || getElementStyleProperty(element, 'visibility') === 'hidden')
return false;

if ((browserUtils.isIE || browserUtils.isAndroid) && isOptionElement(element))
if (browserUtils.isAndroid && isOptionElement(element))
return false;

if (tabIndex !== null && tabIndex < 0)
Expand Down Expand Up @@ -127,33 +126,23 @@ function filterFocusableElements (parent) {
let tagName = null;
let tabIndex = null;

let needPush = false;

for (let i = 0; i < allElements.length; i++) {
element = allElements[i];
tagName = getTagName(element);
tabIndex = getTabIndexAttributeIntValue(element);
needPush = false;

if (!canFocus(element, parent, tabIndex))
continue;

if (inputElementsRegExp.test(tagName))
needPush = true;
else if (element.shadowRoot)
needPush = true;
else if (isIframeElement(element))
needPush = true;
else if (isAnchorElement(element) && element.hasAttribute('href'))
needPush = element.getAttribute('href') !== '' || !browserUtils.isIE || tabIndex !== null;

const contentEditableAttr = element.getAttribute('contenteditable');

if (contentEditableAttr === '' || contentEditableAttr === 'true')
needPush = true;

if (tabIndex !== null)
needPush = true;
const needPush = inputElementsRegExp.test(tagName)
|| element.shadowRoot
|| isIframeElement(element)
|| isAnchorElement(element) && element.hasAttribute('href')
|| contentEditableAttr === ''
|| contentEditableAttr === 'true'
|| tabIndex !== null;

if (needPush)
focusableElements.push(element);
Expand Down Expand Up @@ -265,8 +254,6 @@ export function blocksImplicitSubmission (el) {
inputTypeRegExp = /^(text|password|color|date|time|datetime|datetime-local|email|month|number|search|tel|url|week|image)$/i;
else if (browserUtils.isFirefox)
inputTypeRegExp = /^(text|password|date|time|datetime|datetime-local|email|month|number|search|tel|url|week|image)$/i;
else if (browserUtils.isIE)
inputTypeRegExp = /^(text|password|color|date|time|datetime|datetime-local|email|file|month|number|search|tel|url|week|image)$/i;
else
inputTypeRegExp = /^(text|password|datetime|email|number|search|tel|url|image)$/i;

Expand Down Expand Up @@ -358,11 +345,6 @@ export function remove (el) {
}

export function isIFrameWindowInDOM (win) {
//NOTE: In MS Edge, if an iframe is removed from DOM, the browser throws an exception when accessing window.top
//and window.frameElement. Fortunately, setTimeout is set to undefined in this case.
if (!win.setTimeout)
return false;

let frameElement = null;

try {
Expand All @@ -382,13 +364,7 @@ export function isIFrameWindowInDOM (win) {
}

export function isTopWindow (win) {
try {
//NOTE: MS Edge throws an exception when trying to access window.top from an iframe removed from DOM
return win.top === win;
}
catch (e) {
return false;
}
return win.top === win;
}

export function findIframeByWindow (iframeWindow) {
Expand Down

0 comments on commit 056b9e8

Please sign in to comment.