liveposition.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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, spy;
  71. beforeEach( () => {
  72. live = new LivePosition( root, [ 1, 4, 6 ] );
  73. spy = sinon.spy();
  74. live.on( 'change', spy );
  75. } );
  76. afterEach( () => {
  77. live.detach();
  78. } );
  79. describe( 'insertion', () => {
  80. it( 'is in the same parent and closer offset', () => {
  81. let insertRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  82. doc.fire( 'change', 'insert', { range: insertRange }, null );
  83. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  84. expect( spy.calledOnce ).to.be.true;
  85. } );
  86. it( 'is at the same position and live position is sticking to right side', () => {
  87. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  88. doc.fire( 'change', 'insert', { range: insertRange }, null );
  89. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  90. expect( spy.calledOnce ).to.be.true;
  91. } );
  92. it( 'is before a node from the live position path', () => {
  93. let insertRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  94. doc.fire( 'change', 'insert', { range: insertRange }, null );
  95. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  96. expect( spy.calledOnce ).to.be.true;
  97. } );
  98. } );
  99. describe( 'range move', () => {
  100. it( 'is at the same parent and closer offset', () => {
  101. let moveSource = new Position( root, [ 2 ] );
  102. let moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
  103. let changes = {
  104. range: moveRange,
  105. sourcePosition: moveSource
  106. };
  107. doc.fire( 'change', 'move', changes, null );
  108. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  109. expect( spy.calledOnce ).to.be.true;
  110. } );
  111. it( 'is at the same position and live position is sticking to right side', () => {
  112. let moveSource = new Position( root, [ 2 ] );
  113. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  114. let changes = {
  115. range: moveRange,
  116. sourcePosition: moveSource
  117. };
  118. doc.fire( 'change', 'move', changes, null );
  119. expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
  120. expect( spy.calledOnce ).to.be.true;
  121. } );
  122. it( 'is at a position before a node from the live position path', () => {
  123. let moveSource = new Position( root, [ 2 ] );
  124. let moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  125. let changes = {
  126. range: moveRange,
  127. sourcePosition: moveSource
  128. };
  129. doc.fire( 'change', 'move', changes, null );
  130. expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
  131. expect( spy.calledOnce ).to.be.true;
  132. } );
  133. it( 'is from the same parent and closer offset', () => {
  134. let moveSource = new Position( root, [ 1, 4, 0 ] );
  135. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  136. let changes = {
  137. range: moveRange,
  138. sourcePosition: moveSource
  139. };
  140. doc.fire( 'change', 'move', changes, null );
  141. expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
  142. expect( spy.calledOnce ).to.be.true;
  143. } );
  144. it( 'is from a position before a node from the live position path', () => {
  145. let moveSource = new Position( root, [ 1, 0 ] );
  146. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  147. let changes = {
  148. range: moveRange,
  149. sourcePosition: moveSource
  150. };
  151. doc.fire( 'change', 'move', changes, null );
  152. expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
  153. expect( spy.calledOnce ).to.be.true;
  154. } );
  155. it( 'contains live position (same level)', () => {
  156. let moveSource = new Position( root, [ 1, 4, 4 ] );
  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, 2 ] );
  164. expect( spy.calledOnce ).to.be.true;
  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. expect( spy.calledOnce ).to.be.true;
  176. } );
  177. } );
  178. } );
  179. describe( 'should not get transformed if', () => {
  180. let path, otherRoot, spy;
  181. before( () => {
  182. path = [ 1, 4, 6 ];
  183. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  184. } );
  185. let live;
  186. beforeEach( () => {
  187. live = new LivePosition( root, path );
  188. spy = sinon.spy();
  189. live.on( 'change', spy );
  190. } );
  191. afterEach( () => {
  192. live.detach();
  193. } );
  194. describe( 'insertion', () => {
  195. it( 'is in the same parent and further offset', () => {
  196. let insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  197. doc.fire( 'change', 'insert', { range: insertRange }, null );
  198. expect( live.path ).to.deep.equal( path );
  199. expect( spy.called ).to.be.false;
  200. } );
  201. it( 'is at the same position and live position is sticking to left side', () => {
  202. let live = new LivePosition( root, path, 'sticksToPrevious' );
  203. let insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  204. doc.fire( 'change', 'insert', { range: insertRange }, null );
  205. expect( live.path ).to.deep.equal( path );
  206. expect( spy.called ).to.be.false;
  207. } );
  208. it( 'is after a node from the position path', () => {
  209. let insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  210. doc.fire( 'change', 'insert', { range: insertRange }, null );
  211. expect( live.path ).to.deep.equal( path );
  212. expect( spy.called ).to.be.false;
  213. } );
  214. it( 'is in different root', () => {
  215. let insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
  216. doc.fire( 'change', 'insert', { range: insertRange }, null );
  217. expect( live.path ).to.deep.equal( path );
  218. expect( spy.called ).to.be.false;
  219. } );
  220. } );
  221. describe( 'range move', () => {
  222. it( 'is at the same parent and further offset', () => {
  223. let moveSource = new Position( root, [ 2 ] );
  224. let moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
  225. let changes = {
  226. range: moveRange,
  227. sourcePosition: moveSource
  228. };
  229. doc.fire( 'change', 'move', changes, null );
  230. expect( live.path ).to.deep.equal( path );
  231. expect( spy.called ).to.be.false;
  232. } );
  233. it( 'is at the same position and live position is sticking to left side', () => {
  234. let live = new LivePosition( root, path, 'sticksToPrevious' );
  235. let moveSource = new Position( root, [ 2 ] );
  236. let moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
  237. let changes = {
  238. range: moveRange,
  239. sourcePosition: moveSource
  240. };
  241. doc.fire( 'change', 'move', changes, null );
  242. expect( live.path ).to.deep.equal( path );
  243. expect( spy.called ).to.be.false;
  244. } );
  245. it( 'is at a position after a node from the live position path', () => {
  246. let moveSource = new Position( root, [ 2 ] );
  247. let moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
  248. let changes = {
  249. range: moveRange,
  250. sourcePosition: moveSource
  251. };
  252. doc.fire( 'change', 'move', changes, null );
  253. expect( live.path ).to.deep.equal( path );
  254. expect( spy.called ).to.be.false;
  255. } );
  256. it( 'is from the same parent and further offset', () => {
  257. let moveSource = new Position( root, [ 1, 4, 7 ] );
  258. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  259. let changes = {
  260. range: moveRange,
  261. sourcePosition: moveSource
  262. };
  263. doc.fire( 'change', 'move', changes, null );
  264. expect( live.path ).to.deep.equal( path );
  265. expect( spy.called ).to.be.false;
  266. } );
  267. it( 'is from a position after a node from the live position path', () => {
  268. let moveSource = new Position( root, [ 1, 5 ] );
  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. expect( spy.called ).to.be.false;
  277. } );
  278. it( 'is to different root', () => {
  279. let moveSource = new Position( root, [ 2, 0 ] );
  280. let moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 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. expect( spy.called ).to.be.false;
  288. } );
  289. it( 'is from different root', () => {
  290. let moveSource = new Position( otherRoot, [ 1, 0 ] );
  291. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  292. let changes = {
  293. range: moveRange,
  294. sourcePosition: moveSource
  295. };
  296. doc.fire( 'change', 'move', changes, null );
  297. expect( live.path ).to.deep.equal( path );
  298. expect( spy.called ).to.be.false;
  299. } );
  300. } );
  301. it( 'attributes changed', () => {
  302. let changes = {
  303. range: new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 10 ] ) ),
  304. key: 'foo',
  305. oldValue: null,
  306. newValue: 'bar'
  307. };
  308. doc.fire( 'change', 'setAttribute', changes, null );
  309. expect( live.path ).to.deep.equal( path );
  310. expect( spy.called ).to.be.false;
  311. } );
  312. } );
  313. } );