MCPcopy Create free account
hub / github.com/krasimir/react-in-patterns / getNodeForCharacterOffset

Function getNodeForCharacterOffset

code/new-context-api/public/app.js:5668–5689  ·  view source on GitHub ↗

* Get object describing the nodes which contain characters at offset. * * @param {DOMElement|DOMTextNode} root * @param {number} offset * @return {?object}

(root, offset)

Source from the content-addressed store, hash-verified

5666 * @return {?object}
5667 */
5668function getNodeForCharacterOffset(root, offset) {
5669 var node = getLeafNode(root);
5670 var nodeStart = 0;
5671 var nodeEnd = 0;
5672
5673 while (node) {
5674 if (node.nodeType === TEXT_NODE) {
5675 nodeEnd = nodeStart + node.textContent.length;
5676
5677 if (nodeStart <= offset && nodeEnd >= offset) {
5678 return {
5679 node: node,
5680 offset: offset - nodeStart
5681 };
5682 }
5683
5684 nodeStart = nodeEnd;
5685 }
5686
5687 node = getLeafNode(getSiblingNode(node));
5688 }
5689}
5690
5691/**
5692 * @param {DOMElement} outerNode

Callers 1

setOffsetsFunction · 0.70

Calls 2

getLeafNodeFunction · 0.70
getSiblingNodeFunction · 0.70

Tested by

no test coverage detected