liveposition.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../src/model/model';
  6. import DocumentFragment from '../../src/model/documentfragment';
  7. import Element from '../../src/model/element';
  8. import Text from '../../src/model/text';
  9. import Position from '../../src/model/position';
  10. import LivePosition from '../../src/model/liveposition';
  11. import Range from '../../src/model/range';
  12. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. describe( 'LivePosition', () =>
  14. {
  15. let model, doc, root, ul, p, li1, li2;
  16. beforeEach( () => {
  17. model = new Model();
  18. doc = model.document;
  19. root = doc.createRoot();
  20. li1 = new Element( 'li', [], new Text( 'abcdef' ) );
  21. li2 = new Element( 'li', [], new Text( 'foobar' ) );
  22. ul = new Element( 'ul', [], [ li1, li2 ] );
  23. p = new Element( 'p', [], new Text( 'qwerty' ) );
  24. root._insertChild( 0, [ p, ul ] );
  25. } );
  26. afterEach( () => {
  27. doc.destroy();
  28. } );
  29. it( 'should be an instance of Position', () => {
  30. const live = new LivePosition( root, [ 0 ] );
  31. live.detach();
  32. expect( live ).to.be.instanceof( Position );
  33. } );
  34. it( 'should throw if given root is not a RootElement', () => {
  35. const docFrag = new DocumentFragment();
  36. expectToThrowCKEditorError( () => {
  37. new LivePosition( docFrag, [ 1 ] ); // eslint-disable-line no-new
  38. }, /model-liveposition-root-not-rootelement/, docFrag );
  39. } );
  40. it( 'should listen to the model applyOperation event', () => {
  41. sinon.spy( LivePosition.prototype, 'listenTo' );
  42. const live = new LivePosition( root, [ 0 ] );
  43. live.detach();
  44. expect( live.listenTo.calledWith( model, 'applyOperation' ) ).to.be.true;
  45. LivePosition.prototype.listenTo.restore();
  46. } );
  47. it( 'should stop listening when detached', () => {
  48. sinon.spy( LivePosition.prototype, 'stopListening' );
  49. const live = new LivePosition( root, [ 0 ] );
  50. live.detach();
  51. expect( live.stopListening.called ).to.be.true;
  52. LivePosition.prototype.stopListening.restore();
  53. } );
  54. describe( 'fromPosition()', () => {
  55. it( 'should return LivePosition', () => {
  56. const position = LivePosition.fromPosition( new Position( root, [ 0 ] ) );
  57. expect( position ).to.be.instanceof( LivePosition );
  58. position.detach();
  59. } );
  60. } );
  61. it( '_createBefore should return LivePosition', () => {
  62. const position = LivePosition._createBefore( ul, 'toPrevious' );
  63. expect( position ).to.be.instanceof( LivePosition );
  64. expect( position.stickiness ).to.equal( 'toPrevious' );
  65. position.detach();
  66. } );
  67. it( '_createAfter should return LivePosition', () => {
  68. const position = LivePosition._createAfter( ul, 'toPrevious' );
  69. expect( position ).to.be.instanceof( LivePosition );
  70. expect( position.stickiness ).to.equal( 'toPrevious' );
  71. position.detach();
  72. } );
  73. it( '_createAt should return LivePosition', () => {
  74. const position = LivePosition._createAt( ul, 'end', 'toPrevious' );
  75. expect( position ).to.be.instanceof( LivePosition );
  76. expect( position.stickiness ).to.equal( 'toPrevious' );
  77. position.detach();
  78. } );
  79. describe( 'should get transformed if', () => {
  80. let live, spy;
  81. beforeEach( () => {
  82. live = new LivePosition( root, [ 1, 1, 3 ] );
  83. spy = sinon.spy();
  84. live.on( 'change', spy );
  85. } );
  86. afterEach( () => {
  87. live.detach();
  88. } );
  89. describe( 'insertion', () => {
  90. it( 'is in the same parent and closer offset', () => {
  91. model.change( writer => {
  92. writer.insertText( 'foo', new Position( root, [ 1, 1, 0 ] ) );
  93. } );
  94. expect( live.path ).to.deep.equal( [ 1, 1, 6 ] );
  95. expect( spy.calledOnce ).to.be.true;
  96. } );
  97. it( 'is at the same position and live position is sticking to the next node', () => {
  98. live.stickiness = 'toNext';
  99. model.change( writer => {
  100. writer.insertText( 'foo', new Position( root, [ 1, 1, 3 ] ) );
  101. } );
  102. expect( live.path ).to.deep.equal( [ 1, 1, 6 ] );
  103. expect( spy.calledOnce ).to.be.true;
  104. } );
  105. it( 'is before a node from the live position path', () => {
  106. model.change( writer => {
  107. writer.insert( new Element( 'paragraph' ), new Position( root, [ 1, 0 ] ) );
  108. } );
  109. expect( live.path ).to.deep.equal( [ 1, 2, 3 ] );
  110. expect( spy.calledOnce ).to.be.true;
  111. } );
  112. } );
  113. describe( 'range move', () => {
  114. it( 'is at the same parent and closer offset', () => {
  115. model.change( writer => {
  116. const sourcePosition = new Position( root, [ 1, 0, 1 ] );
  117. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 3 );
  118. const targetPosition = new Position( root, [ 1, 1, 0 ] );
  119. writer.move( sourceRange, targetPosition );
  120. } );
  121. expect( live.path ).to.deep.equal( [ 1, 1, 6 ] );
  122. expect( spy.calledOnce ).to.be.true;
  123. } );
  124. it( 'is at the same position and live position is sticking to right side', () => {
  125. live.stickiness = 'toNext';
  126. model.change( writer => {
  127. const sourcePosition = new Position( root, [ 1, 0, 1 ] );
  128. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 3 );
  129. const targetPosition = new Position( root, [ 1, 1, 3 ] );
  130. writer.move( sourceRange, targetPosition );
  131. } );
  132. expect( live.path ).to.deep.equal( [ 1, 1, 6 ] );
  133. expect( spy.calledOnce ).to.be.true;
  134. } );
  135. it( 'is at a position before a node from the live position path', () => {
  136. model.change( writer => {
  137. const sourcePosition = new Position( root, [ 1, 0, 1 ] );
  138. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 2 );
  139. const targetPosition = new Position( root, [ 1, 0 ] );
  140. writer.move( sourceRange, targetPosition );
  141. } );
  142. expect( live.path ).to.deep.equal( [ 1, 3, 3 ] );
  143. expect( spy.calledOnce ).to.be.true;
  144. } );
  145. it( 'is from the same parent and closer offset', () => {
  146. model.change( writer => {
  147. const sourcePosition = new Position( root, [ 1, 1, 0 ] );
  148. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 2 );
  149. const targetPosition = new Position( root, [ 1, 0, 0 ] );
  150. writer.move( sourceRange, targetPosition );
  151. } );
  152. expect( live.path ).to.deep.equal( [ 1, 1, 1 ] );
  153. expect( spy.calledOnce ).to.be.true;
  154. } );
  155. it( 'is from a position before a node from the live position path', () => {
  156. model.change( writer => {
  157. const sourcePosition = new Position( root, [ 1, 0 ] );
  158. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 1 );
  159. const targetPosition = new Position( root, [ 1, 2 ] );
  160. writer.move( sourceRange, targetPosition );
  161. } );
  162. expect( live.path ).to.deep.equal( [ 1, 0, 3 ] );
  163. expect( spy.calledOnce ).to.be.true;
  164. } );
  165. it( 'contains live position (same level)', () => {
  166. model.change( writer => {
  167. const sourcePosition = new Position( root, [ 1, 1, 2 ] );
  168. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 2 );
  169. const targetPosition = new Position( root, [ 1, 0, 0 ] );
  170. writer.move( sourceRange, targetPosition );
  171. } );
  172. expect( live.path ).to.deep.equal( [ 1, 0, 1 ] );
  173. expect( spy.calledOnce ).to.be.true;
  174. } );
  175. it( 'contains live position (deep)', () => {
  176. model.change( writer => {
  177. const sourcePosition = new Position( root, [ 1, 1 ] );
  178. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 1 );
  179. const targetPosition = new Position( root, [ 1, 0 ] );
  180. writer.move( sourceRange, targetPosition );
  181. } );
  182. expect( live.path ).to.deep.equal( [ 1, 0, 3 ] );
  183. expect( spy.calledOnce ).to.be.true;
  184. } );
  185. } );
  186. } );
  187. describe( 'should not get transformed if', () => {
  188. let path, otherRoot, spy, live;
  189. beforeEach( () => {
  190. path = [ 1, 1, 3 ];
  191. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  192. live = new LivePosition( root, path );
  193. spy = sinon.spy();
  194. live.on( 'change', spy );
  195. } );
  196. afterEach( () => {
  197. live.detach();
  198. } );
  199. describe( 'insertion', () => {
  200. it( 'is in the same parent and further offset', () => {
  201. model.change( writer => {
  202. writer.insertText( 'foo', new Position( root, [ 1, 1, 6 ] ) );
  203. } );
  204. expect( live.path ).to.deep.equal( path );
  205. expect( spy.called ).to.be.false;
  206. } );
  207. it( 'is at the same position and live position is sticking to left side', () => {
  208. const newLive = new LivePosition( root, path, 'toPrevious' );
  209. spy = sinon.spy();
  210. newLive.on( 'change', spy );
  211. model.change( writer => {
  212. writer.insertText( 'foo', new Position( root, [ 1, 1, 3 ] ) );
  213. } );
  214. expect( newLive.path ).to.deep.equal( path );
  215. expect( spy.called ).to.be.false;
  216. newLive.detach();
  217. } );
  218. it( 'is after a node from the position path', () => {
  219. model.change( writer => {
  220. writer.insertElement( 'paragraph', new Position( root, [ 2 ] ) );
  221. } );
  222. expect( live.path ).to.deep.equal( path );
  223. expect( spy.called ).to.be.false;
  224. } );
  225. it( 'is in different root', () => {
  226. model.change( writer => {
  227. writer.insertText( 'foo', new Position( otherRoot, [ 0 ] ) );
  228. } );
  229. expect( live.path ).to.deep.equal( path );
  230. expect( spy.called ).to.be.false;
  231. } );
  232. } );
  233. describe( 'range move', () => {
  234. it( 'is at the same parent and further offset', () => {
  235. model.change( writer => {
  236. const sourcePosition = new Position( root, [ 1, 0, 0 ] );
  237. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 3 );
  238. const targetPosition = new Position( root, [ 1, 1, 6 ] );
  239. writer.move( sourceRange, targetPosition );
  240. } );
  241. expect( live.path ).to.deep.equal( path );
  242. expect( spy.called ).to.be.false;
  243. } );
  244. it( 'is at the same position and live position is sticking to left side', () => {
  245. const newLive = new LivePosition( root, path, 'toPrevious' );
  246. spy = sinon.spy();
  247. newLive.on( 'change', spy );
  248. model.change( writer => {
  249. const sourcePosition = new Position( root, [ 1, 0, 0 ] );
  250. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 3 );
  251. const targetPosition = new Position( root, [ 1, 1, 3 ] );
  252. writer.move( sourceRange, targetPosition );
  253. } );
  254. expect( newLive.path ).to.deep.equal( path );
  255. expect( spy.called ).to.be.false;
  256. newLive.detach();
  257. } );
  258. it( 'is at a position after a node from the live position path', () => {
  259. model.change( writer => {
  260. const sourcePosition = new Position( root, [ 1, 0, 0 ] );
  261. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 3 );
  262. const targetPosition = new Position( root, [ 2 ] );
  263. writer.move( sourceRange, targetPosition );
  264. } );
  265. expect( live.path ).to.deep.equal( path );
  266. expect( spy.called ).to.be.false;
  267. } );
  268. it( 'is from the same parent and further offset', () => {
  269. model.change( writer => {
  270. const sourcePosition = new Position( root, [ 1, 1, 4 ] );
  271. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 2 );
  272. const targetPosition = new Position( otherRoot, [ 0 ] );
  273. writer.move( sourceRange, targetPosition );
  274. } );
  275. expect( live.path ).to.deep.equal( path );
  276. expect( spy.called ).to.be.false;
  277. } );
  278. it( 'is from a position after a node from the live position path', () => {
  279. const newLive = new LivePosition( root, [ 1, 0, 3 ] );
  280. spy = sinon.spy();
  281. newLive.on( 'change', spy );
  282. model.change( writer => {
  283. const sourcePosition = new Position( root, [ 1, 1 ] );
  284. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 1 );
  285. const targetPosition = new Position( otherRoot, [ 0 ] );
  286. writer.move( sourceRange, targetPosition );
  287. } );
  288. expect( newLive.path ).to.deep.equal( [ 1, 0, 3 ] );
  289. expect( spy.called ).to.be.false;
  290. newLive.detach();
  291. } );
  292. it( 'is from different root', () => {
  293. model.change( writer => {
  294. writer.insertText( 'foo', new Position( otherRoot, [ 0 ] ) );
  295. const sourcePosition = new Position( otherRoot, [ 0 ] );
  296. const sourceRange = Range._createFromPositionAndShift( sourcePosition, 1 );
  297. const targetPosition = new Position( otherRoot, [ 3 ] );
  298. writer.move( sourceRange, targetPosition );
  299. } );
  300. expect( live.path ).to.deep.equal( path );
  301. expect( spy.called ).to.be.false;
  302. } );
  303. } );
  304. it( 'attributes changed', () => {
  305. model.change( writer => {
  306. writer.setAttribute( 'foo', 'bar', new Range( new Position( root, [ 1, 1, 0 ] ), new Position( root, [ 1, 1, 6 ] ) ) );
  307. } );
  308. expect( live.path ).to.deep.equal( path );
  309. expect( spy.called ).to.be.false;
  310. } );
  311. } );
  312. } );