8
0

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 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. let 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 ] );
  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. let 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. let 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. let 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. let position = LivePosition.createFromParentAndOffset( ul, 0 );
  55. expect( position ).to.be.instanceof( LivePosition );
  56. position.detach();
  57. } );
  58. it( 'createBefore should return LivePosition', () => {
  59. let position = LivePosition.createBefore( ul );
  60. expect( position ).to.be.instanceof( LivePosition );
  61. position.detach();
  62. } );
  63. it( 'createAfter should return LivePosition', () => {
  64. let 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. let 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. let 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. let 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. let moveSource = new Position( root, [ 2 ] );
  101. let moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  102. let 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. let moveSource = new Position( root, [ 2 ] );
  112. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  113. let 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. let moveSource = new Position( root, [ 2 ] );
  123. let moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  124. let 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. let moveSource = new Position( root, [ 1, 4, 0 ] );
  134. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  135. let 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. let moveSource = new Position( root, [ 1, 0 ] );
  145. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  146. let 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. let moveSource = new Position( root, [ 1, 4, 4 ] );
  156. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  157. let 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. let moveSource = new Position( root, [ 1, 3 ] );
  167. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  168. let 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;
  180. before( () => {
  181. path = [ 1, 4, 6 ];
  182. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  183. } );
  184. let live;
  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. let 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. let newLive = new LivePosition( root, path, 'sticksToPrevious' );
  202. spy = sinon.spy();
  203. newLive.on( 'change', spy );
  204. let 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. let 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. let 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. let moveSource = new Position( root, [ 2 ] );
  226. let moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  227. let 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. let newLive = new LivePosition( root, path, 'sticksToPrevious' );
  237. spy = sinon.spy();
  238. newLive.on( 'change', spy );
  239. let moveSource = new Position( root, [ 2 ] );
  240. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  241. let 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. let moveSource = new Position( root, [ 2 ] );
  252. let moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  253. let 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. let moveSource = new Position( root, [ 1, 4, 7 ] );
  263. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  264. let 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. let moveSource = new Position( root, [ 1, 5 ] );
  274. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  275. let 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. let moveSource = new Position( root, [ 2, 0 ] );
  285. let moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  286. let 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. let moveSource = new Position( otherRoot, [ 1, 0 ] );
  296. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  297. let 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. let 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. } );