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

Function getNodeForCharacterOffset

code/communication/public/app.js:6342–6363  ·  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

6340 * @return {?object}
6341 */
6342function getNodeForCharacterOffset(root, offset) {
6343 var node = getLeafNode(root);
6344 var nodeStart = 0;
6345 var nodeEnd = 0;
6346
6347 while (node) {
6348 if (node.nodeType === TEXT_NODE) {
6349 nodeEnd = nodeStart + node.textContent.length;
6350
6351 if (nodeStart <= offset && nodeEnd >= offset) {
6352 return {
6353 node: node,
6354 offset: offset - nodeStart
6355 };
6356 }
6357
6358 nodeStart = nodeEnd;
6359 }
6360
6361 node = getLeafNode(getSiblingNode(node));
6362 }
6363}
6364
6365/**
6366 * @param {DOMElement} outerNode

Callers 1

setOffsetsFunction · 0.70

Calls 2

getLeafNodeFunction · 0.70
getSiblingNodeFunction · 0.70

Tested by

no test coverage detected