position.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 TextProxy from 'ckeditor5/engine/model/textproxy.js';
  11. import Position from 'ckeditor5/engine/model/position.js';
  12. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  13. import testUtils from 'tests/core/_utils/utils.js';
  14. import { jsonParseStringify } from 'tests/engine/model/_utils/utils.js';
  15. testUtils.createSinonSandbox();
  16. describe( 'position', () => {
  17. let doc, root, otherRoot, p, ul, li1, li2, f, o, z, b, a, r, foz, bar;
  18. // root
  19. // |- p Before: [ 0 ] After: [ 1 ]
  20. // |- ul Before: [ 1 ] After: [ 2 ]
  21. // |- li Before: [ 1, 0 ] After: [ 1, 1 ]
  22. // | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
  23. // | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
  24. // | |- z Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
  25. // |- li Before: [ 1, 1 ] After: [ 1, 2 ]
  26. // |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
  27. // |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
  28. // |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
  29. before( () => {
  30. doc = new Document();
  31. root = doc.createRoot();
  32. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  33. foz = new Text( 'foz' );
  34. li1 = new Element( 'li', [], foz );
  35. f = new TextProxy( foz, 0, 1 );
  36. o = new TextProxy( foz, 1, 1 );
  37. z = new TextProxy( foz, 2, 1 );
  38. bar = new Text( 'bar' );
  39. li2 = new Element( 'li', [], bar );
  40. b = new TextProxy( bar, 0, 1 );
  41. a = new TextProxy( bar, 1, 1 );
  42. r = new TextProxy( bar, 2, 1 );
  43. ul = new Element( 'ul', [], [ li1, li2 ] );
  44. p = new Element( 'p' );
  45. root.insertChildren( 0, [ p, ul ] );
  46. } );
  47. describe( 'constructor()', () => {
  48. it( 'should create a position with path and document', () => {
  49. let position = new Position( root, [ 0 ] );
  50. expect( position ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  51. expect( position ).to.have.property( 'root' ).that.equals( root );
  52. } );
  53. it( 'should accept DocumentFragment as a root', () => {
  54. const frag = new DocumentFragment();
  55. const pos = new Position( frag, [ 0 ] );
  56. expect( pos ).to.have.property( 'root', frag );
  57. } );
  58. it( 'should accept detached Element as a root', () => {
  59. const el = new Element( 'p' );
  60. const pos = new Position( el, [ 0 ] );
  61. expect( pos ).to.have.property( 'root', el );
  62. expect( pos.path ).to.deep.equal( [ 0 ] );
  63. } );
  64. it( 'should normalize attached Element as a root', () => {
  65. const pos = new Position( li1, [ 0, 2 ] );
  66. expect( pos ).to.have.property( 'root', root );
  67. expect( pos.isEqual( Position.createAt( li1, 0, 2 ) ) );
  68. } );
  69. it( 'should normalize Element from a detached branch as a root', () => {
  70. const rootEl = new Element( 'p', null, [ new Element( 'a' ) ] );
  71. const elA = rootEl.getChild( 0 );
  72. const pos = new Position( elA, [ 0 ] );
  73. expect( pos ).to.have.property( 'root', rootEl );
  74. expect( pos.isEqual( Position.createAt( elA, 0 ) ) );
  75. } );
  76. it( 'should throw error if given path is incorrect', () => {
  77. expect( () => {
  78. new Position( root, {} );
  79. } ).to.throw( CKEditorError, /model-position-path-incorrect/ );
  80. expect( () => {
  81. new Position( root, [] );
  82. } ).to.throw( CKEditorError, /model-position-path-incorrect/ );
  83. } );
  84. it( 'should throw error if given root is invalid', () => {
  85. expect( () => {
  86. new Position( new Text( 'a' ) );
  87. } ).to.throw( CKEditorError, /model-position-root-invalid/ );
  88. expect( () => {
  89. new Position();
  90. } ).to.throw( CKEditorError, /model-position-root-invalid/ );
  91. } );
  92. } );
  93. describe( 'createFromParentAndOffset', () => {
  94. it( 'should create positions form node and offset', () => {
  95. expect( Position.createFromParentAndOffset( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  96. expect( Position.createFromParentAndOffset( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  97. expect( Position.createFromParentAndOffset( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  98. expect( Position.createFromParentAndOffset( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
  99. expect( Position.createFromParentAndOffset( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  100. expect( Position.createFromParentAndOffset( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  101. expect( Position.createFromParentAndOffset( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  102. expect( Position.createFromParentAndOffset( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  103. expect( Position.createFromParentAndOffset( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  104. expect( Position.createFromParentAndOffset( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  105. expect( Position.createFromParentAndOffset( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  106. } );
  107. it( 'throws when parent is not an element', () => {
  108. expect( () => {
  109. Position.createFromParentAndOffset( b, 0 );
  110. } ).to.throw( CKEditorError, /^model-position-parent-incorrect/ );
  111. } );
  112. it( 'works with a doc frag', () => {
  113. const frag = new DocumentFragment();
  114. expect( Position.createFromParentAndOffset( frag, 0 ) ).to.have.property( 'root', frag );
  115. } );
  116. } );
  117. describe( 'createAt', () => {
  118. it( 'should create positions from positions', () => {
  119. const spy = testUtils.sinon.spy( Position, 'createFromPosition' );
  120. expect( Position.createAt( Position.createAt( ul ) ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  121. expect( spy.calledOnce ).to.be.true;
  122. } );
  123. it( 'should create positions from node and offset', () => {
  124. expect( Position.createAt( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  125. expect( Position.createAt( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  126. expect( Position.createAt( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  127. } );
  128. it( 'should create positions from node and flag', () => {
  129. expect( Position.createAt( root, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  130. expect( Position.createAt( p, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  131. expect( Position.createAt( a, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  132. expect( Position.createAt( p, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  133. expect( Position.createAt( a, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  134. expect( Position.createAt( ul, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  135. } );
  136. } );
  137. describe( 'createBefore', () => {
  138. it( 'should create positions before elements', () => {
  139. expect( Position.createBefore( p ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  140. expect( Position.createBefore( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  141. expect( Position.createBefore( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  142. expect( Position.createBefore( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  143. expect( Position.createBefore( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  144. expect( Position.createBefore( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  145. expect( Position.createBefore( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  146. expect( Position.createBefore( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
  147. expect( Position.createBefore( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  148. expect( Position.createBefore( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  149. } );
  150. it( 'should throw error if one try to create positions before root', () => {
  151. expect( () => {
  152. Position.createBefore( root );
  153. } ).to.throw( CKEditorError, /model-position-before-root/ );
  154. } );
  155. } );
  156. describe( 'createAfter', () => {
  157. it( 'should create positions after elements', () => {
  158. expect( Position.createAfter( p ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  159. expect( Position.createAfter( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  160. expect( Position.createAfter( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  161. expect( Position.createAfter( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  162. expect( Position.createAfter( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  163. expect( Position.createAfter( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  164. expect( Position.createAfter( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  165. expect( Position.createAfter( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  166. expect( Position.createAfter( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  167. expect( Position.createAfter( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
  168. } );
  169. it( 'should throw error if one try to make positions after root', () => {
  170. expect( () => {
  171. Position.createAfter( root );
  172. } ).to.throw( CKEditorError, /model-position-after-root/ );
  173. } );
  174. } );
  175. describe( 'createFromPosition', () => {
  176. it( 'should create a copy of given position', () => {
  177. let original = new Position( root, [ 1, 2, 3 ] );
  178. let position = Position.createFromPosition( original );
  179. expect( position ).to.be.instanceof( Position );
  180. expect( position.isEqual( original ) ).to.be.true;
  181. expect( position ).not.to.be.equal( original );
  182. } );
  183. } );
  184. it( 'should have parent', () => {
  185. expect( new Position( root, [ 0 ] ) ).to.have.property( 'parent' ).that.equals( root );
  186. expect( new Position( root, [ 1 ] ) ).to.have.property( 'parent' ).that.equals( root );
  187. expect( new Position( root, [ 2 ] ) ).to.have.property( 'parent' ).that.equals( root );
  188. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( p );
  189. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  190. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  191. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  192. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  193. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  194. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  195. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  196. } );
  197. it( 'should have offset', () => {
  198. expect( new Position( root, [ 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  199. expect( new Position( root, [ 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  200. expect( new Position( root, [ 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  201. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  202. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  203. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  204. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  205. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  206. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  207. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  208. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'offset' ).that.equals( 3 );
  209. } );
  210. it( 'should have index', () => {
  211. expect( new Position( root, [ 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  212. expect( new Position( root, [ 1 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  213. expect( new Position( root, [ 2 ] ) ).to.have.property( 'index' ).that.equals( 2 );
  214. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  215. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  216. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  217. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'index' ).that.equals( 2 );
  218. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  219. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  220. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  221. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  222. } );
  223. it( 'should be able to set offset', () => {
  224. let position = new Position( root, [ 1, 0, 2 ] );
  225. position.offset = 4;
  226. expect( position.offset ).to.equal( 4 );
  227. expect( position.path ).to.deep.equal( [ 1, 0, 4 ] );
  228. } );
  229. it( 'should have nodeBefore if it is not inside a text node', () => {
  230. expect( new Position( root, [ 0 ] ).nodeBefore ).to.be.null;
  231. expect( new Position( root, [ 1 ] ).nodeBefore ).to.equal( p );
  232. expect( new Position( root, [ 2 ] ).nodeBefore ).to.equal( ul );
  233. expect( new Position( root, [ 0, 0 ] ).nodeBefore ).to.null;
  234. expect( new Position( root, [ 1, 0 ] ).nodeBefore ).to.be.null;
  235. expect( new Position( root, [ 1, 1 ] ).nodeBefore ).to.equal( li1 );
  236. expect( new Position( root, [ 1, 2 ] ).nodeBefore ).to.equal( li2 );
  237. expect( new Position( root, [ 1, 0, 0 ] ).nodeBefore ).to.be.null;
  238. expect( new Position( root, [ 1, 0, 1 ] ).nodeBefore ).to.be.null;
  239. expect( new Position( root, [ 1, 0, 2 ] ).nodeBefore ).to.be.null;
  240. expect( new Position( root, [ 1, 0, 3 ] ).nodeBefore.data ).to.equal( 'foz' );
  241. } );
  242. it( 'should have nodeAfter if it is not inside a text node', () => {
  243. expect( new Position( root, [ 0 ] ).nodeAfter ).to.equal( p );
  244. expect( new Position( root, [ 1 ] ).nodeAfter ).to.equal( ul );
  245. expect( new Position( root, [ 2 ] ).nodeAfter ).to.be.null;
  246. expect( new Position( root, [ 0, 0 ] ).nodeAfter ).to.be.null;
  247. expect( new Position( root, [ 1, 0 ] ).nodeAfter ).to.equal( li1 );
  248. expect( new Position( root, [ 1, 1 ] ).nodeAfter ).to.equal( li2 );
  249. expect( new Position( root, [ 1, 2 ] ).nodeAfter ).to.be.null;
  250. expect( new Position( root, [ 1, 0, 0 ] ).nodeAfter.data ).to.equal( 'foz' );
  251. expect( new Position( root, [ 1, 0, 1 ] ).nodeAfter ).to.be.null;
  252. expect( new Position( root, [ 1, 0, 2 ] ).nodeAfter ).to.be.null;
  253. expect( new Position( root, [ 1, 0, 3 ] ).nodeAfter ).to.be.null;
  254. } );
  255. it( 'should have a text node property if it is in text node', () => {
  256. expect( new Position( root, [ 0 ] ).textNode ).to.be.null;
  257. expect( new Position( root, [ 1 ] ).textNode ).to.be.null;
  258. expect( new Position( root, [ 2 ] ).textNode ).to.be.null;
  259. expect( new Position( root, [ 0, 0 ] ).textNode ).to.be.null;
  260. expect( new Position( root, [ 1, 0 ] ).textNode ).to.be.null;
  261. expect( new Position( root, [ 1, 1 ] ).textNode ).to.be.null;
  262. expect( new Position( root, [ 1, 2 ] ).textNode ).to.be.null;
  263. expect( new Position( root, [ 1, 0, 0 ] ).textNode ).to.be.null;
  264. expect( new Position( root, [ 1, 0, 1 ] ).textNode ).to.equal( foz );
  265. expect( new Position( root, [ 1, 0, 2 ] ).textNode ).to.equal( foz );
  266. expect( new Position( root, [ 1, 0, 3 ] ).textNode ).to.be.null;
  267. expect( new Position( root, [ 1, 1, 0 ] ).textNode ).to.be.null;
  268. expect( new Position( root, [ 1, 1, 1 ] ).textNode ).to.equal( bar );
  269. expect( new Position( root, [ 1, 1, 2 ] ).textNode ).to.equal( bar );
  270. expect( new Position( root, [ 1, 1, 3 ] ).textNode ).to.be.null;
  271. } );
  272. it( 'should have proper parent path', () => {
  273. let position = new Position( root, [ 1, 2, 3 ] );
  274. expect( position.getParentPath() ).to.deep.equal( [ 1, 2 ] );
  275. } );
  276. describe( 'isBefore', () => {
  277. it( 'should return true if given position has same root and is before this position', () => {
  278. let position = new Position( root, [ 1, 1, 2 ] );
  279. let beforePosition = new Position( root, [ 1, 0 ] );
  280. expect( position.isAfter( beforePosition ) ).to.be.true;
  281. } );
  282. it( 'should return false if given position has same root and is not before this position', () => {
  283. let position = new Position( root, [ 1, 1, 2 ] );
  284. let afterPosition = new Position( root, [ 1, 2 ] );
  285. expect( position.isAfter( afterPosition ) ).to.be.false;
  286. } );
  287. it( 'should return false if given position has different root', () => {
  288. let position = new Position( root, [ 1, 1, 2 ] );
  289. let differentPosition = new Position( otherRoot, [ 1, 0 ] );
  290. expect( position.isAfter( differentPosition ) ).to.be.false;
  291. } );
  292. } );
  293. describe( 'isEqual', () => {
  294. it( 'should return true if given position has same path and root', () => {
  295. let position = new Position( root, [ 1, 1, 2 ] );
  296. let samePosition = new Position( root, [ 1, 1, 2 ] );
  297. expect( position.isEqual( samePosition ) ).to.be.true;
  298. } );
  299. it( 'should return false if given position has different path', () => {
  300. let position = new Position( root, [ 1, 1, 1 ] );
  301. let differentPosition = new Position( root, [ 1, 2, 2 ] );
  302. expect( position.isEqual( differentPosition ) ).to.be.false;
  303. } );
  304. it( 'should return false if given position has different root', () => {
  305. let position = new Position( root, [ 1, 1, 1 ] );
  306. let differentPosition = new Position( otherRoot, [ 1, 1, 1 ] );
  307. expect( position.isEqual( differentPosition ) ).to.be.false;
  308. } );
  309. } );
  310. describe( 'isAfter', () => {
  311. it( 'should return true if given position has same root and is after this position', () => {
  312. let position = new Position( root, [ 1, 1, 2 ] );
  313. let afterPosition = new Position( root, [ 1, 2 ] );
  314. expect( position.isBefore( afterPosition ) ).to.be.true;
  315. } );
  316. it( 'should return false if given position has same root and is not after this position', () => {
  317. let position = new Position( root, [ 1, 1, 2 ] );
  318. let beforePosition = new Position( root, [ 1, 0 ] );
  319. expect( position.isBefore( beforePosition ) ).to.be.false;
  320. } );
  321. it( 'should return false if given position has different root', () => {
  322. let position = new Position( root, [ 1, 1, 2 ] );
  323. let differentPosition = new Position( otherRoot, [ 1, 2 ] );
  324. expect( position.isBefore( differentPosition ) ).to.be.false;
  325. } );
  326. } );
  327. describe( 'isTouching', () => {
  328. it( 'should return true if positions are same', () => {
  329. let position = new Position( root, [ 1, 1, 1 ] );
  330. let result = position.isTouching( new Position( root, [ 1, 1, 1 ] ) );
  331. expect( result ).to.be.true;
  332. } );
  333. it( 'should return true if given position is in next node and there are no whole nodes before it', () => {
  334. let positionA = new Position( root, [ 1 ] );
  335. let positionB = new Position( root, [ 1, 0, 0 ] );
  336. expect( positionA.isTouching( positionB ) ).to.be.true;
  337. expect( positionB.isTouching( positionA ) ).to.be.true;
  338. } );
  339. it( 'should return true if given position is in previous node and there are no whole nodes after it', () => {
  340. let positionA = new Position( root, [ 2 ] );
  341. let positionB = new Position( root, [ 1, 1, 3 ] );
  342. expect( positionA.isTouching( positionB ) ).to.be.true;
  343. expect( positionB.isTouching( positionA ) ).to.be.true;
  344. } );
  345. it( 'should return true if positions are in different sub-trees but there are no whole nodes between them', () => {
  346. let positionA = new Position( root, [ 1, 0, 3 ] );
  347. let positionB = new Position( root, [ 1, 1, 0 ] );
  348. expect( positionA.isTouching( positionB ) ).to.be.true;
  349. expect( positionB.isTouching( positionA ) ).to.be.true;
  350. } );
  351. it( 'should return false if there are whole nodes between positions', () => {
  352. let positionA = new Position( root, [ 2 ] );
  353. let positionB = new Position( root, [ 1, 0, 3 ] );
  354. expect( positionA.isTouching( positionB ) ).to.be.false;
  355. expect( positionB.isTouching( positionA ) ).to.be.false;
  356. } );
  357. it( 'should return false if there are whole nodes between positions', () => {
  358. let positionA = new Position( root, [ 1, 0, 3 ] );
  359. let positionB = new Position( root, [ 1, 1, 1 ] );
  360. expect( positionA.isTouching( positionB ) ).to.be.false;
  361. expect( positionB.isTouching( positionA ) ).to.be.false;
  362. } );
  363. it( 'should return false if positions are in different roots', () => {
  364. let positionA = new Position( root, [ 1, 0, 3 ] );
  365. let positionB = new Position( otherRoot, [ 1, 1, 0 ] );
  366. expect( positionA.isTouching( positionB ) ).to.be.false;
  367. expect( positionB.isTouching( positionA ) ).to.be.false;
  368. } );
  369. } );
  370. describe( 'isAtStart', () => {
  371. it( 'should return true if position is at the beginning of its parent', () => {
  372. expect( new Position( root, [ 0 ] ).isAtStart ).to.be.true;
  373. expect( new Position( root, [ 1 ] ).isAtStart ).to.be.false;
  374. } );
  375. } );
  376. describe( 'isAtEnd', () => {
  377. it( 'should return true if position is at the end of its parent', () => {
  378. expect( new Position( root, [ root.maxOffset ] ).isAtEnd ).to.be.true;
  379. expect( new Position( root, [ 0 ] ).isAtEnd ).to.be.false;
  380. } );
  381. } );
  382. describe( 'getAncestors', () => {
  383. it( 'should return position parent element and it\'s ancestors', () => {
  384. expect( new Position( root, [ 1, 1, 1 ] ).getAncestors() ).to.deep.equal( [ li2, ul, root ] );
  385. } );
  386. } );
  387. describe( 'compareWith', () => {
  388. it( 'should return same if positions are same', () => {
  389. const position = new Position( root, [ 1, 2, 3 ] );
  390. const compared = new Position( root, [ 1, 2, 3 ] );
  391. expect( position.compareWith( compared ) ).to.equal( 'same' );
  392. } );
  393. it( 'should return before if the position is before compared one', () => {
  394. const position = new Position( root, [ 1, 2, 3 ] );
  395. const compared = new Position( root, [ 1, 3 ] );
  396. expect( position.compareWith( compared ) ).to.equal( 'before' );
  397. } );
  398. it( 'should return after if the position is after compared one', () => {
  399. const position = new Position( root, [ 1, 2, 3, 4 ] );
  400. const compared = new Position( root, [ 1, 2, 3 ] );
  401. expect( position.compareWith( compared ) ).to.equal( 'after' );
  402. } );
  403. it( 'should return different if positions are in different roots', () => {
  404. const position = new Position( root, [ 1, 2, 3 ] );
  405. const compared = new Position( otherRoot, [ 1, 2, 3 ] );
  406. expect( position.compareWith( compared ) ).to.equal( 'different' );
  407. } );
  408. } );
  409. describe( '_getTransformedByInsertion', () => {
  410. it( 'should return a new Position instance', () => {
  411. const position = new Position( root, [ 0 ] );
  412. const transformed = position._getTransformedByInsertion( new Position( root, [ 2 ] ), 4, false );
  413. expect( transformed ).not.to.equal( position );
  414. expect( transformed ).to.be.instanceof( Position );
  415. } );
  416. it( 'should increment offset if insertion is in the same parent and closer offset', () => {
  417. const position = new Position( root, [ 1, 2, 3 ] );
  418. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 2 ] ), 2, false );
  419. expect( transformed.offset ).to.equal( 5 );
  420. } );
  421. it( 'should not increment offset if insertion position is in different root', () => {
  422. const position = new Position( root, [ 1, 2, 3 ] );
  423. const transformed = position._getTransformedByInsertion( new Position( otherRoot, [ 1, 2, 2 ] ), 2, false );
  424. expect( transformed.offset ).to.equal( 3 );
  425. } );
  426. it( 'should not increment offset if insertion is in the same parent and the same offset', () => {
  427. const position = new Position( root, [ 1, 2, 3 ] );
  428. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2, false );
  429. expect( transformed.offset ).to.equal( 3 );
  430. } );
  431. it( 'should increment offset if insertion is in the same parent and the same offset and it is inserted before', () => {
  432. const position = new Position( root, [ 1, 2, 3 ] );
  433. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2, true );
  434. expect( transformed.offset ).to.equal( 5 );
  435. } );
  436. it( 'should not increment offset if insertion is in the same parent and further offset', () => {
  437. const position = new Position( root, [ 1, 2, 3 ] );
  438. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 4 ] ), 2, false );
  439. expect( transformed.offset ).to.equal( 3 );
  440. } );
  441. it( 'should update path if insertion position parent is a node from that path and offset is before next node on that path', () => {
  442. const position = new Position( root, [ 1, 2, 3 ] );
  443. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2 ] ), 2, false );
  444. expect( transformed.path ).to.deep.equal( [ 1, 4, 3 ] );
  445. } );
  446. it( 'should not update path if insertion position parent is a node from that path and offset is after next node on that path', () => {
  447. const position = new Position( root, [ 1, 2, 3 ] );
  448. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 3 ] ), 2, false );
  449. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  450. } );
  451. } );
  452. describe( '_getTransformedByDeletion', () => {
  453. it( 'should return a new Position instance', () => {
  454. const position = new Position( root, [ 0 ] );
  455. const transformed = position._getTransformedByDeletion( new Position( root, [ 2 ] ), 4 );
  456. expect( transformed ).not.to.equal( position );
  457. expect( transformed ).to.be.instanceof( Position );
  458. } );
  459. it( 'should return null if original position is inside one of removed nodes', () => {
  460. const position = new Position( root, [ 1, 2 ] );
  461. const transformed = position._getTransformedByDeletion( new Position( root, [ 0 ] ), 2 );
  462. expect( transformed ).to.be.null;
  463. } );
  464. it( 'should decrement offset if deletion is in the same parent and closer offset', () => {
  465. const position = new Position( root, [ 1, 2, 7 ] );
  466. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 2 ] ), 2 );
  467. expect( transformed.offset ).to.equal( 5 );
  468. } );
  469. it( 'should return null if original position is between removed nodes', () => {
  470. const position = new Position( root, [ 1, 2, 4 ] );
  471. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 3 ] ), 5 );
  472. expect( transformed ).to.be.null;
  473. } );
  474. it( 'should not decrement offset if deletion position is in different root', () => {
  475. const position = new Position( root, [ 1, 2, 3 ] );
  476. const transformed = position._getTransformedByDeletion( new Position( otherRoot, [ 1, 2, 1 ] ), 2 );
  477. expect( transformed.offset ).to.equal( 3 );
  478. } );
  479. it( 'should not decrement offset if deletion is in the same parent and further offset', () => {
  480. const position = new Position( root, [ 1, 2, 3 ] );
  481. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 4 ] ), 2 );
  482. expect( transformed.offset ).to.equal( 3 );
  483. } );
  484. it( 'should update path if deletion position parent is a node from that path and offset is before next node on that path', () => {
  485. const position = new Position( root, [ 1, 2, 3 ] );
  486. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 0 ] ), 2 );
  487. expect( transformed.path ).to.deep.equal( [ 1, 0, 3 ] );
  488. } );
  489. it( 'should not update path if deletion position parent is a node from that path and offset is after next node on that path', () => {
  490. const position = new Position( root, [ 1, 2, 3 ] );
  491. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 3 ] ), 2 );
  492. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  493. } );
  494. } );
  495. describe( '_getTransformedByMove', () => {
  496. it( 'should increment offset if a range was moved to the same parent and closer offset', () => {
  497. const position = new Position( root, [ 1, 2, 3 ] );
  498. const transformed = position._getTransformedByMove( new Position( root, [ 2 ] ), new Position( root, [ 1, 2, 0 ] ), 3, false );
  499. expect( transformed.path ).to.deep.equal( [ 1, 2, 6 ] );
  500. } );
  501. it( 'should decrement offset if a range was moved from the same parent and closer offset', () => {
  502. const position = new Position( root, [ 1, 2, 6 ] );
  503. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  504. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  505. } );
  506. it( 'should decrement offset if position was at the end of a range and move was not sticky', () => {
  507. const position = new Position( root, [ 1, 2, 3 ] );
  508. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  509. expect( transformed.path ).to.deep.equal( [ 1, 2, 0 ] );
  510. } );
  511. it( 'should update path if position was at the end of a range and move was sticky', () => {
  512. const position = new Position( root, [ 1, 2, 3 ] );
  513. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false, true );
  514. expect( transformed.path ).to.deep.equal( [ 5 ] );
  515. } );
  516. it( 'should update path if a range contained this position', () => {
  517. const position = new Position( root, [ 1, 2, 3 ] );
  518. const transformed = position._getTransformedByMove( new Position( root, [ 1, 1 ] ), new Position( root, [ 2, 1 ] ), 3, false );
  519. expect( transformed.path ).to.deep.equal( [ 2, 2, 3 ] );
  520. } );
  521. } );
  522. describe( '_getCombined', () => {
  523. it( 'should return correct combination of this and given positions', () => {
  524. const position = new Position( root, [ 1, 3, 4, 2 ] );
  525. const sourcePosition = new Position( root, [ 1, 1 ] );
  526. const targetPosition = new Position( root, [ 2, 5 ] );
  527. const combined = position._getCombined( sourcePosition, targetPosition );
  528. expect( combined.path ).to.deep.equal( [ 2, 7, 4, 2 ] );
  529. } );
  530. } );
  531. describe( 'getShiftedBy', () => {
  532. it( 'should return a new instance of Position with offset changed by shift value', () => {
  533. let position = new Position( root, [ 1, 2, 3 ] );
  534. let shifted = position.getShiftedBy( 2 );
  535. expect( shifted ).to.be.instanceof( Position );
  536. expect( shifted ).to.not.equal( position );
  537. expect( shifted.path ).to.deep.equal( [ 1, 2, 5 ] );
  538. } );
  539. it( 'should accept negative values', () => {
  540. let position = new Position( root, [ 1, 2, 3 ] );
  541. let shifted = position.getShiftedBy( -2 );
  542. expect( shifted.path ).to.deep.equal( [ 1, 2, 1 ] );
  543. } );
  544. it( 'should not let setting offset lower than zero', () => {
  545. let position = new Position( root, [ 1, 2, 3 ] );
  546. let shifted = position.getShiftedBy( -7 );
  547. expect( shifted.path ).to.deep.equal( [ 1, 2, 0 ] );
  548. } );
  549. } );
  550. describe( 'toJSON', () => {
  551. it( 'should serialize position', () => {
  552. let position = new Position( root, [ 0 ] );
  553. let serialized = jsonParseStringify( position );
  554. expect( serialized ).to.deep.equal( { root: 'main', path: [ 0 ] } );
  555. } );
  556. it( 'should serialize position from graveyard', () => {
  557. let position = new Position( doc.graveyard, [ 0 ] );
  558. let serialized = jsonParseStringify( position );
  559. expect( serialized ).to.deep.equal( { root: '$graveyard', path: [ 0 ] } );
  560. } );
  561. } );
  562. describe( 'fromJSON', () => {
  563. it( 'should create object with given document', () => {
  564. let deserialized = Position.fromJSON( { root: 'main', path: [ 0, 1, 2 ] }, doc );
  565. expect( deserialized.root ).to.equal( root );
  566. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  567. } );
  568. it( 'should create object from graveyard', () => {
  569. let deserialized = Position.fromJSON( { root: '$graveyard', path: [ 0, 1, 2 ] }, doc );
  570. expect( deserialized.root ).to.equal( doc.graveyard );
  571. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  572. } );
  573. it( 'should throw error when creating object in document that does not have provided root', () => {
  574. expect(
  575. () => {
  576. Position.fromJSON( { root: 'noroot', path: [ 0 ] }, doc );
  577. }
  578. ).to.throw( CKEditorError, /model-position-fromjson-no-root/ );
  579. } );
  580. } );
  581. } );