How to use correctContentEditableSelectionBeforeDelete method in Testcafe

Best JavaScript code snippet using testcafe

index.js

Source:index.js Github

copy

Full Screen

...2305 selectByNodesAndOffsets(endPosition, startPosition, needFocus);2306 else2307 selectByNodesAndOffsets(startPosition, endPosition, needFocus);2308 }2309 function correctContentEditableSelectionBeforeDelete(el) {2310 var selection = getSelectionByElement(el);2311 var startNode = selection.anchorNode;2312 var endNode = selection.focusNode;2313 var startOffset = selection.anchorOffset;2314 var endOffset = selection.focusOffset;2315 var startNodeFirstNonWhitespaceSymbol = getFirstNonWhitespaceSymbolIndex(startNode.nodeValue);2316 var startNodeLastNonWhitespaceSymbol = getLastNonWhitespaceSymbolIndex(startNode.nodeValue);2317 var endNodeFirstNonWhitespaceSymbol = getFirstNonWhitespaceSymbolIndex(endNode.nodeValue);2318 var endNodeLastNonWhitespaceSymbol = getLastNonWhitespaceSymbolIndex(endNode.nodeValue);2319 var newStartOffset = null;2320 var newEndOffset = null;2321 if (isTextNode(startNode)) {2322 if (startOffset < startNodeFirstNonWhitespaceSymbol && startOffset !== 0)2323 newStartOffset = 0;2324 else if (startOffset !== startNode.nodeValue.length &&2325 (isInvisibleTextNode(startNode) && startOffset !== 0 ||2326 startOffset > startNodeLastNonWhitespaceSymbol))2327 newStartOffset = startNode.nodeValue.length;2328 }2329 if (isTextNode(endNode)) {2330 if (endOffset < endNodeFirstNonWhitespaceSymbol && endOffset !== 0)2331 newEndOffset = 0;2332 else if (endOffset !== endNode.nodeValue.length &&2333 (isInvisibleTextNode(endNode) && endOffset !== 0 ||2334 endOffset > endNodeLastNonWhitespaceSymbol))2335 newEndOffset = endNode.nodeValue.length;2336 }2337 if (browserUtils$6.isWebKit || browserUtils$6.isIE && browserUtils$6.version > 11) {2338 if (newStartOffset !== null) {2339 if (newStartOffset === 0)2340 startNode.nodeValue = startNode.nodeValue.substring(startNodeFirstNonWhitespaceSymbol);2341 else2342 startNode.nodeValue = startNode.nodeValue.substring(0, startNodeLastNonWhitespaceSymbol);2343 }2344 if (newEndOffset !== null) {2345 if (newEndOffset === 0)2346 endNode.nodeValue = endNode.nodeValue.substring(endNodeFirstNonWhitespaceSymbol);2347 else2348 endNode.nodeValue = endNode.nodeValue.substring(0, endNodeLastNonWhitespaceSymbol);2349 }2350 }2351 if (newStartOffset !== null || newEndOffset !== null) {2352 if (newStartOffset !== null)2353 newStartOffset = newStartOffset === 0 ? newStartOffset : startNode.nodeValue.length;2354 else2355 newStartOffset = startOffset;2356 if (newEndOffset !== null)2357 newEndOffset = newEndOffset === 0 ? newEndOffset : endNode.nodeValue.length;2358 else2359 newEndOffset = endOffset;2360 var startPos = { node: startNode, offset: newStartOffset };2361 var endPos = { node: endNode, offset: newEndOffset };2362 selectByNodesAndOffsets(startPos, endPos);2363 }2364 }2365 //API2366 function hasInverseSelectionContentEditable(el) {2367 var curDocument = el ? findDocument(el) : document;2368 var selection = curDocument.getSelection();2369 var range = null;2370 var backward = false;2371 if (selection) {2372 if (!selection.isCollapsed) {2373 range = curDocument.createRange();2374 range.setStart(selection.anchorNode, selection.anchorOffset);2375 range.setEnd(selection.focusNode, selection.focusOffset);2376 backward = range.collapsed;2377 range.detach();2378 }2379 }2380 return backward;2381 }2382 function isInverseSelectionContentEditable(element, startPos, endPos) {2383 var startPosition = calculatePositionByNodeAndOffset(element, startPos);2384 var endPosition = calculatePositionByNodeAndOffset(element, endPos);2385 return startPosition > endPosition;2386 }2387 function getSelectionStart$1(el) {2388 var selection = null;2389 if (!isContentEditableElement(el))2390 return selectionSandbox.getSelection(el).start;2391 if (hasElementContainsSelection(el)) {2392 selection = getSelectionByElement(el);2393 return getSelectionStartPosition(el, selection, hasInverseSelectionContentEditable(el));2394 }2395 return 0;2396 }2397 function getSelectionEnd$1(el) {2398 var selection = null;2399 if (!isContentEditableElement(el))2400 return selectionSandbox.getSelection(el).end;2401 if (hasElementContainsSelection(el)) {2402 selection = getSelectionByElement(el);2403 return getSelectionEndPosition(el, selection, hasInverseSelectionContentEditable(el));2404 }2405 return 0;2406 }2407 function hasInverseSelection(el) {2408 if (isContentEditableElement(el))2409 return hasInverseSelectionContentEditable(el);2410 return (selectionSandbox.getSelection(el).direction || selectionDirection) === BACKWARD_SELECTION_DIRECTION;2411 }2412 function getSelectionByElement(el) {2413 var currentDocument = findDocument(el);2414 return currentDocument ? currentDocument.getSelection() : window.getSelection();2415 }2416 function select(el, from, to) {2417 if (isContentEditableElement(el)) {2418 selectContentEditable(el, from, to, true);2419 return;2420 }2421 var start = from || 0;2422 var end = typeof to === 'undefined' ? getElementValue(el).length : to;2423 var inverse = false;2424 var temp = null;2425 if (start > end) {2426 temp = start;2427 start = end;2428 end = temp;2429 inverse = true;2430 }2431 selectionSandbox.setSelection(el, start, end, inverse ? BACKWARD_SELECTION_DIRECTION : FORWARD_SELECTION_DIRECTION);2432 if (from === to)2433 selectionDirection = NONE_SELECTION_DIRECTION;2434 else2435 selectionDirection = inverse ? BACKWARD_SELECTION_DIRECTION : FORWARD_SELECTION_DIRECTION;2436 }2437 function selectByNodesAndOffsets(startPos, endPos, needFocus) {2438 var startNode = startPos.node;2439 var endNode = endPos.node;2440 var startNodeLength = startNode.nodeValue ? startNode.length : 0;2441 var endNodeLength = endNode.nodeValue ? endNode.length : 0;2442 var startOffset = startPos.offset;2443 var endOffset = endPos.offset;2444 if (!isElementNode(startNode) || !startOffset)2445 startOffset = Math.min(startNodeLength, startPos.offset);2446 if (!isElementNode(endNode) || !endOffset)2447 endOffset = Math.min(endNodeLength, endPos.offset);2448 var parentElement = findContentEditableParent(startNode);2449 var inverse = isInverseSelectionContentEditable(parentElement, startPos, endPos);2450 var selection = getSelectionByElement(parentElement);2451 var curDocument = findDocument(parentElement);2452 var range = curDocument.createRange();2453 var selectionSetter = function () {2454 selection.removeAllRanges();2455 //NOTE: For IE we can't create inverse selection2456 if (!inverse) {2457 range.setStart(startNode, startOffset);2458 range.setEnd(endNode, endOffset);2459 selection.addRange(range);2460 }2461 else if (browserUtils$6.isIE) {2462 range.setStart(endNode, endOffset);2463 range.setEnd(startNode, startOffset);2464 selection.addRange(range);2465 }2466 else {2467 range.setStart(startNode, startOffset);2468 range.setEnd(startNode, startOffset);2469 selection.addRange(range);2470 var shouldCutEndOffset = browserUtils$6.isSafari || browserUtils$6.isChrome && browserUtils$6.version < 58;2471 var extendSelection = function (node, offset) {2472 // NODE: in some cases in Firefox extend method raises error so we use try-catch2473 try {2474 selection.extend(node, offset);2475 }2476 catch (err) {2477 return false;2478 }2479 return true;2480 };2481 if (shouldCutEndOffset && isInvisibleTextNode(endNode)) {2482 if (!extendSelection(endNode, Math.min(endOffset, 1)))2483 extendSelection(endNode, 0);2484 }2485 else2486 extendSelection(endNode, endOffset);2487 }2488 };2489 selectionSandbox.wrapSetterSelection(parentElement, selectionSetter, needFocus, true);2490 }2491 function deleteSelectionRanges(el) {2492 var selection = getSelectionByElement(el);2493 var rangeCount = selection.rangeCount;2494 if (!rangeCount)2495 return;2496 for (var i = 0; i < rangeCount; i++)2497 selection.getRangeAt(i).deleteContents();2498 }2499 function deleteSelectionContents(el, selectAll) {2500 var startSelection = getSelectionStart$1(el);2501 var endSelection = getSelectionEnd$1(el);2502 if (selectAll)2503 selectContentEditable(el);2504 if (startSelection === endSelection)2505 return;2506 // NOTE: If selection is not contain initial and final invisible symbols2507 //we should select its2508 correctContentEditableSelectionBeforeDelete(el);2509 deleteSelectionRanges(el);2510 var selection = getSelectionByElement(el);2511 var range = null;2512 //NOTE: We should try to do selection collapsed2513 if (selection.rangeCount && !selection.getRangeAt(0).collapsed) {2514 range = selection.getRangeAt(0);2515 range.collapse(true);2516 }2517 }2518 function setCursorToLastVisiblePosition(el) {2519 var position = getLastVisiblePosition(el);2520 selectContentEditable(el, position, position);2521 }2522 function hasElementContainsSelection(el) {...

