liveposition.js 12 KB

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