liveposition.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. const modules = bender.amd.require(
  8. 'treemodel/document',
  9. 'treemodel/element',
  10. 'treemodel/position',
  11. 'treemodel/liveposition',
  12. 'treemodel/range',
  13. 'emittermixin'
  14. );
  15. describe( 'LivePosition', () => {
  16. let Document, Element, Position, LivePosition, Range, EmitterMixin;
  17. let doc, root, ul, p, li1, li2;
  18. before( () => {
  19. Document = modules[ 'treemodel/document' ];
  20. Element = modules[ 'treemodel/element' ];
  21. Position = modules[ 'treemodel/position' ];
  22. LivePosition = modules[ 'treemodel/liveposition' ];
  23. Range = modules[ 'treemodel/range' ];
  24. EmitterMixin = modules.emittermixin;
  25. doc = new Document();
  26. root = doc.createRoot( 'root' );
  27. li1 = new Element( 'li', [], 'abcdef' );
  28. li2 = new Element( 'li', [], 'foobar' );
  29. ul = new Element( 'ul', [], [ li1, li2 ] );
  30. p = new Element( 'p', [], 'qwerty' );
  31. root.insertChildren( 0, [ p, ul ] );
  32. } );
  33. it( 'should be an instance of Position', () => {
  34. let live = new LivePosition( root, [ 0 ] );
  35. live.detach();
  36. expect( live ).to.be.instanceof( Position );
  37. } );
  38. it( 'should return instance of Position when cloned', () => {
  39. let live = new LivePosition( root, [ 0 ] );
  40. let clone = live.clone();
  41. expect( clone ).to.be.instanceof( Position );
  42. live.detach();
  43. } );
  44. it( 'should return instance of LivePosition when cloned with flag set to true', () => {
  45. let live = new LivePosition( root, [ 0 ] );
  46. let clone = live.clone( true );
  47. expect( clone ).to.be.instanceof( LivePosition );
  48. live.detach();
  49. clone.detach();
  50. } );
  51. it( 'should listen to a change event of the document that owns this position root', () => {
  52. sinon.spy( LivePosition.prototype, 'listenTo' );
  53. let live = new LivePosition( root, [ 0 ] );
  54. live.detach();
  55. expect( live.listenTo.calledWith( doc, 'change' ) ).to.be.true;
  56. LivePosition.prototype.listenTo.restore();
  57. } );
  58. it( 'should stop listening when detached', () => {
  59. sinon.spy( LivePosition.prototype, 'stopListening' );
  60. let live = new LivePosition( root, [ 0 ] );
  61. live.detach();
  62. expect( live.stopListening.called ).to.be.true;
  63. LivePosition.prototype.stopListening.restore();
  64. } );
  65. it( 'createFromParentAndOffset should return LivePosition', () => {
  66. let position = LivePosition.createFromParentAndOffset( ul, 0 );
  67. expect( position ).to.be.instanceof( LivePosition );
  68. position.detach();
  69. } );
  70. it( 'createBefore should return LivePosition', () => {
  71. let position = LivePosition.createBefore( ul );
  72. expect( position ).to.be.instanceof( LivePosition );
  73. position.detach();
  74. } );
  75. it( 'createAfter should return LivePosition', () => {
  76. let position = LivePosition.createAfter( ul );
  77. expect( position ).to.be.instanceof( LivePosition );
  78. position.detach();
  79. } );
  80. describe( 'should get transformed if', () => {
  81. let live;
  82. beforeEach( () => {
  83. live = new LivePosition( root, [ 1, 4, 6 ] );
  84. } );
  85. afterEach( () => {
  86. live.detach();
  87. } );
  88. describe( 'insertion', () => {
  89. it( 'is in the same parent and closer offset', () => {
  90. let insertRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  91. doc.fire( 'change', 'insert', insertRange, null );
  92. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  93. } );
  94. it( 'is at the same position and live position is sticking to right side', () => {
  95. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  96. doc.fire( 'change', 'insert', insertRange, null );
  97. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  98. } );
  99. it( 'is before a node from the live position path', () => {
  100. let insertRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  101. doc.fire( 'change', 'insert', insertRange, null );
  102. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  103. } );
  104. } );
  105. describe( 'range move', () => {
  106. it( 'is at the same parent and closer offset', () => {
  107. let moveSource = new Position( root, [ 2 ] );
  108. let moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  109. doc.fire( 'change', 'move', moveRange, moveSource, null );
  110. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  111. } );
  112. it( 'is at the same position and live position is sticking to right side', () => {
  113. let moveSource = new Position( root, [ 2 ] );
  114. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  115. doc.fire( 'change', 'move', moveRange, moveSource, null );
  116. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  117. } );
  118. it( 'is at a position before a node from the live position path', () => {
  119. let moveSource = new Position( root, [ 2 ] );
  120. let moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  121. doc.fire( 'change', 'move', moveRange, moveSource, 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. doc.fire( 'change', 'move', moveRange, moveSource, null );
  128. expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
  129. } );
  130. it( 'is from a position before a node from the live position path', () => {
  131. let moveSource = new Position( root, [ 1, 0 ] );
  132. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  133. doc.fire( 'change', 'move', moveRange, moveSource, null );
  134. expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
  135. } );
  136. it( 'contains live position (same level)', () => {
  137. let moveSource = new Position( root, [ 1, 4, 4 ] );
  138. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  139. doc.fire( 'change', 'move', moveRange, moveSource, null );
  140. expect( live.path ).to.deep.equal( [ 2, 2 ] );
  141. } );
  142. it( 'contains live position (deep)', () => {
  143. let moveSource = new Position( root, [ 1, 3 ] );
  144. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  145. doc.fire( 'change', 'move', moveRange, moveSource, null );
  146. expect( live.path ).to.deep.equal( [ 2, 1, 6 ] );
  147. } );
  148. } );
  149. } );
  150. describe( 'should not get transformed if', () => {
  151. let path, otherRoot;
  152. before( () => {
  153. path = [ 1, 4, 6 ];
  154. otherRoot = doc.createRoot( 'otherRoot' );
  155. } );
  156. let live;
  157. beforeEach( () => {
  158. live = new LivePosition( root, path );
  159. } );
  160. afterEach( () => {
  161. live.detach();
  162. } );
  163. describe( 'insertion', () => {
  164. it( 'is in the same parent and further offset', () => {
  165. let insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  166. doc.fire( 'change', 'insert', insertRange, null );
  167. expect( live.path ).to.deep.equal( path );
  168. } );
  169. it( 'is at the same position and live position is sticking to left side', () => {
  170. let live = new LivePosition( root, path, true );
  171. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  172. doc.fire( 'change', 'insert', insertRange, null );
  173. expect( live.path ).to.deep.equal( path );
  174. live.detach();
  175. } );
  176. it( 'is after a node from the position path', () => {
  177. let insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  178. doc.fire( 'change', 'insert', insertRange, null );
  179. expect( live.path ).to.deep.equal( path );
  180. } );
  181. it( 'is in different root', () => {
  182. let insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
  183. doc.fire( 'change', 'insert', insertRange, null );
  184. expect( live.path ).to.deep.equal( path );
  185. } );
  186. } );
  187. describe( 'range move', () => {
  188. it( 'is at the same parent and further offset', () => {
  189. let moveSource = new Position( root, [ 2 ] );
  190. let moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  191. doc.fire( 'change', 'move', moveRange, moveSource, null );
  192. expect( live.path ).to.deep.equal( path );
  193. } );
  194. it( 'is at the same position and live position is sticking to left side', () => {
  195. let live = new LivePosition( root, path, true );
  196. let moveSource = new Position( root, [ 2 ] );
  197. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  198. doc.fire( 'change', 'move', moveRange, moveSource, null );
  199. expect( live.path ).to.deep.equal( path );
  200. live.detach();
  201. } );
  202. it( 'is at a position after a node from the live position path', () => {
  203. let moveSource = new Position( root, [ 2 ] );
  204. let moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  205. doc.fire( 'change', 'move', moveRange, moveSource, null );
  206. expect( live.path ).to.deep.equal( path );
  207. } );
  208. it( 'is from the same parent and further offset', () => {
  209. let moveSource = new Position( root, [ 1, 4, 7 ] );
  210. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  211. doc.fire( 'change', 'move', moveRange, moveSource, null );
  212. expect( live.path ).to.deep.equal( path );
  213. } );
  214. it( 'is from a position after a node from the live position path', () => {
  215. let moveSource = new Position( root, [ 1, 5 ] );
  216. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  217. doc.fire( 'change', 'move', moveRange, moveSource, null );
  218. expect( live.path ).to.deep.equal( path );
  219. } );
  220. it( 'is to different root', () => {
  221. let moveSource = new Position( root, [ 2, 0 ] );
  222. let moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  223. doc.fire( 'change', 'move', moveRange, moveSource, null );
  224. expect( live.path ).to.deep.equal( path );
  225. } );
  226. it( 'is from different root', () => {
  227. let moveSource = new Position( otherRoot, [ 1, 0 ] );
  228. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  229. doc.fire( 'change', 'move', moveRange, moveSource, null );
  230. expect( live.path ).to.deep.equal( path );
  231. } );
  232. } );
  233. } );
  234. } );