Full Screen

Full Screen

text_selection.js

Source:text_selection.js Github

copy

Full Screen

...134 if (!startPosition.node || !endPosition.node)135 return;136 exports.selectByNodesAndOffsets(startPosition.node, startPosition.offset, endPosition.node, endPosition.offset, needFocus, inverse);137 }138 function correctContentEditableSelectionBeforeDelete(el) {139 var selection = exports.getSelectionByElement(el),140 startNode = selection.anchorNode,141 endNode = selection.focusNode,142 startOffset = selection.anchorOffset,143 endOffset = selection.focusOffset,144 startNodeFirstNonWhitespaceSymbol = ContentEditableHelper.getFirstNonWhitespaceSymbolIndex(startNode.nodeValue),145 startNodeLastNonWhitespaceSymbol = ContentEditableHelper.getLastNonWhitespaceSymbolIndex(startNode.nodeValue),146 endNodeFirstNonWhitespaceSymbol = ContentEditableHelper.getFirstNonWhitespaceSymbolIndex(endNode.nodeValue),147 endNodeLastNonWhitespaceSymbol = ContentEditableHelper.getLastNonWhitespaceSymbolIndex(endNode.nodeValue),148 newStartOffset = null,149 newEndOffset = null;150 if (startNode.nodeType === 3) {151 if (startOffset < startNodeFirstNonWhitespaceSymbol && startOffset !== 0)152 newStartOffset = 0;153 else if (startOffset !== startNode.nodeValue.length && ((ContentEditableHelper.isInvisibleTextNode(startNode) && startOffset !== 0) ||154 (startOffset > startNodeLastNonWhitespaceSymbol)))155 newStartOffset = startNode.nodeValue.length;156 }157 if (endNode.nodeType === 3) {158 if (endOffset < endNodeFirstNonWhitespaceSymbol && endOffset !== 0)159 newEndOffset = 0;160 else if (endOffset !== endNode.nodeValue.length && ((ContentEditableHelper.isInvisibleTextNode(endNode) && endOffset !== 0) ||161 (endOffset > endNodeLastNonWhitespaceSymbol)))162 newEndOffset = endNode.nodeValue.length;163 }164 if ($.browser.webkit) {165 if (newStartOffset !== null) {166 if (newStartOffset === 0)167 startNode.nodeValue = startNode.nodeValue.substring(startNodeFirstNonWhitespaceSymbol);168 else169 startNode.nodeValue = startNode.nodeValue.substring(0, startNodeLastNonWhitespaceSymbol);170 }171 if (newEndOffset !== null) {172 if (newEndOffset === 0)173 endNode.nodeValue = endNode.nodeValue.substring(endNodeFirstNonWhitespaceSymbol);174 else175 endNode.nodeValue = endNode.nodeValue.substring(0, endNodeLastNonWhitespaceSymbol);176 }177 }178 if (newStartOffset !== null || newEndOffset !== null) {179 newStartOffset = newStartOffset !== null ? (newStartOffset === 0 ? newStartOffset : startNode.nodeValue.length) : startOffset;180 newEndOffset = newEndOffset !== null ? (newEndOffset === 0 ? newEndOffset : endNode.nodeValue.length) : endOffset;181 exports.selectByNodesAndOffsets(startNode, newStartOffset, endNode, newEndOffset);182 }183 }184 function correctRectangle(currentRect, options) {185 var documentScroll = options.documentScroll,186 iFrameDocumentScroll = options.iFrameDocumentScroll,187 iFrameOffset = options.iFrameOffset,188 iFramePadding = options.iFramePadding,189 iFrameBorders = options.iFrameBorders,190 currentRectHeight = currentRect.top + options.elementHeight - 1,191 clientOffset = null,192 currentLeft = null,193 currentTop = null,194 currentBottom = null;195 if (Util.isIE && !Util.isIE11 && options.isInProcessedIFrame) {196 if (Util.browserVersion === 9 && !options.isContentEditable) {197 currentLeft = Math.ceil(currentRect.left) + options.windowTopScroll.left - options.crossDomainIFrameOffset.left - options.crossDomainIFrameBorders.left - options.crossDomainIFramePadding.left;198 currentTop = Math.ceil(currentRect.top) + options.windowTopScroll.top - options.crossDomainIFrameOffset.top - options.crossDomainIFrameBorders.top - options.crossDomainIFramePadding.top;199 currentBottom = Math.ceil(currentRect.bottom) + options.windowTopScroll.top - options.crossDomainIFrameOffset.top - options.crossDomainIFrameBorders.top - options.crossDomainIFramePadding.top;200 } else if (Util.browserVersion === 10 || options.isContentEditable) {201 currentLeft = Math.ceil(currentRect.left);202 currentTop = Math.ceil(currentRect.top);203 currentBottom = Math.ceil(currentRect.bottom);204 }205 } else {206 if (options.isTextarea) {207 currentLeft = Math.ceil(currentRect.left);208 currentTop = Math.ceil(currentRect.top);209 currentBottom = Math.ceil(currentRect.bottom);210 } else {211 if (options.isInIFrame && (options.isContentEditable || Util.isIE)) {212 clientOffset = options.elementOffset;213 clientOffset.left -= (iFrameOffset.left + iFrameBorders.left + iFramePadding.left);214 clientOffset.top -= (iFrameOffset.top + iFrameBorders.top + iFramePadding.top);215 clientOffset = Util.offsetToClientCoords({x: clientOffset.left, y: clientOffset.top});216 } else217 clientOffset = Util.offsetToClientCoords({x: options.elementOffset.left, y: options.elementOffset.top});218 currentLeft = Math.ceil(Math.ceil(currentRect.left) <= clientOffset.x ? clientOffset.x + options.elementBorders.left + 1 : currentRect.left);219 currentTop = Math.ceil(Math.ceil(currentRect.top) <= clientOffset.y ? clientOffset.y + options.elementBorders.top + 1 : currentRect.top);220 currentBottom = Math.floor(Math.floor(currentRect.bottom) >= (clientOffset.y + options.elementBorders.top + options.elementBorders.bottom + options.elementHeight) ? currentRectHeight : currentRect.bottom);221 }222 }223 if (options.isInIFrame && (options.isContentEditable || (Util.isIE && Util.browserVersion !== 9))) {224 currentLeft = currentLeft + iFrameDocumentScroll.left + iFrameOffset.left + iFrameBorders.left + iFramePadding.left;225 currentTop = currentTop + iFrameDocumentScroll.top + iFrameOffset.top + iFrameBorders.top + iFramePadding.top;226 currentBottom = currentBottom + iFrameDocumentScroll.top + iFrameOffset.top + iFrameBorders.top + iFramePadding.top;227 } else if (options.isInIFrame && Util.isIE && Util.browserVersion === 9) {228 currentLeft = currentLeft + iFrameDocumentScroll.left + documentScroll.left;229 currentTop = currentTop + iFrameDocumentScroll.top + documentScroll.top;230 currentBottom = currentBottom + iFrameDocumentScroll.top + documentScroll.top;231 } else if (options.isContentEditable || (Util.isIE && !Util.isIE11)) {232 currentLeft = currentLeft + documentScroll.left;233 currentTop = currentTop + documentScroll.top;234 currentBottom = currentBottom + documentScroll.top;235 } else {236 currentLeft = currentLeft + documentScroll.left + iFrameDocumentScroll.left;237 currentTop = currentTop + documentScroll.top + iFrameDocumentScroll.top;238 currentBottom = currentBottom + documentScroll.top + iFrameDocumentScroll.top;239 }240 return {241 bottom: currentBottom,242 left: currentLeft,243 top: currentTop244 };245 }246 //API247 exports.getSelectionStart = function (el) {248 var selection = null;249 if (!Util.isContentEditableElement(el))250 return EventSandbox.getSelection(el).start;251 if (exports.hasElementContainsSelection(el)) {252 selection = exports.getSelectionByElement(el);253 return ContentEditableHelper.getSelectionStartPosition(el, selection, hasInverseSelectionContentEditable(el));254 }255 return 0;256 };257 exports.getSelectionEnd = function (el) {258 var selection = null;259 if (!Util.isContentEditableElement(el))260 return EventSandbox.getSelection(el).end;261 if (exports.hasElementContainsSelection(el)) {262 selection = exports.getSelectionByElement(el);263 return ContentEditableHelper.getSelectionEndPosition(el, selection, hasInverseSelectionContentEditable(el));264 }265 return 0;266 };267 exports.getSelectedText = function (el) {268 return el.value.substring(exports.getSelectionStart(el), exports.getSelectionEnd(el));269 };270 exports.hasInverseSelection = function (el) {271 if (Util.isContentEditableElement(el))272 return hasInverseSelectionContentEditable(el);273 return (EventSandbox.getSelection(el).direction || selectionDirection) === BACKWARD_SELECTION_DIRECTION;274 };275 exports.hasInverseSelectionContentEditable = hasInverseSelectionContentEditable;276 exports.getSelectionByElement = function (el) {277 var currentDocument = Util.findDocument(el);278 return currentDocument ? currentDocument.getSelection() : window.getSelection();279 };280 exports.getPositionCoordinates = function (el, position, correctOptions) {281 var range = null,282 rects = null,283 selectionPosition = null,284 rect = null,285 isTextarea = el.tagName.toLowerCase() === 'textarea',286 isContentEditable = Util.isContentEditableElement(el),287 offset = Util.getOffsetPosition(el);288 //NOTE: we don't create fake div element for contentEditable elements289 //because we can get the selection dimensions directly290 if (isContentEditable) {291 range = Util.findDocument(el).createRange();292 selectionPosition = ContentEditableHelper.calculateNodeAndOffsetByPosition(el, position);293 range.setStart(selectionPosition.node, Math.min(selectionPosition.offset, selectionPosition.node.length));294 range.setEnd(selectionPosition.node, Math.min(selectionPosition.offset, selectionPosition.node.length));295 rect = range.getClientRects()[0];296 return rect ? correctRectangle(rect, correctOptions) : null;297 }298 //NOTE: for IE299 if (typeof el.createTextRange === "function") {300 range = el.createTextRange();301 range.collapse(true);302 range.moveStart('character', position);303 range.moveEnd('character', position);304 range.collapse(true);305 rect = range.getBoundingClientRect();306 return rect ? correctRectangle(rect, correctOptions) : null;307 }308 var $body = $(document).find('body'),309 bodyMargin = Util.getElementMargin($body),310 bodyLeft = null,311 bodyTop = null,312 elementMargin = Util.getElementMargin($(el)),313 elementTop = offset.top - elementMargin.top,314 elementLeft = offset.left - elementMargin.left,315 width = el.scrollWidth,316 $fakeDiv = $('<div></div>'),317 fakeDivCssStyles = 'white-space:pre-wrap;border-style:solid;',318 listOfModifiers = ['direction', 'font-family', 'font-size', 'font-size-adjust', 'font-variant', 'font-weight', 'font-style', 'letter-spacing', 'line-height', 'text-align', 'text-indent', 'text-transform', 'word-wrap', 'word-spacing', 'padding-top', 'padding-left', 'padding-right', 'padding-bottom', 'margin-top', 'margin-left', 'margin-right', 'margin-bottom', 'border-top-width', 'border-left-width', 'border-right-width', 'border-bottom-width'];319 if (Util.getCssStyleValue($body[0], 'position') === 'absolute') {320 elementLeft -= bodyMargin.left;321 elementTop -= bodyMargin.top;322 bodyLeft = Util.getCssStyleValue($body[0], 'left');323 if (bodyLeft !== 'auto')324 elementLeft -= parseInt(bodyLeft.replace('px', ''));325 bodyTop = Util.getCssStyleValue($body[0], 'top');326 if (bodyTop !== 'auto')327 elementTop -= parseInt(bodyTop.replace('px', ''));328 }329 $.each(listOfModifiers, function (index, value) {330 fakeDivCssStyles += value + ':' + Util.getCssStyleValue(el, value) + ';';331 });332 $fakeDiv.appendTo($body);333 try {334 $fakeDiv.css({335 cssText: fakeDivCssStyles,336 position: 'absolute',337 top: elementTop,338 left: elementLeft,339 width: width,340 height: el.scrollHeight341 });342 $fakeDiv[0].textContent = !el.value.length ? ' ' : el.value;343 range = document.createRange(); //B254723344 range.setStart($fakeDiv[0].firstChild, Math.min(position, el.value.length));345 range.setEnd($fakeDiv[0].firstChild, Math.min(position, el.value.length));346 if (isTextarea) {347 rects = range.getClientRects();348 rect = range.getBoundingClientRect();349 if (rect.width === 0 && rect.height === 0)350 rect = rects[0];351 } else352 rect = range.getClientRects()[0];353 $fakeDiv.remove();354 } catch (err) {355 $fakeDiv.remove();356 return {};357 }358 return rect ? correctRectangle(rect, correctOptions) : null;359 };360 exports.select = function (el, from, to, inverse) {361 if (Util.isContentEditableElement(el)) {362 selectContentEditable(el, from, to, true, inverse);363 return;364 }365 var start = from || 0,366 end = typeof to === 'undefined' ? el.value.length : to,367 temp = null;368 if (start > end) {369 temp = start;370 start = end;371 end = temp;372 inverse = true;373 }374 EventSandbox.setSelection(el, start, end, inverse ? BACKWARD_SELECTION_DIRECTION : FORWARD_SELECTION_DIRECTION);375 selectionDirection = from === to ?376 NONE_SELECTION_DIRECTION :377 inverse ? BACKWARD_SELECTION_DIRECTION : FORWARD_SELECTION_DIRECTION;378 };379 exports.selectByNodesAndOffsets = function (startNode, startOffset, endNode, endOffset, needFocus, inverse) {380 var parentElement = ContentEditableHelper.findContentEditableParent(startNode),381 curDocument = Util.findDocument(parentElement),382 selection = exports.getSelectionByElement(parentElement),383 range = curDocument.createRange(),384 startNodeLength = startNode.nodeValue ? startNode.length : 0,385 endNodeLength = endNode.nodeValue ? endNode.length : 0;386 var selectionSetter = function () {387 selection.removeAllRanges();388 //NOTE: For IE we can't create inverse selection389 if (!inverse || Util.isIE) {390 range.setStart(startNode, Math.min(startNodeLength, startOffset));391 range.setEnd(endNode, Math.min(endNodeLength, endOffset));392 selection.addRange(range);393 } else {394 range.setStart(endNode, Math.min(endNodeLength, endOffset));395 range.setEnd(endNode, Math.min(endNodeLength, endOffset));396 selection.addRange(range);397 if ($.browser.webkit && ContentEditableHelper.isInvisibleTextNode(startNode)) {398 try {399 selection.extend(startNode, Math.min(startOffset, 1));400 } catch (err) {401 selection.extend(startNode, 0);402 }403 } else404 selection.extend(startNode, Math.min(startNodeLength, startOffset));405 }406 };407 EventSandbox.wrapSetterSelection(parentElement, selectionSetter, needFocus, true);408 };409 exports.deleteSelectionContents = function (el, selectAll) {410 var startSelection = exports.getSelectionStart(el),411 endSelection = exports.getSelectionEnd(el);412 function deleteSelectionRanges(el) {413 var selection = exports.getSelectionByElement(el),414 rangeCount = selection.rangeCount;415 if (!rangeCount)416 return;417 for (var i = 0; i < rangeCount; i++)418 selection.getRangeAt(i).deleteContents();419 }420 if (selectAll)421 selectContentEditable(el);422 if (startSelection === endSelection)423 return;424 // NOTE: If selection is not contain initial and final invisible symbols425 //we should select its426 correctContentEditableSelectionBeforeDelete(el);427 deleteSelectionRanges(el);428 var selection = exports.getSelectionByElement(el),429 range = null;430 //NOTE: We should try to do selection collapsed431 if (selection.rangeCount && !selection.getRangeAt(0).collapsed) {432 range = selection.getRangeAt(0);433 range.collapse(true);434 }435 };436 exports.setCursorToLastVisiblePosition = function (el) {437 var position = ContentEditableHelper.getLastVisiblePosition(el);438 selectContentEditable(el, position, position);439 };440 exports.hasElementContainsSelection = function (el) {...

