Home / Function/ getModernOffsetsFromPoints() — react Function Reference

getModernOffsetsFromPoints() — react Function Reference

Architecture documentation for the getModernOffsetsFromPoints() function in ReactDOMSelection.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  0895fc77_95ae_fed1_2bf7_43d55dc5cac0["getModernOffsetsFromPoints()"]
  5672f9bd_6a0a_e8f0_9857_0fb959fc7759["ReactDOMSelection.js"]
  0895fc77_95ae_fed1_2bf7_43d55dc5cac0 -->|defined in| 5672f9bd_6a0a_e8f0_9857_0fb959fc7759
  f22fbac9_78b0_dbd3_6b8a_d487d7efa09e["getOffsets()"]
  f22fbac9_78b0_dbd3_6b8a_d487d7efa09e -->|calls| 0895fc77_95ae_fed1_2bf7_43d55dc5cac0
  style 0895fc77_95ae_fed1_2bf7_43d55dc5cac0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-dom-bindings/src/client/ReactDOMSelection.js lines 60–139

export function getModernOffsetsFromPoints(
  outerNode,
  anchorNode,
  anchorOffset,
  focusNode,
  focusOffset,
) {
  let length = 0;
  let start = -1;
  let end = -1;
  let indexWithinAnchor = 0;
  let indexWithinFocus = 0;
  let node = outerNode;
  let parentNode = null;

  outer: while (true) {
    let next = null;

    while (true) {
      if (
        node === anchorNode &&
        (anchorOffset === 0 || node.nodeType === TEXT_NODE)
      ) {
        start = length + anchorOffset;
      }
      if (
        node === focusNode &&
        (focusOffset === 0 || node.nodeType === TEXT_NODE)
      ) {
        end = length + focusOffset;
      }

      if (node.nodeType === TEXT_NODE) {
        length += node.nodeValue.length;
      }

      if ((next = node.firstChild) === null) {
        break;
      }
      // Moving from `node` to its first child `next`.
      parentNode = node;
      node = next;
    }

    while (true) {
      if (node === outerNode) {
        // If `outerNode` has children, this is always the second time visiting
        // it. If it has no children, this is still the first loop, and the only
        // valid selection is anchorNode and focusNode both equal to this node
        // and both offsets 0, in which case we will have handled above.
        break outer;
      }
      if (parentNode === anchorNode && ++indexWithinAnchor === anchorOffset) {
        start = length;
      }
      if (parentNode === focusNode && ++indexWithinFocus === focusOffset) {
        end = length;
      }
      if ((next = node.nextSibling) !== null) {
        break;
      }
      node = parentNode;
      parentNode = node.parentNode;
    }

    // Moving from `node` to its next sibling `next`.
    node = next;
  }

  if (start === -1 || end === -1) {
    // This should never happen. (Would happen if the anchor/focus nodes aren't
    // actually inside the passed-in node.)
    return null;
  }

  return {
    start: start,
    end: end,
  };
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does getModernOffsetsFromPoints() do?
getModernOffsetsFromPoints() is a function in the react codebase, defined in packages/react-dom-bindings/src/client/ReactDOMSelection.js.
Where is getModernOffsetsFromPoints() defined?
getModernOffsetsFromPoints() is defined in packages/react-dom-bindings/src/client/ReactDOMSelection.js at line 60.
What calls getModernOffsetsFromPoints()?
getModernOffsetsFromPoints() is called by 1 function(s): getOffsets.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free