liveposition.js 13 KB

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