8
0

liveposition.js 12 KB

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