Skip to content

Commit

Permalink
refactor: removed IE leftovers from src\client\core\utils (part 2) (#…
Browse files Browse the repository at this point in the history
…7999)

<!--
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 316f158 commit 87d42a3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 144 deletions.
10 changes: 5 additions & 5 deletions src/client/core/utils/key-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ const SPECIAL_KEYS = {
};

const KEY_PROPERTY = {
left: browserUtils.isIE ? 'Left' : 'ArrowLeft',
down: browserUtils.isIE ? 'Down' : 'ArrowDown',
right: browserUtils.isIE ? 'Right' : 'ArrowRight',
up: browserUtils.isIE ? 'Up' : 'ArrowUp',
left: 'ArrowLeft',
down: 'ArrowDown',
right: 'ArrowRight',
up: 'ArrowUp',
backspace: 'Backspace',
capslock: 'CapsLock',
delete: 'Delete',
Expand All @@ -69,7 +69,7 @@ const KEY_PROPERTY = {
ins: 'Insert',
pagedown: 'PageDown',
pageup: 'PageUp',
space: browserUtils.isIE ? 'Spacebar' : ' ',
space: ' ',
tab: 'Tab',
alt: 'Alt',
ctrl: 'Control',
Expand Down
3 changes: 1 addition & 2 deletions src/client/core/utils/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export function getClientDimensions (target: HTMLElement): Dimensions {

export function getElementFromPoint ({ x, y }: AxisValuesData<number>): Element | null {
// @ts-ignore
const ieFn = document.getElementFromPoint;
const func = ieFn || document.elementFromPoint;
const func = document.elementFromPoint;

let el: Element | null = null;

Expand Down
19 changes: 5 additions & 14 deletions src/client/core/utils/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@ import { utils, nativeMethods } from '../deps/hammerhead';
import * as domUtils from './dom';
import * as styleUtils from './style';

const SCROLLABLE_OVERFLOW_STYLE_RE = /auto|scroll|hidden/i;
const DEFAULT_IE_SCROLLABLE_OVERFLOW_STYLE_VALUE = 'visible';
const SCROLLABLE_OVERFLOW_STYLE_RE = /auto|scroll|hidden/i;

function getScrollable (el: Element): AxisValues<boolean> {
const overflowX = styleUtils.get(el, 'overflowX') as string;
const overflowY = styleUtils.get(el, 'overflowY') as string;
let scrollableHorizontally = SCROLLABLE_OVERFLOW_STYLE_RE.test(overflowX);
let scrollableVertically = SCROLLABLE_OVERFLOW_STYLE_RE.test(overflowY);

// IE11 and MS Edge bug: There are two properties: overflow-x and overflow-y.
// If one property is set so that the browser may show scrollbars (`auto` or `scroll`) and the second one is set to 'visible',
// then the second one will work as if it had the 'auto' value.
if (utils.browser.isIE) {
scrollableHorizontally = scrollableHorizontally || scrollableVertically && overflowX === DEFAULT_IE_SCROLLABLE_OVERFLOW_STYLE_VALUE;
scrollableVertically = scrollableVertically || scrollableHorizontally && overflowY === DEFAULT_IE_SCROLLABLE_OVERFLOW_STYLE_VALUE;
}
const overflowX = styleUtils.get(el, 'overflowX') as string;
const overflowY = styleUtils.get(el, 'overflowY') as string;
const scrollableHorizontally = SCROLLABLE_OVERFLOW_STYLE_RE.test(overflowX);
const scrollableVertically = SCROLLABLE_OVERFLOW_STYLE_RE.test(overflowY);

return new AxisValues(scrollableHorizontally, scrollableVertically);
}
Expand Down
125 changes: 2 additions & 123 deletions src/client/core/utils/text-selection.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,18 @@
import hammerhead from '../deps/hammerhead';
import * as domUtils from './dom';
import * as contentEditable from './content-editable';
import * as eventUtils from './event';


const browserUtils = hammerhead.utils.browser;
const nativeMethods = hammerhead.nativeMethods;
const selectionSandbox = hammerhead.eventSandbox.selection;


//NOTE: we can't determine selection direction in ie from dom api. Therefore we should listen selection changes,
// and calculate direction using it.
const BACKWARD_SELECTION_DIRECTION = 'backward';
const FORWARD_SELECTION_DIRECTION = 'forward';
const NONE_SELECTION_DIRECTION = 'none';


let selectionDirection = NONE_SELECTION_DIRECTION;
let initialLeft = 0;
let initialTop = 0;
let lastSelectionHeight = 0;
let lastSelectionLeft = 0;
let lastSelectionLength = 0;
let lastSelectionTop = 0;

function stateChanged (left, top, height, width, selectionLength) {
if (!selectionLength) {
initialLeft = left;
initialTop = top;
selectionDirection = NONE_SELECTION_DIRECTION;
}
else {
switch (selectionDirection) {
case NONE_SELECTION_DIRECTION:
if (top === lastSelectionTop && (left === lastSelectionLeft || height > lastSelectionHeight))
selectionDirection = FORWARD_SELECTION_DIRECTION;
else if (left < lastSelectionLeft || top < lastSelectionTop)
selectionDirection = BACKWARD_SELECTION_DIRECTION;

break;

case FORWARD_SELECTION_DIRECTION:
if (left === lastSelectionLeft && top === lastSelectionTop ||
left < lastSelectionLeft && height > lastSelectionHeight ||
top === lastSelectionTop && height === lastSelectionHeight &&
selectionLength > lastSelectionLength &&
left + width !== initialLeft)
break;
else if (left < lastSelectionLeft || top < lastSelectionTop)
selectionDirection = BACKWARD_SELECTION_DIRECTION;

break;

case BACKWARD_SELECTION_DIRECTION:
if ((left < lastSelectionLeft || top < lastSelectionTop) && selectionLength > lastSelectionLength)
break;
else if (top === initialTop && (left >= initialLeft || height > lastSelectionHeight))
selectionDirection = FORWARD_SELECTION_DIRECTION;

break;
}
}

lastSelectionHeight = height;
lastSelectionLeft = left;
lastSelectionLength = selectionLength;
lastSelectionTop = top;
}

function onSelectionChange () {
let activeElement = null;
let endSelection = null;
let range = null;
let rect = null;
let startSelection = null;

try {
if (this.selection)
range = this.selection.createRange();
else {
//HACK: we need do this for IE11 because otherwise we can not get TextRange properties
activeElement = nativeMethods.documentActiveElementGetter.call(this);

if (!activeElement || !domUtils.isTextEditableElement(activeElement)) {
selectionDirection = NONE_SELECTION_DIRECTION;

return;
}

startSelection = getSelectionStart(activeElement);
endSelection = getSelectionEnd(activeElement);

if (activeElement.createTextRange) {
range = activeElement.createTextRange();
range.collapse(true);
range.moveStart('character', startSelection);
range.moveEnd('character', endSelection - startSelection);
}
else if (document.createRange) {
//NOTE: for MSEdge
range = document.createRange();

const textNode = hammerhead.nativeMethods.nodeFirstChildGetter.call(activeElement);

range.setStart(textNode, startSelection);
range.setEnd(textNode, endSelection);
rect = range.getBoundingClientRect();
}
}
}
catch (e) {
//NOTE: in ie it raises error when there are not a real selection
selectionDirection = NONE_SELECTION_DIRECTION;

return;
}

const rangeLeft = rect ? Math.ceil(rect.left) : range.offsetLeft;
const rangeTop = rect ? Math.ceil(rect.top) : range.offsetTop;
const rangeHeight = rect ? Math.ceil(rect.height) : range.boundingHeight;
const rangeWidth = rect ? Math.ceil(rect.width) : range.boundingWidth;
const rangeHTMLTextLength = range.htmlText ? range.htmlText.length : 0;
const rangeTextLength = rect ? range.toString().length : rangeHTMLTextLength;

stateChanged(rangeLeft, rangeTop, rangeHeight, rangeWidth, rangeTextLength);
}

if (browserUtils.isIE)
eventUtils.bind(document, 'selectionchange', onSelectionChange, true);
let selectionDirection = NONE_SELECTION_DIRECTION;

//utils for contentEditable
function selectContentEditable (el, from, to, needFocus) {
Expand Down Expand Up @@ -212,7 +97,7 @@ function correctContentEditableSelectionBeforeDelete (el) {
newEndOffset = endNode.nodeValue.length;
}

if (browserUtils.isWebKit || browserUtils.isIE && browserUtils.version > 11) {
if (browserUtils.isWebKit) {
if (newStartOffset !== null) {
if (newStartOffset === 0)
startNode.nodeValue = startNode.nodeValue.substring(startNodeFirstNonWhitespaceSymbol);
Expand Down Expand Up @@ -369,17 +254,11 @@ export function selectByNodesAndOffsets (startPos, endPos, needFocus) {
const selectionSetter = function () {
selection.removeAllRanges();

//NOTE: For IE we can't create inverse selection
if (!inverse) {
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
selection.addRange(range);
}
else if (browserUtils.isIE) {
range.setStart(endNode, endOffset);
range.setEnd(startNode, startOffset);
selection.addRange(range);
}
else {
range.setStart(startNode, startOffset);
range.setEnd(startNode, startOffset);
Expand Down

0 comments on commit 87d42a3

Please sign in to comment.