range.js 5.5 KB

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