liveposition.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 right side', () => {
  91. model.change( writer => {
  92. writer.insertText( 'foo', new Position( root, [ 1, 1, 3 ] ) );
  93. } );
  94. expect( live.path ).to.deep.equal( [ 1, 1, 6 ] );
  95. expect( spy.calledOnce ).to.be.true;
  96. } );
  97. it( 'is before a node from the live position path', () => {
  98. model.change( writer => {
  99. writer.insert( new Element( 'paragraph' ), new Position( root, [ 1, 0 ] ) );
  100. } );
  101. expect( live.path ).to.deep.equal( [ 1, 2, 3 ] );
  102. expect( spy.calledOnce ).to.be.true;
  103. } );
  104. } );
  105. describe( 'range move', () => {
  106. it( 'is at the same parent and closer offset', () => {
  107. model.change( writer => {
  108. const sourcePosition = new Position( root, [ 1, 0, 1 ] );
  109. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  110. const targetPosition = new Position( root, [ 1, 1, 0 ] );
  111. writer.move( sourceRange, targetPosition );
  112. } );
  113. expect( live.path ).to.deep.equal( [ 1, 1, 6 ] );
  114. expect( spy.calledOnce ).to.be.true;
  115. } );
  116. it( 'is at the same position and live position is sticking to right side', () => {
  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, 'sticksToPrevious' );
  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, 'sticksToPrevious' );
  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. } );