Best JavaScript code snippet using testcafe
index.js
Source:index.js  
...1421    //NOTE: before such elements (like div or p) adds line breaks before and after it1422    // (except line break before first visible element in contentEditable parent)1423    // this line breaks is not contained in node values1424    //so we should take it into account manually1425    function isNodeBlockWithBreakLine(parent, node) {1426        var parentFirstVisibleChild = null;1427        var firstVisibleChild = null;1428        if (isShadowUIElement(parent) || isShadowUIElement(node))1429            return false;1430        if (!isTheSameNode(node, parent) && getChildNodesLength(node.childNodes) &&1431            /div|p/.test(getTagName(node))) {1432            parentFirstVisibleChild = getOwnFirstVisibleNode(parent);1433            if (!parentFirstVisibleChild || isTheSameNode(node, parentFirstVisibleChild))1434                return false;1435            firstVisibleChild = getFirstVisibleTextNode(parentFirstVisibleChild);1436            if (!firstVisibleChild || isTheSameNode(node, firstVisibleChild))1437                return false;1438            return getOwnFirstVisibleTextNode(node);1439        }1440        return false;1441    }1442    function isNodeAfterNodeBlockWithBreakLine(parent, node) {1443        var isRenderedNode$1 = isRenderedNode(node);1444        var parentFirstVisibleChild = null;1445        var firstVisibleChild = null;1446        var previousSibling = null;1447        if (isShadowUIElement(parent) || isShadowUIElement(node))1448            return false;1449        if (!isTheSameNode(node, parent) &&1450            (isRenderedNode$1 && isElementNode(node) && getChildNodesLength(node.childNodes) &&1451                !/div|p/.test(getTagName(node)) ||1452                isVisibleTextNode(node) && !isTheSameNode(node, parent) && node.nodeValue.length)) {1453            if (isRenderedNode$1 && isElementNode(node)) {1454                parentFirstVisibleChild = getOwnFirstVisibleNode(parent);1455                if (!parentFirstVisibleChild || isTheSameNode(node, parentFirstVisibleChild))1456                    return false;1457                firstVisibleChild = getFirstVisibleTextNode(parentFirstVisibleChild);1458                if (!firstVisibleChild || isTheSameNode(node, firstVisibleChild))1459                    return false;1460            }1461            previousSibling = getOwnPreviousVisibleSibling(node);1462            return previousSibling && isElementNode(previousSibling) &&1463                /div|p/.test(getTagName(previousSibling)) && getOwnFirstVisibleTextNode(previousSibling);1464        }1465        return false;1466    }1467    function getFirstTextNode(el, onlyVisible) {1468        var children = el.childNodes;1469        var childrenLength = getChildNodesLength(children);1470        var curNode = null;1471        var child = null;1472        var isNotContentEditableElement = null;1473        var checkTextNode = onlyVisible ? isVisibleTextNode : isTextNode;1474        if (!childrenLength && checkTextNode(el))1475            return el;1476        for (var i = 0; i < childrenLength; i++) {1477            curNode = children[i];1478            isNotContentEditableElement = isElementNode(curNode) && !isContentEditableElement(curNode);1479            if (checkTextNode(curNode))1480                return curNode;1481            else if (isRenderedNode(curNode) && hasVisibleChildren(curNode) && !isNotContentEditableElement) {1482                child = getFirstTextNode(curNode, onlyVisible);1483                if (child)1484                    return child;1485            }1486        }1487        return child;1488    }1489    function getFirstVisibleTextNode(el) {1490        return getFirstTextNode(el, true);1491    }1492    function getLastTextNode(el, onlyVisible) {1493        var children = el.childNodes;1494        var childrenLength = getChildNodesLength(children);1495        var curNode = null;1496        var child = null;1497        var isNotContentEditableElement = null;1498        var visibleTextNode = null;1499        if (!childrenLength && isVisibleTextNode(el))1500            return el;1501        for (var i = childrenLength - 1; i >= 0; i--) {1502            curNode = children[i];1503            isNotContentEditableElement = isElementNode(curNode) && !isContentEditableElement(curNode);1504            visibleTextNode = isTextNode(curNode) &&1505                (onlyVisible ? !isInvisibleTextNode(curNode) : true);1506            if (visibleTextNode)1507                return curNode;1508            else if (isRenderedNode(curNode) && hasVisibleChildren(curNode) && !isNotContentEditableElement) {1509                child = getLastTextNode(curNode, false);1510                if (child)1511                    return child;1512            }1513        }1514        return child;1515    }1516    function getFirstNonWhitespaceSymbolIndex(nodeValue, startFrom) {1517        if (!nodeValue || !nodeValue.length)1518            return 0;1519        var valueLength = nodeValue.length;1520        var index = startFrom || 0;1521        for (var i = index; i < valueLength; i++) {1522            if (nodeValue.charCodeAt(i) === 10 || nodeValue.charCodeAt(i) === 32)1523                index++;1524            else1525                break;1526        }1527        return index;1528    }1529    function getLastNonWhitespaceSymbolIndex(nodeValue) {1530        if (!nodeValue || !nodeValue.length)1531            return 0;1532        var valueLength = nodeValue.length;1533        var index = valueLength;1534        for (var i = valueLength - 1; i >= 0; i--) {1535            if (nodeValue.charCodeAt(i) === 10 || nodeValue.charCodeAt(i) === 32)1536                index--;1537            else1538                break;1539        }1540        return index;1541    }1542    function isInvisibleTextNode(node) {1543        if (!isTextNode(node))1544            return false;1545        var nodeValue = node.nodeValue;1546        var firstVisibleIndex = getFirstNonWhitespaceSymbolIndex(nodeValue);1547        var lastVisibleIndex = getLastNonWhitespaceSymbolIndex(nodeValue);1548        return firstVisibleIndex === nodeValue.length && lastVisibleIndex === 0;1549    }1550    function isVisibleTextNode(node) {1551        return isTextNode(node) && !isInvisibleTextNode(node);1552    }1553    function isSkippableNode(node) {1554        return !isRenderedNode(node) || isShadowUIElement(node);1555    }1556    //dom utils1557    function hasContentEditableAttr(el) {1558        var attrValue = el.getAttribute ? el.getAttribute('contenteditable') : null;1559        return attrValue === '' || attrValue === 'true';1560    }1561    function findContentEditableParent(element) {1562        var elParents = getParents(element);1563        if (hasContentEditableAttr(element) && isContentEditableElement(element))1564            return element;1565        var currentDocument = findDocument(element);1566        if (currentDocument.designMode === 'on')1567            return currentDocument.body;1568        return find(elParents, function (parent) { return hasContentEditableAttr(parent) &&1569            isContentEditableElement(parent); });1570    }1571    function getNearestCommonAncestor(node1, node2) {1572        if (isTheSameNode(node1, node2)) {1573            if (isTheSameNode(node2, findContentEditableParent(node1)))1574                return node1;1575            return hammerhead.nativeMethods.nodeParentNodeGetter.call(node1);1576        }1577        var ancestors = [];1578        var contentEditableParent = findContentEditableParent(node1);1579        var curNode = null;1580        if (!isElementContainsNode(contentEditableParent, node2))1581            return null;1582        for (curNode = node1; curNode !== contentEditableParent; curNode = hammerhead.nativeMethods.nodeParentNodeGetter.call(curNode))1583            ancestors.push(curNode);1584        for (curNode = node2; curNode !== contentEditableParent; curNode = hammerhead.nativeMethods.nodeParentNodeGetter.call(curNode)) {1585            if (indexOf(ancestors, curNode) !== -1)1586                return curNode;1587        }1588        return contentEditableParent;1589    }1590    //selection utils1591    function getSelectedPositionInParentByOffset(node, offset) {1592        var currentNode = null;1593        var currentOffset = null;1594        var childCount = getChildNodesLength(node.childNodes);1595        var isSearchForLastChild = offset >= childCount;1596        // NOTE: we get a child element by its offset index in the parent1597        if (isShadowUIElement(node))1598            return { node: node, offset: offset };1599        // NOTE: IE behavior1600        if (isSearchForLastChild)1601            currentNode = node.childNodes[childCount - 1];1602        else {1603            currentNode = node.childNodes[offset];1604            currentOffset = 0;1605        }1606        // NOTE: skip shadowUI elements1607        if (isShadowUIElement(currentNode)) {1608            if (childCount <= 1)1609                return { node: node, offset: 0 };1610            isSearchForLastChild = offset - 1 >= childCount;1611            if (isSearchForLastChild)1612                currentNode = node.childNodes[childCount - 2];1613            else {1614                currentNode = node.childNodes[offset - 1];1615                currentOffset = 0;1616            }1617        }1618        // NOTE: we try to find text node1619        while (!isSkippableNode(currentNode) && isElementNode(currentNode)) {1620            var visibleChildren = getVisibleChildren(currentNode);1621            if (visibleChildren.length)1622                currentNode = visibleChildren[isSearchForLastChild ? visibleChildren.length - 1 : 0];1623            else {1624                //NOTE: if we didn't find a text node then always set offset to zero1625                currentOffset = 0;1626                break;1627            }1628        }1629        if (currentOffset !== 0 && !isSkippableNode(currentNode))1630            currentOffset = currentNode.nodeValue ? currentNode.nodeValue.length : 0;1631        return {1632            node: currentNode,1633            offset: currentOffset1634        };1635    }1636    function getSelectionStart(el, selection, inverseSelection) {1637        var startNode = inverseSelection ? selection.focusNode : selection.anchorNode;1638        var startOffset = inverseSelection ? selection.focusOffset : selection.anchorOffset;1639        var correctedStartPosition = {1640            node: startNode,1641            offset: startOffset1642        };1643        //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it1644        if ((isTheSameNode(el, startNode) || isElementNode(startNode)) && hasSelectableChildren(startNode))1645            correctedStartPosition = getSelectedPositionInParentByOffset(startNode, startOffset);1646        return {1647            node: correctedStartPosition.node,1648            offset: correctedStartPosition.offset1649        };1650    }1651    function getSelectionEnd(el, selection, inverseSelection) {1652        var endNode = inverseSelection ? selection.anchorNode : selection.focusNode;1653        var endOffset = inverseSelection ? selection.anchorOffset : selection.focusOffset;1654        var correctedEndPosition = {1655            node: endNode,1656            offset: endOffset1657        };1658        //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it1659        if ((isTheSameNode(el, endNode) || isElementNode(endNode)) && hasSelectableChildren(endNode))1660            correctedEndPosition = getSelectedPositionInParentByOffset(endNode, endOffset);1661        return {1662            node: correctedEndPosition.node,1663            offset: correctedEndPosition.offset1664        };1665    }1666    function getSelection(el, selection, inverseSelection) {1667        return {1668            startPos: getSelectionStart(el, selection, inverseSelection),1669            endPos: getSelectionEnd(el, selection, inverseSelection)1670        };1671    }1672    function getSelectionStartPosition(el, selection, inverseSelection) {1673        var correctedSelectionStart = getSelectionStart(el, selection, inverseSelection);1674        return calculatePositionByNodeAndOffset(el, correctedSelectionStart);1675    }1676    function getSelectionEndPosition(el, selection, inverseSelection) {1677        var correctedSelectionEnd = getSelectionEnd(el, selection, inverseSelection);1678        return calculatePositionByNodeAndOffset(el, correctedSelectionEnd);1679    }1680    function getElementOffset(target) {1681        var offset = 0;1682        var firstBreakElement = find(target.childNodes, function (node, index) {1683            offset = index;1684            return getTagName(node) === 'br';1685        });1686        return firstBreakElement ? offset : 0;1687    }1688    function isNodeSelectable(node, includeDescendants) {1689        if (isNotVisibleNode(node))1690            return false;1691        if (isTextNode(node))1692            return true;1693        if (!isElementNode(node))1694            return false;1695        if (hasSelectableChildren(node))1696            return includeDescendants;1697        var parent = hammerhead.nativeMethods.nodeParentNodeGetter.call(node);1698        var isContentEditableRoot = !isContentEditableElement(parent);1699        var visibleChildren = getVisibleChildren(node);1700        var hasBreakLineElements = some(visibleChildren, function (child) { return getTagName(child) === 'br'; });1701        return isContentEditableRoot || hasBreakLineElements;1702    }1703    function calculateNodeAndOffsetByPosition(el, offset) {1704        var point = {1705            node: null,1706            offset: offset1707        };1708        function checkChildNodes(target) {1709            var childNodes = target.childNodes;1710            var childNodesLength = getChildNodesLength(childNodes);1711            if (point.node)1712                return point;1713            if (isSkippableNode(target))1714                return point;1715            if (isTextNode(target)) {1716                if (point.offset <= target.nodeValue.length) {1717                    point.node = target;1718                    return point;1719                }1720                else if (target.nodeValue.length) {1721                    if (!point.node && isNodeAfterNodeBlockWithBreakLine(el, target))1722                        point.offset--;1723                    point.offset -= target.nodeValue.length;1724                }1725            }1726            else if (isElementNode(target)) {1727                if (!isVisibleNode(target))1728                    return point;1729                if (point.offset === 0 && isNodeSelectable(target, false)) {1730                    point.node = target;1731                    point.offset = getElementOffset(target);1732                    return point;1733                }1734                if (!point.node && (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target)))1735                    point.offset--;1736                else if (!childNodesLength && getTagName(target) === 'br')1737                    point.offset--;1738            }1739            for (var i = 0; i < childNodesLength; i++)1740                point = checkChildNodes(childNodes[i]);1741            return point;1742        }1743        return checkChildNodes(el);1744    }1745    function calculatePositionByNodeAndOffset(el, _a) {1746        var node = _a.node, offset = _a.offset;1747        var currentOffset = 0;1748        var find = false;1749        function checkChildNodes(target) {1750            var childNodes = target.childNodes;1751            var childNodesLength = getChildNodesLength(childNodes);1752            if (find)1753                return currentOffset;1754            if (isTheSameNode(node, target)) {1755                if (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target))1756                    currentOffset++;1757                find = true;1758                return currentOffset + offset;1759            }1760            if (isSkippableNode(target))1761                return currentOffset;1762            if (!childNodesLength && target.nodeValue && target.nodeValue.length) {1763                if (!find && isNodeAfterNodeBlockWithBreakLine(el, target))1764                    currentOffset++;1765                currentOffset += target.nodeValue.length;1766            }1767            else if (!childNodesLength && isElementNode(target) && getTagName(target) === 'br')1768                currentOffset++;1769            else if (!find && (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target)))1770                currentOffset++;1771            for (var i = 0; i < childNodesLength; i++)1772                currentOffset = checkChildNodes(childNodes[i]);1773            return currentOffset;1774        }1775        return checkChildNodes(el);1776    }1777    function getElementBySelection(selection) {1778        var el = getNearestCommonAncestor(selection.anchorNode, selection.focusNode);1779        return isTextNode(el) ? el.parentElement : el;1780    }1781    //NOTE: We can not determine first visible symbol of node in all cases,1782    // so we should create a range and select all text contents of the node.1783    // Then range object will contain information about node's the first and last visible symbol....content_editable_helper.js
Source:content_editable_helper.js  
...49    //NOTE: before such elements (like div or p) adds line breaks before and after it50    // (except line break before first visible element in contentEditable parent)51    // this line breaks is not contained in node values52    //so we should take it into account manually53    function isNodeBlockWithBreakLine(parent, node) {54        var parentFirstVisibleChild = null,55            firstVisibleChild = null;56        if (!Util.isTheSameNode(node, parent) && node.childNodes.length && /div|p/.test(node.tagName.toLowerCase())) {57            parentFirstVisibleChild = getOwnFirstVisibleNode(parent);58            if (!parentFirstVisibleChild || Util.isTheSameNode(node, parentFirstVisibleChild))59                return false;60            firstVisibleChild = exports.getFirstVisibleTextNode(parentFirstVisibleChild);61            if (!firstVisibleChild || Util.isTheSameNode(node, firstVisibleChild))62                return false;63            return getOwnFirstVisibleTextNode(node);64        }65        return false;66    }67    function isNodeAfterNodeBlockWithBreakLine(parent, node) {68        var isRenderedNode = Util.isRenderedNode(node),69            parentFirstVisibleChild = null,70            firstVisibleChild = null,71            previousSibling = null;72        if (!Util.isTheSameNode(node, parent) &&73            ((isRenderedNode && node.nodeType === 1 && node.childNodes.length && !/div|p/.test(node.tagName.toLowerCase())) ||74             (node.nodeType === 3 && !Util.isTheSameNode(node, parent) && node.nodeValue.length && !exports.isInvisibleTextNode(node)))) {75            if (isRenderedNode && node.nodeType === 1) {76                parentFirstVisibleChild = getOwnFirstVisibleNode(parent);77                if (!parentFirstVisibleChild || Util.isTheSameNode(node, parentFirstVisibleChild))78                    return false;79                firstVisibleChild = exports.getFirstVisibleTextNode(parentFirstVisibleChild);80                if (!firstVisibleChild || Util.isTheSameNode(node, firstVisibleChild))81                    return false;82            }83            previousSibling = getOwnPreviousVisibleSibling(node);84            return (previousSibling && previousSibling.nodeType === 1 &&85                    /div|p/.test(previousSibling.tagName.toLowerCase()) && getOwnFirstVisibleTextNode(previousSibling));86        }87        return false;88    }89    exports.getFirstVisibleTextNode = function (el) {90        var childrenArray = $.makeArray(el.childNodes),91            element = null;92        if (!childrenArray.length && el.nodeType === 3 && !exports.isInvisibleTextNode(el))93            return el;94        $.each(childrenArray, function (index, value) {95            if (value.nodeType === 3 && !exports.isInvisibleTextNode(value)) {96                element = value;97                return false;98            }99            else if (Util.isRenderedNode(value) && (value.nodeType === 1 || (value.childNodes && value.childNodes.length))) {100                element = exports.getFirstVisibleTextNode(value);101                if (element)102                    return false;103            }104        });105        return element;106    };107    exports.getLastVisibleTextNode = function (el, onlyVisible) {108        var childrenArray = $.makeArray(el.childNodes),109            element = null;110        if (!childrenArray.length && el.nodeType === 3 && !exports.isInvisibleTextNode(el))111            return el;112        if (childrenArray.length)113            childrenArray = childrenArray.reverse();114        $.each(childrenArray, function (index, value) {115            if (value.nodeType === 3 && (onlyVisible ? !exports.isInvisibleTextNode(value) : true)) {116                element = value;117                return false;118            }119            else if (Util.isRenderedNode(value) && (value.nodeType === 1 || (value.childNodes && value.childNodes.length))) {120                element = exports.getLastVisibleTextNode(value, false);121                if (element)122                    return false;123            }124        });125        return element;126    };127    exports.getFirstNonWhitespaceSymbolIndex = function (nodeValue, startFrom) {128        if (!nodeValue || !nodeValue.length)129            return 0;130        var valueLength = nodeValue.length,131            index = startFrom || 0;132        for (var i = index; i < valueLength; i++) {133            if (nodeValue.charCodeAt(i) === 10 || nodeValue.charCodeAt(i) === 32)134                index++;135            else136                break;137        }138        return index;139    };140    exports.getLastNonWhitespaceSymbolIndex = function (nodeValue) {141        if (!nodeValue || !nodeValue.length)142            return 0;143        var valueLength = nodeValue.length,144            index = valueLength;145        for (var i = valueLength - 1; i >= 0; i--) {146            if (nodeValue.charCodeAt(i) === 10 || nodeValue.charCodeAt(i) === 32)147                index--;148            else149                break;150        }151        return index;152    };153    exports.isInvisibleTextNode = function (node) {154        if (node.nodeType !== 3)155            return false;156        var nodeValue = node.nodeValue,157            firstVisibleIndex = exports.getFirstNonWhitespaceSymbolIndex(nodeValue),158            lastVisibleIndex = exports.getLastNonWhitespaceSymbolIndex(nodeValue);159        return firstVisibleIndex === nodeValue.length && lastVisibleIndex === 0;160    };161    //dom utils162    exports.findContentEditableParent = function (el) {163        var $elParents = $(el).parents(),164            currentDocument = null,165            parent = null;166        function hasContentEditableAttr(el) {167            return typeof $(el).attr('contenteditable') !== 'undefined' && $(el).attr('contenteditable') !== 'false' && $(el).attr('contenteditable') !== 'inherit';168        }169        if (hasContentEditableAttr(el) && Util.isContentEditableElement(el))170            return el;171        currentDocument = Util.findDocument(el);172        if (currentDocument.designMode === 'on')173            return currentDocument.body;174        $.each($elParents, function (index, item) {175            if (hasContentEditableAttr(item) && Util.isContentEditableElement(item)) {176                parent = item;177                return false;178            }179        });180        return parent;181    };182    exports.getNearestCommonAncestor = function (node1, node2) {183        if (Util.isTheSameNode(node1, node2)) {184            if (Util.isTheSameNode(node2, exports.findContentEditableParent(node1)))185                return node1;186            return node1.parentNode;187        }188        var ancestors = [],189            contentEditableParent = exports.findContentEditableParent(node1),190            curNode = null;191        if (!Util.isElementContainsNode(contentEditableParent, node2))192            return null;193        for (curNode = node1; curNode !== contentEditableParent; curNode = curNode.parentNode)194            ancestors.push(curNode);195        for (curNode = node2; curNode !== contentEditableParent; curNode = curNode.parentNode) {196            if ($.inArray(curNode, ancestors) !== -1)197                return curNode;198        }199        return contentEditableParent;200    };201    //selection utils202    function getSelectedPositionInParentByOffset(node, offset) {203        var currentNode = null,204            currentOffset = null,205            isSearchForLastChild = offset >= node.childNodes.length;206        //NOTE: IE behavior207        if (isSearchForLastChild)208            currentNode = node.childNodes[node.childNodes.length - 1];209        else {210            currentNode = node.childNodes[offset];211            currentOffset = 0;212        }213        while (Util.isRenderedNode(currentNode) && currentNode.nodeType === 1) {214            if (currentNode.childNodes && currentNode.childNodes.length)215                currentNode = currentNode.childNodes[isSearchForLastChild ? currentNode.childNodes.length - 1 : 0];216            else {217                //NOTE: if we didn't find a text node then always set offset to zero218                currentOffset = 0;219                break;220            }221        }222        if (currentOffset !== 0 && Util.isRenderedNode(currentNode))223            currentOffset = currentNode.nodeValue ? currentNode.nodeValue.length : 0;224        return {225            node: currentNode,226            offset: currentOffset227        };228    }229    function getSelectionStart(el, selection, inverseSelection) {230        var startNode = inverseSelection ? selection.focusNode : selection.anchorNode,231            startOffset = inverseSelection ? selection.focusOffset : selection.anchorOffset,232            correctedStartPosition = {233                node: startNode,234                offset: startOffset235            };236        //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it237        if ((Util.isTheSameNode(el, startNode) || startNode.nodeType === 1) && startNode.childNodes && startNode.childNodes.length)238            correctedStartPosition = getSelectedPositionInParentByOffset(startNode, startOffset);239        return {240            node: correctedStartPosition.node,241            offset: correctedStartPosition.offset242        };243    }244    function getSelectionEnd(el, selection, inverseSelection) {245        var endNode = inverseSelection ? selection.anchorNode : selection.focusNode,246            endOffset = inverseSelection ? selection.anchorOffset : selection.focusOffset,247            correctedEndPosition = {248                node: endNode,249                offset: endOffset250            };251        //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it252        if ((Util.isTheSameNode(el, endNode) || endNode.nodeType === 1) && endNode.childNodes && endNode.childNodes.length)253            correctedEndPosition = getSelectedPositionInParentByOffset(endNode, endOffset);254        return {255            node: correctedEndPosition.node,256            offset: correctedEndPosition.offset257        };258    }259    exports.getSelection = function (el, selection, inverseSelection) {260        var correctedStart = getSelectionStart(el, selection, inverseSelection),261            correctedEnd = getSelectionEnd(el, selection, inverseSelection);262        return {263            startNode: correctedStart.node,264            startOffset: correctedStart.offset,265            endNode: correctedEnd.node,266            endOffset: correctedEnd.offset267        };268    };269    exports.getSelectionStartPosition = function (el, selection, inverseSelection) {270        var correctedSelectionStart = getSelectionStart(el, selection, inverseSelection);271        return exports.calculatePositionByNodeAndOffset(el, correctedSelectionStart.node, correctedSelectionStart.offset);272    };273    exports.getSelectionEndPosition = function (el, selection, inverseSelection) {274        var correctedSelectionEnd = getSelectionEnd(el, selection, inverseSelection);275        return exports.calculatePositionByNodeAndOffset(el, correctedSelectionEnd.node, correctedSelectionEnd.offset);276    };277    exports.calculateNodeAndOffsetByPosition = function (el, offset) {278        var point = {279            offset: offset,280            node: null281        };282        function checkChildNodes(target) {283            var childNodes = target.childNodes;284            if (point.node)285                return point;286            if (!Util.isRenderedNode(target))287                return point;288            if (target.nodeType === 3) {289                if (point.offset <= target.nodeValue.length) {290                    point.node = target;291                    return point;292                }293                else if (target.nodeValue.length) {294                    if (!point.node && isNodeAfterNodeBlockWithBreakLine(el, target))295                        point.offset--;296                    point.offset -= target.nodeValue.length;297                }298            }299            else if (target.nodeType === 1) {300                if (point.offset === 0 && !exports.getContentEditableValue(target).length) {301                    point.node = target;302                    return point;303                }304                if (!point.node && (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target)))305                    point.offset--;306                else if (!childNodes.length && target.nodeType === 1 && target.tagName.toLowerCase() === 'br')307                    point.offset--;308            }309            $.each(childNodes, function (index, value) {310                point = checkChildNodes(value);311            });312            return point;313        }314        return checkChildNodes(el);315    };316    exports.calculatePositionByNodeAndOffset = function (el, node, offset) {317        var currentOffset = 0,318            find = false;319        function checkChildNodes(target) {320            var childNodes = target.childNodes;321            if (find)322                return currentOffset;323            if (Util.isTheSameNode(node, target)) {324                if (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target))325                    currentOffset++;326                find = true;327                return currentOffset + offset;328            }329            if (!Util.isRenderedNode(target))330                return currentOffset;331            if (!childNodes.length && target.nodeValue && target.nodeValue.length) {332                if (!find && isNodeAfterNodeBlockWithBreakLine(el, target))333                    currentOffset++;334                currentOffset += target.nodeValue.length;335            }336            else if (!childNodes.length && target.nodeType === 1 && target.tagName.toLowerCase() === 'br')337                currentOffset++;338            else if (!find && (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target)))339                currentOffset++;340            $.each(childNodes, function (index, value) {341                currentOffset = checkChildNodes(value);342            });343            return currentOffset;344        }345        return checkChildNodes(el);346    };347    exports.getElementBySelection = function (selection) {348        var el = exports.getNearestCommonAncestor(selection.anchorNode, selection.focusNode);349        return Util.isTextNode(el) ? el.parentElement : el;350    };351    //NOTE: We can not determine first visible symbol of node in all cases,352    // so we should create a range and select all text contents of the node....content-editable.js
Source:content-editable.js  
...310            if (point.offset === 0 && !getContentEditableValue(target).length) {311                point.node = target;312                return point;313            }314            if (!point.node && (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target)))315                point.offset--;316            else if (!childNodes.length && domUtils.isElementNode(target) && domUtils.getTagName(target) === 'br')317                point.offset--;318        }319        arrayUtils.forEach(childNodes, node => {320            point = checkChildNodes(node);321        });322        return point;323    }324    return checkChildNodes(el);325}326export function calculatePositionByNodeAndOffset (el, { node, offset }) {327    var currentOffset = 0;328    var find          = false;329    function checkChildNodes (target) {330        var childNodes = target.childNodes;331        if (find)332            return currentOffset;333        if (domUtils.isTheSameNode(node, target)) {334            if (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target))335                currentOffset++;336            find = true;337            return currentOffset + offset;338        }339        if (isSkippableNode(target))340            return currentOffset;341        if (!childNodes.length && target.nodeValue && target.nodeValue.length) {342            if (!find && isNodeAfterNodeBlockWithBreakLine(el, target))343                currentOffset++;344            currentOffset += target.nodeValue.length;345        }346        else if (!childNodes.length && domUtils.isElementNode(target) && domUtils.getTagName(target) === 'br')347            currentOffset++;348        else if (!find && (isNodeBlockWithBreakLine(el, target) || isNodeAfterNodeBlockWithBreakLine(el, target)))349            currentOffset++;350        arrayUtils.forEach(childNodes, currentNode => {351            currentOffset = checkChildNodes(currentNode);352        });353        return currentOffset;354    }355    return checkChildNodes(el);356}357export function getElementBySelection (selection) {358    var el = getNearestCommonAncestor(selection.anchorNode, selection.focusNode);359    return domUtils.isTextNode(el) ? el.parentElement : el;360}361//NOTE: We can not determine first visible symbol of node in all cases,362// so we should create a range and select all text contents of the node....Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3        .typeText('#developer-name', 'John Smith')4        .click('#submit-button');5});6import { Selector } from 'testcafe';7import { ClientFunction } from 'testcafe';8test('My first test', async t => {9    const getLocation = ClientFunction(() =>Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3        .typeText('#developer-name', 'John Smith')4        .click('#submit-button');5    const articleHeader = await Selector('.result-content').find('h1');6    let headerText = await articleHeader.innerText;7    let id = await articleHeader.id;8    let classAttr = await articleHeader.class;9});10import { Selector } from 'testcafe';11test('My first test', async t => {12        .typeText('#developer-name', 'John Smith')13        .click('#submit-button');14    const articleHeader = await Selector('.result-content').find('h1');15    let headerText = await articleHeader.innerText;16    let id = await articleHeader.id;17    let classAttr = await articleHeader.class;18});19import { Selector } from 'testcafe';20test('My first test', async t => {21        .typeText('#developer-name', 'John Smith')22        .click('#submit-button');23    const articleHeader = await Selector('.result-content').find('h1');24    let headerText = await articleHeader.innerText;Using AI Code Generation
1import { Selector } from 'testcafe';2test('My test', async t => {3        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(0))4        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(1))5        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(2))6        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(3))7        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(4))8        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(5))9        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(6))10        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(7))11        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(8))12        .click(Selector('#tried-test-cafe').find('label').withText('I have tried TestCafe').parent().find('input').withAttribute('type', 'radio').nth(9))13        .click(Selector('#tried-test-cafe').find('Using AI Code Generation
1import { Selector, t } from 'testcafe';2test('My first test', async t => {3});4import { Selector, t } from 'testcafe';5test('My first test', async t => {6});Using AI Code Generation
1import { Selector, t } from 'testcafe';2test('My Test', async t => {3    const developerName = Selector('#developer-name');4        .typeText(developerName, 'Peter')5        .expect(developerName.value).eql('Peter');6});7import { Selector, t } from 'testcafe';8test('My Test', async t => {9    const developerName = Selector('#developer-name');10        .typeText(developerName, 'Peter')11        .expect(developerName.value).eql('Peter');12});13import { Selector, t } from 'testcafe';14test('My Test', async t => {15    const developerName = Selector('#developer-name');16        .typeText(developerName, 'Peter')17        .expect(developerName.value).eql('Peter');18});19import { Selector, t } from 'testcafe';20test('My Test', async t => {21    const developerName = Selector('#developer-name');22        .typeText(developerName, 'Peter')23        .expect(developerName.value).eql('Peter');24});25import { Selector, t } from 'testcafe';26test('My Test', async t => {27    const developerName = Selector('#developer-name');28        .typeText(developerName,Using AI Code Generation
1import { Selector } from 'testcafe';2test('My Test', async t => {3    await t.expect(Selector('body').isNodeBlockWithBreakLine).ok();4});5import { Selector } from 'testcafe';6test('My Test', async t => {7    await t.expect(Selector('body').isNodeBlockWithBreakLine).ok();8});9import { Selector } from 'testcafe';10test('My Test', async t => {11    await t.expect(Selector('body').isNodeBlockWithBreakLine).ok();12});13import { Selector } from 'testcafe';14test('My Test', async t => {15    await t.expect(Selector('body').isNodeBlockWithBreakLine).ok();16});17import { Selector } from 'testcafe';18test('My Test', async t => {19    await t.expect(Selector('body').isNodeBlockWithBreakUsing AI Code Generation
1import { Selector } from 'testcafe';2test('My Test', async t => {3        .click(Selector('.someClass'))4        .expect(Selector('h1').innerText).eql('My Page Title');5});6import { Selector } from 'testcafe';7test('My Test', async t => {8        .click(Selector('.someClass'))9        .expect(Selector('h1').innerText).eql('My Page Title');10});11function getPromise() {12    return new Promise((resolve, reject) => {13        setTimeout(() => {14            resolve('success');15        }, 1000);16    });17}18test('getPromise', async t => {19    const result = await getPromise();20    await t.expect(result).eql('success');21});22        at Selector._getRelatedNodeSnapshot (C:\Users\user\Documents\testcafe\node_modules\testcafe\lib\client-functions\selector-builder\add-api.js:466:15)23        at Selector._getNodeSnapshot (C:\Users\user\Documents\testcafe\node_modules\testcafe\lib\client-functions\selector-builder\add-api.js:481:23)Using AI Code Generation
1test('Test', async t => {2    const nodeBlock = await Selector('div').withText('Hello, world!');3    const hasBreakLine = await t.isNodeBlockWithBreakLine(nodeBlock);4    console.log(hasBreakLine);5});6test('Test', async t => {7    const nodeBlock = await Selector('div').withText('Hello, world!');8    const hasBreakLine = await t.isNodeBlockWithBreakLine(nodeBlock, { skipWhitespace: true });9    console.log(hasBreakLine);10});11test('Test', async t => {12    const nodeBlock = await Selector('div').withText('Hello, world!');13    const hasBreakLine = await t.isNodeBlockWithBreakLine(nodeBlock, { skipWhitespace: true, skipComments: true });14    console.log(hasBreakLine);15});16test('Test', async t => {17    const nodeBlock = await Selector('div').withText('Hello, world!');18    const hasBreakLine = await t.isNodeBlockWithBreakLine(nodeBlock, { skipWhitespace: true, skipComments: true, skipText: true });19    console.log(hasBreakLine);20});21test('Test', async t => {22    const nodeBlock = await Selector('div').withText('Hello, world!');23    const hasBreakLine = await t.isNodeBlockWithBreakLine(nodeBlock, { skipWhitespace: true, skipComments: true, skipText: true, skipTextNodes: true });24    console.log(hasBreakLine);25});26test('Test', async t => {27    const nodeBlock = await Selector('div').withText('Hello, world!');28    const hasBreakLine = await t.isNodeBlockWithBreakLine(nodeBlock, { skipWhitespace: true, skipComments: true, skipText: true, skipTextNodes: true, skipEmptyTextNodes: true });29    console.log(hasBreakLine);30});31test('Test', async t => {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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
