<commit_before>import $ from 'jquery'

import Editable from '../src/core'
import Highlighting from '../src/highlighting'
import WordHighlighter from '../src/plugins/highlighting/text-highlighting'


describe('Highlighting', function () {

  // Specs

  beforeEach(() => {
    this.editable = new Editable()
  })

  describe('new Highlighting()', () => {

    it('creates an instance with a reference to editable', () => {
      const highlighting = new Highlighting(this.editable, {})
      expect(highlighting.editable).toEqual(this.editable)
    })

  })

  describe('WordHighlighter', () => {

    beforeEach(() => {
      const markerNode = $('<span class="highlight"></span>')[0]
      this.highlighter = new WordHighlighter(markerNode)
    })

    it('finds the word "a"', () => {
      const text = 'a'
      const matches = this.highlighter.findMatches(text, ['a'])

      const firstMatch = matches[0]
      expect(firstMatch.match).toEqual('a')
      expect(firstMatch.startIndex).toEqual(0)
      expect(firstMatch.endIndex).toEqual(1)
    })

    it('does not find the word "b"', () => {
      const text = 'a'
      const matches = this.highlighter.findMatches(text, ['b'])
      expect(matches.length).toEqual(0)
    })

    it('finds the word "juice"', () => {
      const text = 'Some juice.'
      const matches = this.highlighter.findMatches(text, ['juice'])
      const firstMatch = matches[0]
      expect(firstMatch.match).toEqual('juice')
      expect(firstMatch.startIndex).toEqual(5)
      expect(firstMatch.endIndex).toEqual(10)
    })
  })

  describe('highlightSupport', () => {

    beforeEach(() => {
      this.$div = $('<div>Foobarista</div>').appendTo(document.body)
      this.editable = new Editable()
      this.editable.add(this.$div)
    })

    afterEach(() => {
      this.$div.remove()
      this.editable.off()
      this.editable = undefined
    })

    it('can highlight text range matches', () => {
      const startIndex = this.editable.highlight({
        editableHost: this.$div[0],
        text: 'bari',
        highlightId: 'myId',
        textRange: {start: 3, end: 7}
      })
      const highlightSpan = this.$div.find('[data-word-id="myId"]')
      expect(highlightSpan.length).toEqual(1)
      expect(highlightSpan.text()).toEqual('bari')
      expect(startIndex).toEqual(3)
    })

    it('skips if a highlight with the given id is already present', () => {
      this.editable.highlight({
        editableHost: this.$div[0],
        text: 'bari',
        highlightId: 'myId',
        textRange: {start: 3, end: 7}
      })
      this.editable.highlight({
        editableHost: this.$div[0],
        text: 'Foobari',
        highlightId: 'myId',
        textRange: {start: 0, end: 7}
      })
      const highlightSpan = this.$div.find('[data-word-id="myId"]')
      expect(highlightSpan.length).toEqual(1)
      expect(highlightSpan.text()).toEqual('bari')
    })

    it('skips if an invalid range object was passed', () => {
      this.editable.highlight({
        editableHost: this.$div[0],
        text: 'bari',
        highlightId: 'myId',
        textRange: {foo: 3, bar: 7}
      })
      const highlightSpan = this.$div.find('[data-word-id="myId"]')
      expect(highlightSpan.length).toEqual(0)
    })
  })

})<commit_msg>test: highlight position extraction<commit_after>import $ from 'jquery'

import Selection from '../src/selection'
import rangy from 'rangy'
import highlightSupport from '../src/highlight-support'
import Editable from '../src/core'
import Highlighting from '../src/highlighting'
import WordHighlighter from '../src/plugins/highlighting/text-highlighting'

