position.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. 'use strict';
  7. import Position from '/ckeditor5/engine/view/position.js';
  8. import Node from '/ckeditor5/engine/view/node.js';
  9. import Element from '/ckeditor5/engine/view/element.js';
  10. import EditableElement from '/ckeditor5/engine/view/editableelement.js';
  11. import Document from '/ckeditor5/engine/view/document.js';
  12. import Text from '/ckeditor5/engine/view/text.js';
  13. import TextProxy from '/ckeditor5/engine/view/textproxy.js';
  14. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  15. import { parse } from '/tests/engine/_utils/view.js';
  16. describe( 'Position', () => {
  17. const parentMock = {};
  18. describe( 'constructor', () => {
  19. it( 'should create element without attributes', () => {
  20. const elem = new Position( parentMock, 5 );
  21. expect( elem ).to.have.property( 'parent' ).that.equals( parentMock );
  22. expect( elem ).to.have.property( 'offset' ).that.equals( 5 );
  23. } );
  24. } );
  25. describe( 'nodeBefore', () => {
  26. it( 'should equal to node that is before position', () => {
  27. const b1 = new Element( 'b' );
  28. const el = new Element( 'p', null, [ b1 ] );
  29. const position = new Position( el, 1 );
  30. expect( position.nodeBefore ).to.equal( b1 );
  31. } );
  32. it( 'should equal null if there is no node before', () => {
  33. const b1 = new Element( 'b' );
  34. const el = new Element( 'p', null, [ b1 ] );
  35. const position = new Position( el, 0 );
  36. expect( position.nodeBefore ).to.be.null;
  37. } );
  38. it( 'should equal null if position is located inside text node', () => {
  39. const text = new Text( 'foobar' );
  40. const position = new Position( text, 3 );
  41. expect( position.nodeBefore ).to.be.null;
  42. } );
  43. } );
  44. describe( 'nodeAfter', () => {
  45. it( 'should equal to node that is after position', () => {
  46. const b1 = new Element( 'b' );
  47. const el = new Element( 'p', null, [ b1 ] );
  48. const position = new Position( el, 0 );
  49. expect( position.nodeAfter ).to.equal( b1 );
  50. } );
  51. it( 'should equal null if there is no node before', () => {
  52. const b1 = new Element( 'b' );
  53. const el = new Element( 'p', null, [ b1 ] );
  54. const position = new Position( el, 1 );
  55. expect( position.nodeAfter ).to.be.null;
  56. } );
  57. it( 'should equal null if position is located inside text node', () => {
  58. const text = new Text( 'foobar' );
  59. const position = new Position( text, 3 );
  60. expect( position.nodeAfter ).to.be.null;
  61. } );
  62. } );
  63. describe( 'getShiftedBy', () => {
  64. it( 'returns new instance with shifted offset', () => {
  65. const position = new Position( parentMock, 10 );
  66. const shifted = position.getShiftedBy( 12 );
  67. expect( shifted.offset ).to.equal( 22 );
  68. } );
  69. it( 'accepts negative values', () => {
  70. const position = new Position( parentMock, 10 );
  71. const shifted = position.getShiftedBy( -5 );
  72. expect( shifted.offset ).to.equal( 5 );
  73. } );
  74. it( 'prevents offset to be a negative value', () => {
  75. const position = new Position( parentMock, 10 );
  76. const shifted = position.getShiftedBy( -20 );
  77. expect( shifted.offset ).to.equal( 0 );
  78. } );
  79. } );
  80. describe( 'createFromPosition', () => {
  81. it( 'creates new Position with same parent and offset', () => {
  82. const offset = 50;
  83. const position = new Position( parentMock, offset );
  84. const newPosition = Position.createFromPosition( position );
  85. expect( position ).to.not.equal( newPosition );
  86. expect( position.offset ).to.equal( offset );
  87. expect( position.parent ).to.equal( parentMock );
  88. } );
  89. } );
  90. describe( 'isEqual', () => {
  91. it( 'should return true for same object', () => {
  92. const position = new Position( {}, 12 );
  93. expect( position.isEqual( position ) ).to.be.true;
  94. } );
  95. it( 'should return true for positions with same parent and offset', () => {
  96. const parentMock = {};
  97. const position1 = new Position( parentMock, 12 );
  98. const position2 = new Position( parentMock, 12 );
  99. expect( position1.isEqual( position2 ) ).to.be.true;
  100. } );
  101. it( 'should return false for positions with different parents', () => {
  102. const position1 = new Position( {}, 12 );
  103. const position2 = new Position( {}, 12 );
  104. expect( position1.isEqual( position2 ) ).to.be.false;
  105. } );
  106. it( 'should return false for positions with different positions', () => {
  107. const parentMock = {};
  108. const position1 = new Position( parentMock, 12 );
  109. const position2 = new Position( parentMock, 2 );
  110. expect( position1.isEqual( position2 ) ).to.be.false;
  111. } );
  112. } );
  113. describe( 'isBefore', () => {
  114. it( 'should return false for same positions', () => {
  115. const node = new Node();
  116. const position1 = new Position( node, 10 );
  117. const position2 = new Position( node, 10 );
  118. expect( position1.isBefore( position1 ) ).to.be.false;
  119. expect( position1.isBefore( position2 ) ).to.be.false;
  120. expect( position2.isBefore( position1 ) ).to.be.false;
  121. } );
  122. it( 'should return false if no common ancestor is found', () => {
  123. const t1 = new Text( 'foo' );
  124. const t2 = new Text( 'bar' );
  125. const e1 = new Element( 'p', null, [ t1 ] );
  126. const e2 = new Element( 'p', null, [ t2 ] );
  127. const position1 = new Position( e1, 0 );
  128. const position2 = new Position( e2, 1 );
  129. expect( position1.isBefore( position2 ) );
  130. expect( position2.isBefore( position1 ) );
  131. } );
  132. it( 'should return true if position is before in same node', () => {
  133. const node = new Node();
  134. const p1 = new Position( node, 10 );
  135. const p2 = new Position( node, 5 );
  136. expect( p2.isBefore( p1 ) ).to.be.true;
  137. expect( p1.isBefore( p2 ) ).to.be.false;
  138. } );
  139. it( 'should compare positions that have common parent', () => {
  140. const t1 = new Text( 'foo' );
  141. const t2 = new Text( 'bar' );
  142. const root = new Element( 'p', null, [ t1, t2 ] );
  143. const position1 = new Position( t1, 2 );
  144. const position2 = new Position( t2, 0 );
  145. const position3 = new Position( root, 0 );
  146. const position4 = new Position( root, 2 );
  147. const position5 = new Position( t1, 0 );
  148. const position6 = new Position( root, 1 );
  149. expect( position1.isBefore( position2 ) ).to.be.true;
  150. expect( position2.isBefore( position1 ) ).to.be.false;
  151. expect( position3.isBefore( position1 ) ).to.be.true;
  152. expect( position3.isBefore( position2 ) ).to.be.true;
  153. expect( position1.isBefore( position3 ) ).to.be.false;
  154. expect( position2.isBefore( position3 ) ).to.be.false;
  155. expect( position4.isBefore( position1 ) ).to.be.false;
  156. expect( position4.isBefore( position3 ) ).to.be.false;
  157. expect( position3.isBefore( position4 ) ).to.be.true;
  158. expect( position3.isBefore( position5 ) ).to.be.true;
  159. expect( position6.isBefore( position2 ) ).to.be.true;
  160. expect( position1.isBefore( position6 ) ).to.be.true;
  161. } );
  162. } );
  163. describe( 'isAfter', () => {
  164. it( 'should return false for same positions', () => {
  165. const node = new Node();
  166. const position1 = new Position( node, 10 );
  167. const position2 = new Position( node, 10 );
  168. expect( position1.isAfter( position1 ) ).to.be.false;
  169. expect( position1.isAfter( position2 ) ).to.be.false;
  170. expect( position2.isAfter( position1 ) ).to.be.false;
  171. } );
  172. it( 'should return false if no common ancestor is found', () => {
  173. const t1 = new Text( 'foo' );
  174. const t2 = new Text( 'bar' );
  175. const e1 = new Element( 'p', null, [ t1 ] );
  176. const e2 = new Element( 'p', null, [ t2 ] );
  177. const position1 = new Position( e1, 0 );
  178. const position2 = new Position( e2, 1 );
  179. expect( position1.isAfter( position2 ) );
  180. expect( position2.isAfter( position1 ) );
  181. } );
  182. it( 'should return true if position is after in same node', () => {
  183. const node = new Node();
  184. const p1 = new Position( node, 10 );
  185. const p2 = new Position( node, 5 );
  186. expect( p2.isAfter( p1 ) ).to.be.false;
  187. expect( p1.isAfter( p2 ) ).to.be.true;
  188. } );
  189. it( 'should compare positions that have common parent', () => {
  190. const t1 = new Text( 'foo' );
  191. const t2 = new Text( 'bar' );
  192. const root = new Element( 'p', null, [ t1, t2 ] );
  193. const position1 = new Position( t1, 2 );
  194. const position2 = new Position( t2, 0 );
  195. const position3 = new Position( root, 0 );
  196. const position4 = new Position( root, 2 );
  197. const position5 = new Position( t1, 0 );
  198. const position6 = new Position( root, 1 );
  199. expect( position1.isAfter( position2 ) ).to.be.false;
  200. expect( position2.isAfter( position1 ) ).to.be.true;
  201. expect( position3.isAfter( position1 ) ).to.be.false;
  202. expect( position3.isAfter( position2 ) ).to.be.false;
  203. expect( position1.isAfter( position3 ) ).to.be.true;
  204. expect( position2.isAfter( position3 ) ).to.be.true;
  205. expect( position4.isAfter( position1 ) ).to.be.true;
  206. expect( position4.isAfter( position3 ) ).to.be.true;
  207. expect( position3.isAfter( position4 ) ).to.be.false;
  208. expect( position5.isAfter( position3 ) ).to.be.true;
  209. expect( position2.isAfter( position6 ) ).to.be.true;
  210. } );
  211. } );
  212. describe( 'compareWith', () => {
  213. it( 'should return same if positions are same', () => {
  214. const root = new Node();
  215. const position = new Position( root, 0 );
  216. const compared = new Position( root, 0 );
  217. expect( position.compareWith( compared ) ).to.equal( 'same' );
  218. } );
  219. it( 'should return before if the position is before compared one', () => {
  220. const root = new Node();
  221. const position = new Position( root, 0 );
  222. const compared = new Position( root, 1 );
  223. expect( position.compareWith( compared ) ).to.equal( 'before' );
  224. } );
  225. it( 'should return after if the position is after compared one', () => {
  226. const root = new Node();
  227. const position = new Position( root, 4 );
  228. const compared = new Position( root, 1 );
  229. expect( position.compareWith( compared ) ).to.equal( 'after' );
  230. } );
  231. it( 'should return different if positions are in different roots', () => {
  232. const root1 = new Node();
  233. const root2 = new Node();
  234. const position = new Position( root1, 4 );
  235. const compared = new Position( root2, 1 );
  236. expect( position.compareWith( compared ) ).to.equal( 'different' );
  237. } );
  238. } );
  239. describe( 'createBefore', () => {
  240. it( 'should throw error if one try to create positions before root', () => {
  241. expect( () => {
  242. Position.createBefore( parse( '<p></p>' ) );
  243. } ).to.throw( CKEditorError, /position-before-root/ );
  244. } );
  245. it( 'should create positions before `Node`', () => {
  246. const { selection } = parse( '<p>[]<b></b></p>' );
  247. const position = selection.getFirstPosition();
  248. const nodeAfter = position.nodeAfter;
  249. expect( Position.createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
  250. } );
  251. it( 'should create positions before `TextProxy`', () => {
  252. const text = new Text( 'abc' );
  253. const textProxy = new TextProxy( text, 1, 1 );
  254. const position = new Position( text, 1 );
  255. expect( Position.createBefore( textProxy ) ).deep.equal( position );
  256. } );
  257. } );
  258. describe( 'createAfter', () => {
  259. it( 'should throw error if one try to create positions after root', () => {
  260. expect( () => {
  261. Position.createAfter( parse( '<p></p>' ) );
  262. } ).to.throw( CKEditorError, /position-after-root/ );
  263. } );
  264. it( 'should create positions after `Node`', () => {
  265. const { selection } = parse( '<p><b></b>[]</p>' );
  266. const position = selection.getFirstPosition();
  267. const nodeBefore = position.nodeBefore;
  268. expect( Position.createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
  269. } );
  270. it( 'should create positions after `TextProxy`', () => {
  271. const text = new Text( 'abcd' );
  272. const textProxy = new TextProxy( text, 1, 2 );
  273. const position = new Position( text, 3 );
  274. expect( Position.createAfter( textProxy ) ).deep.equal( position );
  275. } );
  276. } );
  277. describe( 'getEditableElement', () => {
  278. it( 'should return null if position is not inside EditableElement', () => {
  279. const position = new Position( new Element( 'p' ), 0 );
  280. expect( position.getEditableElement() ).to.be.null;
  281. } );
  282. it( 'should return EditableElement when position is placed inside', () => {
  283. const document = new Document();
  284. const p = new Element( 'p' );
  285. const editable = new EditableElement( document, 'div', null, p );
  286. const position = new Position( p, 0 );
  287. expect( position.getEditableElement() ).to.equal( editable );
  288. } );
  289. } );
  290. } );