liveposition.js 11 KB

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