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

Function getModernOffsetsFromPoints

code/communication/public/app.js:6410–6477  ·  view source on GitHub ↗

* Returns {start, end} where `start` is the character/codepoint index of * (anchorNode, anchorOffset) within the textContent of `outerNode`, and * `end` is the index of (focusNode, focusOffset). * * Returns null if you pass in garbage input but we should probably just crash. * * Exported only

(outerNode, anchorNode, anchorOffset, focusNode, focusOffset)

Source from the content-addressed store, hash-verified

6408 * Exported only for testing.
6409 */
6410function getModernOffsetsFromPoints(outerNode, anchorNode, anchorOffset, focusNode, focusOffset) {
6411 var length = 0;
6412 var start = -1;
6413 var end = -1;
6414 var indexWithinAnchor = 0;
6415 var indexWithinFocus = 0;
6416 var node = outerNode;
6417 var parentNode = null;
6418
6419 outer: while (true) {
6420 var next = null;
6421
6422 while (true) {
6423 if (node === anchorNode && (anchorOffset === 0 || node.nodeType === TEXT_NODE)) {
6424 start = length + anchorOffset;
6425 }
6426 if (node === focusNode && (focusOffset === 0 || node.nodeType === TEXT_NODE)) {
6427 end = length + focusOffset;
6428 }
6429
6430 if (node.nodeType === TEXT_NODE) {
6431 length += node.nodeValue.length;
6432 }
6433
6434 if ((next = node.firstChild) === null) {
6435 break;
6436 }
6437 // Moving from `node` to its first child `next`.
6438 parentNode = node;
6439 node = next;
6440 }
6441
6442 while (true) {
6443 if (node === outerNode) {
6444 // If `outerNode` has children, this is always the second time visiting
6445 // it. If it has no children, this is still the first loop, and the only
6446 // valid selection is anchorNode and focusNode both equal to this node
6447 // and both offsets 0, in which case we will have handled above.
6448 break outer;
6449 }
6450 if (parentNode === anchorNode && ++indexWithinAnchor === anchorOffset) {
6451 start = length;
6452 }
6453 if (parentNode === focusNode && ++indexWithinFocus === focusOffset) {
6454 end = length;
6455 }
6456 if ((next = node.nextSibling) !== null) {
6457 break;
6458 }
6459 node = parentNode;
6460 parentNode = node.parentNode;
6461 }
6462
6463 // Moving from `node` to its next sibling `next`.
6464 node = next;
6465 }
6466
6467 if (start === -1 || end === -1) {

Callers 1

getOffsetsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected