8
0

range.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Range from '/ckeditor5/engine/treeview/range.js';
  8. import Position from '/ckeditor5/engine/treeview/position.js';
  9. import Element from '/ckeditor5/engine/treeview/element.js';
  10. import Text from '/ckeditor5/engine/treeview/text.js';
  11. describe( 'Range', () => {
  12. describe( 'constructor', () => {
  13. it( 'creates range from provided positions', () => {
  14. const start = new Position( {}, 1 );
  15. const end = new Position( {}, 2 );
  16. const range = new Range( start, end );
  17. expect( range ).to.be.an.instanceof( Range );
  18. expect( range ).to.have.property( 'start' ).that.not.equals( start );
  19. expect( range ).to.have.property( 'end' ).that.not.equals( end );
  20. expect( range.start.parent ).to.equal( start.parent );
  21. expect( range.end.parent ).to.equal( end.parent );
  22. expect( range.start.offset ).to.equal( start.offset );
  23. expect( range.end.offset ).to.equal( end.offset );
  24. } );
  25. } );
  26. describe( 'isEqual', () => {
  27. it( 'should return true for the same range', () => {
  28. const start = new Position( {}, 1 );
  29. const end = new Position( {}, 2 );
  30. const range = new Range( start, end );
  31. expect( range.isEqual( range ) ).to.be.true;
  32. } );
  33. it( 'should return true for ranges with same start and end positions', () => {
  34. const start = new Position( {}, 1 );
  35. const end = new Position( {}, 2 );
  36. const range1 = new Range( start, end );
  37. const range2 = new Range( start, end );
  38. expect( range1.isEqual( range2 ) ).to.be.true;
  39. } );
  40. it( 'should return false if start position is different', () => {
  41. const start1 = new Position( {}, 1 );
  42. const start2 = new Position( {}, 1 );
  43. const end = new Position( {}, 2 );
  44. const range1 = new Range( start1, end );
  45. const range2 = new Range( start2, end );
  46. expect( range1.isEqual( range2 ) ).to.be.false;
  47. } );
  48. it( 'should return false if end position is different', () => {
  49. const start = new Position( {}, 1 );
  50. const end1 = new Position( {}, 2 );
  51. const end2 = new Position( {}, 2 );
  52. const range1 = new Range( start, end1 );
  53. const range2 = new Range( start, end2 );
  54. expect( range1.isEqual( range2 ) ).to.be.false;
  55. } );
  56. it( 'should return false for ranges with same root and different offsets', () => {
  57. const mockObject = {};
  58. const range1 = new Range( new Position( mockObject, 0 ), new Position( mockObject, 10 ) );
  59. const range2 = new Range( new Position( mockObject, 2 ), new Position( mockObject, 10 ) );
  60. expect( range1.isEqual( range2 ) ).to.be.false;
  61. } );
  62. } );
  63. describe( 'isIntersecting', () => {
  64. let root, p1, p2, t1, t2, t3;
  65. // root
  66. // __________|__________
  67. // | |
  68. // ___p1___ p2
  69. // | | |
  70. // t1 t2 t3
  71. beforeEach( () => {
  72. t1 = new Text( 'foo' );
  73. t2 = new Text( 'bar' );
  74. t3 = new Text( 'baz' );
  75. p1 = new Element( 'p', null, [ t1, t2 ] );
  76. p2 = new Element( 'p', null, t3 );
  77. root = new Element( 'div', null, [ p1, p2 ] );
  78. } );
  79. it( 'should return true if given range is equal', () => {
  80. const range = Range.createFromParentsAndOffsets( t1, 0, t3, 2 );
  81. const otherRange = Range.createFromRange( range );
  82. expect( range.isIntersecting( otherRange ) ).to.be.true;
  83. expect( otherRange.isIntersecting( range ) ).to.be.true;
  84. } );
  85. it( 'should return true if given range contains this range', () => {
  86. const range = Range.createFromParentsAndOffsets( t1, 0, t3, 3 );
  87. const otherRange = Range.createFromParentsAndOffsets( p1, 1, t2, 2 );
  88. expect( range.isIntersecting( otherRange ) ).to.be.true;
  89. expect( otherRange.isIntersecting( range ) ).to.be.true;
  90. } );
  91. it( 'should return true if given range ends in this range', () => {
  92. const range = Range.createFromParentsAndOffsets( root, 1, t3, 3 );
  93. const otherRange = Range.createFromParentsAndOffsets( t1, 0, p2, 0 );
  94. expect( range.isIntersecting( otherRange ) ).to.be.true;
  95. expect( otherRange.isIntersecting( range ) ).to.be.true;
  96. } );
  97. it( 'should return true if given range starts in this range', () => {
  98. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  99. const otherRange = Range.createFromParentsAndOffsets( p1, 1, p2, 0 );
  100. expect( range.isIntersecting( otherRange ) ).to.be.true;
  101. expect( otherRange.isIntersecting( range ) ).to.be.true;
  102. } );
  103. it( 'should return false if given range is fully before/after this range', () => {
  104. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  105. const otherRange = Range.createFromParentsAndOffsets( root, 1, t3, 0 );
  106. expect( range.isIntersecting( otherRange ) ).to.be.false;
  107. expect( otherRange.isIntersecting( range ) ).to.be.false;
  108. } );
  109. it( 'should return false if ranges are in different roots', () => {
  110. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  111. const otherRange = Range.createFromParentsAndOffsets( new Element( 'div' ), 1, t3, 0 );
  112. expect( range.isIntersecting( otherRange ) ).to.be.false;
  113. expect( otherRange.isIntersecting( range ) ).to.be.false;
  114. } );
  115. } );
  116. describe( 'createFromRange', () => {
  117. it( 'should create a new instance of Range that is equal to passed range', () => {
  118. const range = new Range( new Position( {}, 0 ), new Position( {}, 1 ) );
  119. const clone = Range.createFromRange( range );
  120. expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
  121. expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
  122. } );
  123. } );
  124. } );