liveposition.js 12 KB

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