text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  /**
   * From a DOM selection's `node` and `offset`, normalize so that it always
   * refers to a text node.
   *
   * @param {Element} node
   * @param {Number} offset
   * @return {Object}
   */
  
  function normalizeNodeAndOffset(node, offset) {
    // If it's an element node, its offset refers to the index of its children
    // including comment nodes, so try to find the right text child node.
    if (node.nodeType == 1 && node.childNodes.length) {
      var isLast = offset == node.childNodes.length;
      var direction = isLast ? 'backward' : 'forward';
      var index = isLast ? offset - 1 : offset;
      node = getEditableChild(node, index, direction);
  
      // If the node has children, traverse until we have a leaf node. Leaf nodes
      // can be either text nodes, or other void DOM nodes.
      while (node.nodeType == 1 && node.childNodes.length) {
        var i = isLast ? node.childNodes.length - 1 : 0;
        node = getEditableChild(node, i, direction);
      }
  
      // Determine the new offset inside the text node.
      offset = isLast ? node.textContent.length : 0;
    }
  
    // Return the node and offset.
    return { node: node, offset: offset };
  }
  
  /**
   * Get the nearest editable child at `index` in a `parent`, preferring
   * `direction`.
   *
   * @param {Element} parent
   * @param {Number} index
   * @param {String} direction ('forward' or 'backward')
   * @return {Element|Null}
   */
  
  function getEditableChild(parent, index, direction) {
    var childNodes = parent.childNodes;
  
    var child = childNodes[index];
    var i = index;
    var triedForward = false;
    var triedBackward = false;
  
    // While the child is a comment node, or an element node with no children,
    // keep iterating to find a sibling non-void, non-comment node.
    while (child.nodeType == 8 || child.nodeType == 1 && child.childNodes.length == 0 || child.nodeType == 1 && child.getAttribute('contenteditable') == 'false') {
      if (triedForward && triedBackward) break;
  
      if (i >= childNodes.length) {
        triedForward = true;
        i = index - 1;
        direction = 'backward';
        continue;
      }
  
      if (i < 0) {
        triedBackward = true;
        i = index + 1;
        direction = 'forward';
        continue;
      }
  
      child = childNodes[i];
      if (direction == 'forward') i++;
      if (direction == 'backward') i--;
    }
  
    return child || null;
  }
  
  /**
   * Export.
   *
   * @type {Function}
   */
  
  exports.default = normalizeNodeAndOffset;