<commit_before>var expect = require('expect');
var assert = require('assert');

var Selection = require('../Selection');

describe('Selection', () => {
  var div, p1, p2, thisIs, strong, em, some, text, andMore;

  // this is some text
  // <p>this is <strong><em>some</em></strong> text</p>
  // <p>and more</p>
  function createNode() {
    div = document.createElement('div');
    div.setAttribute('class', 'arc-Editor-Content');
    div.setAttribute('data-top', 'true');

    p1 = document.createElement('p');
    p1.setAttribute('class', 'arc-Editor-Block arc-Editor-Block--0101');
    p1.setAttribute('name',  '0101');
    p1.setAttribute('data-align', 'center');
    p1.setAttribute('data-block', 'true');

    p2 = document.createElement('p');
    p2.setAttribute('class', 'arc-Editor-Block arc-Editor-Block--0102');
    p2.setAttribute('name',  '0102');
    p2.setAttribute('data-block',  'true');

    thisIs = document.createTextNode('this is ');
    strong = document.createElement('strong');
    em     = document.createElement('em');
    some   = document.createTextNode('some');
    text   = document.createTextNode(' text');
    andMore = document.createTextNode('and more');

    em.appendChild(some);
    strong.appendChild(em);

    p1.appendChild(thisIs);
    p1.appendChild(strong);
    p1.appendChild(text);
    p2.appendChild(andMore);

    div.appendChild(p1);
    div.appendChild(p2);

    document.body.innerHTML = '';
    document.body.appendChild(div);

    return div;
  }
  function createSelection(startNode, endNode, startOffset, endOffset) {
    var range = document.createRange();
    range.setStart(startNode, startOffset);
    range.setEnd(endNode, endOffset);

    var selection = document.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
    return selection;
  }

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

    it('sets text from doc selection', () => {
      createNode();
      var sel = createSelection(some, some, 0, 4);

      var selection = new Selection(sel);
      expect(selection.text).toBe('some');
    });
  });

  describe('#reselect', () => {

    it('doesnt reselect if there is no anchor or focus', () => {
      createNode();
      var sel = createSelection(some, some, 0, 4);
      var selection = new Selection(sel);

      selection.anchor = false;
      assert(!selection.reselect());
    });

    it('doesnt reselect if there is a selection range', () => {
      createNode();
      var sel = createSelection(some, some, 0, 4);
      var selection = new Selection(sel);

      assert(!selection.reselect());
    });

    it('doesnt reselect if no start or end is found', () => {
      createNode();
      var sel = createSelection(some, some, 0, 0);
      var selection = new Selection(sel);
      selection.anchor.guid = 'foo';

      assert(!selection.reselect());
    });

    it('doesnt reselect range for text nodes', () => {
      createNode();
      var sel = createSelection(thisIs, thisIs, 0, 0);
      var selection = new Selection(sel);
      selection.selType = 'Range';

      selection.anchor.focusOn('0101', 9);
      selection.focus.focusOn('0101', 12);
      assert(!selection.reselect());
    });

    it('reinitializes bounds after selection', () => {
      createNode();
      var sel = createSelection(thisIs, thisIs, 0, 0);
      var selection = new Selection(sel);
      selection.selType = 'Range';

      var boundsBefore = selection.bounds;

      selection.anchor.focusOn('0101', 9);
      selection.focus.focusOn('0101', 12);
      selection.reselect();

      var boundsAfter = selection.bounds;

      expect(boundsBefore.height).toNotBe(boundsAfter.height);
      expect(boundsBefore.width).toNotBe(boundsAfter.width);
      expect(boundsBefore.left).toNotBe(boundsAfter.left);
      expect(boundsBefore.right).toNotBe(boundsAfter.right);<commit_msg>fix some tests for reselection of text nodes<commit_after>var expect = require('expect');
var assert = require('assert');

var Selection = require('../Selection');

describe('Selection', () => {
  var div, p1, p2, thisIs, strong, em, some, text, andMore;

  // this is some text
  // <p>this is <strong><em>some</em></strong> text</p>
  // <p>and more</p>
  function createNode() {
    div = document.createElement('div');
    div.setAttribute('class', 'arc-Editor-Content');
    div.setAttribute('data-top', 'true');

    p1 = document.createElement('p');
    p1.setAttribute('class', 'arc-Editor-Block arc-Editor-Block--0101');
    p1.setAttribute('name',  '0101');
    p1.setAttribute('data-align', 'center');
    p1.setAttribute('data-block', 'true');

    p2 = document.createElement('p');
    p2.setAttribute('class', 'arc-Editor-Block arc-Editor-Block--0102');
    p2.setAttribute('name',  '0102');
    p2.setAttribute('data-block',  'true');

    thisIs = document.createTextNode('this is ');
    strong = document.createElement('strong');
    em     = document.createElement('em');
    some   = document.createTextNode('some');
    text   = document.createTextNode(' text');
    andMore = document.createTextNode('and more');

    em.appendChild(some);
    strong.appendChild(em);

    p1.appendChild(thisIs);
    p1.appendChild(strong);
    p1.appendChild(text);
    p2.appendChild(andMore);

    div.appendChild(p1);
    div.appendChild(p2);

    document.body.innerHTML = '';
    document.body.appendChild(div);

    return div;
  }
  function createSelection(startNode, endNode, startOffset, endOffset) {
    var range = document.createRange();
    range.setStart(startNode, startOffset);
    range.setEnd(endNode, endOffset);

    var selection = document.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
    return selection;
  }

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

    it('sets text from doc selection', () => {
      createNode();
      var sel = createSelection(some, some, 0, 4);

      var selection = new Selection(sel);
      expect(selection.text).toBe('some');
    });
  });

  describe('#reselect', () => {

    it('doesnt reselect if there is no anchor or focus', () => {
      createNode();
      var sel = createSelection(some, some, 0, 4);
      var selection = new Selection(sel);

      selection.anchor = false;
      assert(!selection.reselect());
    });

    it('doesnt reselect if there is a selection range', () => {
      createNode();
      var sel = createSelection(some, some, 0, 4);
      var selection = new Selection(sel);

      assert(!selection.reselect());
    });

    it('doesnt reselect if no start or end is found', () => {
      createNode();
      var sel = createSelection(some, some, 0, 0);
      var selection = new Selection(sel);
      selection.anchor.guid = 'foo';

      assert(!selection.reselect());
    });

    it('doesnt reselect range for collapsed text nodes', () => {
      createNode();
      var sel = createSelection(thisIs, thisIs, 0, 0);
      var selection = new Selection(sel);
      selection.selType = 'Range';

      selection.anchor.focusOn('0101', 9);
      selection.focus.focusOn('0101', 9);
      assert(!selection.reselect());
    });

    it('reselects range for range text nodes', () => {
      createNode();
      var sel = createSelection(thisIs, thisIs, 0, 0);
      var selection = new Selection(sel);
      selection.selType = 'Range';

      selection.anchor.focusOn('0101', 4);
      selection.focus.focusOn('0101', 9);
      assert(selection.reselect());
    });

    it('reinitializes bounds after selection', () => {
      createNode();
      var sel = createSelection(thisIs, thisIs, 0, 0);
      var selection = new Selection(sel);
      selection.selType = 'Range';

      var boundsBefore = selection.bounds;

      selection.anchor.focusOn('0101', 9);
      selection.focus.focusOn('0101', 12);
      selection.reselect();

      var boundsAfter = selection.bounds;

      expect(boundsBefore.height).toNotBe(boundsAfter.height);
      expect(boundsBefore.width).toNotBe(boundsAfter.width);
      expect(boundsBefore.left).toNotBe(boundsAfter.left);
      expect(boundsBefore.right).toNotBe(boundsAfter.right);
{'fraction_non_alphanumeric': 0.15970849802371542, 'fraction_numerical': 0.0179100790513834, 'mean_word_length': 3.2705696202531644, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 38, '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': '402712', 'n_tokens_mistral': 2498, 'n_tokens_neox': 2368, 'n_words': 521}