Best JavaScript code snippet using testcafe
index.js
Source:index.js  
...1389        return find(children, function (node) { return isVisibleTextNode(node); });1390    }1391    function getOwnFirstVisibleNode(el) {1392        return find(el.childNodes, function (node) { return isVisibleTextNode(node) ||1393            !isSkippableNode(node) && getOwnFirstVisibleNode(node); });1394    }1395    function getOwnPreviousVisibleSibling(el) {1396        var sibling = null;1397        var current = el;1398        while (!sibling) {1399            current = current.previousSibling;1400            if (!current)1401                break;1402            else if (!isSkippableNode(current) && !isInvisibleTextNode(current)) {1403                sibling = current;1404                break;1405            }1406        }1407        return sibling;1408    }1409    function isVisibleNode(node) {1410        return isTextNode(node) || isElementNode(node) && isElementVisible(node);1411    }1412    function getVisibleChildren(node) {1413        return filter(node.childNodes, isVisibleNode);1414    }1415    function hasVisibleChildren(node) {1416        return some(node.childNodes, isVisibleNode);1417    }1418    function hasSelectableChildren(node) {1419        return some(node.childNodes, function (child) { return isNodeSelectable(child, true); });1420    }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.1784    function getFirstVisiblePosition(el) {1785        var firstVisibleTextChild = isTextNode(el) ? el : getFirstVisibleTextNode(el);1786        var curDocument = findDocument(el);1787        var range = curDocument.createRange();1788        if (firstVisibleTextChild) {1789            range.selectNodeContents(firstVisibleTextChild);1790            return calculatePositionByNodeAndOffset(el, { node: firstVisibleTextChild, offset: range.startOffset });1791        }1792        return 0;1793    }1794    function getLastVisiblePosition(el) {1795        var lastVisibleTextChild = isTextNode(el) ? el : getLastTextNode(el, true);1796        if (!lastVisibleTextChild || isResetAnchorOffsetRequired(lastVisibleTextChild, el))1797            return 0;1798        var curDocument = findDocument(el);1799        var range = curDocument.createRange();1800        range.selectNodeContents(lastVisibleTextChild);1801        return calculatePositionByNodeAndOffset(el, { node: lastVisibleTextChild, offset: range.endOffset });1802    }1803    function isResetAnchorOffsetRequired(lastVisibleTextChild, el) {1804        var firstVisibleTextChild = isTextNode(el) ? el : getFirstTextNode(el, false);1805        var isSingleTextNode = lastVisibleTextChild === firstVisibleTextChild;1806        var isNewLineChar = lastVisibleTextChild.nodeValue === String.fromCharCode(10);1807        return isSingleTextNode && isNewLineChar && hasWhiteSpacePreStyle(lastVisibleTextChild, el);1808    }1809    function hasWhiteSpacePreStyle(el, container) {1810        var whiteSpacePreStyles = ['pre', 'pre-wrap', 'pre-line'];1811        while (el !== container) {1812            el = hammerhead.nativeMethods.nodeParentNodeGetter.call(el);1813            if (indexOf(whiteSpacePreStyles, get(el, 'white-space')) > -1)1814                return true;1815        }1816        return false;1817    }1818    function getContentEditableNodes(target) {1819        var result = [];1820        var childNodes = target.childNodes;1821        var childNodesLength = getChildNodesLength(childNodes);1822        if (!isSkippableNode(target) && !childNodesLength && isTextNode(target))1823            result.push(target);1824        for (var i = 0; i < childNodesLength; i++)1825            result = result.concat(getContentEditableNodes(childNodes[i]));1826        return result;1827    }1828    // contents util1829    function getContentEditableValue(target) {1830        return map(getContentEditableNodes(target), function (node) { return node.nodeValue; }).join('');1831    }1832    var contentEditable = /*#__PURE__*/Object.freeze({1833        __proto__: null,1834        getFirstVisibleTextNode: getFirstVisibleTextNode,1835        getLastTextNode: getLastTextNode,1836        getFirstNonWhitespaceSymbolIndex: getFirstNonWhitespaceSymbolIndex,...content-editable.js
Source:content-editable.js  
...8    return arrayUtils.find(children, node => isVisibleTextNode(node));9}10function getOwnFirstVisibleNode (el) {11    return arrayUtils.find(el.childNodes, node => isVisibleTextNode(node) ||12                                                  !isSkippableNode(node) && getOwnFirstVisibleNode(node));13}14function getOwnPreviousVisibleSibling (el) {15    var sibling = null;16    var current = el;17    while (!sibling) {18        current = current.previousSibling;19        if (!current)20            break;21        else if (!isSkippableNode(current) && !isInvisibleTextNode(current)) {22            sibling = current;23            break;24        }25    }26    return sibling;27}28function hasChildren (node) {29    return node.childNodes && node.childNodes.length;30}31function isElementWithChildren (node) {32    return domUtils.isElementNode(node) || hasChildren(node);33}34//NOTE: before such elements (like div or p) adds line breaks before and after it35// (except line break before first visible element in contentEditable parent)36// this line breaks is not contained in node values37//so we should take it into account manually38function isNodeBlockWithBreakLine (parent, node) {39    var parentFirstVisibleChild = null;40    var firstVisibleChild       = null;41    if (domUtils.isShadowUIElement(parent) || domUtils.isShadowUIElement(node))42        return false;43    if (!domUtils.isTheSameNode(node, parent) && node.childNodes.length && /div|p/.test(domUtils.getTagName(node))) {44        parentFirstVisibleChild = getOwnFirstVisibleNode(parent);45        if (!parentFirstVisibleChild || domUtils.isTheSameNode(node, parentFirstVisibleChild))46            return false;47        firstVisibleChild = getFirstVisibleTextNode(parentFirstVisibleChild);48        if (!firstVisibleChild || domUtils.isTheSameNode(node, firstVisibleChild))49            return false;50        return getOwnFirstVisibleTextNode(node);51    }52    return false;53}54function isNodeAfterNodeBlockWithBreakLine (parent, node) {55    var isRenderedNode          = domUtils.isRenderedNode(node);56    var parentFirstVisibleChild = null;57    var firstVisibleChild       = null;58    var previousSibling         = null;59    if (domUtils.isShadowUIElement(parent) || domUtils.isShadowUIElement(node))60        return false;61    if (!domUtils.isTheSameNode(node, parent) &&62        (isRenderedNode && domUtils.isElementNode(node) && node.childNodes.length &&63         !/div|p/.test(domUtils.getTagName(node)) ||64         isVisibleTextNode(node) && !domUtils.isTheSameNode(node, parent) && node.nodeValue.length)) {65        if (isRenderedNode && domUtils.isElementNode(node)) {66            parentFirstVisibleChild = getOwnFirstVisibleNode(parent);67            if (!parentFirstVisibleChild || domUtils.isTheSameNode(node, parentFirstVisibleChild))68                return false;69            firstVisibleChild = getFirstVisibleTextNode(parentFirstVisibleChild);70            if (!firstVisibleChild || domUtils.isTheSameNode(node, firstVisibleChild))71                return false;72        }73        previousSibling = getOwnPreviousVisibleSibling(node);74        return previousSibling && domUtils.isElementNode(previousSibling) &&75               /div|p/.test(domUtils.getTagName(previousSibling)) && getOwnFirstVisibleTextNode(previousSibling);76    }77    return false;78}79export function getFirstVisibleTextNode (el) {80    var children                    = el.childNodes;81    var childrenLength              = children.length;82    var curNode                     = null;83    var child                       = null;84    var isNotContentEditableElement = null;85    if (!childrenLength && isVisibleTextNode(el))86        return el;87    for (var i = 0; i < childrenLength; i++) {88        curNode                     = children[i];89        isNotContentEditableElement = domUtils.isElementNode(curNode) && !domUtils.isContentEditableElement(curNode);90        if (isVisibleTextNode(curNode))91            return curNode;92        else if (domUtils.isRenderedNode(curNode) && isElementWithChildren(curNode) && !isNotContentEditableElement) {93            child = getFirstVisibleTextNode(curNode);94            if (child)95                return child;96        }97    }98    return child;99}100export function getLastTextNode (el, onlyVisible) {101    var children                    = el.childNodes;102    var childrenLength              = children.length;103    var curNode                     = null;104    var child                       = null;105    var isNotContentEditableElement = null;106    var visibleTextNode             = null;107    if (!childrenLength && isVisibleTextNode(el))108        return el;109    for (var i = childrenLength - 1; i >= 0; i--) {110        curNode                     = children[i];111        isNotContentEditableElement = domUtils.isElementNode(curNode) && !domUtils.isContentEditableElement(curNode);112        visibleTextNode             = domUtils.isTextNode(curNode) &&113                                      (onlyVisible ? !isInvisibleTextNode(curNode) : true);114        if (visibleTextNode)115            return curNode;116        else if (domUtils.isRenderedNode(curNode) && isElementWithChildren(curNode) && !isNotContentEditableElement) {117            child = getLastTextNode(curNode, false);118            if (child)119                return child;120        }121    }122    return child;123}124export function getFirstNonWhitespaceSymbolIndex (nodeValue, startFrom) {125    if (!nodeValue || !nodeValue.length)126        return 0;127    var valueLength = nodeValue.length;128    var index       = startFrom || 0;129    for (var i = index; i < valueLength; i++) {130        if (nodeValue.charCodeAt(i) === 10 || nodeValue.charCodeAt(i) === 32)131            index++;132        else133            break;134    }135    return index;136}137export function getLastNonWhitespaceSymbolIndex (nodeValue) {138    if (!nodeValue || !nodeValue.length)139        return 0;140    var valueLength = nodeValue.length;141    var index       = valueLength;142    for (var i = valueLength - 1; i >= 0; i--) {143        if (nodeValue.charCodeAt(i) === 10 || nodeValue.charCodeAt(i) === 32)144            index--;145        else146            break;147    }148    return index;149}150export function isInvisibleTextNode (node) {151    if (!domUtils.isTextNode(node))152        return false;153    var nodeValue         = node.nodeValue;154    var firstVisibleIndex = getFirstNonWhitespaceSymbolIndex(nodeValue);155    var lastVisibleIndex  = getLastNonWhitespaceSymbolIndex(nodeValue);156    return firstVisibleIndex === nodeValue.length && lastVisibleIndex === 0;157}158function isVisibleTextNode (node) {159    return domUtils.isTextNode(node) && !isInvisibleTextNode(node);160}161function isSkippableNode (node) {162    return !domUtils.isRenderedNode(node) || domUtils.isShadowUIElement(node);163}164//dom utils165function hasContentEditableAttr (el) {166    var attrValue = el.getAttribute ? el.getAttribute('contenteditable') : null;167    return attrValue === '' || attrValue === 'true';168}169export function findContentEditableParent (element) {170    var elParents = domUtils.getParents(element);171    if (hasContentEditableAttr(element) && domUtils.isContentEditableElement(element))172        return element;173    var currentDocument = domUtils.findDocument(element);174    if (currentDocument.designMode === 'on')175        return currentDocument.body;176    return arrayUtils.find(elParents, parent => hasContentEditableAttr(parent) &&177                                                domUtils.isContentEditableElement(parent));178}179export function getNearestCommonAncestor (node1, node2) {180    if (domUtils.isTheSameNode(node1, node2)) {181        if (domUtils.isTheSameNode(node2, findContentEditableParent(node1)))182            return node1;183        return node1.parentNode;184    }185    var ancestors             = [];186    var contentEditableParent = findContentEditableParent(node1);187    var curNode               = null;188    if (!domUtils.isElementContainsNode(contentEditableParent, node2))189        return null;190    for (curNode = node1; curNode !== contentEditableParent; curNode = curNode.parentNode)191        ancestors.push(curNode);192    for (curNode = node2; curNode !== contentEditableParent; curNode = curNode.parentNode) {193        if (arrayUtils.indexOf(ancestors, curNode) !== -1)194            return curNode;195    }196    return contentEditableParent;197}198//selection utils199function getSelectedPositionInParentByOffset (node, offset) {200    var currentNode          = null;201    var currentOffset        = null;202    var childCount           = node.childNodes.length;203    var isSearchForLastChild = offset >= childCount;204    // NOTE: we get a child element by its offset index in the parent205    if (domUtils.isShadowUIElement(node))206        return { node, offset };207    // NOTE: IE behavior208    if (isSearchForLastChild)209        currentNode = node.childNodes[childCount - 1];210    else {211        currentNode   = node.childNodes[offset];212        currentOffset = 0;213    }214    // NOTE: skip shadowUI elements215    if (domUtils.isShadowUIElement(currentNode)) {216        if (childCount <= 1)217            return { node, offset: 0 };218        isSearchForLastChild = offset - 1 >= childCount;219        if (isSearchForLastChild)220            currentNode = node.childNodes[childCount - 2];221        else {222            currentNode   = node.childNodes[offset - 1];223            currentOffset = 0;224        }225    }226    // NOTE: we try to find text node227    while (!isSkippableNode(currentNode) && domUtils.isElementNode(currentNode)) {228        if (hasChildren(currentNode))229            currentNode = currentNode.childNodes[isSearchForLastChild ? currentNode.childNodes.length - 1 : 0];230        else {231            //NOTE: if we didn't find a text node then always set offset to zero232            currentOffset = 0;233            break;234        }235    }236    if (currentOffset !== 0 && !isSkippableNode(currentNode))237        currentOffset = currentNode.nodeValue ? currentNode.nodeValue.length : 0;238    return {239        node:   currentNode,240        offset: currentOffset241    };242}243function getSelectionStart (el, selection, inverseSelection) {244    var startNode   = inverseSelection ? selection.focusNode : selection.anchorNode;245    var startOffset = inverseSelection ? selection.focusOffset : selection.anchorOffset;246    var correctedStartPosition = {247        node:   startNode,248        offset: startOffset249    };250    //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it251    if ((domUtils.isTheSameNode(el, startNode) || domUtils.isElementNode(startNode)) && hasChildren(startNode))252        correctedStartPosition = getSelectedPositionInParentByOffset(startNode, startOffset);253    return {254        node:   correctedStartPosition.node,255        offset: correctedStartPosition.offset256    };257}258function getSelectionEnd (el, selection, inverseSelection) {259    var endNode   = inverseSelection ? selection.anchorNode : selection.focusNode;260    var endOffset = inverseSelection ? selection.anchorOffset : selection.focusOffset;261    var correctedEndPosition = {262        node:   endNode,263        offset: endOffset264    };265    //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it266    if ((domUtils.isTheSameNode(el, endNode) || domUtils.isElementNode(endNode)) && hasChildren(endNode))267        correctedEndPosition = getSelectedPositionInParentByOffset(endNode, endOffset);268    return {269        node:   correctedEndPosition.node,270        offset: correctedEndPosition.offset271    };272}273export function getSelection (el, selection, inverseSelection) {274    return {275        startPos: getSelectionStart(el, selection, inverseSelection),276        endPos:   getSelectionEnd(el, selection, inverseSelection)277    };278}279export function getSelectionStartPosition (el, selection, inverseSelection) {280    var correctedSelectionStart = getSelectionStart(el, selection, inverseSelection);281    return calculatePositionByNodeAndOffset(el, correctedSelectionStart);282}283export function getSelectionEndPosition (el, selection, inverseSelection) {284    var correctedSelectionEnd = getSelectionEnd(el, selection, inverseSelection);285    return calculatePositionByNodeAndOffset(el, correctedSelectionEnd);286}287export function calculateNodeAndOffsetByPosition (el, offset) {288    var point = {289        node:   null,290        offset: offset291    };292    function checkChildNodes (target) {293        var childNodes = target.childNodes;294        if (point.node)295            return point;296        if (isSkippableNode(target))297            return point;298        if (domUtils.isTextNode(target)) {299            if (point.offset <= target.nodeValue.length) {300                point.node = target;301                return point;302            }303            else if (target.nodeValue.length) {304                if (!point.node && isNodeAfterNodeBlockWithBreakLine(el, target))305                    point.offset--;306                point.offset -= target.nodeValue.length;307            }308        }309        else if (domUtils.isElementNode(target)) {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.363// Then range object will contain information about node's the first and last visible symbol.364export function getFirstVisiblePosition (el) {365    var firstVisibleTextChild = domUtils.isTextNode(el) ? el : getFirstVisibleTextNode(el);366    var curDocument           = domUtils.findDocument(el);367    var range                 = curDocument.createRange();368    if (firstVisibleTextChild) {369        range.selectNodeContents(firstVisibleTextChild);370        return calculatePositionByNodeAndOffset(el, { node: firstVisibleTextChild, offset: range.startOffset });371    }372    return 0;373}374export function getLastVisiblePosition (el) {375    var lastVisibleTextChild = domUtils.isTextNode(el) ? el : getLastTextNode(el, true);376    var curDocument          = domUtils.findDocument(el);377    var range                = curDocument.createRange();378    if (lastVisibleTextChild) {379        range.selectNodeContents(lastVisibleTextChild);380        return calculatePositionByNodeAndOffset(el, { node: lastVisibleTextChild, offset: range.endOffset });381    }382    return 0;383}384//contents util385export function getContentEditableValue (target) {386    var elementValue = '';387    var childNodes   = target.childNodes;388    if (isSkippableNode(target))389        return elementValue;390    if (!childNodes.length && domUtils.isTextNode(target))391        return target.nodeValue;392    else if (childNodes.length === 1 && domUtils.isTextNode(childNodes[0]))393        return childNodes[0].nodeValue;394    arrayUtils.forEach(childNodes, node => {395        elementValue += getContentEditableValue(node);396    });397    return elementValue;...Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3    const skipButton = Selector('.skip-to-content');4        .click(skipButton)5        .expect(Selector('#article-header').innerText).eql('Article header');6});Using AI Code Generation
1import { Selector } from 'testcafe';2test('My Test', async t => {3        .click(Selector('label').withText('I have tried TestCafe'))4        .click('#tried-test-cafe')5        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input'))6        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(0))7        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(1))8        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(2))9        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(3))10        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(4))11        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(5))12        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(6))13        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(7))14        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(8))15        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(9))16        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(10))17        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(11))18        .click(Selector('label').withText('I have tried TestCafe').parent('span').child('input').nth(12))19        .click(Selector('label').withText('Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3        .click('#populate')4        .click('#submit-button')5        .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');6});7const puppeteer = require('puppeteer');8let browser;9let page;10before(async function () {11    browser = await puppeteer.launch({12    });13    page = await browser.newPage();14    await page.setDefaultTimeout(10000);15    await page.setDefaultNavigationTimeout(20000);16});17after(async function () {18    await browser.close();19});20it('My first test', async function () {21    await page.waitForSelector('#populate');22    await page.click('#populate');23    await page.click('#submit-button');24    await page.waitForSelector('#article-header');25    const header = await page.$eval('#article-header', e => e.textContent);26    expect(header).to.equal('Thank you, John Smith!');27});28const { chromium } = require('playwright');29(async () => {30    const browser = await chromium.launch({ headless: false, slowMo: 50 });31    const context = await browser.newContext();32    const page = await context.newPage();33    await page.click('#populate');34    await page.click('#submit-button');35    await page.waitForSelector('#article-header');36    const header = await page.$eval('#article-header', e => e.textContent);37    expect(header).to.equal('Thank you, John Smith!');38    await browser.close();39})();40const {Builder, By, Key, until} = require('selenium-webdriver');41const test = require('selenium-webdriver/testing');42const assert = require('assert');43test.describe('Google Search', function() {44    let driver;45    test.before(function *() {46        driver = yield new Builder().forBrowser('chrome').build();47    });Using AI Code Generation
1import { Selector, t } from 'testcafe';2test('My first test', async t => {3        .typeText('#developer-name', 'John Smith')4        .click('#windows')5        .click('#submit-button');6});7import { Selector, t } from 'testcafe';8test('My first test', async t => {9        .typeText('#developer-name', 'John Smith')10        .click('#windows')11        .click('#submit-button');12});13import { Selector, t } from 'testcafe';14test('My first test', async t => {15        .typeText('#developer-name', 'John Smith')16        .click('#windows')17        .click('#submit-button');18});19import { Selector, t } from 'testcafe';20test('My first test', async t => {21        .typeText('#developer-name', 'John Smith')22        .click('#windows')23        .click('#submit-button');24});25import { Selector, t } from 'testcafe';26test('My first test', async t => {27        .typeText('#developer-name', 'John Smith')28        .click('#windows')29        .click('#submit-button');30});31import { Selector, t } from 'testcafe';Using AI Code Generation
1import { isSkippableNode } from 'testcafe/lib/client-functions/selector-builder/is-skippable-node';2const isSkippableNode = isSkippableNode();3const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node');4const isSkippableNode = isSkippableNode();5const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node').isSkippableNode;6const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node');7const isSkippableNode = isSkippableNode.isSkippableNode;8const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node').isSkippableNode;9const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node').isSkippableNode;10const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node').isSkippableNode;11const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node').isSkippableNode;12const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node').isSkippableNode;13const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippable-node').isSkippableNode;14const isSkippableNode = require('testcafe/lib/client-functions/selector-builder/is-skippUsing AI Code Generation
1import { Selector } from 'testcafe';2const skipButton = Selector('button').withAttribute('data-test', 'skip-button');3await t.click(skipButton);4import { Selector } from 'testcafe';5const skipButton = Selector('button').withAttribute('data-test', 'skip-button');6await t.click(skipButton);7import { Selector } from 'testcafe';8const skipButton = Selector('button').withAttribute('data-test', 'skip-button');9await t.click(skipButton);10import { Selector } from 'testcafe';11const skipButton = Selector('button').withAttribute('data-test', 'skip-button');12await t.click(skipButton);13import { Selector } from 'testcafe';14const skipButton = Selector('button').withAttribute('data-test', 'skip-button');15await t.click(skipButton);16import { Selector } from 'testcafe';17const skipButton = Selector('button').withAttribute('data-test', 'skip-button');18await t.click(skipButton);19import { Selector } from 'testcafe';20const skipButton = Selector('button').withAttribute('data-test', 'skip-button');21await t.click(skipButton);22import { Selector } from 'testcaUsing AI Code Generation
1import { Selector } from 'testcafe';2const skipNode = Selector((node, testController) => {3    return testController.isSkippableNode(node);4});5const skipNode = Selector((node, testController) => {6    return testController.isSkippableNode(node);7});8const skipNode = Selector((node, testController) => {9    return testController.isSkippableNode(node);10});11const skipNode = Selector((node, testController) => {12    return testController.isSkippableNode(node);13});14const skipNode = Selector((node, testController) => {15    return testController.isSkippableNode(node);16});17const skipNode = Selector((node, testController) => {18    return testController.isSkippableNode(node);19});20const skipNode = Selector((node, testController) => {21    return testController.isSkippableNode(node);22});23const skipNode = Selector((node, testController) => {24    return testController.isSkippableNode(node);25});26const skipNode = Selector((node, testController) => {27    return testController.isSkippableNode(node);28});29const skipNode = Selector((node, testController) => {30    return testController.isSkippableNode(node);31});32const skipNode = Selector((node, testController) => {33    return testController.isSkippableNode(node);34});35const skipNode = Selector((node, testController) => {36    return testController.isSkippableNode(node);37});38const skipNode = Selector((node, testController) => {39    return testController.isSkippableNode(node);40});41const skipNode = Selector((node, testController) => {42    return testController.isSkippableNode(node);43});44const skipNode = Selector((node, testController) => {45    return testController.isSkippableNode(node);46});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!!