Full Screen

Full Screen

text-selection.js

Source:text-selection.js Github

copy

Full Screen

...340 if (startSelection === endSelection)341 return;342 // NOTE: If selection is not contain initial and final invisible symbols343 //we should select its344 correctContentEditableSelectionBeforeDelete(el);345 deleteSelectionRanges(el);346 const selection = getSelectionByElement(el);347 let range = null;348 //NOTE: We should try to do selection collapsed349 if (selection.rangeCount && !selection.getRangeAt(0).collapsed) {350 range = selection.getRangeAt(0);351 range.collapse(true);352 }353}354export function setCursorToLastVisiblePosition (el) {355 const position = contentEditable.getLastVisiblePosition(el);356 selectContentEditable(el, position, position);357}358export function hasElementContainsSelection (el) {...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { ClientFunction } from 'testcafe';2import { Selector } from 'testcafe';3import { t } from 'testcafe';4const correctContentEditableSelectionBeforeDelete = ClientFunction(() => {5 const selection = window.getSelection();6 if (selection.rangeCount > 0) {7 const range = selection.getRangeAt(0);8 if (range.startOffset === 0 && range.endOffset === 0) {9 const previousNode = range.startContainer.previousSibling;10 if (previousNode && previousNode.nodeType === Node.TEXT_NODE) {11 range.setStart(previousNode, previousNode.nodeValue.length);12 range.setEnd(previousNode, previousNode.nodeValue.length);13 selection.removeAllRanges();14 selection.addRange(range);15 }16 }17 }18});19test('Test', async t => {20 .click(Selector('#tryhome > div:nth-child(1) > div > div.w3-col.m9 > div:nth-child(2) > div:nth-child(2) > div > iframe'))21 .switchToIframe(Selector('#iframeResult'))22 .click(Selector('#myDate'))23 .typeText(Selector('#myDate'), '2000-01-01')24 .pressKey('enter')25 .switchToMainWindow()26 .click(Selector('#tryhome > div:nth-child(1) > div > div.w3-col.m9 > div:nth-child(2) > div:nth-child(2) > div > iframe'))27 .switchToIframe(Selector('#iframeResult'))28 .click(Selector('#myDate'))29 .typeText(Selector('#myDate'), '2000-01-01')30 .pressKey('enter')31 .switchToMainWindow()32 .click(Selector('#tryhome > div:nth-child(1) > div > div.w3-col.m9 > div:nth-child(2) > div:nth-child(2) > div > iframe'))33 .switchToIframe(Selector('#iframeResult'))34 .click(Selector('#myDate'))35 .typeText(Selector('#myDate'), '2000-01-01')36 .pressKey('enter')37 .switchToMainWindow()38 .click(Selector('#tryhome > div

