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

Function getNodeForCharacterOffset

code/composition/public/app.js:6445–6466  ·  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

6443 * @return {?object}
6444 */
6445function getNodeForCharacterOffset(root, offset) {
6446 var node = getLeafNode(root);
6447 var nodeStart = 0;
6448 var nodeEnd = 0;
6449
6450 while (node) {
6451 if (node.nodeType === TEXT_NODE) {
6452 nodeEnd = nodeStart + node.textContent.length;
6453
6454 if (nodeStart <= offset && nodeEnd >= offset) {
6455 return {
6456 node: node,
6457 offset: offset - nodeStart
6458 };
6459 }
6460
6461 nodeStart = nodeEnd;
6462 }
6463
6464 node = getLeafNode(getSiblingNode(node));
6465 }
6466}
6467
6468/**
6469 * @param {DOMElement} outerNode

Callers 1

setOffsetsFunction · 0.70

Calls 2

getLeafNodeFunction · 0.70
getSiblingNodeFunction · 0.70

Tested by

no test coverage detected