8
0

liveposition.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import Text from '/ckeditor5/engine/model/text.js';
  11. import Position from '/ckeditor5/engine/model/position.js';
  12. import LivePosition from '/ckeditor5/engine/model/liveposition.js';
  13. import Range from '/ckeditor5/engine/model/range.js';
  14. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  15. describe( 'LivePosition', () => {
  16. let doc, root, ul, p, li1, li2;
  17. before( () => {
  18. doc = new Document();
  19. root = doc.createRoot();
  20. li1 = new Element( 'li', [], new Text( 'abcdef' ) );
  21. li2 = new Element( 'li', [], new Text( 'foobar' ) );
  22. ul = new Element( 'ul', [], [ li1, li2 ] );
  23. p = new Element( 'p', [], new Text( 'qwerty' ) );
  24. root.insertChildren( 0, [ p, ul ] );
  25. } );
  26. it( 'should be an instance of Position', () => {
  27. let live = new LivePosition( root, [ 0 ] );
  28. live.detach();
  29. expect( live ).to.be.instanceof( Position );
  30. } );
  31. it( 'should throw if given root is not a RootElement', () => {
  32. expect( () => {
  33. new LivePosition( new DocumentFragment(), [ 1 ] );
  34. } ).to.throw( CKEditorError, /liveposition-root-not-rootelement/ );
  35. } );
  36. it( 'should listen to a change event of the document that owns this position root', () => {
  37. sinon.spy( LivePosition.prototype, 'listenTo' );
  38. let live = new LivePosition( root, [ 0 ] );
  39. live.detach();
  40. expect( live.listenTo.calledWith( doc, 'change' ) ).to.be.true;
  41. LivePosition.prototype.listenTo.restore();
  42. } );
  43. it( 'should stop listening when detached', () => {
  44. sinon.spy( LivePosition.prototype, 'stopListening' );
  45. let live = new LivePosition( root, [ 0 ] );
  46. live.detach();
  47. expect( live.stopListening.called ).to.be.true;
  48. LivePosition.prototype.stopListening.restore();
  49. } );
  50. it( 'createFromPosition should return LivePosition', () => {
  51. let position = LivePosition.createFromPosition( new Position( root, [ 0 ] ) );
  52. expect( position ).to.be.instanceof( LivePosition );
  53. position.detach();
  54. } );
  55. it( 'createFromParentAndOffset should return LivePosition', () => {
  56. let position = LivePosition.createFromParentAndOffset( ul, 0 );
  57. expect( position ).to.be.instanceof( LivePosition );
  58. position.detach();
  59. } );
  60. it( 'createBefore should return LivePosition', () => {
  61. let position = LivePosition.createBefore( ul );
  62. expect( position ).to.be.instanceof( LivePosition );
  63. position.detach();
  64. } );
  65. it( 'createAfter should return LivePosition', () => {
  66. let position = LivePosition.createAfter( ul );
  67. expect( position ).to.be.instanceof( LivePosition );
  68. position.detach();
  69. } );
  70. describe( 'should get transformed if', () => {
  71. let live;
  72. beforeEach( () => {
  73. live = new LivePosition( root, [ 1, 4, 6 ] );
  74. } );
  75. afterEach( () => {
  76. live.detach();
  77. } );
  78. describe( 'insertion', () => {
  79. it( 'is in the same parent and closer offset', () => {
  80. let insertRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  81. doc.fire( 'change', 'insert', { range: insertRange }, null );
  82. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  83. } );
  84. it( 'is at the same position and live position is sticking to right side', () => {
  85. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  86. doc.fire( 'change', 'insert', { range: insertRange }, null );
  87. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  88. } );
  89. it( 'is before a node from the live position path', () => {
  90. let insertRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  91. doc.fire( 'change', 'insert', { range: insertRange }, null );
  92. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  93. } );
  94. } );
  95. describe( 'range move', () => {
  96. it( 'is at the same parent and closer offset', () => {
  97. let moveSource = new Position( root, [ 2 ] );
  98. let moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  99. let changes = {
  100. range: moveRange,
  101. sourcePosition: moveSource
  102. };
  103. doc.fire( 'change', 'move', changes, null );
  104. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  105. } );
  106. it( 'is at the same position and live position is sticking to right side', () => {
  107. let moveSource = new Position( root, [ 2 ] );
  108. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  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 a position before a node from the live position path', () => {
  117. let moveSource = new Position( root, [ 2 ] );
  118. let moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  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, 6, 6 ] );
  125. } );
  126. it( 'is from the same parent and closer offset', () => {
  127. let moveSource = new Position( root, [ 1, 4, 0 ] );
  128. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  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, 4, 2 ] );
  135. } );
  136. it( 'is from a position before a node from the live position path', () => {
  137. let moveSource = new Position( root, [ 1, 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, 0, 6 ] );
  145. } );
  146. it( 'contains live position (same level)', () => {
  147. let moveSource = new Position( root, [ 1, 4, 4 ] );
  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( [ 2, 2 ] );
  155. } );
  156. it( 'contains live position (deep)', () => {
  157. let moveSource = new Position( root, [ 1, 3 ] );
  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, 1, 6 ] );
  165. } );
  166. } );
  167. } );
  168. describe( 'should not get transformed if', () => {
  169. let path, otherRoot;
  170. before( () => {
  171. path = [ 1, 4, 6 ];
  172. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  173. } );
  174. let live;
  175. beforeEach( () => {
  176. live = new LivePosition( root, path );
  177. } );
  178. afterEach( () => {
  179. live.detach();
  180. } );
  181. describe( 'insertion', () => {
  182. it( 'is in the same parent and further offset', () => {
  183. let insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  184. doc.fire( 'change', 'insert', { range: insertRange }, null );
  185. expect( live.path ).to.deep.equal( path );
  186. } );
  187. it( 'is at the same position and live position is sticking to left side', () => {
  188. let live = new LivePosition( root, path, 'STICKS_TO_PREVIOUS' );
  189. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  190. doc.fire( 'change', 'insert', { range: insertRange }, null );
  191. expect( live.path ).to.deep.equal( path );
  192. live.detach();
  193. } );
  194. it( 'is after a node from the position path', () => {
  195. let insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  196. doc.fire( 'change', 'insert', { range: insertRange }, null );
  197. expect( live.path ).to.deep.equal( path );
  198. } );
  199. it( 'is in different root', () => {
  200. let insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
  201. doc.fire( 'change', 'insert', { range: insertRange }, null );
  202. expect( live.path ).to.deep.equal( path );
  203. } );
  204. } );
  205. describe( 'range move', () => {
  206. it( 'is at the same parent and further offset', () => {
  207. let moveSource = new Position( root, [ 2 ] );
  208. let moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  209. let changes = {
  210. range: moveRange,
  211. sourcePosition: moveSource
  212. };
  213. doc.fire( 'change', 'move', changes, null );
  214. expect( live.path ).to.deep.equal( path );
  215. } );
  216. it( 'is at the same position and live position is sticking to left side', () => {
  217. let live = new LivePosition( root, path, 'STICKS_TO_PREVIOUS' );
  218. let moveSource = new Position( root, [ 2 ] );
  219. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  220. let changes = {
  221. range: moveRange,
  222. sourcePosition: moveSource
  223. };
  224. doc.fire( 'change', 'move', changes, null );
  225. expect( live.path ).to.deep.equal( path );
  226. live.detach();
  227. } );
  228. it( 'is at a position after a node from the live position path', () => {
  229. let moveSource = new Position( root, [ 2 ] );
  230. let moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  231. let changes = {
  232. range: moveRange,
  233. sourcePosition: moveSource
  234. };
  235. doc.fire( 'change', 'move', changes, null );
  236. expect( live.path ).to.deep.equal( path );
  237. } );
  238. it( 'is from the same parent and further offset', () => {
  239. let moveSource = new Position( root, [ 1, 4, 7 ] );
  240. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  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 a position after a node from the live position path', () => {
  249. let moveSource = new Position( root, [ 1, 5 ] );
  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 to different root', () => {
  259. let moveSource = new Position( root, [ 2, 0 ] );
  260. let moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 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 from different root', () => {
  269. let moveSource = new Position( otherRoot, [ 1, 0 ] );
  270. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 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. } );
  279. it( 'attributes changed', () => {
  280. let changes = {
  281. range: new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 10 ] ) ),
  282. key: 'foo',
  283. oldValue: null,
  284. newValue: 'bar'
  285. };
  286. doc.fire( 'change', 'setAttribute', changes, null );
  287. expect( live.path ).to.deep.equal( path );
  288. } );
  289. } );
  290. } );