liveposition.js 12 KB

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