selection.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Selection from '/ckeditor5/engine/treeview/selection.js';
  8. import Range from '/ckeditor5/engine/treeview/range.js';
  9. import Element from '/ckeditor5/engine/treeview/element.js';
  10. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. describe( 'Selection', () => {
  12. let selection;
  13. beforeEach( () => {
  14. selection = new Selection();
  15. } );
  16. describe( 'anchor', () => {
  17. it( 'should return null if no ranges in selection', () => {
  18. expect( selection.anchor ).to.be.null;
  19. } );
  20. it( 'should return start of single range in selection', () => {
  21. const el = new Element( 'p' );
  22. const range = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  23. selection.addRange( range );
  24. const anchor = selection.anchor;
  25. expect( anchor.isEqual( range.start ) ).to.be.true;
  26. expect( anchor ).to.not.equal( range.start );
  27. } );
  28. it( 'should return end of single range in selection when added as backward', () => {
  29. const el = new Element( 'p' );
  30. const range = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  31. selection.addRange( range, true );
  32. const anchor = selection.anchor;
  33. expect( anchor.isEqual( range.end ) ).to.be.true;
  34. expect( anchor ).to.not.equal( range.end );
  35. } );
  36. it( 'should get anchor from last inserted range', () => {
  37. const el = new Element( 'p' );
  38. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  39. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 20 );
  40. selection.addRange( range1 );
  41. selection.addRange( range2 );
  42. expect( selection.anchor.isEqual( range2.start ) ).to.be.true;
  43. } );
  44. } );
  45. describe( 'focus', () => {
  46. it( 'should return null if no ranges in selection', () => {
  47. expect( selection.focus ).to.be.null;
  48. } );
  49. it( 'should return end of single range in selection', () => {
  50. const el = new Element( 'p' );
  51. const range = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  52. selection.addRange( range );
  53. const focus = selection.focus;
  54. expect( focus.isEqual( range.end ) ).to.be.true;
  55. } );
  56. it( 'should return start of single range in selection when added as backward', () => {
  57. const el = new Element( 'p' );
  58. const range = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  59. selection.addRange( range, true );
  60. const focus = selection.focus;
  61. expect( focus.isEqual( range.start ) ).to.be.true;
  62. expect( focus ).to.not.equal( range.start );
  63. } );
  64. it( 'should get focus from last inserted range', () => {
  65. const el = new Element( 'p' );
  66. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  67. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 20 );
  68. selection.addRange( range1 );
  69. selection.addRange( range2 );
  70. expect( selection.focus.isEqual( range2.end ) ).to.be.true;
  71. } );
  72. } );
  73. describe( 'isCollapsed', () => {
  74. it( 'should return true when all ranges are collapsed', () => {
  75. const el = new Element( 'p' );
  76. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  77. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
  78. selection.addRange( range1 );
  79. selection.addRange( range2 );
  80. expect( selection.isCollapsed ).to.be.true;
  81. } );
  82. it( 'should return false when not all ranges are collapsed', () => {
  83. const el = new Element( 'p' );
  84. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  85. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  86. selection.addRange( range1 );
  87. selection.addRange( range2 );
  88. expect( selection.isCollapsed ).to.be.false;
  89. } );
  90. } );
  91. describe( 'rangeCount', () => {
  92. it( 'should return proper range count', () => {
  93. const el = new Element( 'p' );
  94. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  95. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  96. expect( selection.rangeCount ).to.equal( 0 );
  97. selection.addRange( range1 );
  98. expect( selection.rangeCount ).to.equal( 1 );
  99. selection.addRange( range2 );
  100. expect( selection.rangeCount ).to.equal( 2 );
  101. } );
  102. } );
  103. describe( 'addRange', () => {
  104. it( 'should add range to selection ranges', () => {
  105. const el = new Element( 'p' );
  106. const range = Range.createFromParentsAndOffsets( el, 0, el, 1 );
  107. selection.addRange( range );
  108. expect( selection.getRangeAt( 0 ).isEqual( range ) ).to.be.true;
  109. } );
  110. it( 'should fire change event', ( done ) => {
  111. const el = new Element( 'p' );
  112. const range = Range.createFromParentsAndOffsets( el, 0, el, 1 );
  113. selection.once( 'change:range', () => {
  114. expect( selection.getRangeAt( 0 ).isEqual( range ) ).to.be.true;
  115. done();
  116. } );
  117. selection.addRange( range );
  118. } );
  119. it( 'should throw when range is intersecting with already added range', () => {
  120. const el = new Element( 'p' );
  121. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  122. const range2 = Range.createFromParentsAndOffsets( el, 7, el, 15 );
  123. selection.addRange( range1 );
  124. expect( () => {
  125. selection.addRange( range2 );
  126. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  127. expect( () => {
  128. selection.addRange( range1 );
  129. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  130. } );
  131. } );
  132. } );