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

Function getNodeForCharacterOffset

code/styling/public/app.js:6307–6328  ·  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

6305 * @return {?object}
6306 */
6307function getNodeForCharacterOffset(root, offset) {
6308 var node = getLeafNode(root);
6309 var nodeStart = 0;
6310 var nodeEnd = 0;
6311
6312 while (node) {
6313 if (node.nodeType === TEXT_NODE) {
6314 nodeEnd = nodeStart + node.textContent.length;
6315
6316 if (nodeStart <= offset && nodeEnd >= offset) {
6317 return {
6318 node: node,
6319 offset: offset - nodeStart
6320 };
6321 }
6322
6323 nodeStart = nodeEnd;
6324 }
6325
6326 node = getLeafNode(getSiblingNode(node));
6327 }
6328}
6329
6330/**
6331 * @param {DOMElement} outerNode

Callers 1

setOffsetsFunction · 0.70

Calls 2

getLeafNodeFunction · 0.70
getSiblingNodeFunction · 0.70

Tested by

no test coverage detected