8
0

liveposition.js 12 KB

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