liveposition.js 12 KB

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