liveposition.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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', { range: 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', { range: 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', { range: 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. let changes = {
  110. range: moveRange,
  111. sourcePosition: moveSource
  112. };
  113. doc.fire( 'change', 'move', changes, null );
  114. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  115. } );
  116. it( 'is at the same position and live position is sticking to right side', () => {
  117. let moveSource = new Position( root, [ 2 ] );
  118. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  119. let changes = {
  120. range: moveRange,
  121. sourcePosition: moveSource
  122. };
  123. doc.fire( 'change', 'move', changes, null );
  124. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  125. } );
  126. it( 'is at a position before a node from the live position path', () => {
  127. let moveSource = new Position( root, [ 2 ] );
  128. let moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  129. let changes = {
  130. range: moveRange,
  131. sourcePosition: moveSource
  132. };
  133. doc.fire( 'change', 'move', changes, null );
  134. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  135. } );
  136. it( 'is from the same parent and closer offset', () => {
  137. let moveSource = new Position( root, [ 1, 4, 0 ] );
  138. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  139. let changes = {
  140. range: moveRange,
  141. sourcePosition: moveSource
  142. };
  143. doc.fire( 'change', 'move', changes, null );
  144. expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
  145. } );
  146. it( 'is from a position before a node from the live position path', () => {
  147. let moveSource = new Position( root, [ 1, 0 ] );
  148. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  149. let changes = {
  150. range: moveRange,
  151. sourcePosition: moveSource
  152. };
  153. doc.fire( 'change', 'move', changes, null );
  154. expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
  155. } );
  156. it( 'contains live position (same level)', () => {
  157. let moveSource = new Position( root, [ 1, 4, 4 ] );
  158. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  159. let changes = {
  160. range: moveRange,
  161. sourcePosition: moveSource
  162. };
  163. doc.fire( 'change', 'move', changes, null );
  164. expect( live.path ).to.deep.equal( [ 2, 2 ] );
  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. } );
  176. } );
  177. } );
  178. describe( 'should not get transformed if', () => {
  179. let path, otherRoot;
  180. before( () => {
  181. path = [ 1, 4, 6 ];
  182. otherRoot = doc.createRoot( 'otherRoot' );
  183. } );
  184. let live;
  185. beforeEach( () => {
  186. live = new LivePosition( root, path );
  187. } );
  188. afterEach( () => {
  189. live.detach();
  190. } );
  191. describe( 'insertion', () => {
  192. it( 'is in the same parent and further offset', () => {
  193. let insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  194. doc.fire( 'change', 'insert', { range: insertRange }, null );
  195. expect( live.path ).to.deep.equal( path );
  196. } );
  197. it( 'is at the same position and live position is sticking to left side', () => {
  198. let live = new LivePosition( root, path, true );
  199. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  200. doc.fire( 'change', 'insert', { range: insertRange }, null );
  201. expect( live.path ).to.deep.equal( path );
  202. live.detach();
  203. } );
  204. it( 'is after a node from the position path', () => {
  205. let insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  206. doc.fire( 'change', 'insert', { range: insertRange }, null );
  207. expect( live.path ).to.deep.equal( path );
  208. } );
  209. it( 'is in different root', () => {
  210. let insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
  211. doc.fire( 'change', 'insert', { range: insertRange }, null );
  212. expect( live.path ).to.deep.equal( path );
  213. } );
  214. } );
  215. describe( 'range move', () => {
  216. it( 'is at the same parent and further offset', () => {
  217. let moveSource = new Position( root, [ 2 ] );
  218. let moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  219. let changes = {
  220. range: moveRange,
  221. sourcePosition: moveSource
  222. };
  223. doc.fire( 'change', 'move', changes, null );
  224. expect( live.path ).to.deep.equal( path );
  225. } );
  226. it( 'is at the same position and live position is sticking to left side', () => {
  227. let live = new LivePosition( root, path, true );
  228. let moveSource = new Position( root, [ 2 ] );
  229. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  230. let changes = {
  231. range: moveRange,
  232. sourcePosition: moveSource
  233. };
  234. doc.fire( 'change', 'move', changes, null );
  235. expect( live.path ).to.deep.equal( path );
  236. live.detach();
  237. } );
  238. it( 'is at a position after a node from the live position path', () => {
  239. let moveSource = new Position( root, [ 2 ] );
  240. let moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  241. let changes = {
  242. range: moveRange,
  243. sourcePosition: moveSource
  244. };
  245. doc.fire( 'change', 'move', changes, null );
  246. expect( live.path ).to.deep.equal( path );
  247. } );
  248. it( 'is from the same parent and further offset', () => {
  249. let moveSource = new Position( root, [ 1, 4, 7 ] );
  250. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  251. let changes = {
  252. range: moveRange,
  253. sourcePosition: moveSource
  254. };
  255. doc.fire( 'change', 'move', changes, null );
  256. expect( live.path ).to.deep.equal( path );
  257. } );
  258. it( 'is from a position after a node from the live position path', () => {
  259. let moveSource = new Position( root, [ 1, 5 ] );
  260. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  261. let changes = {
  262. range: moveRange,
  263. sourcePosition: moveSource
  264. };
  265. doc.fire( 'change', 'move', changes, null );
  266. expect( live.path ).to.deep.equal( path );
  267. } );
  268. it( 'is to different root', () => {
  269. let moveSource = new Position( root, [ 2, 0 ] );
  270. let moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  271. let changes = {
  272. range: moveRange,
  273. sourcePosition: moveSource
  274. };
  275. doc.fire( 'change', 'move', changes, null );
  276. expect( live.path ).to.deep.equal( path );
  277. } );
  278. it( 'is from different root', () => {
  279. let moveSource = new Position( otherRoot, [ 1, 0 ] );
  280. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  281. let changes = {
  282. range: moveRange,
  283. sourcePosition: moveSource
  284. };
  285. doc.fire( 'change', 'move', changes, null );
  286. expect( live.path ).to.deep.equal( path );
  287. } );
  288. } );
  289. } );
  290. } );