position.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../src/model/model';
  6. import DocumentFragment from '../../src/model/documentfragment';
  7. import Element from '../../src/model/element';
  8. import Text from '../../src/model/text';
  9. import TextProxy from '../../src/model/textproxy';
  10. import {
  11. default as Position,
  12. getTextNodeAtPosition,
  13. getNodeAfterPosition,
  14. getNodeBeforePosition
  15. } from '../../src/model/position';
  16. import Range from '../../src/model/range';
  17. import MarkerOperation from '../../src/model/operation/markeroperation';
  18. import AttributeOperation from '../../src/model/operation/attributeoperation';
  19. import InsertOperation from '../../src/model/operation/insertoperation';
  20. import MoveOperation from '../../src/model/operation/moveoperation';
  21. import RenameOperation from '../../src/model/operation/renameoperation';
  22. import MergeOperation from '../../src/model/operation/mergeoperation';
  23. import SplitOperation from '../../src/model/operation/splitoperation';
  24. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  25. import LivePosition from '../../src/model/liveposition';
  26. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  27. describe( 'Position', () => {
  28. let doc, model, root, otherRoot, p, ul, li1, li2, f, o, z, b, a, r, foz, bar;
  29. testUtils.createSinonSandbox();
  30. // root
  31. // |- p Before: [ 0 ] After: [ 1 ]
  32. // |- ul Before: [ 1 ] After: [ 2 ]
  33. // |- li Before: [ 1, 0 ] After: [ 1, 1 ]
  34. // | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
  35. // | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
  36. // | |- z Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
  37. // |- li Before: [ 1, 1 ] After: [ 1, 2 ]
  38. // |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
  39. // |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
  40. // |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
  41. beforeEach( () => {
  42. model = new Model();
  43. doc = model.document;
  44. root = doc.createRoot();
  45. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  46. foz = new Text( 'foz' );
  47. li1 = new Element( 'li', [], foz );
  48. f = new TextProxy( foz, 0, 1 );
  49. o = new TextProxy( foz, 1, 1 );
  50. z = new TextProxy( foz, 2, 1 );
  51. bar = new Text( 'bar' );
  52. li2 = new Element( 'li', [], bar );
  53. b = new TextProxy( bar, 0, 1 );
  54. a = new TextProxy( bar, 1, 1 );
  55. r = new TextProxy( bar, 2, 1 );
  56. ul = new Element( 'ul', [], [ li1, li2 ] );
  57. p = new Element( 'p' );
  58. root._insertChild( 0, [ p, ul ] );
  59. } );
  60. describe( 'constructor()', () => {
  61. it( 'should create a position with path and document', () => {
  62. const position = new Position( root, [ 0 ] );
  63. expect( position ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  64. expect( position ).to.have.property( 'root' ).that.equals( root );
  65. } );
  66. it( 'should accept DocumentFragment as a root', () => {
  67. const frag = new DocumentFragment();
  68. const pos = new Position( frag, [ 0 ] );
  69. expect( pos ).to.have.property( 'root', frag );
  70. } );
  71. it( 'should accept detached Element as a root', () => {
  72. const el = new Element( 'p' );
  73. const pos = new Position( el, [ 0 ] );
  74. expect( pos ).to.have.property( 'root', el );
  75. expect( pos.path ).to.deep.equal( [ 0 ] );
  76. } );
  77. it( 'should normalize attached Element as a root', () => {
  78. const pos = new Position( li1, [ 0, 2 ] );
  79. expect( pos ).to.have.property( 'root', root );
  80. expect( pos.isEqual( Position._createAt( li1, 0, 2 ) ) );
  81. } );
  82. it( 'should normalize Element from a detached branch as a root', () => {
  83. const rootEl = new Element( 'p', null, [ new Element( 'a' ) ] );
  84. const elA = rootEl.getChild( 0 );
  85. const pos = new Position( elA, [ 0 ] );
  86. expect( pos ).to.have.property( 'root', rootEl );
  87. expect( pos.isEqual( Position._createAt( elA, 0 ) ) );
  88. } );
  89. it( 'should throw error if given path is incorrect', () => {
  90. expectToThrowCKEditorError( () => {
  91. new Position( root, {} ); // eslint-disable-line no-new
  92. }, /model-position-path-incorrect-format/, model );
  93. expectToThrowCKEditorError( () => {
  94. new Position( root, [] ); // eslint-disable-line no-new
  95. }, /model-position-path-incorrect-format/, model );
  96. } );
  97. it( 'should throw error if given root is invalid', () => {
  98. expectToThrowCKEditorError( () => {
  99. new Position( new Text( 'a' ) ); // eslint-disable-line no-new
  100. }, /model-position-root-invalid/ );
  101. expect( () => {
  102. new Position(); // eslint-disable-line no-new
  103. } ).to.throw();
  104. } );
  105. } );
  106. describe( 'is()', () => {
  107. let position;
  108. beforeEach( () => {
  109. position = new Position( root, [ 0 ] );
  110. } );
  111. it( 'should return true for "position"', () => {
  112. expect( position.is( 'position' ) ).to.be.true;
  113. expect( position.is( 'model:position' ) ).to.be.true;
  114. } );
  115. it( 'should return false for incorrect values', () => {
  116. expect( position.is( 'model' ) ).to.be.false;
  117. expect( position.is( 'model:node' ) ).to.be.false;
  118. expect( position.is( '$text' ) ).to.be.false;
  119. expect( position.is( 'element', 'paragraph' ) ).to.be.false;
  120. } );
  121. } );
  122. describe( 'static creators', () => {
  123. describe( '_createAt()', () => {
  124. it( 'should throw if no offset is passed', () => {
  125. expectToThrowCKEditorError( () => {
  126. Position._createAt( ul );
  127. }, /model-createPositionAt-offset-required/, model );
  128. } );
  129. it( 'should create positions from positions', () => {
  130. const position = Position._createAt( ul, 0 );
  131. const positionCopy = Position._createAt( position );
  132. expect( positionCopy ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  133. expect( positionCopy ).to.have.property( 'root' ).that.equals( position.root );
  134. expect( positionCopy ).to.not.equal( position );
  135. } );
  136. it( 'should create positions from LivePosition', () => {
  137. const position = new LivePosition( root, [ 0, 0 ] );
  138. const created = Position._createAt( position );
  139. expect( created.isEqual( position ) ).to.be.true;
  140. expect( created ).to.not.be.equal( position );
  141. expect( created ).to.be.instanceof( Position );
  142. expect( created ).to.not.be.instanceof( LivePosition );
  143. } );
  144. it( 'should create positions from node and offset', () => {
  145. expect( Position._createAt( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  146. expect( Position._createAt( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  147. expect( Position._createAt( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  148. expect( Position._createAt( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
  149. expect( Position._createAt( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  150. expect( Position._createAt( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  151. expect( Position._createAt( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  152. expect( Position._createAt( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  153. expect( Position._createAt( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  154. expect( Position._createAt( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  155. expect( Position._createAt( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  156. } );
  157. it( 'should create positions from node and flag', () => {
  158. expect( Position._createAt( root, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  159. expect( Position._createAt( p, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  160. expect( Position._createAt( a, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  161. expect( Position._createAt( p, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  162. expect( Position._createAt( a, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  163. expect( Position._createAt( ul, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  164. } );
  165. it( 'should set stickiness (if not cloning other position)', () => {
  166. expect( Position._createAt( root, 'end', 'toPrevious' ) ).to.have.property( 'stickiness' ).that.equals( 'toPrevious' );
  167. } );
  168. it( 'throws when parent is not an element', () => {
  169. expectToThrowCKEditorError( () => {
  170. Position._createAt( b, 0 );
  171. }, /^model-position-parent-incorrect/, model );
  172. } );
  173. it( 'works with a doc frag', () => {
  174. const frag = new DocumentFragment();
  175. expect( Position._createAt( frag, 0 ) ).to.have.property( 'root', frag );
  176. } );
  177. } );
  178. describe( '_createBefore()', () => {
  179. it( 'should create positions before elements', () => {
  180. expect( Position._createBefore( p ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  181. expect( Position._createBefore( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  182. expect( Position._createBefore( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  183. expect( Position._createBefore( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  184. expect( Position._createBefore( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  185. expect( Position._createBefore( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  186. expect( Position._createBefore( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  187. expect( Position._createBefore( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
  188. expect( Position._createBefore( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  189. expect( Position._createBefore( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  190. } );
  191. it( 'should set stickiness', () => {
  192. expect( Position._createBefore( p, 'toPrevious' ) ).to.have.property( 'stickiness' ).that.equals( 'toPrevious' );
  193. } );
  194. it( 'should throw error if one try to create positions before root', () => {
  195. expectToThrowCKEditorError( () => {
  196. Position._createBefore( root );
  197. }, /model-position-before-root/, model );
  198. } );
  199. } );
  200. describe( '_createAfter()', () => {
  201. it( 'should create positions after elements', () => {
  202. expect( Position._createAfter( p ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  203. expect( Position._createAfter( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  204. expect( Position._createAfter( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  205. expect( Position._createAfter( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  206. expect( Position._createAfter( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  207. expect( Position._createAfter( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  208. expect( Position._createAfter( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  209. expect( Position._createAfter( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  210. expect( Position._createAfter( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  211. expect( Position._createAfter( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
  212. } );
  213. it( 'should set stickiness', () => {
  214. expect( Position._createAfter( p, 'toPrevious' ) ).to.have.property( 'stickiness' ).that.equals( 'toPrevious' );
  215. } );
  216. it( 'should throw error if one try to make positions after root', () => {
  217. expectToThrowCKEditorError( () => {
  218. Position._createAfter( root );
  219. }, /model-position-after-root/, model );
  220. } );
  221. } );
  222. } );
  223. describe( '#parent', () => {
  224. it( 'should have parent', () => {
  225. expect( new Position( root, [ 0 ] ) ).to.have.property( 'parent' ).that.equals( root );
  226. expect( new Position( root, [ 1 ] ) ).to.have.property( 'parent' ).that.equals( root );
  227. expect( new Position( root, [ 2 ] ) ).to.have.property( 'parent' ).that.equals( root );
  228. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( p );
  229. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  230. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  231. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  232. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  233. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  234. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  235. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  236. } );
  237. it( 'should work with positions rooted in document fragment', () => {
  238. const docFrag = new DocumentFragment();
  239. expect( new Position( docFrag, [ 0 ] ) ).to.have.property( 'parent' ).that.equals( docFrag );
  240. } );
  241. it( 'should throw when path out of bounds', () => {
  242. const position = new Position( root, [ 0, 0 ] );
  243. expect( position ).to.have.property( 'parent' ).that.equals( p );
  244. root._removeChildren( 0, 2 );
  245. expectToThrowCKEditorError( () => {
  246. position.parent;
  247. }, /^model-position-path-incorrect:/, position, { position } );
  248. } );
  249. it( 'should throw when based on a path, the parent would be a text node', () => {
  250. // 1,0,0 points at: <p></p><ul><li>^foz</li>...
  251. const position = new Position( root, [ 1, 0, 0, 0 ] );
  252. expectToThrowCKEditorError( () => {
  253. position.parent;
  254. }, /^model-position-path-incorrect:/, position, { position } );
  255. } );
  256. } );
  257. describe( '#offset', () => {
  258. it( 'should have offset', () => {
  259. expect( new Position( root, [ 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  260. expect( new Position( root, [ 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  261. expect( new Position( root, [ 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  262. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  263. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  264. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  265. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  266. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  267. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  268. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  269. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'offset' ).that.equals( 3 );
  270. } );
  271. } );
  272. describe( '#index', () => {
  273. it( 'should have index', () => {
  274. expect( new Position( root, [ 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  275. expect( new Position( root, [ 1 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  276. expect( new Position( root, [ 2 ] ) ).to.have.property( 'index' ).that.equals( 2 );
  277. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  278. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  279. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  280. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'index' ).that.equals( 2 );
  281. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  282. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  283. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  284. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  285. } );
  286. it( 'should be able to set offset', () => {
  287. const position = new Position( root, [ 1, 0, 2 ] );
  288. position.offset = 4;
  289. expect( position.offset ).to.equal( 4 );
  290. expect( position.path ).to.deep.equal( [ 1, 0, 4 ] );
  291. } );
  292. it( 'should throw when path out of bounds', () => {
  293. const position = new Position( root, [ 0, 0 ] );
  294. expect( position ).to.have.property( 'index' ).that.equals( 0 );
  295. root._removeChildren( 0, 2 );
  296. expectToThrowCKEditorError( () => {
  297. position.index;
  298. }, /^model-position-path-incorrect:/, position, { position } );
  299. } );
  300. } );
  301. describe( '#nodeBefore', () => {
  302. it( 'should have nodeBefore if it is not inside a text node', () => {
  303. expect( new Position( root, [ 0 ] ).nodeBefore ).to.be.null;
  304. expect( new Position( root, [ 1 ] ).nodeBefore ).to.equal( p );
  305. expect( new Position( root, [ 2 ] ).nodeBefore ).to.equal( ul );
  306. expect( new Position( root, [ 0, 0 ] ).nodeBefore ).to.null;
  307. expect( new Position( root, [ 1, 0 ] ).nodeBefore ).to.be.null;
  308. expect( new Position( root, [ 1, 1 ] ).nodeBefore ).to.equal( li1 );
  309. expect( new Position( root, [ 1, 2 ] ).nodeBefore ).to.equal( li2 );
  310. expect( new Position( root, [ 1, 0, 0 ] ).nodeBefore ).to.be.null;
  311. expect( new Position( root, [ 1, 0, 1 ] ).nodeBefore ).to.be.null;
  312. expect( new Position( root, [ 1, 0, 2 ] ).nodeBefore ).to.be.null;
  313. expect( new Position( root, [ 1, 0, 3 ] ).nodeBefore.data ).to.equal( 'foz' );
  314. } );
  315. it( 'should throw when path out of bounds', () => {
  316. const position = new Position( root, [ 1, 1 ] );
  317. expect( position ).to.have.property( 'nodeBefore' ).that.equals( li1 );
  318. root._removeChildren( 0, 2 );
  319. expectToThrowCKEditorError( () => {
  320. position.nodeBefore;
  321. }, /^model-nodelist-offset-out-of-bounds:/, position );
  322. } );
  323. } );
  324. describe( '#nodeAfter', () => {
  325. it( 'should have nodeAfter if it is not inside a text node', () => {
  326. expect( new Position( root, [ 0 ] ).nodeAfter ).to.equal( p );
  327. expect( new Position( root, [ 1 ] ).nodeAfter ).to.equal( ul );
  328. expect( new Position( root, [ 2 ] ).nodeAfter ).to.be.null;
  329. expect( new Position( root, [ 0, 0 ] ).nodeAfter ).to.be.null;
  330. expect( new Position( root, [ 1, 0 ] ).nodeAfter ).to.equal( li1 );
  331. expect( new Position( root, [ 1, 1 ] ).nodeAfter ).to.equal( li2 );
  332. expect( new Position( root, [ 1, 2 ] ).nodeAfter ).to.be.null;
  333. expect( new Position( root, [ 1, 0, 0 ] ).nodeAfter.data ).to.equal( 'foz' );
  334. expect( new Position( root, [ 1, 0, 1 ] ).nodeAfter ).to.be.null;
  335. expect( new Position( root, [ 1, 0, 2 ] ).nodeAfter ).to.be.null;
  336. expect( new Position( root, [ 1, 0, 3 ] ).nodeAfter ).to.be.null;
  337. } );
  338. it( 'should throw when path out of bounds', () => {
  339. const position = new Position( root, [ 1, 1 ] );
  340. expect( position ).to.have.property( 'nodeAfter' ).that.equals( li2 );
  341. root._removeChildren( 0, 2 );
  342. expectToThrowCKEditorError( () => {
  343. position.nodeAfter;
  344. }, /^model-nodelist-offset-out-of-bounds:/, position );
  345. } );
  346. } );
  347. describe( '#textNode', () => {
  348. it( 'should have a text node property if it is in text node', () => {
  349. expect( new Position( root, [ 0 ] ).textNode ).to.be.null;
  350. expect( new Position( root, [ 1 ] ).textNode ).to.be.null;
  351. expect( new Position( root, [ 2 ] ).textNode ).to.be.null;
  352. expect( new Position( root, [ 0, 0 ] ).textNode ).to.be.null;
  353. expect( new Position( root, [ 1, 0 ] ).textNode ).to.be.null;
  354. expect( new Position( root, [ 1, 1 ] ).textNode ).to.be.null;
  355. expect( new Position( root, [ 1, 2 ] ).textNode ).to.be.null;
  356. expect( new Position( root, [ 1, 0, 0 ] ).textNode ).to.be.null;
  357. expect( new Position( root, [ 1, 0, 1 ] ).textNode ).to.equal( foz );
  358. expect( new Position( root, [ 1, 0, 2 ] ).textNode ).to.equal( foz );
  359. expect( new Position( root, [ 1, 0, 3 ] ).textNode ).to.be.null;
  360. expect( new Position( root, [ 1, 1, 0 ] ).textNode ).to.be.null;
  361. expect( new Position( root, [ 1, 1, 1 ] ).textNode ).to.equal( bar );
  362. expect( new Position( root, [ 1, 1, 2 ] ).textNode ).to.equal( bar );
  363. expect( new Position( root, [ 1, 1, 3 ] ).textNode ).to.be.null;
  364. } );
  365. it( 'should throw when path out of bounds', () => {
  366. const position = new Position( root, [ 1, 0, 1 ] );
  367. expect( position ).to.have.property( 'textNode' ).that.equals( foz );
  368. root._removeChildren( 0, 2 );
  369. expectToThrowCKEditorError( () => {
  370. position.textNode;
  371. }, /^model-nodelist-offset-out-of-bounds:/, position );
  372. } );
  373. } );
  374. describe( 'getParentPath()', () => {
  375. it( 'should have proper parent path', () => {
  376. const position = new Position( root, [ 1, 2, 3 ] );
  377. expect( position.getParentPath() ).to.deep.equal( [ 1, 2 ] );
  378. } );
  379. } );
  380. describe( 'isBefore()', () => {
  381. it( 'should return true if given position has same root and is before this position', () => {
  382. const position = new Position( root, [ 1, 1, 2 ] );
  383. const beforePosition = new Position( root, [ 1, 0 ] );
  384. expect( position.isAfter( beforePosition ) ).to.be.true;
  385. } );
  386. it( 'should return false if given position has same root and is not before this position', () => {
  387. const position = new Position( root, [ 1, 1, 2 ] );
  388. const afterPosition = new Position( root, [ 1, 2 ] );
  389. expect( position.isAfter( afterPosition ) ).to.be.false;
  390. } );
  391. it( 'should return false if given position has different root', () => {
  392. const position = new Position( root, [ 1, 1, 2 ] );
  393. const differentPosition = new Position( otherRoot, [ 1, 0 ] );
  394. expect( position.isAfter( differentPosition ) ).to.be.false;
  395. } );
  396. } );
  397. describe( 'isEqual()', () => {
  398. it( 'should return true if given position has same path and root', () => {
  399. const position = new Position( root, [ 1, 1, 2 ] );
  400. const samePosition = new Position( root, [ 1, 1, 2 ] );
  401. expect( position.isEqual( samePosition ) ).to.be.true;
  402. } );
  403. it( 'should return false if given position has different path', () => {
  404. const position = new Position( root, [ 1, 1, 1 ] );
  405. const differentPosition = new Position( root, [ 1, 2, 2 ] );
  406. expect( position.isEqual( differentPosition ) ).to.be.false;
  407. } );
  408. it( 'should return false if given position has different root', () => {
  409. const position = new Position( root, [ 1, 1, 1 ] );
  410. const differentPosition = new Position( otherRoot, [ 1, 1, 1 ] );
  411. expect( position.isEqual( differentPosition ) ).to.be.false;
  412. } );
  413. } );
  414. describe( 'isAfter()', () => {
  415. it( 'should return true if given position has same root and is after this position', () => {
  416. const position = new Position( root, [ 1, 1, 2 ] );
  417. const afterPosition = new Position( root, [ 1, 2 ] );
  418. expect( position.isBefore( afterPosition ) ).to.be.true;
  419. } );
  420. it( 'should return false if given position has same root and is not after this position', () => {
  421. const position = new Position( root, [ 1, 1, 2 ] );
  422. const beforePosition = new Position( root, [ 1, 0 ] );
  423. expect( position.isBefore( beforePosition ) ).to.be.false;
  424. } );
  425. it( 'should return false if given position has different root', () => {
  426. const position = new Position( root, [ 1, 1, 2 ] );
  427. const differentPosition = new Position( otherRoot, [ 1, 2 ] );
  428. expect( position.isBefore( differentPosition ) ).to.be.false;
  429. } );
  430. } );
  431. describe( 'isTouching()', () => {
  432. it( 'should return true if positions are same', () => {
  433. const position = new Position( root, [ 1, 1, 1 ] );
  434. const result = position.isTouching( new Position( root, [ 1, 1, 1 ] ) );
  435. expect( result ).to.be.true;
  436. } );
  437. it( 'should return true if given position is in next node and there are no whole nodes before it', () => {
  438. const positionA = new Position( root, [ 1 ] );
  439. const positionB = new Position( root, [ 1, 0, 0 ] );
  440. expect( positionA.isTouching( positionB ) ).to.be.true;
  441. expect( positionB.isTouching( positionA ) ).to.be.true;
  442. } );
  443. it( 'should return true if given position is in previous node and there are no whole nodes after it', () => {
  444. const positionA = new Position( root, [ 2 ] );
  445. const positionB = new Position( root, [ 1, 1, 3 ] );
  446. expect( positionA.isTouching( positionB ) ).to.be.true;
  447. expect( positionB.isTouching( positionA ) ).to.be.true;
  448. } );
  449. it( 'should return true if positions are in different sub-trees but there are no whole nodes between them', () => {
  450. const positionA = new Position( root, [ 1, 0, 3 ] );
  451. const positionB = new Position( root, [ 1, 1, 0 ] );
  452. expect( positionA.isTouching( positionB ) ).to.be.true;
  453. expect( positionB.isTouching( positionA ) ).to.be.true;
  454. } );
  455. it( 'should return false if there are whole nodes between positions', () => {
  456. const positionA = new Position( root, [ 2 ] );
  457. const positionB = new Position( root, [ 1, 0, 3 ] );
  458. expect( positionA.isTouching( positionB ) ).to.be.false;
  459. expect( positionB.isTouching( positionA ) ).to.be.false;
  460. } );
  461. it( 'should return false if there are whole nodes between positions (same depth)', () => {
  462. const positionA = new Position( root, [ 1, 0 ] );
  463. const positionB = new Position( root, [ 1, 1 ] );
  464. expect( positionA.isTouching( positionB ) ).to.be.false;
  465. expect( positionB.isTouching( positionA ) ).to.be.false;
  466. } );
  467. it( 'should return false if there are whole nodes between positions (same depth, but deeper)', () => {
  468. const positionA = new Position( root, [ 1, 0, 3 ] );
  469. const positionB = new Position( root, [ 1, 1, 1 ] );
  470. expect( positionA.isTouching( positionB ) ).to.be.false;
  471. expect( positionB.isTouching( positionA ) ).to.be.false;
  472. } );
  473. it( 'should return false if positions are in different roots', () => {
  474. const positionA = new Position( root, [ 1, 0, 3 ] );
  475. const positionB = new Position( otherRoot, [ 1, 1, 0 ] );
  476. expect( positionA.isTouching( positionB ) ).to.be.false;
  477. expect( positionB.isTouching( positionA ) ).to.be.false;
  478. } );
  479. } );
  480. describe( 'hasSameParentAs()', () => {
  481. it( 'should return false if positions have different roots', () => {
  482. const posA = new Position( root, [ 1, 2 ] );
  483. const posB = new Position( doc.graveyard, [ 1, 0 ] );
  484. expect( posA.hasSameParentAs( posB ) ).to.be.false;
  485. expect( posB.hasSameParentAs( posA ) ).to.be.false;
  486. } );
  487. it( 'should return false if positions have different parents', () => {
  488. const posA = new Position( root, [ 0, 1 ] );
  489. const posB = new Position( root, [ 1, 1 ] );
  490. expect( posA.hasSameParentAs( posB ) ).to.be.false;
  491. expect( posB.hasSameParentAs( posA ) ).to.be.false;
  492. } );
  493. it( 'should return true if positions have same parent', () => {
  494. const posA = new Position( root, [ 0, 4, 8 ] );
  495. const posB = new Position( root, [ 0, 4, 2 ] );
  496. expect( posA.hasSameParentAs( posB ) ).to.be.true;
  497. expect( posB.hasSameParentAs( posA ) ).to.be.true;
  498. } );
  499. } );
  500. describe( 'isAtStart()', () => {
  501. it( 'should return true if position is at the beginning of its parent', () => {
  502. expect( new Position( root, [ 0 ] ).isAtStart ).to.be.true;
  503. expect( new Position( root, [ 1 ] ).isAtStart ).to.be.false;
  504. } );
  505. } );
  506. describe( 'isAtEnd()', () => {
  507. it( 'should return true if position is at the end of its parent', () => {
  508. expect( new Position( root, [ root.maxOffset ] ).isAtEnd ).to.be.true;
  509. expect( new Position( root, [ 0 ] ).isAtEnd ).to.be.false;
  510. } );
  511. } );
  512. describe( 'getAncestors()', () => {
  513. it( 'should return position parent element and it\'s ancestors', () => {
  514. expect( new Position( root, [ 1, 1, 1 ] ).getAncestors() ).to.deep.equal( [ root, ul, li2 ] );
  515. } );
  516. it( 'should return DocumentFragment if position is directly in document fragment', () => {
  517. const docFrag = new DocumentFragment();
  518. expect( new Position( docFrag, [ 0 ] ).getAncestors() ).to.deep.equal( [ docFrag ] );
  519. } );
  520. } );
  521. describe( 'findAncestor()', () => {
  522. it( 'should return position parent element', () => {
  523. expect( new Position( root, [ 1, 1, 1 ] ).findAncestor( 'li' ) ).to.equal( li2 );
  524. } );
  525. it( 'should return deeper ancestor element', () => {
  526. expect( new Position( root, [ 1, 1, 1 ] ).findAncestor( 'ul' ) ).to.equal( ul );
  527. } );
  528. it( 'should return null if ancestor is not found', () => {
  529. expect( new Position( root, [ 1, 1, 1 ] ).findAncestor( 'p' ) ).to.be.null;
  530. } );
  531. it( 'should return null if position is not in an element', () => {
  532. const docFrag = new DocumentFragment();
  533. expect( new Position( docFrag, [ 0 ] ).findAncestor( 'li' ) ).to.be.null;
  534. } );
  535. } );
  536. describe( 'getCommonPath()', () => {
  537. it( 'returns the common part', () => {
  538. const pos1 = new Position( root, [ 1, 0, 0 ] );
  539. const pos2 = new Position( root, [ 1, 0, 1 ] );
  540. expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [ 1, 0 ] );
  541. } );
  542. it( 'returns the common part when paths are equal', () => {
  543. const pos1 = new Position( root, [ 1, 0, 1 ] );
  544. const pos2 = new Position( root, [ 1, 0, 1 ] );
  545. const commonPath = pos1.getCommonPath( pos2 );
  546. // Ensure that we have a clone
  547. expect( commonPath ).to.not.equal( pos1.path );
  548. expect( commonPath ).to.not.equal( pos2.path );
  549. expect( commonPath ).to.deep.equal( [ 1, 0, 1 ] );
  550. } );
  551. it( 'returns empty array when paths totally differ', () => {
  552. const pos1 = new Position( root, [ 1, 1 ] );
  553. const pos2 = new Position( root, [ 0 ] );
  554. expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [] );
  555. } );
  556. it( 'returns empty array when roots differ, but paths are the same', () => {
  557. const pos1 = new Position( root, [ 1, 1 ] );
  558. const pos2 = new Position( otherRoot, [ 1, 1 ] );
  559. expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [] );
  560. } );
  561. } );
  562. describe( 'compareWith()', () => {
  563. it( 'should return same if positions are same', () => {
  564. const position = new Position( root, [ 1, 2, 3 ] );
  565. const compared = new Position( root, [ 1, 2, 3 ] );
  566. expect( position.compareWith( compared ) ).to.equal( 'same' );
  567. } );
  568. it( 'should return before if the position is before compared one', () => {
  569. const position = new Position( root, [ 1, 2, 3 ] );
  570. const compared = new Position( root, [ 1, 3 ] );
  571. expect( position.compareWith( compared ) ).to.equal( 'before' );
  572. } );
  573. it( 'should return after if the position is after compared one', () => {
  574. const position = new Position( root, [ 1, 2, 3, 4 ] );
  575. const compared = new Position( root, [ 1, 2, 3 ] );
  576. expect( position.compareWith( compared ) ).to.equal( 'after' );
  577. } );
  578. it( 'should return different if positions are in different roots', () => {
  579. const position = new Position( root, [ 1, 2, 3 ] );
  580. const compared = new Position( otherRoot, [ 1, 2, 3 ] );
  581. expect( position.compareWith( compared ) ).to.equal( 'different' );
  582. } );
  583. } );
  584. describe( 'getLastMatchingPosition()', () => {
  585. it( 'should skip forward', () => {
  586. let position = new Position( root, [ 1, 0, 0 ] );
  587. position = position.getLastMatchingPosition( value => value.type == 'text' );
  588. expect( position.path ).to.deep.equal( [ 1, 0, 3 ] );
  589. } );
  590. it( 'should skip backward', () => {
  591. let position = new Position( root, [ 1, 0, 2 ] );
  592. position = position.getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } );
  593. expect( position.path ).to.deep.equal( [ 1, 0, 0 ] );
  594. } );
  595. } );
  596. describe( 'clone()', () => {
  597. it( 'should return new instance of position', () => {
  598. const position = Position._createAt( ul, 0 );
  599. const positionCopy = position.clone();
  600. expect( positionCopy ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  601. expect( positionCopy ).to.have.property( 'root' ).that.equals( position.root );
  602. expect( positionCopy ).to.not.equal( position );
  603. } );
  604. } );
  605. // Note: We don't create model element structure in these tests because this method
  606. // is used by OT so it must not check the structure.
  607. describe( 'getTransformedByOperation()', () => {
  608. let pos;
  609. beforeEach( () => {
  610. pos = new Position( root, [ 3, 2 ] );
  611. } );
  612. describe( 'by AttributeOperation', () => {
  613. it( 'nothing should change', () => {
  614. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 6 ) );
  615. const op = new AttributeOperation( range, 'key', true, false, 1 );
  616. const transformed = pos.getTransformedByOperation( op );
  617. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  618. } );
  619. } );
  620. describe( 'by InsertOperation', () => {
  621. it( 'should use _getTransformedByInsertion', () => {
  622. sinon.spy( pos, '_getTransformedByInsertion' );
  623. const op = new InsertOperation( new Position( root, [ 1 ] ), [ new Element( 'paragraph' ) ], 1 );
  624. pos.getTransformedByOperation( op );
  625. expect( pos._getTransformedByInsertion.calledWithExactly( op.position, op.howMany ) ).to.be.true;
  626. } );
  627. } );
  628. describe( 'by MarkerOperation', () => {
  629. it( 'nothing should change', () => {
  630. const op = new MarkerOperation(
  631. 'marker', null, new Range( Position._createAt( root, 1 ), Position._createAt( root, 6 ) ), model.markers, true, 1
  632. );
  633. const transformed = pos.getTransformedByOperation( op );
  634. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  635. } );
  636. } );
  637. describe( 'by MoveOperation', () => {
  638. it( 'should use _getTransformedByMove', () => {
  639. sinon.spy( pos, '_getTransformedByMove' );
  640. const op = new MoveOperation( new Position( root, [ 1 ] ), 2, new Position( root, [ 5 ] ), 1 );
  641. pos.getTransformedByOperation( op );
  642. expect( pos._getTransformedByMove.calledWithExactly( op.sourcePosition, op.targetPosition, op.howMany ) ).to.be.true;
  643. } );
  644. } );
  645. describe( 'by RenameOperation', () => {
  646. it( 'nothing should change', () => {
  647. const op = new RenameOperation( new Position( root, [ 3 ] ), 'old', 'new', 1 );
  648. const transformed = pos.getTransformedByOperation( op );
  649. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  650. } );
  651. } );
  652. describe( 'by SplitOperation', () => {
  653. it( 'transformed position is at the split position', () => {
  654. const op = new SplitOperation( new Position( root, [ 3, 2 ] ), 3, null, 1 );
  655. const transformed = pos.getTransformedByOperation( op );
  656. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  657. } );
  658. it( 'transformed position is after the split position', () => {
  659. const op = new SplitOperation( new Position( root, [ 3, 1 ] ), 3, null, 1 );
  660. const transformed = pos.getTransformedByOperation( op );
  661. expect( transformed.path ).to.deep.equal( [ 4, 1 ] );
  662. } );
  663. it( 'transformed position is before the split position', () => {
  664. const op = new SplitOperation( new Position( root, [ 3, 3 ] ), 3, null, 1 );
  665. const transformed = pos.getTransformedByOperation( op );
  666. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  667. } );
  668. it( 'transformed position is after the split element', () => {
  669. const op = new SplitOperation( new Position( root, [ 3, 1, 5 ] ), 3, null, 1 );
  670. const transformed = pos.getTransformedByOperation( op );
  671. expect( transformed.path ).to.deep.equal( [ 3, 3 ] );
  672. } );
  673. it( 'transformed position is before the split element', () => {
  674. const op = new SplitOperation( new Position( root, [ 3, 3, 5 ] ), 3, null, 1 );
  675. const transformed = pos.getTransformedByOperation( op );
  676. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  677. } );
  678. it( 'transformed position is in graveyard and split position uses graveyard element', () => {
  679. pos = new Position( doc.graveyard, [ 1 ] );
  680. const op = new SplitOperation( new Position( root, [ 3, 2 ] ), 3, new Position( doc.graveyard, [ 0 ] ), 1 );
  681. const transformed = pos.getTransformedByOperation( op );
  682. expect( transformed.path ).to.deep.equal( [ 0 ] );
  683. } );
  684. } );
  685. describe( 'by MergeOperation', () => {
  686. it( 'position is inside merged element', () => {
  687. const op = new MergeOperation(
  688. new Position( root, [ 3, 0 ] ), 3, new Position( root, [ 2, 2 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  689. );
  690. const transformed = pos.getTransformedByOperation( op );
  691. expect( transformed.path ).to.deep.equal( [ 2, 4 ] );
  692. } );
  693. it( 'position is inside merged-to element', () => {
  694. const op = new MergeOperation(
  695. new Position( root, [ 4, 0 ] ), 3, new Position( root, [ 3, 5 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  696. );
  697. const transformed = pos.getTransformedByOperation( op );
  698. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  699. } );
  700. it( 'position is before merged element', () => {
  701. const op = new MergeOperation(
  702. new Position( root, [ 3, 2, 0 ] ), 3, new Position( root, [ 3, 1, 2 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  703. );
  704. const transformed = pos.getTransformedByOperation( op );
  705. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  706. } );
  707. it( 'position is after merged element', () => {
  708. const op = new MergeOperation(
  709. new Position( root, [ 3, 1, 0 ] ), 3, new Position( root, [ 3, 0, 2 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  710. );
  711. const transformed = pos.getTransformedByOperation( op );
  712. expect( transformed.path ).to.deep.equal( [ 3, 1 ] );
  713. } );
  714. it( 'position is inside graveyard', () => {
  715. pos = new Position( doc.graveyard, [ 0 ] );
  716. const op = new MergeOperation(
  717. new Position( root, [ 3, 1, 0 ] ), 3, new Position( root, [ 3, 0, 2 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  718. );
  719. const transformed = pos.getTransformedByOperation( op );
  720. expect( transformed.path ).to.deep.equal( [ 1 ] );
  721. } );
  722. it( 'merge source position is before merge target position and position is in merged element', () => {
  723. const op = new MergeOperation(
  724. new Position( root, [ 3, 0 ] ), 3, new Position( root, [ 4, 5 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  725. );
  726. const transformed = pos.getTransformedByOperation( op );
  727. expect( transformed.path ).to.deep.equal( [ 3, 7 ] );
  728. } );
  729. } );
  730. } );
  731. describe( '_getTransformedByInsertion()', () => {
  732. it( 'should return a new Position instance', () => {
  733. const position = new Position( root, [ 0 ] );
  734. const transformed = position._getTransformedByInsertion( new Position( root, [ 2 ] ), 4 );
  735. expect( transformed ).not.to.equal( position );
  736. expect( transformed ).to.be.instanceof( Position );
  737. } );
  738. it( 'should increment offset if insertion is in the same parent and closer offset', () => {
  739. const position = new Position( root, [ 1, 2, 3 ] );
  740. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 2 ] ), 2 );
  741. expect( transformed.offset ).to.equal( 5 );
  742. } );
  743. it( 'should not increment offset if insertion position is in different root', () => {
  744. const position = new Position( root, [ 1, 2, 3 ] );
  745. const transformed = position._getTransformedByInsertion( new Position( otherRoot, [ 1, 2, 2 ] ), 2 );
  746. expect( transformed.offset ).to.equal( 3 );
  747. } );
  748. it( 'should increment offset if insertion is in the same parent and the same offset', () => {
  749. const position = new Position( root, [ 1, 2, 3 ] );
  750. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2 );
  751. expect( transformed.offset ).to.equal( 5 );
  752. } );
  753. it( 'should increment offset if insertion is in the same parent and the same offset and it is inserted before', () => {
  754. const position = new Position( root, [ 1, 2, 3 ] );
  755. position.stickiness = 'toNext';
  756. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2 );
  757. expect( transformed.offset ).to.equal( 5 );
  758. } );
  759. it( 'should not increment offset if insertion is in the same parent and further offset', () => {
  760. const position = new Position( root, [ 1, 2, 3 ] );
  761. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 4 ] ), 2 );
  762. expect( transformed.offset ).to.equal( 3 );
  763. } );
  764. it( 'should update path if insertion position parent is a node from that path and offset is before next node on that path', () => {
  765. const position = new Position( root, [ 1, 2, 3 ] );
  766. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2 ] ), 2 );
  767. expect( transformed.path ).to.deep.equal( [ 1, 4, 3 ] );
  768. } );
  769. it( 'should not update path if insertion position parent is a node from that path and offset is ' +
  770. 'after next node on that path', () => {
  771. const position = new Position( root, [ 1, 2, 3 ] );
  772. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 3 ] ), 2 );
  773. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  774. } );
  775. it( 'should not update if insertion is in different path', () => {
  776. const position = new Position( root, [ 1, 1 ] );
  777. const transformed = position._getTransformedByInsertion( new Position( root, [ 2, 0 ] ), 2 );
  778. expect( transformed.path ).to.deep.equal( [ 1, 1 ] );
  779. } );
  780. } );
  781. describe( '_getTransformedByDeletion()', () => {
  782. it( 'should return a new Position instance', () => {
  783. const position = new Position( root, [ 0 ] );
  784. const transformed = position._getTransformedByDeletion( new Position( root, [ 2 ] ), 4 );
  785. expect( transformed ).not.to.equal( position );
  786. expect( transformed ).to.be.instanceof( Position );
  787. } );
  788. it( 'should return null if original position is inside one of removed nodes', () => {
  789. const position = new Position( root, [ 1, 2 ] );
  790. const transformed = position._getTransformedByDeletion( new Position( root, [ 0 ] ), 2 );
  791. expect( transformed ).to.be.null;
  792. } );
  793. it( 'should decrement offset if deletion is in the same parent and closer offset', () => {
  794. const position = new Position( root, [ 1, 2, 7 ] );
  795. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 2 ] ), 2 );
  796. expect( transformed.offset ).to.equal( 5 );
  797. } );
  798. it( 'should return null if original position is between removed nodes', () => {
  799. const position = new Position( root, [ 1, 2, 4 ] );
  800. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 3 ] ), 5 );
  801. expect( transformed ).to.be.null;
  802. } );
  803. it( 'should not decrement offset if deletion position is in different root', () => {
  804. const position = new Position( root, [ 1, 2, 3 ] );
  805. const transformed = position._getTransformedByDeletion( new Position( otherRoot, [ 1, 2, 1 ] ), 2 );
  806. expect( transformed.offset ).to.equal( 3 );
  807. } );
  808. it( 'should not decrement offset if deletion is in the same parent and further offset', () => {
  809. const position = new Position( root, [ 1, 2, 3 ] );
  810. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 4 ] ), 2 );
  811. expect( transformed.offset ).to.equal( 3 );
  812. } );
  813. it( 'should update path if deletion position parent is a node from that path and offset is before next node on that path', () => {
  814. const position = new Position( root, [ 1, 2, 3 ] );
  815. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 0 ] ), 2 );
  816. expect( transformed.path ).to.deep.equal( [ 1, 0, 3 ] );
  817. } );
  818. it( 'should not update path if deletion position parent is a node from that path and ' +
  819. 'offset is after next node on that path', () => {
  820. const position = new Position( root, [ 1, 2, 3 ] );
  821. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 3 ] ), 2 );
  822. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  823. } );
  824. } );
  825. describe( '_getTransformedByMove()', () => {
  826. it( 'should increment offset if a range was moved to the same parent and closer offset', () => {
  827. const position = new Position( root, [ 1, 2, 3 ] );
  828. const transformed = position._getTransformedByMove( new Position( root, [ 2 ] ), new Position( root, [ 1, 2, 0 ] ), 3, false );
  829. expect( transformed.path ).to.deep.equal( [ 1, 2, 6 ] );
  830. } );
  831. it( 'should decrement offset if a range was moved from the same parent and closer offset', () => {
  832. const position = new Position( root, [ 1, 2, 6 ] );
  833. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  834. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  835. } );
  836. it( 'should decrement offset if position was at the end of a range and move was not sticky', () => {
  837. const position = new Position( root, [ 1, 2, 3 ] );
  838. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  839. expect( transformed.path ).to.deep.equal( [ 1, 2, 0 ] );
  840. } );
  841. it( 'should update path if position was at the end of a range and move was sticky', () => {
  842. const position = new Position( root, [ 1, 2, 3 ] );
  843. position.stickiness = 'toPrevious';
  844. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  845. expect( transformed.path ).to.deep.equal( [ 5 ] );
  846. } );
  847. it( 'should update path if a range contained this position', () => {
  848. const position = new Position( root, [ 1, 2, 3 ] );
  849. const transformed = position._getTransformedByMove( new Position( root, [ 1, 1 ] ), new Position( root, [ 2, 1 ] ), 3, false );
  850. expect( transformed.path ).to.deep.equal( [ 2, 2, 3 ] );
  851. } );
  852. it( 'should not update if targetPosition is equal to sourcePosition (because nothing is really moving)', () => {
  853. const position = new Position( root, [ 3 ] );
  854. const transformed = position._getTransformedByMove( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ), 3, false );
  855. expect( transformed.path ).to.deep.equal( [ 3 ] );
  856. } );
  857. it( 'should update if position is before moved range and sticks to next node', () => {
  858. const position = new Position( root, [ 2, 1 ] );
  859. position.stickiness = 'toNext';
  860. const transformed = position._getTransformedByMove( new Position( root, [ 2, 1 ] ), new Position( root, [ 3, 3 ] ), 2, false );
  861. expect( transformed.path ).to.deep.equal( [ 3, 3 ] );
  862. } );
  863. } );
  864. describe( '_getCombined()', () => {
  865. it( 'should return correct combination of this and given positions', () => {
  866. const position = new Position( root, [ 1, 3, 4, 2 ] );
  867. const sourcePosition = new Position( root, [ 1, 1 ] );
  868. const targetPosition = new Position( root, [ 2, 5 ] );
  869. const combined = position._getCombined( sourcePosition, targetPosition );
  870. expect( combined.path ).to.deep.equal( [ 2, 7, 4, 2 ] );
  871. } );
  872. } );
  873. describe( 'getShiftedBy()', () => {
  874. it( 'should return a new instance of Position with offset changed by shift value', () => {
  875. const position = new Position( root, [ 1, 2, 3 ] );
  876. const shifted = position.getShiftedBy( 2 );
  877. expect( shifted ).to.be.instanceof( Position );
  878. expect( shifted ).to.not.equal( position );
  879. expect( shifted.path ).to.deep.equal( [ 1, 2, 5 ] );
  880. } );
  881. it( 'should accept negative values', () => {
  882. const position = new Position( root, [ 1, 2, 3 ] );
  883. const shifted = position.getShiftedBy( -2 );
  884. expect( shifted.path ).to.deep.equal( [ 1, 2, 1 ] );
  885. } );
  886. it( 'should not let setting offset lower than zero', () => {
  887. const position = new Position( root, [ 1, 2, 3 ] );
  888. const shifted = position.getShiftedBy( -7 );
  889. expect( shifted.path ).to.deep.equal( [ 1, 2, 0 ] );
  890. } );
  891. } );
  892. describe( 'toJSON()', () => {
  893. it( 'should serialize position', () => {
  894. const position = new Position( root, [ 0 ] );
  895. const serialized = position.toJSON();
  896. expect( serialized ).to.deep.equal( { root: 'main', path: [ 0 ], stickiness: 'toNone' } );
  897. } );
  898. it( 'should serialize position from graveyard', () => {
  899. const position = new Position( doc.graveyard, [ 0 ] );
  900. position.stickiness = 'toPrevious';
  901. const serialized = position.toJSON();
  902. expect( serialized ).to.deep.equal( { root: '$graveyard', path: [ 0 ], stickiness: 'toPrevious' } );
  903. } );
  904. } );
  905. describe( 'fromJSON()', () => {
  906. it( 'should create object with given document', () => {
  907. const deserialized = Position.fromJSON( { root: 'main', path: [ 0, 1, 2 ] }, doc );
  908. expect( deserialized.root ).to.equal( root );
  909. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  910. } );
  911. it( 'should create object from graveyard', () => {
  912. const deserialized = Position.fromJSON( { root: '$graveyard', path: [ 0, 1, 2 ] }, doc );
  913. expect( deserialized.root ).to.equal( doc.graveyard );
  914. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  915. } );
  916. it( 'should throw error when creating object in document that does not have provided root', () => {
  917. expectToThrowCKEditorError( () => {
  918. Position.fromJSON( { root: 'noroot', path: [ 0 ] }, doc );
  919. }, /model-position-fromjson-no-root/, model );
  920. } );
  921. } );
  922. describe( 'getCommonAncestor()', () => {
  923. it( 'returns null when roots of both positions are not the same', () => {
  924. const pos1 = new Position( root, [ 0 ] );
  925. const pos2 = new Position( otherRoot, [ 0 ] );
  926. testAncestor( pos1, pos2, null );
  927. } );
  928. it( 'for two the same positions returns the parent element #1', () => {
  929. const fPosition = new Position( root, [ 1, 0, 0 ] );
  930. const otherPosition = new Position( root, [ 1, 0, 0 ] );
  931. testAncestor( fPosition, otherPosition, li1 );
  932. } );
  933. it( 'for two the same positions returns the parent element #2', () => {
  934. const model = new Model();
  935. const doc = model.document;
  936. const root = doc.createRoot();
  937. const p = new Element( 'p', null, 'foobar' );
  938. root._appendChild( p );
  939. const postion = new Position( root, [ 0, 3 ] ); // <p>foo^bar</p>
  940. testAncestor( postion, postion, p );
  941. } );
  942. it( 'for two positions in the same element returns the element', () => {
  943. const fPosition = new Position( root, [ 1, 0, 0 ] );
  944. const zPosition = new Position( root, [ 1, 0, 2 ] );
  945. testAncestor( fPosition, zPosition, li1 );
  946. } );
  947. it( 'works when one positions is nested deeper than the other', () => {
  948. const zPosition = new Position( root, [ 1, 0, 2 ] );
  949. const liPosition = new Position( root, [ 1, 1 ] );
  950. testAncestor( liPosition, zPosition, ul );
  951. } );
  952. // Checks if by mistake someone didn't use getCommonPath() + getNodeByPath().
  953. it( 'works if position is located before an element', () => {
  954. const model = new Model();
  955. const doc = model.document;
  956. const root = doc.createRoot();
  957. const p = new Element( 'p', null, new Element( 'a' ) );
  958. root._appendChild( p );
  959. const postion = new Position( root, [ 0, 0 ] ); // <p>^<a></a></p>
  960. testAncestor( postion, postion, p );
  961. } );
  962. it( 'works fine with positions located in DocumentFragment', () => {
  963. const docFrag = new DocumentFragment( [ p, ul ] );
  964. const zPosition = new Position( docFrag, [ 1, 0, 2 ] );
  965. const afterLiPosition = new Position( docFrag, [ 1, 2 ] );
  966. testAncestor( zPosition, afterLiPosition, ul );
  967. } );
  968. function testAncestor( positionA, positionB, lca ) {
  969. expect( positionA.getCommonAncestor( positionB ) ).to.equal( lca );
  970. expect( positionB.getCommonAncestor( positionA ) ).to.equal( lca );
  971. }
  972. } );
  973. describe( 'getTextNodeAtPosition() util', () => {
  974. it( 'returns a text node at the given position', () => {
  975. const position = new Position( root, [ 1, 0, 1 ] );
  976. const positionParent = position.parent;
  977. expect( getTextNodeAtPosition( position, positionParent ) ).to.equal( foz );
  978. } );
  979. // This util is covered with tests by Position#textNode tests.
  980. } );
  981. describe( 'getNodeAfterPosition() util', () => {
  982. it( 'returns a node after the position', () => {
  983. const position = new Position( root, [ 1, 0 ] );
  984. const positionParent = position.parent;
  985. const textNode = getTextNodeAtPosition( position, positionParent );
  986. expect( getNodeAfterPosition( position, positionParent, textNode ) ).to.equal( li1 );
  987. } );
  988. // This util is covered with tests by Position#nodeAfter tests.
  989. } );
  990. describe( 'getNodeBeforePosition() util', () => {
  991. it( 'returns a node before the position', () => {
  992. const position = new Position( root, [ 1, 1 ] );
  993. const positionParent = position.parent;
  994. const textNode = getTextNodeAtPosition( position, positionParent );
  995. expect( getNodeBeforePosition( position, positionParent, textNode ) ).to.equal( li1 );
  996. } );
  997. // This util is covered with tests by Position#nodeBefore tests.
  998. } );
  999. } );