8
0

liveposition.js 13 KB

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