describe('Highlighting', function () {
  // Specs

  beforeEach(() => {
    this.editable = new Editable()
  })

  describe('new Highlighting()', () => {
    it('creates an instance with a reference to editable', () => {
      const highlighting = new Highlighting(this.editable, {})
      expect(highlighting.editable).toEqual(this.editable)
    })
  })

  describe('WordHighlighter', () => {
    beforeEach(() => {
      const markerNode = $('<span class="highlight"></span>')[0]
      this.highlighter = new WordHighlighter(markerNode)
    })

    it('finds the word "a"', () => {
      const text = 'a'
      const matches = this.highlighter.findMatches(text, ['a'])

      const firstMatch = matches[0]
      expect(firstMatch.match).toEqual('a')
      expect(firstMatch.startIndex).toEqual(0)
      expect(firstMatch.endIndex).toEqual(1)
    })

    it('does not find the word "b"', () => {
      const text = 'a'
      const matches = this.highlighter.findMatches(text, ['b'])
      expect(matches.length).toEqual(0)
    })

    it('finds the word "juice"', () => {
      const text = 'Some juice.'
      const matches = this.highlighter.findMatches(text, ['juice'])
      const firstMatch = matches[0]
      expect(firstMatch.match).toEqual('juice')
      expect(firstMatch.startIndex).toEqual(5)
      expect(firstMatch.endIndex).toEqual(10)
    })
  })

  describe('highlightSupport', () => {
    beforeEach(() => {
      this.$div = $('<div>Foobarista</div>').appendTo(document.body)
      this.editable = new Editable()
      this.editable.add(this.$div)
      const range = rangy.createRange()
      range.selectNodeContents(this.$div[0])
      this.selection = new Selection(this.$div[0], range)
    })

    afterEach(() => {
      this.$div.remove()
      this.editable.off()
      this.editable = undefined
    })

    it('can highlight text range matches', () => {
      const startIndex = this.editable.highlight({
        editableHost: this.$div[0],
        text: 'bari',
        highlightId: 'myId',
        textRange: { start: 3, end: 7 }
      })
      const highlightSpan = this.$div.find('[data-word-id="myId"]')
      expect(highlightSpan.length).toEqual(1)
      expect(highlightSpan.text()).toEqual('bari')
      expect(startIndex).toEqual(3)
    })

    it('can extract a text-based range object', () => {
      const editableHost = this.$div[0]
      this.editable.highlight({
        editableHost,
        text: 'bari',
        highlightId: 'myId',
        textRange: { start: 3, end: 7 }
      })

      this.editable.highlight({
        editableHost,
        text: 'Fo',
        highlightId: 'mySecondId',
        textRange: { start: 0, end: 2 }
      })
      const extractedHighlightPositions = highlightSupport.extractHighlightedRanges(editableHost, this.selection)
      expect(extractedHighlightPositions.myId).toBeDefined()
      expect(extractedHighlightPositions.myId.start).toEqual(3)
      expect(extractedHighlightPositions.myId.end).toEqual(7)
      expect(extractedHighlightPositions.mySecondId).toBeDefined()
      expect(extractedHighlightPositions.mySecondId.start).toEqual(0)
      expect(extractedHighlightPositions.mySecondId.end).toEqual(2)
    })

    it('skips if a highlight with the given id is already present', () => {
      this.editable.highlight({
        editableHost: this.$div[0],
        text: 'bari',
        highlightId: 'myId',
        textRange: { start: 3, end: 7 }
      })
      this.editable.highlight({
        editableHost: this.$div[0],
        text: 'Foobari',
        highlightId: 'myId',
        textRange: { start: 0, end: 7 }
      })
      const highlightSpan = this.$div.find('[data-word-id="myId"]')
      expect(highlightSpan.length).toEqual(1)
      expect(highlightSpan.text()).toEqual('bari')
    })

    it('skips if an invalid range object was passed', () => {
      this.editable.highlight({
        editableHost: this.$div[0],
        text: 'bari',
        highlightId: 'myId',
        textRange: { foo: 3, bar: 7 }
      })
      const highlightSpan = this.$div.find('[data-word-id="myId"]')
      expect(highlightSpan.length).toEqual(0)
    })
  })
})
{'fraction_non_alphanumeric': 0.1590909090909091, 'fraction_numerical': 0.00783256291730868, 'mean_word_length': 2.8407297830374754, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 40, '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': '1043108', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2419, 'n_words': 458}