liveposition.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 doc, root, ul, p, li1, li2;
  15. before( () => {
  16. const 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.insertChildren( 0, [ p, ul ] );
  24. } );
  25. it( 'should be an instance of Position', () => {
  26. const live = new LivePosition( root, [ 0 ] );
  27. live.detach();
  28. expect( live ).to.be.instanceof( Position );
  29. } );
  30. it( 'should throw if given root is not a RootElement', () => {
  31. expect( () => {
  32. new LivePosition( new DocumentFragment(), [ 1 ] ); // eslint-disable-line no-new
  33. } ).to.throw( CKEditorError, /model-liveposition-root-not-rootelement/ );
  34. } );
  35. it( 'should listen to a change event of the document that owns this position root', () => {
  36. sinon.spy( LivePosition.prototype, 'listenTo' );
  37. const live = new LivePosition( root, [ 0 ] );
  38. live.detach();
  39. expect( live.listenTo.calledWith( doc, 'change' ) ).to.be.true;
  40. LivePosition.prototype.listenTo.restore();
  41. } );
  42. it( 'should stop listening when detached', () => {
  43. sinon.spy( LivePosition.prototype, 'stopListening' );
  44. const live = new LivePosition( root, [ 0 ] );
  45. live.detach();
  46. expect( live.stopListening.called ).to.be.true;
  47. LivePosition.prototype.stopListening.restore();
  48. } );
  49. it( 'createFromPosition should return LivePosition', () => {
  50. const position = LivePosition.createFromPosition( new Position( root, [ 0 ] ) );
  51. expect( position ).to.be.instanceof( LivePosition );
  52. position.detach();
  53. } );
  54. it( 'createFromParentAndOffset should return LivePosition', () => {
  55. const position = LivePosition.createFromParentAndOffset( ul, 0 );
  56. expect( position ).to.be.instanceof( LivePosition );
  57. position.detach();
  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, 4, 6 ] );
  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. const insertRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  82. doc.fire( 'change', 'insert', { range: insertRange }, null );
  83. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  84. expect( spy.calledOnce ).to.be.true;
  85. } );
  86. it( 'is at the same position and live position is sticking to right side', () => {
  87. const insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  88. doc.fire( 'change', 'insert', { range: insertRange }, null );
  89. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  90. expect( spy.calledOnce ).to.be.true;
  91. } );
  92. it( 'is before a node from the live position path', () => {
  93. const insertRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  94. doc.fire( 'change', 'insert', { range: insertRange }, null );
  95. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  96. expect( spy.calledOnce ).to.be.true;
  97. } );
  98. } );
  99. describe( 'range move', () => {
  100. it( 'is at the same parent and closer offset', () => {
  101. const moveSource = new Position( root, [ 2 ] );
  102. const moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  103. const changes = {
  104. range: moveRange,
  105. sourcePosition: moveSource
  106. };
  107. doc.fire( 'change', 'move', changes, null );
  108. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  109. expect( spy.calledOnce ).to.be.true;
  110. } );
  111. it( 'is at the same position and live position is sticking to right side', () => {
  112. const moveSource = new Position( root, [ 2 ] );
  113. const moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  114. const changes = {
  115. range: moveRange,
  116. sourcePosition: moveSource
  117. };
  118. doc.fire( 'change', 'move', changes, null );
  119. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  120. expect( spy.calledOnce ).to.be.true;
  121. } );
  122. it( 'is at a position before a node from the live position path', () => {
  123. const moveSource = new Position( root, [ 2 ] );
  124. const moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  125. const changes = {
  126. range: moveRange,
  127. sourcePosition: moveSource
  128. };
  129. doc.fire( 'change', 'move', changes, null );
  130. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  131. expect( spy.calledOnce ).to.be.true;
  132. } );
  133. it( 'is from the same parent and closer offset', () => {
  134. const moveSource = new Position( root, [ 1, 4, 0 ] );
  135. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  136. const changes = {
  137. range: moveRange,
  138. sourcePosition: moveSource
  139. };
  140. doc.fire( 'change', 'move', changes, null );
  141. expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
  142. expect( spy.calledOnce ).to.be.true;
  143. } );
  144. it( 'is from a position before a node from the live position path', () => {
  145. const moveSource = new Position( root, [ 1, 0 ] );
  146. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  147. const changes = {
  148. range: moveRange,
  149. sourcePosition: moveSource
  150. };
  151. doc.fire( 'change', 'move', changes, null );
  152. expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
  153. expect( spy.calledOnce ).to.be.true;
  154. } );
  155. it( 'contains live position (same level)', () => {
  156. const moveSource = new Position( root, [ 1, 4, 4 ] );
  157. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  158. const changes = {
  159. range: moveRange,
  160. sourcePosition: moveSource
  161. };
  162. doc.fire( 'change', 'move', changes, null );
  163. expect( live.path ).to.deep.equal( [ 2, 2 ] );
  164. expect( spy.calledOnce ).to.be.true;
  165. } );
  166. it( 'contains live position (deep)', () => {
  167. const moveSource = new Position( root, [ 1, 3 ] );
  168. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  169. const changes = {
  170. range: moveRange,
  171. sourcePosition: moveSource
  172. };
  173. doc.fire( 'change', 'move', changes, null );
  174. expect( live.path ).to.deep.equal( [ 2, 1, 6 ] );
  175. expect( spy.calledOnce ).to.be.true;
  176. } );
  177. } );
  178. } );
  179. describe( 'should not get transformed if', () => {
  180. let path, otherRoot, spy, live;
  181. before( () => {
  182. path = [ 1, 4, 6 ];
  183. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  184. } );
  185. beforeEach( () => {
  186. live = new LivePosition( root, path );
  187. spy = sinon.spy();
  188. live.on( 'change', spy );
  189. } );
  190. afterEach( () => {
  191. live.detach();
  192. } );
  193. describe( 'insertion', () => {
  194. it( 'is in the same parent and further offset', () => {
  195. const insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  196. doc.fire( 'change', 'insert', { range: insertRange }, null );
  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, 'sticksToPrevious' );
  202. spy = sinon.spy();
  203. newLive.on( 'change', spy );
  204. const insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  205. doc.fire( 'change', 'insert', { range: insertRange }, null );
  206. expect( newLive.path ).to.deep.equal( path );
  207. expect( spy.called ).to.be.false;
  208. newLive.detach();
  209. } );
  210. it( 'is after a node from the position path', () => {
  211. const insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  212. doc.fire( 'change', 'insert', { range: insertRange }, null );
  213. expect( live.path ).to.deep.equal( path );
  214. expect( spy.called ).to.be.false;
  215. } );
  216. it( 'is in different root', () => {
  217. const insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
  218. doc.fire( 'change', 'insert', { range: insertRange }, null );
  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. const moveSource = new Position( root, [ 2 ] );
  226. const moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  227. const changes = {
  228. range: moveRange,
  229. sourcePosition: moveSource
  230. };
  231. doc.fire( 'change', 'move', changes, null );
  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. const moveSource = new Position( root, [ 2 ] );
  240. const moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  241. const changes = {
  242. range: moveRange,
  243. sourcePosition: moveSource
  244. };
  245. doc.fire( 'change', 'move', changes, null );
  246. expect( newLive.path ).to.deep.equal( path );
  247. expect( spy.called ).to.be.false;
  248. newLive.detach();
  249. } );
  250. it( 'is at a position after a node from the live position path', () => {
  251. const moveSource = new Position( root, [ 2 ] );
  252. const moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  253. const changes = {
  254. range: moveRange,
  255. sourcePosition: moveSource
  256. };
  257. doc.fire( 'change', 'move', changes, null );
  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. const moveSource = new Position( root, [ 1, 4, 7 ] );
  263. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  264. const changes = {
  265. range: moveRange,
  266. sourcePosition: moveSource
  267. };
  268. doc.fire( 'change', 'move', changes, null );
  269. expect( live.path ).to.deep.equal( path );
  270. expect( spy.called ).to.be.false;
  271. } );
  272. it( 'is from a position after a node from the live position path', () => {
  273. const moveSource = new Position( root, [ 1, 5 ] );
  274. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  275. const changes = {
  276. range: moveRange,
  277. sourcePosition: moveSource
  278. };
  279. doc.fire( 'change', 'move', changes, null );
  280. expect( live.path ).to.deep.equal( path );
  281. expect( spy.called ).to.be.false;
  282. } );
  283. it( 'is to different root', () => {
  284. const moveSource = new Position( root, [ 2, 0 ] );
  285. const moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  286. const changes = {
  287. range: moveRange,
  288. sourcePosition: moveSource
  289. };
  290. doc.fire( 'change', 'move', changes, null );
  291. expect( live.path ).to.deep.equal( path );
  292. expect( spy.called ).to.be.false;
  293. } );
  294. it( 'is from different root', () => {
  295. const moveSource = new Position( otherRoot, [ 1, 0 ] );
  296. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  297. const changes = {
  298. range: moveRange,
  299. sourcePosition: moveSource
  300. };
  301. doc.fire( 'change', 'move', changes, null );
  302. expect( live.path ).to.deep.equal( path );
  303. expect( spy.called ).to.be.false;
  304. } );
  305. } );
  306. it( 'attributes changed', () => {
  307. const changes = {
  308. range: new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 10 ] ) ),
  309. key: 'foo',
  310. oldValue: null,
  311. newValue: 'bar'
  312. };
  313. doc.fire( 'change', 'setAttribute', changes, null );
  314. expect( live.path ).to.deep.equal( path );
  315. expect( spy.called ).to.be.false;
  316. } );
  317. } );
  318. } );