<commit_before>Scribe = require('./scribe')


# TODO fix this entire file, esp findDeepestNode
class Scribe.Position
  @findLeafNode: (editor, node, offset) ->
    [node, offset] = Scribe.DOM.findDeepestNode(node, offset)
    if node.nodeType == node.TEXT_NODE
      offset = Scribe.Position.getIndex(node, offset, node.parentNode)
      node = node.parentNode
    return [node, offset]
  
  @getIndex: (node, index, offsetNode = null) ->
    while node != offsetNode and node.parentNode != node.ownerDocument.body
      while node.previousSibling?
        node = node.previousSibling
        index += Scribe.Utils.getNodeLength(node)
      node = node.parentNode
    return index


  # constructor: (Editor editor, Object node, Number offset) ->
  # constructor: (Editor editor, Number index) -> 
  constructor: (@editor, @leafNode, @offset) ->
    if _.isNumber(@leafNode)
      @offset = @index = @leafNode
      @leafNode = @editor.root
    else
      @index = Scribe.Position.getIndex(@leafNode, @offset)
    [@leafNode, @offset] = Scribe.Position.findLeafNode(@editor, @leafNode, @offset)

  getLeaf: ->
    return @leaf if @leaf?
    @leaf = @editor.doc.findLeaf(@leafNode)
    return @leaf
<commit_msg>ownerDocument is null if element is document in Firefox<commit_after>Scribe = require('./scribe')


# TODO fix this entire file, esp findDeepestNode
class Scribe.Position
  @findLeafNode: (editor, node, offset) ->
    [node, offset] = Scribe.DOM.findDeepestNode(node, offset)
    if node.nodeType == node.TEXT_NODE
      offset = Scribe.Position.getIndex(node, offset, node.parentNode)
      node = node.parentNode
    return [node, offset]
  
  @getIndex: (node, index, offsetNode = null) ->
    while node != offsetNode and node.ownerDocument? and node.parentNode != node.ownerDocument.body
      while node.previousSibling?
        node = node.previousSibling
        index += Scribe.Utils.getNodeLength(node)
      node = node.parentNode
    return index


  # constructor: (Editor editor, Object node, Number offset) ->
  # constructor: (Editor editor, Number index) -> 
  constructor: (@editor, @leafNode, @offset) ->
    if _.isNumber(@leafNode)
      @offset = @index = @leafNode
      @leafNode = @editor.root
    else
      @index = Scribe.Position.getIndex(@leafNode, @offset)
    [@leafNode, @offset] = Scribe.Position.findLeafNode(@editor, @leafNode, @offset)

  getLeaf: ->
    return @leaf if @leaf?
    @leaf = @editor.doc.findLeaf(@leafNode)
    return @leaf

{'fraction_non_alphanumeric': 0.12308930008045052, 'fraction_numerical': 0.0, 'mean_word_length': 3.425266903914591, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '5174841', 'n_tokens_mistral': 761, 'n_tokens_neox': 761, 'n_words': 196}