liveposition.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. 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.throwCKEditorError( /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;
  70. beforeEach( () => {
  71. live = new LivePosition( root, [ 1, 4, 6 ] );
  72. } );
  73. afterEach( () => {
  74. live.detach();
  75. } );
  76. describe( 'insertion', () => {
  77. it( 'is in the same parent and closer offset', () => {
  78. let insertRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  79. doc.fire( 'change', 'insert', { range: insertRange }, null );
  80. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  81. } );
  82. it( 'is at the same position and live position is sticking to right side', () => {
  83. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  84. doc.fire( 'change', 'insert', { range: insertRange }, null );
  85. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  86. } );
  87. it( 'is before a node from the live position path', () => {
  88. let insertRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  89. doc.fire( 'change', 'insert', { range: insertRange }, null );
  90. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  91. } );
  92. } );
  93. describe( 'range move', () => {
  94. it( 'is at the same parent and closer offset', () => {
  95. let moveSource = new Position( root, [ 2 ] );
  96. let moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  97. let changes = {
  98. range: moveRange,
  99. sourcePosition: moveSource
  100. };
  101. doc.fire( 'change', 'move', changes, null );
  102. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  103. } );
  104. it( 'is at the same position and live position is sticking to right side', () => {
  105. let moveSource = new Position( root, [ 2 ] );
  106. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  107. let changes = {
  108. range: moveRange,
  109. sourcePosition: moveSource
  110. };
  111. doc.fire( 'change', 'move', changes, null );
  112. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  113. } );
  114. it( 'is at a position before a node from the live position path', () => {
  115. let moveSource = new Position( root, [ 2 ] );
  116. let moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  117. let changes = {
  118. range: moveRange,
  119. sourcePosition: moveSource
  120. };
  121. doc.fire( 'change', 'move', changes, null );
  122. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  123. } );
  124. it( 'is from the same parent and closer offset', () => {
  125. let moveSource = new Position( root, [ 1, 4, 0 ] );
  126. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  127. let changes = {
  128. range: moveRange,
  129. sourcePosition: moveSource
  130. };
  131. doc.fire( 'change', 'move', changes, null );
  132. expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
  133. } );
  134. it( 'is from a position before a node from the live position path', () => {
  135. let moveSource = new Position( root, [ 1, 0 ] );
  136. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  137. let changes = {
  138. range: moveRange,
  139. sourcePosition: moveSource
  140. };
  141. doc.fire( 'change', 'move', changes, null );
  142. expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
  143. } );
  144. it( 'contains live position (same level)', () => {
  145. let moveSource = new Position( root, [ 1, 4, 4 ] );
  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( [ 2, 2 ] );
  153. } );
  154. it( 'contains live position (deep)', () => {
  155. let moveSource = new Position( root, [ 1, 3 ] );
  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, 1, 6 ] );
  163. } );
  164. } );
  165. } );
  166. describe( 'should not get transformed if', () => {
  167. let path, otherRoot;
  168. before( () => {
  169. path = [ 1, 4, 6 ];
  170. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  171. } );
  172. let live;
  173. beforeEach( () => {
  174. live = new LivePosition( root, path );
  175. } );
  176. afterEach( () => {
  177. live.detach();
  178. } );
  179. describe( 'insertion', () => {
  180. it( 'is in the same parent and further offset', () => {
  181. let insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  182. doc.fire( 'change', 'insert', { range: insertRange }, null );
  183. expect( live.path ).to.deep.equal( path );
  184. } );
  185. it( 'is at the same position and live position is sticking to left side', () => {
  186. let live = new LivePosition( root, path, 'STICKS_TO_PREVIOUS' );
  187. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  188. doc.fire( 'change', 'insert', { range: insertRange }, null );
  189. expect( live.path ).to.deep.equal( path );
  190. live.detach();
  191. } );
  192. it( 'is after a node from the position path', () => {
  193. let insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  194. doc.fire( 'change', 'insert', { range: insertRange }, null );
  195. expect( live.path ).to.deep.equal( path );
  196. } );
  197. it( 'is in different root', () => {
  198. let insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
  199. doc.fire( 'change', 'insert', { range: insertRange }, null );
  200. expect( live.path ).to.deep.equal( path );
  201. } );
  202. } );
  203. describe( 'range move', () => {
  204. it( 'is at the same parent and further offset', () => {
  205. let moveSource = new Position( root, [ 2 ] );
  206. let moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  207. let changes = {
  208. range: moveRange,
  209. sourcePosition: moveSource
  210. };
  211. doc.fire( 'change', 'move', changes, null );
  212. expect( live.path ).to.deep.equal( path );
  213. } );
  214. it( 'is at the same position and live position is sticking to left side', () => {
  215. let live = new LivePosition( root, path, 'STICKS_TO_PREVIOUS' );
  216. let moveSource = new Position( root, [ 2 ] );
  217. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  218. let changes = {
  219. range: moveRange,
  220. sourcePosition: moveSource
  221. };
  222. doc.fire( 'change', 'move', changes, null );
  223. expect( live.path ).to.deep.equal( path );
  224. live.detach();
  225. } );
  226. it( 'is at a position after a node from the live position path', () => {
  227. let moveSource = new Position( root, [ 2 ] );
  228. let moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  229. let changes = {
  230. range: moveRange,
  231. sourcePosition: moveSource
  232. };
  233. doc.fire( 'change', 'move', changes, null );
  234. expect( live.path ).to.deep.equal( path );
  235. } );
  236. it( 'is from the same parent and further offset', () => {
  237. let moveSource = new Position( root, [ 1, 4, 7 ] );
  238. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  239. let changes = {
  240. range: moveRange,
  241. sourcePosition: moveSource
  242. };
  243. doc.fire( 'change', 'move', changes, null );
  244. expect( live.path ).to.deep.equal( path );
  245. } );
  246. it( 'is from a position after a node from the live position path', () => {
  247. let moveSource = new Position( root, [ 1, 5 ] );
  248. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  249. let changes = {
  250. range: moveRange,
  251. sourcePosition: moveSource
  252. };
  253. doc.fire( 'change', 'move', changes, null );
  254. expect( live.path ).to.deep.equal( path );
  255. } );
  256. it( 'is to different root', () => {
  257. let moveSource = new Position( root, [ 2, 0 ] );
  258. let moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  259. let changes = {
  260. range: moveRange,
  261. sourcePosition: moveSource
  262. };
  263. doc.fire( 'change', 'move', changes, null );
  264. expect( live.path ).to.deep.equal( path );
  265. } );
  266. it( 'is from different root', () => {
  267. let moveSource = new Position( otherRoot, [ 1, 0 ] );
  268. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  269. let changes = {
  270. range: moveRange,
  271. sourcePosition: moveSource
  272. };
  273. doc.fire( 'change', 'move', changes, null );
  274. expect( live.path ).to.deep.equal( path );
  275. } );
  276. } );
  277. it( 'attributes changed', () => {
  278. let changes = {
  279. range: new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 10 ] ) ),
  280. key: 'foo',
  281. oldValue: null,
  282. newValue: 'bar'
  283. };
  284. doc.fire( 'change', 'setAttribute', changes, null );
  285. expect( live.path ).to.deep.equal( path );
  286. } );
  287. } );
  288. } );