liveposition.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  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. doc = new Document();
  17. root = doc.createRoot();
  18. li1 = new Element( 'li', [], new Text( 'abcdef' ) );
  19. li2 = new Element( 'li', [], new Text( 'foobar' ) );
  20. ul = new Element( 'ul', [], [ li1, li2 ] );
  21. p = new Element( 'p', [], new Text( 'qwerty' ) );
  22. root.insertChildren( 0, [ p, ul ] );
  23. } );
  24. it( 'should be an instance of Position', () => {
  25. const live = new LivePosition( root, [ 0 ] );
  26. live.detach();
  27. expect( live ).to.be.instanceof( Position );
  28. } );
  29. it( 'should throw if given root is not a RootElement', () => {
  30. expect( () => {
  31. new LivePosition( new DocumentFragment(), [ 1 ] ); // eslint-disable-line no-new
  32. } ).to.throw( CKEditorError, /model-liveposition-root-not-rootelement/ );
  33. } );
  34. it( 'should listen to a change event of the document that owns this position root', () => {
  35. sinon.spy( LivePosition.prototype, 'listenTo' );
  36. const live = new LivePosition( root, [ 0 ] );
  37. live.detach();
  38. expect( live.listenTo.calledWith( doc, 'change' ) ).to.be.true;
  39. LivePosition.prototype.listenTo.restore();
  40. } );
  41. it( 'should stop listening when detached', () => {
  42. sinon.spy( LivePosition.prototype, 'stopListening' );
  43. const live = new LivePosition( root, [ 0 ] );
  44. live.detach();
  45. expect( live.stopListening.called ).to.be.true;
  46. LivePosition.prototype.stopListening.restore();
  47. } );
  48. it( 'createFromPosition should return LivePosition', () => {
  49. const position = LivePosition.createFromPosition( new Position( root, [ 0 ] ) );
  50. expect( position ).to.be.instanceof( LivePosition );
  51. position.detach();
  52. } );
  53. it( 'createFromParentAndOffset should return LivePosition', () => {
  54. const position = LivePosition.createFromParentAndOffset( ul, 0 );
  55. expect( position ).to.be.instanceof( LivePosition );
  56. position.detach();
  57. } );
  58. it( 'createBefore should return LivePosition', () => {
  59. const position = LivePosition.createBefore( ul );
  60. expect( position ).to.be.instanceof( LivePosition );
  61. position.detach();
  62. } );
  63. it( 'createAfter should return LivePosition', () => {
  64. const position = LivePosition.createAfter( ul );
  65. expect( position ).to.be.instanceof( LivePosition );
  66. position.detach();
  67. } );
  68. describe( 'should get transformed if', () => {
  69. let live, spy;
  70. beforeEach( () => {
  71. live = new LivePosition( root, [ 1, 4, 6 ] );
  72. spy = sinon.spy();
  73. live.on( 'change', spy );
  74. } );
  75. afterEach( () => {
  76. live.detach();
  77. } );
  78. describe( 'insertion', () => {
  79. it( 'is in the same parent and closer offset', () => {
  80. const insertRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  81. doc.fire( 'change', 'insert', { range: insertRange }, null );
  82. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  83. expect( spy.calledOnce ).to.be.true;
  84. } );
  85. it( 'is at the same position and live position is sticking to right side', () => {
  86. const insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  87. doc.fire( 'change', 'insert', { range: insertRange }, null );
  88. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  89. expect( spy.calledOnce ).to.be.true;
  90. } );
  91. it( 'is before a node from the live position path', () => {
  92. const insertRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  93. doc.fire( 'change', 'insert', { range: insertRange }, null );
  94. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  95. expect( spy.calledOnce ).to.be.true;
  96. } );
  97. } );
  98. describe( 'range move', () => {
  99. it( 'is at the same parent and closer offset', () => {
  100. const moveSource = new Position( root, [ 2 ] );
  101. const moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  102. const changes = {
  103. range: moveRange,
  104. sourcePosition: moveSource
  105. };
  106. doc.fire( 'change', 'move', changes, null );
  107. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  108. expect( spy.calledOnce ).to.be.true;
  109. } );
  110. it( 'is at the same position and live position is sticking to right side', () => {
  111. const moveSource = new Position( root, [ 2 ] );
  112. const moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  113. const changes = {
  114. range: moveRange,
  115. sourcePosition: moveSource
  116. };
  117. doc.fire( 'change', 'move', changes, null );
  118. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  119. expect( spy.calledOnce ).to.be.true;
  120. } );
  121. it( 'is at a position before a node from the live position path', () => {
  122. const moveSource = new Position( root, [ 2 ] );
  123. const moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  124. const changes = {
  125. range: moveRange,
  126. sourcePosition: moveSource
  127. };
  128. doc.fire( 'change', 'move', changes, null );
  129. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  130. expect( spy.calledOnce ).to.be.true;
  131. } );
  132. it( 'is from the same parent and closer offset', () => {
  133. const moveSource = new Position( root, [ 1, 4, 0 ] );
  134. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  135. const changes = {
  136. range: moveRange,
  137. sourcePosition: moveSource
  138. };
  139. doc.fire( 'change', 'move', changes, null );
  140. expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
  141. expect( spy.calledOnce ).to.be.true;
  142. } );
  143. it( 'is from a position before a node from the live position path', () => {
  144. const moveSource = new Position( root, [ 1, 0 ] );
  145. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  146. const changes = {
  147. range: moveRange,
  148. sourcePosition: moveSource
  149. };
  150. doc.fire( 'change', 'move', changes, null );
  151. expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
  152. expect( spy.calledOnce ).to.be.true;
  153. } );
  154. it( 'contains live position (same level)', () => {
  155. const moveSource = new Position( root, [ 1, 4, 4 ] );
  156. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  157. const changes = {
  158. range: moveRange,
  159. sourcePosition: moveSource
  160. };
  161. doc.fire( 'change', 'move', changes, null );
  162. expect( live.path ).to.deep.equal( [ 2, 2 ] );
  163. expect( spy.calledOnce ).to.be.true;
  164. } );
  165. it( 'contains live position (deep)', () => {
  166. const moveSource = new Position( root, [ 1, 3 ] );
  167. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  168. const changes = {
  169. range: moveRange,
  170. sourcePosition: moveSource
  171. };
  172. doc.fire( 'change', 'move', changes, null );
  173. expect( live.path ).to.deep.equal( [ 2, 1, 6 ] );
  174. expect( spy.calledOnce ).to.be.true;
  175. } );
  176. } );
  177. } );
  178. describe( 'should not get transformed if', () => {
  179. let path, otherRoot, spy, live;
  180. before( () => {
  181. path = [ 1, 4, 6 ];
  182. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  183. } );
  184. beforeEach( () => {
  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. const insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  195. doc.fire( 'change', 'insert', { range: insertRange }, null );
  196. expect( live.path ).to.deep.equal( path );
  197. expect( spy.called ).to.be.false;
  198. } );
  199. it( 'is at the same position and live position is sticking to left side', () => {
  200. const newLive = new LivePosition( root, path, 'sticksToPrevious' );
  201. spy = sinon.spy();
  202. newLive.on( 'change', spy );
  203. const insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  204. doc.fire( 'change', 'insert', { range: insertRange }, null );
  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. const insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  211. doc.fire( 'change', 'insert', { range: insertRange }, null );
  212. expect( live.path ).to.deep.equal( path );
  213. expect( spy.called ).to.be.false;
  214. } );
  215. it( 'is in different root', () => {
  216. const insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
  217. doc.fire( 'change', 'insert', { range: insertRange }, null );
  218. expect( live.path ).to.deep.equal( path );
  219. expect( spy.called ).to.be.false;
  220. } );
  221. } );
  222. describe( 'range move', () => {
  223. it( 'is at the same parent and further offset', () => {
  224. const moveSource = new Position( root, [ 2 ] );
  225. const moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  226. const changes = {
  227. range: moveRange,
  228. sourcePosition: moveSource
  229. };
  230. doc.fire( 'change', 'move', changes, null );
  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, 'sticksToPrevious' );
  236. spy = sinon.spy();
  237. newLive.on( 'change', spy );
  238. const moveSource = new Position( root, [ 2 ] );
  239. const moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  240. const changes = {
  241. range: moveRange,
  242. sourcePosition: moveSource
  243. };
  244. doc.fire( 'change', 'move', changes, null );
  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. const moveSource = new Position( root, [ 2 ] );
  251. const moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  252. const changes = {
  253. range: moveRange,
  254. sourcePosition: moveSource
  255. };
  256. doc.fire( 'change', 'move', changes, null );
  257. expect( live.path ).to.deep.equal( path );
  258. expect( spy.called ).to.be.false;
  259. } );
  260. it( 'is from the same parent and further offset', () => {
  261. const moveSource = new Position( root, [ 1, 4, 7 ] );
  262. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  263. const changes = {
  264. range: moveRange,
  265. sourcePosition: moveSource
  266. };
  267. doc.fire( 'change', 'move', changes, null );
  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 moveSource = new Position( root, [ 1, 5 ] );
  273. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  274. const changes = {
  275. range: moveRange,
  276. sourcePosition: moveSource
  277. };
  278. doc.fire( 'change', 'move', changes, null );
  279. expect( live.path ).to.deep.equal( path );
  280. expect( spy.called ).to.be.false;
  281. } );
  282. it( 'is to different root', () => {
  283. const moveSource = new Position( root, [ 2, 0 ] );
  284. const moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  285. const changes = {
  286. range: moveRange,
  287. sourcePosition: moveSource
  288. };
  289. doc.fire( 'change', 'move', changes, null );
  290. expect( live.path ).to.deep.equal( path );
  291. expect( spy.called ).to.be.false;
  292. } );
  293. it( 'is from different root', () => {
  294. const moveSource = new Position( otherRoot, [ 1, 0 ] );
  295. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  296. const changes = {
  297. range: moveRange,
  298. sourcePosition: moveSource
  299. };
  300. doc.fire( 'change', 'move', changes, null );
  301. expect( live.path ).to.deep.equal( path );
  302. expect( spy.called ).to.be.false;
  303. } );
  304. } );
  305. it( 'attributes changed', () => {
  306. const changes = {
  307. range: new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 10 ] ) ),
  308. key: 'foo',
  309. oldValue: null,
  310. newValue: 'bar'
  311. };
  312. doc.fire( 'change', 'setAttribute', changes, null );
  313. expect( live.path ).to.deep.equal( path );
  314. expect( spy.called ).to.be.false;
  315. } );
  316. } );
  317. } );