<commit_before>import getWindow from 'get-window'

import findNode from './find-node'
import findRange from './find-range'

/**
 * Get the target range from a DOM `event`.
 *
 * @param {Event} event
 * @param {Value} value
 * @return {Range}
 */

function getEventRange(event, value) {
  if (event.nativeEvent) {
    event = event.nativeEvent
  }

  const { x, y, target } = event
  if (x == null || y == null) return null

  const { document, schema } = value
  const node = findNode(target, value)
  if (!node) return null

  // If the drop target is inside a void node, move it into either the next or
  // previous node, depending on which side the `x` and `y` coordinates are
  // closest to.
  if (schema.isVoid(node)) {
    const rect = target.getBoundingClientRect()
    const isPrevious =
      node.object == 'inline'
        ? x - rect.left < rect.left + rect.width - x
        : y - rect.top < rect.top + rect.height - y

    const text = node.getFirstText()
    const range = document.createRange()

    if (isPrevious) {
      const previousText = document.getPreviousText(text.key)

      if (previousText) {
        return range.moveToEndOf(previousText)
      }
    }

    const nextText = document.getNextText(text.key)
    return nextText ? range.moveToStartOf(nextText) : null
  }

  // Else resolve a range from the caret position where the drop occured.
  const window = getWindow(target)
  let native

  // COMPAT: In Firefox, `caretRangeFromPoint` doesn't exist. (2016/07/25)
  if (window.document.caretRangeFromPoint) {
    native = window.document.caretRangeFromPoint(x, y)
  } else if (window.document.caretPositionFromPoint) {
    const position = window.document.caretPositionFromPoint(x, y)
    native = window.document.createRange()
    native.setStart(position.offsetNode, position.offset)
    native.setEnd(position.offsetNode, position.offset)
  } else if (window.document.body.createTextRange) {
    // COMPAT: In IE, `caretRangeFromPoint` and<commit_msg>Fix deprecated moveToStartOf and moveToEndOf (#2155)<commit_after>import getWindow from 'get-window'

import findNode from './find-node'
import findRange from './find-range'

/**
 * Get the target range from a DOM `event`.
 *
 * @param {Event} event
 * @param {Value} value
 * @return {Range}
 */

function getEventRange(event, value) {
  if (event.nativeEvent) {
    event = event.nativeEvent
  }

  const { x, y, target } = event
  if (x == null || y == null) return null

  const { document, schema } = value
  const node = findNode(target, value)
  if (!node) return null

  // If the drop target is inside a void node, move it into either the next or
  // previous node, depending on which side the `x` and `y` coordinates are
  // closest to.
  if (schema.isVoid(node)) {
    const rect = target.getBoundingClientRect()
    const isPrevious =
      node.object == 'inline'
        ? x - rect.left < rect.left + rect.width - x
        : y - rect.top < rect.top + rect.height - y

    const text = node.getFirstText()
    const range = document.createRange()

    if (isPrevious) {
      const previousText = document.getPreviousText(text.key)

      if (previousText) {
        return range.moveToEndOfNode(previousText)
      }
    }

    const nextText = document.getNextText(text.key)
    return nextText ? range.moveToStartOfNode(nextText) : null
  }

  // Else resolve a range from the caret position where the drop occured.
  const window = getWindow(target)
  let native

  // COMPAT: In Firefox, `caretRangeFromPoint` doesn't exist. (2016/07/25)
  if (window.document.caretRangeFromPoint) {
    native = window.document.caretRangeFromPoint(x, y)
  } else if (window.document.caretPositionFromPoint) {
    const position = window.document.caretPositionFromPoint(x, y)
    native = window.document.createRange()
    native.setStart(position.offsetNode, position.offset)
    native.setEnd(position.offsetNode, position.offset)
  } else if (window.document.body.createTextRange) {
    // COMPAT: In IE, `caretRangeFromPoint` and
{'fraction_non_alphanumeric': 0.11199404022845791, 'fraction_numerical': 0.004966476285075739, 'mean_word_length': 3.23109243697479, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6339002', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1225, 'n_words': 364}