position.js 31 KB

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