liveposition.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Document from 'ckeditor5/engine/model/document.js';
  7. import DocumentFragment from 'ckeditor5/engine/model/documentfragment.js';
  8. import Element from 'ckeditor5/engine/model/element.js';
  9. import Text from 'ckeditor5/engine/model/text.js';
  10. import Position from 'ckeditor5/engine/model/position.js';
  11. import LivePosition from 'ckeditor5/engine/model/liveposition.js';
  12. import Range from 'ckeditor5/engine/model/range.js';
  13. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  14. describe( 'LivePosition', () => {
  15. let doc, root, ul, p, li1, li2;
  16. before( () => {
  17. doc = new 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. let 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 ] );
  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. let 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. let 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. let 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. let position = LivePosition.createFromParentAndOffset( ul, 0 );
  56. expect( position ).to.be.instanceof( LivePosition );
  57. position.detach();
  58. } );
  59. it( 'createBefore should return LivePosition', () => {
  60. let position = LivePosition.createBefore( ul );
  61. expect( position ).to.be.instanceof( LivePosition );
  62. position.detach();
  63. } );
  64. it( 'createAfter should return LivePosition', () => {
  65. let 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. let 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. let 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. let 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. let moveSource = new Position( root, [ 2 ] );
  102. let moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  103. let 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. let moveSource = new Position( root, [ 2 ] );
  113. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  114. let 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. let moveSource = new Position( root, [ 2 ] );
  124. let moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  125. let 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. let moveSource = new Position( root, [ 1, 4, 0 ] );
  135. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  136. let 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. let moveSource = new Position( root, [ 1, 0 ] );
  146. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  147. let 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. let moveSource = new Position( root, [ 1, 4, 4 ] );
  157. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  158. let 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. let moveSource = new Position( root, [ 1, 3 ] );
  168. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  169. let 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;
  181. before( () => {
  182. path = [ 1, 4, 6 ];
  183. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  184. } );
  185. let live;
  186. beforeEach( () => {
  187. live = new LivePosition( root, path );
  188. spy = sinon.spy();
  189. live.on( 'change', spy );
  190. } );
  191. afterEach( () => {
  192. live.detach();
  193. } );
  194. describe( 'insertion', () => {
  195. it( 'is in the same parent and further offset', () => {
  196. let insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  197. doc.fire( 'change', 'insert', { range: insertRange }, null );
  198. expect( live.path ).to.deep.equal( path );
  199. expect( spy.called ).to.be.false;
  200. } );
  201. it( 'is at the same position and live position is sticking to left side', () => {
  202. let newLive = new LivePosition( root, path, 'sticksToPrevious' );
  203. spy = sinon.spy();
  204. newLive.on( 'change', spy );
  205. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  206. doc.fire( 'change', 'insert', { range: insertRange }, null );
  207. expect( newLive.path ).to.deep.equal( path );
  208. expect( spy.called ).to.be.false;
  209. newLive.detach();
  210. } );
  211. it( 'is after a node from the position path', () => {
  212. let insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  213. doc.fire( 'change', 'insert', { range: insertRange }, null );
  214. expect( live.path ).to.deep.equal( path );
  215. expect( spy.called ).to.be.false;
  216. } );
  217. it( 'is in different root', () => {
  218. let insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
  219. doc.fire( 'change', 'insert', { range: insertRange }, null );
  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. let moveSource = new Position( root, [ 2 ] );
  227. let moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  228. let changes = {
  229. range: moveRange,
  230. sourcePosition: moveSource
  231. };
  232. doc.fire( 'change', 'move', changes, null );
  233. expect( live.path ).to.deep.equal( path );
  234. expect( spy.called ).to.be.false;
  235. } );
  236. it( 'is at the same position and live position is sticking to left side', () => {
  237. let newLive = new LivePosition( root, path, 'sticksToPrevious' );
  238. spy = sinon.spy();
  239. newLive.on( 'change', spy );
  240. let moveSource = new Position( root, [ 2 ] );
  241. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  242. let changes = {
  243. range: moveRange,
  244. sourcePosition: moveSource
  245. };
  246. doc.fire( 'change', 'move', changes, null );
  247. expect( newLive.path ).to.deep.equal( path );
  248. expect( spy.called ).to.be.false;
  249. newLive.detach();
  250. } );
  251. it( 'is at a position after a node from the live position path', () => {
  252. let moveSource = new Position( root, [ 2 ] );
  253. let moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  254. let changes = {
  255. range: moveRange,
  256. sourcePosition: moveSource
  257. };
  258. doc.fire( 'change', 'move', changes, null );
  259. expect( live.path ).to.deep.equal( path );
  260. expect( spy.called ).to.be.false;
  261. } );
  262. it( 'is from the same parent and further offset', () => {
  263. let moveSource = new Position( root, [ 1, 4, 7 ] );
  264. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  265. let changes = {
  266. range: moveRange,
  267. sourcePosition: moveSource
  268. };
  269. doc.fire( 'change', 'move', changes, null );
  270. expect( live.path ).to.deep.equal( path );
  271. expect( spy.called ).to.be.false;
  272. } );
  273. it( 'is from a position after a node from the live position path', () => {
  274. let moveSource = new Position( root, [ 1, 5 ] );
  275. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  276. let changes = {
  277. range: moveRange,
  278. sourcePosition: moveSource
  279. };
  280. doc.fire( 'change', 'move', changes, null );
  281. expect( live.path ).to.deep.equal( path );
  282. expect( spy.called ).to.be.false;
  283. } );
  284. it( 'is to different root', () => {
  285. let moveSource = new Position( root, [ 2, 0 ] );
  286. let moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  287. let changes = {
  288. range: moveRange,
  289. sourcePosition: moveSource
  290. };
  291. doc.fire( 'change', 'move', changes, null );
  292. expect( live.path ).to.deep.equal( path );
  293. expect( spy.called ).to.be.false;
  294. } );
  295. it( 'is from different root', () => {
  296. let moveSource = new Position( otherRoot, [ 1, 0 ] );
  297. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  298. let changes = {
  299. range: moveRange,
  300. sourcePosition: moveSource
  301. };
  302. doc.fire( 'change', 'move', changes, null );
  303. expect( live.path ).to.deep.equal( path );
  304. expect( spy.called ).to.be.false;
  305. } );
  306. } );
  307. it( 'attributes changed', () => {
  308. let changes = {
  309. range: new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 10 ] ) ),
  310. key: 'foo',
  311. oldValue: null,
  312. newValue: 'bar'
  313. };
  314. doc.fire( 'change', 'setAttribute', changes, null );
  315. expect( live.path ).to.deep.equal( path );
  316. expect( spy.called ).to.be.false;
  317. } );
  318. } );
  319. } );