Full Screen

Using AI Code Generation

copy

Full Screen

1import { correctContentEditableSelectionBeforeDelete } from 'testcafe/lib/client-functions';2import { Selector } from 'testcafe';3test('My first test', async t => {4 const contentEditable = Selector('#contentEditable');5 .click(contentEditable)6 .typeText(contentEditable, 'Hello, world!')7 .pressKey('backspace')8 .expect(contentEditable.textContent).eql('Hello, worl');9});10correctContentEditableSelectionBeforeDelete();11Selector('#contentEditable');12import { Selector } from 'testcafe';13test('My first test', async t => {14 const contentEditable = Selector('#contentEditable');15 .click(contentEditable)16 .typeText(contentEditable, 'Hello, world!')17 .pressKey('backspace')18 .expect(contentEditable.textContent).eql('Hello, worl');19});

Full Screen

Using AI Code Generation

copy

Full Screen

1import { ClientFunction } from 'testcafe';2import { Selector } from 'testcafe';3import { t } from 'testcafe';4const correctContentEditableSelectionBeforeDelete = ClientFunction((selector, text) => {5 const el = document.querySelector(selector);6 const range = document.createRange();7 const sel = window.getSelection();8 range.setStart(el.childNodes[0], text.length);9 range.collapse(true);10 sel.removeAllRanges();11 sel.addRange(range);12});13test('My first test', async t => {14 .typeText('#lst-ib', 'hello')

Full Screen

Using AI Code Generation

copy

Full Screen

1import { correctContentEditableSelectionBeforeDelete } from 'testcafe';2test('My Test', async t => {3 .typeText('#editor', 'Hello', { caretPos: 5 })4 .pressKey('delete')5 .expect(correctContentEditableSelectionBeforeDelete('#editor').innerText).eql('Helo');6});7import { correctContentEditableSelectionBeforeDelete } from 'testcafe';8test('My Test', async t => {9 .typeText('#editor', 'Hello', { caretPos: 5 })10 .pressKey('delete')11 .expect(correctContentEditableSelectionBeforeDelete('#editor').innerText).eql('Helo');12});13import { correctContentEditableSelectionBeforeDelete } from 'testcafe';14test('My Test', async t => {15 .typeText('#editor', 'Hello', { caretPos: 5 })16 .pressKey('delete')17 .expect(correctContentEditableSelectionBeforeDelete('#editor').innerText).eql('Helo');18});19import { correctContentEditableSelectionBeforeDelete } from 'testcafe';20test('My Test', async t => {21 .typeText('#editor', 'Hello', { caretPos: 5 })22 .pressKey('delete')23 .expect(correctContentEditableSelectionBeforeDelete('#editor').innerText).eql('Helo');24});25import { correctContentEditableSelectionBeforeDelete } from 'testcafe';26test('My Test', async t => {27 .typeText('#editor', 'Hello',

Full Screen

Using AI Code Generation

copy

Full Screen

1import { correctContentEditableSelectionBeforeDelete } from './correctContentEditableSelectionBeforeDelete';2import { Selector } from 'testcafe';3test('Test', async t => {4 .click(Selector('#contenteditable'))5 .pressKey('delete')6 .expect(Selector('#contenteditable').innerText).eql('Hello World');7});8export function correctContentEditableSelectionBeforeDelete (editableElement, selection) {9 const selectionStart = selection.start;10 const selectionEnd = selection.end;11 const selectionStartNode = selectionStart.node;12 const selectionEndNode = selectionEnd.node;13 const selectionStartOffset = selectionStart.offset;14 const selectionEndOffset = selectionEnd.offset;15 const isSelectionForward = selectionStartNode.compareDocumentPosition(selectionEndNode) === Node.DOCUMENT_POSITION_FOLLOWING;16 const startNode = isSelectionForward ? selectionStartNode : selectionEndNode;17 const endNode = isSelectionForward ? selectionEndNode : selectionStartNode;18 const startOffset = isSelectionForward ? selectionStartOffset : selectionEndOffset;19 const endOffset = isSelectionForward ? selectionEndOffset : selectionStartOffset;20 let startNodeIsEditable = startNode.nodeType === Node.TEXT_NODE && startNode.parentElement === editableElement;21 let endNodeIsEditable = endNode.nodeType === Node.TEXT_NODE && endNode.parentElement === editableElement;22 if (!startNodeIsEditable && !endNodeIsEditable) {23 const startNodeParent = startNode.parentElement;24 const endNodeParent = endNode.parentElement;25 const startNodeParentIsEditable = startNodeParent === editableElement;26 const endNodeParentIsEditable = endNodeParent === editableElement;27 if (startNodeParentIsEditable && endNodeParentIsEditable) {28 const startNodeParentChildren = Array.from(startNodeParent.childNodes);29 const endNodeParentChildren = Array.from(endNodeParent.childNodes);30 const startNodeIndex = startNodeParentChildren.indexOf(startNode);31 const endNodeIndex = endNodeParentChildren.indexOf(endNode);32 startNodeIsEditable = startNodeIndex >= 0;33 endNodeIsEditable = endNodeIndex >= 0;34 if (startNodeIsEditable && endNodeIsEditable

Full Screen

Using AI Code Generation

copy

Full Screen

1import { correctContentEditableSelectionBeforeDelete } from './testcafe-fix'2test('My test', async t => {3 .typeText('#editor', 'Some text')4 .pressKey('ctrl+a')5 .pressKey('delete')6 .expect(Selector('#editor').textContent).eql('')7})8export function correctContentEditableSelectionBeforeDelete (selection) {9 const selectionStart = selection.anchorOffset;10 const selectionEnd = selection.focusOffset;11 if (selectionStart === selectionEnd && selectionStart === 0)12 return;13 if (selectionStart > selectionEnd) {14 selection.setBaseAndExtent(selection.focusNode, selectionEnd, selection.anchorNode, selectionStart);15 }16}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { correctContentEditableSelectionBeforeDelete } from 'testcafe/lib/client-functions/selectors/correct-content-editable-selection-before-delete';2const selector = Selector('div');3const textContent = await selector.textContent;4const selectionStart = await selector.getSelectionStart();5const selectionEnd = await selector.getSelectionEnd();6const selectionDirection = await selector.getSelectionDirection();7await correctContentEditableSelectionBeforeDelete(textContent, selectionStart, selectionEnd, selectionDirection);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcafe automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful