writer.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals beforeEach, describe, it */
  6. /* bender-tags: treeview */
  7. 'use strict';
  8. import Writer from '/ckeditor5/core/treeview/writer.js';
  9. import Element from '/ckeditor5/core/treeview/element.js';
  10. import Text from '/ckeditor5/core/treeview/text.js';
  11. import Position from '/ckeditor5/core/treeview/position.js';
  12. import Range from '/ckeditor5/core/treeview/range.js';
  13. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  14. describe( 'Writer', () => {
  15. let writer;
  16. beforeEach( () => {
  17. writer = new Writer();
  18. } );
  19. /**
  20. * Helper function that is used to test output of writer methods by providing declarative description of the
  21. * expected output.
  22. * Examples:
  23. * test element: `<p>fo[o<b>ba]r</b></p>`
  24. * description: {
  25. * name: 'p',
  26. * instanceOf: Element,
  27. * children:[
  28. * {
  29. * instanceOf: Text,
  30. * data: 'foo',
  31. * rangeStart: 2
  32. * },
  33. * {
  34. * name: 'b'
  35. * instanceOf: Element
  36. * priority: 1,
  37. * children: [
  38. * { instanceOf: Text, data: 'bar', rangeEnd: 2 }
  39. * ]
  40. * }
  41. * ]
  42. * }
  43. *
  44. *
  45. * @param {core.treeView.Writer} writer Writer instance. Used to test priority.
  46. * @param {core.treeView.Range|core.treeView.Position } location Range instance or Position instance.
  47. * Treated as Range when when `rangeStart`, `rangeEnd` is used, treated as Position when `position` is used.
  48. * @param {core.treeView.Node} node Element to check.
  49. * @param {Object} description Object describing expected element and its children.
  50. */
  51. function test( writer, location, node, description ) {
  52. if ( description.instanceOf ) {
  53. expect( node ).to.be.instanceof( description.instanceOf );
  54. }
  55. if ( description.name ) {
  56. expect( description.name ).to.equal( node.name );
  57. }
  58. if ( description.data ) {
  59. expect( description.data ).to.equal( node.data );
  60. }
  61. if ( description.priority !== undefined ) {
  62. expect( description.priority ).to.equal( writer.getPriority( node ) );
  63. }
  64. if ( description.rangeStart !== undefined ) {
  65. expect( node ).to.equal( location.start.parent );
  66. expect( description.rangeStart ).to.equal( location.start.offset );
  67. }
  68. if ( description.rangeEnd !== undefined ) {
  69. expect( node ).to.equal( location.end.parent );
  70. expect( description.rangeEnd ).to.equal( location.end.offset );
  71. }
  72. if ( description.attributes ) {
  73. Object.keys( description.attributes ).forEach( ( key ) => {
  74. expect( description.attributes[ key ] ).to.equal( node.getAttribute( key ) );
  75. } );
  76. }
  77. if ( description.position !== undefined ) {
  78. expect( node ).to.equal( location.parent );
  79. expect( description.position ).to.equal( location.offset );
  80. }
  81. if ( description.children ) {
  82. expect( description.children.length ).to.equal( node.getChildCount() );
  83. description.children.forEach( ( desc, index ) => {
  84. test( writer, location, node.getChild( index ), desc );
  85. } );
  86. }
  87. }
  88. /**
  89. * Helper function that is used to create treeView elements from description object.
  90. *
  91. * @param {core.treeView.Writer} writer Writer instance. Used to set priorities.
  92. * @param {Object} description Description object.
  93. * @param {core.treeView.Range} [range] Optional parameter, used in recurrent calls.
  94. * @param {core.treeView.Position} [position] Optional parameter, used in recurrent calls.
  95. * @returns {Object} Returns object with `node`, `range`, `position` fields, containing created node and, optionally
  96. * range and position if description object contain information about them.
  97. */
  98. function create( writer, description, range, position ) {
  99. const node = new description.instanceOf();
  100. if ( !range ) {
  101. range = Range.createFromParentsAndOffsets( node, 0, node, 0 );
  102. }
  103. if ( !position ) {
  104. position = new Position( node, 0 );
  105. }
  106. if ( description.name ) {
  107. node.name = description.name;
  108. }
  109. if ( description.data ) {
  110. node.data = description.data;
  111. }
  112. if ( description.attributes ) {
  113. Object.keys( description.attributes ).forEach( ( key ) => {
  114. node.setAttribute( key, description.attributes[ key ] );
  115. } );
  116. }
  117. if ( description.priority !== undefined ) {
  118. writer.setPriority( node, description.priority );
  119. }
  120. if ( description.rangeStart !== undefined ) {
  121. range.start.parent = node;
  122. range.start.offset = description.rangeStart;
  123. }
  124. if ( description.rangeEnd !== undefined ) {
  125. range.end.parent = node;
  126. range.end.offset = description.rangeEnd;
  127. }
  128. if ( description.position !== undefined ) {
  129. position.parent = node;
  130. position.offset = description.position;
  131. }
  132. if ( description.children ) {
  133. description.children.forEach( ( desc, index ) => {
  134. const created = create( writer, desc, range, position );
  135. node.insertChildren( index, created.node );
  136. } );
  137. }
  138. return { node, range, position };
  139. }
  140. describe( 'isContainer', () => {
  141. it( 'should return true for container elements', () => {
  142. const containerElement = new Element( 'p' );
  143. const attributeElement = new Element( 'b' );
  144. writer._priorities.set( attributeElement, 1 );
  145. expect( writer.isContainer( containerElement ) ).to.be.true;
  146. expect( writer.isContainer( attributeElement ) ).to.be.false;
  147. } );
  148. } );
  149. describe( 'isAttribute', () => {
  150. it( 'should return true for container elements', () => {
  151. const containerElement = new Element( 'p' );
  152. const attributeElement = new Element( 'b' );
  153. writer._priorities.set( attributeElement, 1 );
  154. expect( writer.isAttribute( containerElement ) ).to.be.false;
  155. expect( writer.isAttribute( attributeElement ) ).to.be.true;
  156. } );
  157. } );
  158. describe( 'setPriority', () => {
  159. it( 'sets node priority', () => {
  160. const nodeMock = {};
  161. writer.setPriority( nodeMock, 10 );
  162. expect( writer._priorities.get( nodeMock ) ).to.equal( 10 );
  163. } );
  164. } );
  165. describe( 'getPriority', () => {
  166. it( 'gets node priority', () => {
  167. const nodeMock = {};
  168. writer._priorities.set( nodeMock, 12 );
  169. expect( writer.getPriority( nodeMock ) ).to.equal( 12 );
  170. } );
  171. } );
  172. describe( 'getParentContainer', () => {
  173. it( 'should return parent container of the node', () => {
  174. const text = new Text( 'foobar' );
  175. const b = new Element( 'b', null, [ text ] );
  176. const parent = new Element( 'p', null, [ b ] );
  177. writer.setPriority( b, 1 );
  178. const container = writer.getParentContainer( new Position( text, 0 ) );
  179. expect( container ).to.equal( parent );
  180. } );
  181. it( 'should return null if no parent container', () => {
  182. const text = new Text( 'foobar' );
  183. const b = new Element( 'b', null, [ text ] );
  184. writer.setPriority( b, 1 );
  185. const container = writer.getParentContainer( new Position( text, 0 ) );
  186. expect( container ).to.equal( null );
  187. } );
  188. } );
  189. describe( 'breakAttributes', () => {
  190. // <p>{|foobar}</p> -> <p>|{foobar}</p>
  191. it( '<p>{|foobar}</p>', () => {
  192. const created = create( writer, {
  193. instanceOf: Element,
  194. name: 'p',
  195. children: [
  196. { instanceOf: Text, data: 'foobar', position: 0 }
  197. ]
  198. } );
  199. const newPosition = writer.breakAttributes( created.position );
  200. test( writer, newPosition, created.node, {
  201. instanceOf: Element,
  202. name: 'p',
  203. position: 0,
  204. children: [
  205. { instanceOf: Text, data: 'foobar' }
  206. ]
  207. } );
  208. } );
  209. it( '<p>foo|bar</p>', () => {
  210. // <p>{foo|bar}</p> -> <p>{foo}|{bar}</p>
  211. const created = create( writer, {
  212. instanceOf: Element,
  213. name: 'p',
  214. children: [
  215. { instanceOf: Text, data: 'foobar', position: 3 }
  216. ]
  217. } );
  218. const newPosition = writer.breakAttributes( created.position );
  219. test( writer, newPosition, created.node, {
  220. instanceOf: Element,
  221. name: 'p',
  222. position: 1,
  223. children: [
  224. { instanceOf: Text, data: 'foo' },
  225. { instanceOf: Text, data: 'bar' }
  226. ]
  227. } );
  228. } );
  229. it( '<p>{foobar|}</p>', () => {
  230. // <p>{foobar|}</p> -> <p>{foobar}|</p>
  231. const created = create( writer, {
  232. instanceOf: Element,
  233. name: 'p',
  234. children: [
  235. { instanceOf: Text, data: 'foobar', position: 6 }
  236. ]
  237. } );
  238. const newPosition = writer.breakAttributes( created.position );
  239. test( writer, newPosition, created.node, {
  240. instanceOf: Element,
  241. name: 'p',
  242. position: 1,
  243. children: [
  244. { instanceOf: Text, data: 'foobar' }
  245. ]
  246. } );
  247. } );
  248. it( '<p><b>{foo|bar}</b></p>', () => {
  249. // <p><b>{foo|bar}</b></p> -> <p><b>{foo}</b>|<b>{bar}</b></p>
  250. const created = create( writer, {
  251. instanceOf: Element,
  252. name: 'p',
  253. children: [
  254. {
  255. instanceOf: Element,
  256. name: 'b',
  257. priority: 1,
  258. children: [
  259. { instanceOf: Text, data: 'foobar', position: 3 }
  260. ]
  261. }
  262. ]
  263. } );
  264. const newPosition = writer.breakAttributes( created.position );
  265. test( writer, newPosition, created.node, {
  266. instanceOf: Element,
  267. name: 'p',
  268. position: 1,
  269. children: [
  270. {
  271. instanceOf: Element,
  272. name: 'b',
  273. priority: 1,
  274. children: [
  275. { instanceOf: Text, data: 'foo' }
  276. ]
  277. },
  278. {
  279. instanceOf: Element,
  280. name: 'b',
  281. priority: 1,
  282. children: [
  283. { instanceOf: Text, data: 'bar' }
  284. ]
  285. }
  286. ]
  287. } );
  288. } );
  289. it( '<p><b><u>{|foobar}</u></b></p>', () => {
  290. // <p><b><u>{|foobar}</u></b></p> -> <p>|<b><u>{foobar}</u></b></p>
  291. const created = create( writer, {
  292. instanceOf: Element,
  293. name: 'p',
  294. children: [
  295. {
  296. instanceOf: Element,
  297. name: 'b',
  298. priority: 1,
  299. children: [
  300. {
  301. instanceOf: Element,
  302. name: 'u',
  303. priority: 1,
  304. children: [
  305. { instanceOf: Text, data: 'foobar', position: 0 }
  306. ]
  307. }
  308. ]
  309. }
  310. ]
  311. } );
  312. const newPosition = writer.breakAttributes( created.position );
  313. test( writer, newPosition, created.node, {
  314. instanceOf: Element,
  315. name: 'p',
  316. position: 0,
  317. children: [
  318. {
  319. instanceOf: Element,
  320. name: 'b',
  321. priority: 1,
  322. children: [
  323. {
  324. instanceOf: Element,
  325. name: 'u',
  326. priority: 1,
  327. children: [
  328. { instanceOf: Text, data: 'foobar' }
  329. ]
  330. }
  331. ]
  332. }
  333. ]
  334. } );
  335. } );
  336. // <p><b><u>{foo|ba}r</u></b></p> -> <p><b><u>{foo}</u></b>|<b></u>{bar}</u></b></p>
  337. it( '<p><b><u>{foo|bar}</u></b></p>', () => {
  338. const created = create( writer, {
  339. instanceOf: Element,
  340. name: 'p',
  341. children: [
  342. {
  343. instanceOf: Element,
  344. name: 'b',
  345. priority: 1,
  346. children: [
  347. {
  348. instanceOf: Element,
  349. name: 'u',
  350. priority: 1,
  351. children: [
  352. { instanceOf: Text, data: 'foobar', position: 3 }
  353. ]
  354. }
  355. ]
  356. }
  357. ]
  358. } );
  359. const newPosition = writer.breakAttributes( created.position );
  360. test( writer, newPosition, created.node, {
  361. instanceOf: Element,
  362. name: 'p',
  363. position: 1,
  364. children: [
  365. {
  366. instanceOf: Element,
  367. name: 'b',
  368. priority: 1,
  369. children: [
  370. {
  371. instanceOf: Element,
  372. name: 'u',
  373. priority: 1,
  374. children: [
  375. { instanceOf: Text, data: 'foo' }
  376. ]
  377. }
  378. ]
  379. },
  380. {
  381. instanceOf: Element,
  382. name: 'b',
  383. priority: 1,
  384. children: [
  385. {
  386. instanceOf: Element,
  387. name: 'u',
  388. priority: 1,
  389. children: [
  390. { instanceOf: Text, data: 'bar' }
  391. ]
  392. }
  393. ]
  394. }
  395. ]
  396. } );
  397. } );
  398. it( '<p><b><u>{foobar|}</u></b></p>', () => {
  399. // <p><b><u>{foobar|}</u></b></p> -> <p><b><u>{foobar}</u></b>|</p>
  400. const created = create( writer, {
  401. instanceOf: Element,
  402. name: 'p',
  403. children: [
  404. {
  405. instanceOf: Element,
  406. name: 'b',
  407. priority: 1,
  408. children: [
  409. {
  410. instanceOf: Element,
  411. name: 'u',
  412. priority: 1,
  413. children: [
  414. { instanceOf: Text, data: 'foobar', position: 6 }
  415. ]
  416. }
  417. ]
  418. }
  419. ]
  420. } );
  421. const newPosition = writer.breakAttributes( created.position );
  422. test( writer, newPosition, created.node, {
  423. instanceOf: Element,
  424. name: 'p',
  425. position: 1,
  426. children: [
  427. {
  428. instanceOf: Element,
  429. name: 'b',
  430. priority: 1,
  431. children: [
  432. {
  433. instanceOf: Element,
  434. name: 'u',
  435. priority: 1,
  436. children: [
  437. { instanceOf: Text, data: 'foobar' }
  438. ]
  439. }
  440. ]
  441. }
  442. ]
  443. } );
  444. } );
  445. } );
  446. describe( 'mergeAttributes', () => {
  447. it( 'should not merge if inside text node', () => {
  448. // <p>{fo|obar}</p>
  449. const description = {
  450. instanceOf: Element,
  451. name: 'p',
  452. children: [
  453. { instanceOf: Text, data: 'foobar', position: 2 }
  454. ]
  455. };
  456. const created = create( writer, description );
  457. const newPosition = writer.mergeAttributes( created.position );
  458. test( writer, newPosition, created.node, description );
  459. } );
  460. it( 'should not merge if between containers', () => {
  461. // <div><p>{foo}</p>|<p>{bar}</p></div>
  462. const description = {
  463. instanceOf: Element,
  464. name: 'div',
  465. position: 1,
  466. children: [
  467. {
  468. instanceOf: Element,
  469. name: 'p',
  470. children: [
  471. { instanceOf: Text, data: 'foo' }
  472. ]
  473. },
  474. {
  475. instanceOf: Element,
  476. name: 'p',
  477. children: [
  478. { instanceOf: Text, data: 'bar' }
  479. ]
  480. }
  481. ]
  482. };
  483. const created = create( writer, description );
  484. const newPosition = writer.mergeAttributes( created.position );
  485. test( writer, newPosition, created.node, description );
  486. } );
  487. it( 'should return same position when inside empty container', () => {
  488. // <p>|</p>
  489. const description = { instanceOf: Element, name: 'p', position: 0 };
  490. const created = create( writer, description );
  491. const newPosition = writer.mergeAttributes( created.position );
  492. test( writer, newPosition, created.node, description );
  493. } );
  494. it( 'should not merge when position is placed at the beginning of the container', () => {
  495. // <p>|<b></b></p>
  496. const description = {
  497. instanceOf: Element,
  498. name: 'p',
  499. position: 0,
  500. children: [
  501. {
  502. instanceOf: Element,
  503. name: 'b',
  504. priority: 1
  505. }
  506. ]
  507. };
  508. const created = create( writer, description );
  509. const newPosition = writer.mergeAttributes( created.position );
  510. test( writer, newPosition, created.node, description );
  511. } );
  512. it( 'should not merge when position is placed at the end of the container', () => {
  513. // <p><b></b>|</p>
  514. const description = {
  515. instanceOf: Element,
  516. name: 'p',
  517. position: 1,
  518. children: [
  519. {
  520. instanceOf: Element,
  521. name: 'b',
  522. priority: 1
  523. }
  524. ]
  525. };
  526. const created = create( writer, description );
  527. const newPosition = writer.mergeAttributes( created.position );
  528. test( writer, newPosition, created.node, description );
  529. } );
  530. it( 'should merge when placed between two text nodes', () => {
  531. // <p>{foo}|{bar}</p> -> <p>{foo|bar}</p>
  532. const created = create( writer, {
  533. instanceOf: Element,
  534. name: 'p',
  535. position: 1,
  536. children: [
  537. { instanceOf: Text, data: 'foo' },
  538. { instanceOf: Text, data: 'bar' }
  539. ]
  540. } );
  541. const newPosition = writer.mergeAttributes( created.position );
  542. test( writer, newPosition, created.node, {
  543. instanceOf: Element,
  544. name: 'p',
  545. children: [
  546. { instanceOf: Text, data: 'foobar', position: 3 }
  547. ]
  548. } );
  549. } );
  550. it( 'should merge when placed between similar attribute nodes', () => {
  551. // <p><b foo="bar"></b>|<b foo="bar"></b></p> -> <p><b foo="bar">|</b></p>
  552. const created = create( writer, {
  553. instanceOf: Element,
  554. name: 'p',
  555. position: 1,
  556. children: [
  557. {
  558. instanceOf: Element,
  559. name: 'b',
  560. priority: 1,
  561. attributes: { foo: 'bar' }
  562. },
  563. {
  564. instanceOf: Element,
  565. name: 'b',
  566. priority: 1,
  567. attributes: { foo: 'bar' }
  568. }
  569. ]
  570. } );
  571. const newPosition = writer.mergeAttributes( created.position );
  572. test( writer, newPosition, created.node, {
  573. instanceOf: Element,
  574. name: 'p',
  575. children: [
  576. {
  577. instanceOf: Element,
  578. name: 'b',
  579. priority: 1,
  580. position: 0,
  581. attributes: { foo: 'bar' }
  582. }
  583. ]
  584. } );
  585. } );
  586. it( 'should not merge when placed between non-similar attribute nodes', () => {
  587. // <p><b foo="bar"></b>|<b foo="baz"></b></p> ->
  588. // <p><b foo="bar"></b>|<b foo="baz"></b></p>
  589. const description = {
  590. instanceOf: Element,
  591. name: 'p',
  592. position: 1,
  593. children: [
  594. {
  595. instanceOf: Element,
  596. name: 'b',
  597. priority: 1,
  598. attributes: { foo: 'bar' }
  599. },
  600. {
  601. instanceOf: Element,
  602. name: 'b',
  603. priority: 1,
  604. attributes: { foo: 'baz' }
  605. }
  606. ]
  607. };
  608. const created = create( writer, description );
  609. const newPosition = writer.mergeAttributes( created.position );
  610. test( writer, newPosition, created.node, description );
  611. } );
  612. it( 'should not merge when placed between similar attribute nodes with different priority', () => {
  613. // <p><b foo="bar"></b>|<b foo="bar"></b></p> -> <p><b foo="bar"></b>|<b foo="bar"></b></p>
  614. const description = {
  615. instanceOf: Element,
  616. name: 'p',
  617. position: 1,
  618. children: [
  619. {
  620. instanceOf: Element,
  621. name: 'b',
  622. priority: 1,
  623. attributes: { foo: 'bar' }
  624. },
  625. {
  626. instanceOf: Element,
  627. name: 'b',
  628. priority: 2,
  629. attributes: { foo: 'bar' }
  630. }
  631. ]
  632. };
  633. const created = create( writer,description );
  634. const newPosition = writer.mergeAttributes( created.position );
  635. test( writer, newPosition, created.node, description );
  636. } );
  637. it( 'should merge attribute nodes and their contents if possible', () => {
  638. // <p><b foo="bar">{foo}</b>|<b foo="bar">{bar}</b></p>
  639. // <p><b foo="bar">{foo}|{bar}</b></p>
  640. // <p><b foo="bar">{foo|bar}</b></p>
  641. const created = create( writer, {
  642. instanceOf: Element,
  643. name: 'p',
  644. position: 1,
  645. children: [
  646. {
  647. instanceOf: Element,
  648. name: 'b',
  649. priority: 1,
  650. attributes: { foo: 'bar' },
  651. children: [
  652. { instanceOf: Text, data: 'foo' }
  653. ]
  654. },
  655. {
  656. instanceOf: Element,
  657. name: 'b',
  658. priority: 1,
  659. attributes: { foo: 'bar' },
  660. children: [
  661. { instanceOf: Text, data: 'bar' }
  662. ]
  663. }
  664. ]
  665. } );
  666. const newPosition = writer.mergeAttributes( created.position );
  667. test( writer, newPosition, created.node, {
  668. instanceOf: Element,
  669. name: 'p',
  670. children: [
  671. {
  672. instanceOf: Element,
  673. name: 'b',
  674. priority: 1,
  675. attributes: { foo: 'bar' },
  676. children: [
  677. { instanceOf: Text, data: 'foobar', position: 3 }
  678. ]
  679. }
  680. ]
  681. } );
  682. } );
  683. } );
  684. describe( 'insert', () => {
  685. it( 'should insert text into another text node', () => {
  686. // <p>{foo|bar}</p> insert {baz}
  687. // <p>{foo[baz]bar}</p>
  688. const created = create( writer, {
  689. instanceOf: Element,
  690. name: 'p',
  691. children: [
  692. { instanceOf: Text, data: 'foobar', position: 3 }
  693. ]
  694. } );
  695. const newRange = writer.insert( created.position, new Text( 'baz' ) );
  696. test( writer, newRange, created.node, {
  697. instanceOf: Element,
  698. name: 'p',
  699. children: [
  700. { instanceOf: Text, data: 'foobazbar' }
  701. ]
  702. } );
  703. } );
  704. it( 'should break attributes when inserting into text node', () => {
  705. // <p>{foo|bar}</p> insert <b>{baz}</b>
  706. // <p>{foo}[<b>baz</b>]{bar}</p>
  707. const created = create( writer, {
  708. instanceOf: Element,
  709. name: 'p',
  710. children: [
  711. { instanceOf: Text, data: 'foobar', position: 3 }
  712. ]
  713. } );
  714. const toInsert = create( writer, {
  715. instanceOf: Element,
  716. name: 'b',
  717. priority: 1,
  718. children: [
  719. { instanceOf: Text, data: 'baz' }
  720. ]
  721. } );
  722. const newRange = writer.insert( created.position, toInsert.node );
  723. test( writer, newRange, created.node, {
  724. instanceOf: Element,
  725. name: 'p',
  726. rangeStart: 1,
  727. rangeEnd: 2,
  728. children: [
  729. { instanceOf: Text, data: 'foo' },
  730. {
  731. instanceOf: Element,
  732. name: 'b',
  733. priority: 1,
  734. children: [
  735. { instanceOf: Text, data: 'baz' }
  736. ]
  737. },
  738. { instanceOf: Text, data: 'bar' }
  739. ]
  740. } );
  741. } );
  742. it( 'should merge same attribute nodes', () => {
  743. // <p><b>{foo|bar}</b></p> insert <b>{baz}</b>
  744. // <p><b>{foo[baz]bar}</b></p>
  745. const created = create( writer, {
  746. instanceOf: Element,
  747. name: 'p',
  748. children: [
  749. {
  750. instanceOf: Element,
  751. name: 'b',
  752. priority: 1,
  753. children: [
  754. { instanceOf: Text, data: 'foobar', position: 3 }
  755. ]
  756. }
  757. ]
  758. } );
  759. const toInsert = create( writer, {
  760. instanceOf: Element,
  761. name: 'b',
  762. priority: 1,
  763. children: [
  764. { instanceOf: Text, data: 'baz' }
  765. ]
  766. } );
  767. const newRange = writer.insert( created.position, toInsert.node );
  768. test( writer, newRange, created.node, {
  769. instanceOf: Element,
  770. name: 'p',
  771. children: [
  772. {
  773. instanceOf: Element,
  774. name: 'b',
  775. priority: 1,
  776. children: [
  777. { instanceOf: Text, data: 'foobazbar', rangeStart: 3, rangeEnd: 6 }
  778. ]
  779. }
  780. ]
  781. } );
  782. } );
  783. it( 'should not merge different attributes', () => {
  784. // <p><b>{foo|bar}</b></p> insert <b>{baz}</b> ( different priority )
  785. // <p><b>{foo}</b>[<b>{baz}</b>]<b>{bar}</b></p>
  786. const created = create( writer, {
  787. instanceOf: Element,
  788. name: 'p',
  789. children: [
  790. {
  791. instanceOf: Element,
  792. name: 'b',
  793. priority: 1,
  794. children: [
  795. { instanceOf: Text, data: 'foobar', position: 3 }
  796. ]
  797. }
  798. ]
  799. } );
  800. const toInsert = create( writer, {
  801. instanceOf: Element,
  802. name: 'b',
  803. priority: 2,
  804. children: [
  805. { instanceOf: Text, data: 'baz' }
  806. ]
  807. } );
  808. const newRange = writer.insert( created.position, toInsert.node );
  809. test( writer, newRange, created.node, {
  810. instanceOf: Element,
  811. name: 'p',
  812. rangeStart: 1,
  813. rangeEnd: 2,
  814. children: [
  815. {
  816. instanceOf: Element,
  817. name: 'b',
  818. priority: 1,
  819. children: [
  820. { instanceOf: Text, data: 'foo' }
  821. ]
  822. },
  823. {
  824. instanceOf: Element,
  825. name: 'b',
  826. priority: 2,
  827. children: [
  828. { instanceOf: Text, data: 'baz' }
  829. ]
  830. },
  831. {
  832. instanceOf: Element,
  833. name: 'b',
  834. priority: 1,
  835. children: [
  836. { instanceOf: Text, data: 'bar' }
  837. ]
  838. }
  839. ]
  840. } );
  841. } );
  842. it( 'should allow to insert multiple nodes', () => {
  843. // <p>|</p> insert <b>{foo}</b>{bar}
  844. // <p>[<b>{foo}</b>{bar}]</p>
  845. const root = new Element( 'p' );
  846. const toInsert = create( writer, {
  847. instanceOf: Element,
  848. name: 'fake',
  849. children: [
  850. {
  851. instanceOf: Element,
  852. name: 'b',
  853. priority: 1,
  854. children: [
  855. { instanceOf: Text, data: 'foo' }
  856. ]
  857. },
  858. { instanceOf: Text, data: 'bar' }
  859. ]
  860. } ).node.getChildren();
  861. const position = new Position( root, 0 );
  862. const newRange = writer.insert( position, toInsert );
  863. test( writer, newRange, root, {
  864. instanceOf: Element,
  865. name: 'p',
  866. rangeStart: 0,
  867. rangeEnd: 2,
  868. children: [
  869. {
  870. instanceOf: Element,
  871. name: 'b',
  872. priority: 1,
  873. children: [
  874. { instanceOf: Text, data: 'foo' }
  875. ]
  876. },
  877. { instanceOf: Text, data: 'bar' }
  878. ]
  879. } );
  880. } );
  881. it( 'should merge after inserting multiple nodes', () => {
  882. // <p><b>{qux}</b>|{baz}</p> insert <b>{foo}</b>{bar}
  883. // <p><b>{qux[foo}</b>{bar]baz}</p>
  884. const created = create( writer, {
  885. instanceOf: Element,
  886. name: 'p',
  887. position: 1,
  888. children: [
  889. {
  890. instanceOf: Element,
  891. name: 'b',
  892. priority: 1,
  893. children: [
  894. { instanceOf: Text, data: 'qux' }
  895. ]
  896. },
  897. { instanceOf: Text, data: 'baz' }
  898. ]
  899. } );
  900. const toInsert = create( writer, {
  901. instanceOf: Element,
  902. name: 'fake',
  903. children: [
  904. {
  905. instanceOf: Element,
  906. name: 'b',
  907. priority: 1,
  908. children: [
  909. { instanceOf: Text, data: 'foo' }
  910. ]
  911. },
  912. { instanceOf: Text, data: 'bar' }
  913. ]
  914. } ).node.getChildren();
  915. const newRange = writer.insert( created.position, toInsert );
  916. test( writer, newRange, created.node, {
  917. instanceOf: Element,
  918. name: 'p',
  919. children: [
  920. {
  921. instanceOf: Element,
  922. name: 'b',
  923. priority: 1,
  924. children: [
  925. { instanceOf: Text, data: 'quxfoo', rangeStart: 3 }
  926. ]
  927. },
  928. { instanceOf: Text, data: 'barbaz', rangeEnd: 3 }
  929. ]
  930. } );
  931. } );
  932. } );
  933. describe( 'remove', () => {
  934. it( 'should throw when range placed in two containers', () => {
  935. const p1 = new Element( 'p' );
  936. const p2 = new Element( 'p' );
  937. expect( () => {
  938. writer.remove( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  939. } ).to.throw( 'treeview-writer-invalid-range' );
  940. } );
  941. it( 'should return same range when range is collapsed', () => {
  942. const p = new Element( 'p' );
  943. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  944. const newRange = writer.remove( range );
  945. expect( newRange.isEqual( range ) ).to.be.true;
  946. } );
  947. it( 'should remove single text node', () => {
  948. // <p>[{foobar}]</p> -> <p>|</p>
  949. const created = create( writer, {
  950. instanceOf: Element,
  951. name: 'p',
  952. rangeStart: 0,
  953. rangeEnd: 1,
  954. children: [
  955. { instanceOf: Text, data: 'foobar' }
  956. ]
  957. } );
  958. const newPosition = writer.remove( created.range );
  959. test( writer, newPosition, created.node, {
  960. instanceOf: Element,
  961. name: 'p',
  962. position: 0,
  963. children: []
  964. } );
  965. } );
  966. it( 'should not leave empty text nodes', () => {
  967. // <p>{[foobar]}</p> -> <p>|</p>
  968. const created = create( writer, {
  969. instanceOf: Element,
  970. name: 'p',
  971. children: [
  972. { instanceOf: Text, data: 'foobar', rangeStart: 0, rangeEnd: 6 }
  973. ]
  974. } );
  975. const newPosition = writer.remove( created.range );
  976. test( writer, newPosition, created.node, {
  977. instanceOf: Element,
  978. name: 'p',
  979. position: 0,
  980. children: []
  981. } );
  982. } );
  983. it( 'should remove part of the text node', () => {
  984. // <p>{f[oob]ar}</p> -> <p>{f|ar}</p>
  985. const created = create( writer, {
  986. instanceOf: Element,
  987. name: 'p',
  988. children: [
  989. { instanceOf: Text, data: 'foobar', rangeStart: 1, rangeEnd: 4 }
  990. ]
  991. } );
  992. const newPosition = writer.remove( created.range );
  993. test( writer, newPosition, created.node, {
  994. instanceOf: Element,
  995. name: 'p',
  996. children: [
  997. { instanceOf: Text, data: 'far', position: 1 }
  998. ]
  999. } );
  1000. } );
  1001. it( 'should remove parts of nodes', () => {
  1002. // <p>{f[oo}<b>{ba]r}</b></p> -> <p>{f}|<b>r</b></p>
  1003. const created = create( writer, {
  1004. instanceOf: Element,
  1005. name: 'p',
  1006. children: [
  1007. { instanceOf: Text, data: 'foo', rangeStart: 1 },
  1008. {
  1009. instanceOf: Element,
  1010. name: 'b',
  1011. priority: 1,
  1012. children: [
  1013. { instanceOf: Text, data: 'bar', rangeEnd: 2 }
  1014. ]
  1015. }
  1016. ]
  1017. } );
  1018. const newPosition = writer.remove( created.range );
  1019. test( writer, newPosition, created.node, {
  1020. instanceOf: Element,
  1021. name: 'p',
  1022. position: 1,
  1023. children: [
  1024. { instanceOf: Text, data: 'f' },
  1025. {
  1026. instanceOf: Element,
  1027. name: 'b',
  1028. priority: 1,
  1029. children: [
  1030. { instanceOf: Text, data: 'r' }
  1031. ]
  1032. }
  1033. ]
  1034. } );
  1035. } );
  1036. it( 'should merge arfer removing #1', () => {
  1037. // <p><b>foo</b>[{bar}]<b>bazqux</b></p> -> <p><b>foo|bazqux</b></p>
  1038. const created = create( writer, {
  1039. instanceOf: Element,
  1040. name: 'p',
  1041. rangeStart: 1,
  1042. rangeEnd: 2,
  1043. children: [
  1044. {
  1045. instanceOf: Element,
  1046. name: 'b',
  1047. priority: 1,
  1048. children: [
  1049. { instanceOf: Text, data: 'foo' }
  1050. ]
  1051. },
  1052. { instanceOf: Text, data: 'bar' },
  1053. {
  1054. instanceOf: Element,
  1055. name: 'b',
  1056. priority: 1,
  1057. children: [
  1058. { instanceOf: Text, data: 'bazqux' }
  1059. ]
  1060. }
  1061. ]
  1062. } );
  1063. const newPosition = writer.remove( created.range );
  1064. test( writer, newPosition, created.node, {
  1065. instanceOf: Element,
  1066. name: 'p',
  1067. children: [
  1068. {
  1069. instanceOf: Element,
  1070. name: 'b',
  1071. priority: 1,
  1072. children: [
  1073. { instanceOf: Text, data: 'foobazqux', position: 3 }
  1074. ]
  1075. }
  1076. ]
  1077. } );
  1078. } );
  1079. it( 'should merge arfer removing #2', () => {
  1080. // <p><b>fo[o</b>{bar}<b>ba]zqux</b></p> -> <p><b>fo|zqux</b></p>
  1081. const created = create( writer, {
  1082. instanceOf: Element,
  1083. name: 'p',
  1084. children: [
  1085. {
  1086. instanceOf: Element,
  1087. name: 'b',
  1088. priority: 1,
  1089. children: [
  1090. { instanceOf: Text, data: 'foo', rangeStart: 2 }
  1091. ]
  1092. },
  1093. { instanceOf: Text, data: 'bar' },
  1094. {
  1095. instanceOf: Element,
  1096. name: 'b',
  1097. priority: 1,
  1098. children: [
  1099. { instanceOf: Text, data: 'bazqux', rangeEnd: 2 }
  1100. ]
  1101. }
  1102. ]
  1103. } );
  1104. const newPosition = writer.remove( created.range );
  1105. test( writer, newPosition, created.node, {
  1106. instanceOf: Element,
  1107. name: 'p',
  1108. children: [
  1109. {
  1110. instanceOf: Element,
  1111. name: 'b',
  1112. priority: 1,
  1113. children: [
  1114. { instanceOf: Text, data: 'fozqux', position: 2 }
  1115. ]
  1116. }
  1117. ]
  1118. } );
  1119. } );
  1120. } );
  1121. describe( 'wrap', () => {
  1122. // TODO: tests - different priorities
  1123. // TODO: tests - empty containers left
  1124. // TODO: merge with elements outside range at the ends
  1125. it( 'should do nothing on collapsed ranges', () => {
  1126. const description = {
  1127. instanceOf: Element,
  1128. name: 'p',
  1129. children: [
  1130. { instanceOf: Text, data: 'foo', rangeStart: 1, rangeEnd: 1 }
  1131. ]
  1132. };
  1133. const created = create( writer, description );
  1134. const newRange = writer.wrap( created.range, new Element( 'b' ), 1 );
  1135. test( writer, newRange, created.node, description );
  1136. } );
  1137. it( 'wraps single text node', () => {
  1138. // <p>[{foobar}]</p>
  1139. // wrap <b>
  1140. // <p>[<b>{foobar}<b>]</p>
  1141. const created = create( writer, {
  1142. instanceOf: Element,
  1143. name: 'p',
  1144. rangeStart: 0,
  1145. rangeEnd: 1,
  1146. children: [
  1147. { instanceOf: Text, data: 'foobar' }
  1148. ]
  1149. } );
  1150. const b = new Element( 'b' );
  1151. const newRange = writer.wrap( created.range, b, 1 );
  1152. test( writer, newRange, created.node, {
  1153. instanceOf: Element,
  1154. name: 'p',
  1155. rangeStart: 0,
  1156. rangeEnd: 1,
  1157. children: [
  1158. {
  1159. instanceOf: Element,
  1160. name: 'b',
  1161. priority: 1,
  1162. children: [
  1163. { instanceOf: Text, data: 'foobar' }
  1164. ]
  1165. }
  1166. ]
  1167. } );
  1168. } );
  1169. it( 'should throw error when range placed in two containers', () => {
  1170. const container1 = new Element( 'p' );
  1171. const container2 = new Element( 'p' );
  1172. const range = new Range(
  1173. new Position( container1, 0 ),
  1174. new Position( container2, 1 )
  1175. );
  1176. const b = new Element( 'b' );
  1177. expect( () => {
  1178. writer.wrap( range, b, 1 );
  1179. } ).to.throw( CKEditorError, 'treeview-writer-unwrap-invalid-range' );
  1180. } );
  1181. it( 'wraps part of a single text node #1', () => {
  1182. // <p>[{foo]bar}</p>
  1183. // wrap with <b>
  1184. // <p>[<b>{foo}</b>]{bar}</p>
  1185. const created = create( writer, {
  1186. instanceOf: Element,
  1187. name: 'p',
  1188. rangeStart: 0,
  1189. children: [
  1190. { instanceOf: Text, data: 'foobar', rangeEnd: 3 }
  1191. ]
  1192. } );
  1193. const b = new Element( 'b' );
  1194. const newRange = writer.wrap( created.range, b, 2 );
  1195. test( writer, newRange, created.node, {
  1196. instanceOf: Element,
  1197. name: 'p',
  1198. rangeStart: 0,
  1199. rangeEnd: 1,
  1200. children: [
  1201. {
  1202. instanceOf: Element,
  1203. name: 'b',
  1204. priority: 2,
  1205. children: [
  1206. { instanceOf: Text, data: 'foo' }
  1207. ]
  1208. },
  1209. { instanceOf: Text, data: 'bar' }
  1210. ]
  1211. } );
  1212. } );
  1213. it( 'wraps part of a single text node #2', () => {
  1214. // <p>{[foo]bar}</p>
  1215. // wrap with <b>
  1216. // <p>[<b>{foo}</b>]{bar}</p>
  1217. const created = create( writer, {
  1218. instanceOf: Element,
  1219. name: 'p',
  1220. children: [
  1221. { instanceOf: Text, data: 'foobar', rangeStart: 0, rangeEnd: 3 }
  1222. ]
  1223. } );
  1224. const b = new Element( 'b' );
  1225. const newRange = writer.wrap( created.range, b, 2 );
  1226. test( writer, newRange, created.node, {
  1227. instanceOf: Element,
  1228. name: 'p',
  1229. rangeStart: 0,
  1230. rangeEnd: 1,
  1231. children: [
  1232. {
  1233. instanceOf: Element,
  1234. name: 'b',
  1235. priority: 2,
  1236. children: [
  1237. { instanceOf: Text, data: 'foo' }
  1238. ]
  1239. },
  1240. { instanceOf: Text, data: 'bar' }
  1241. ]
  1242. } );
  1243. } );
  1244. it( 'wraps part of a single text node #3', () => {
  1245. // <p>{foo[bar]}</p>
  1246. // wrap with <b>
  1247. // <p>{foo}[<b>{bar}</b>]</p>
  1248. const created = create( writer, {
  1249. instanceOf: Element,
  1250. name: 'p',
  1251. children: [
  1252. { instanceOf: Text, data: 'foobar', rangeStart: 3, rangeEnd: 6 }
  1253. ]
  1254. } );
  1255. const b = new Element( 'b' );
  1256. const newRange = writer.wrap( created.range, b, 2 );
  1257. test( writer, newRange, created.node, {
  1258. instanceOf: Element,
  1259. name: 'p',
  1260. rangeStart: 1,
  1261. rangeEnd: 2,
  1262. children: [
  1263. { instanceOf: Text, data: 'foo' },
  1264. {
  1265. instanceOf: Element,
  1266. name: 'b',
  1267. priority: 2,
  1268. children: [
  1269. { instanceOf: Text, data: 'bar' }
  1270. ]
  1271. }
  1272. ]
  1273. } );
  1274. } );
  1275. it( 'should not wrap inside nested containers', () => {
  1276. // <div>[{foobar}<p>{baz}</p>]</div>
  1277. // wrap with <b>
  1278. // <div>[<b>{foobar}</b><p>{baz}</p>]</div>
  1279. const created = create( writer, {
  1280. instanceOf: Element,
  1281. name: 'div',
  1282. rangeStart: 0,
  1283. rangeEnd: 2,
  1284. children: [
  1285. { instanceOf: Text, data: 'foobar' },
  1286. {
  1287. instanceOf: Element,
  1288. name: 'p',
  1289. children: [
  1290. { instanceOf: Text, data: 'baz' }
  1291. ]
  1292. }
  1293. ]
  1294. } );
  1295. const newRange = writer.wrap( created.range, new Element( 'b' ), 1 );
  1296. test( writer, newRange, created.node, {
  1297. instanceOf: Element,
  1298. name: 'div',
  1299. rangeStart: 0,
  1300. rangeEnd: 2,
  1301. children: [
  1302. {
  1303. instanceOf: Element,
  1304. name: 'b',
  1305. priority: 1,
  1306. children: [
  1307. { instanceOf: Text, data: 'foobar' }
  1308. ]
  1309. },
  1310. {
  1311. instanceOf: Element,
  1312. name: 'p',
  1313. children: [
  1314. { instanceOf: Text, data: 'baz' }
  1315. ]
  1316. }
  1317. ]
  1318. } );
  1319. } );
  1320. it( 'wraps according to priorities', () => {
  1321. // <p>[<u>{foobar}</u>]</p>
  1322. // wrap with <b> that has higher priority than <u>
  1323. // <p>[<u><b>{foobar}</b></u>]</p>
  1324. const created = create( writer, {
  1325. instanceOf: Element,
  1326. name: 'p',
  1327. rangeStart: 0,
  1328. rangeEnd: 1,
  1329. children: [
  1330. {
  1331. instanceOf: Element,
  1332. name: 'u',
  1333. priority: 1,
  1334. children: [
  1335. { instanceOf: Text, data: 'foobar' }
  1336. ]
  1337. }
  1338. ]
  1339. } );
  1340. const b = new Element( 'b' );
  1341. const newRange = writer.wrap( created.range, b, 2 );
  1342. test( writer, newRange, created.node, {
  1343. instanceOf: Element,
  1344. name: 'p',
  1345. rangeStart: 0,
  1346. rangeEnd: 1,
  1347. children: [
  1348. {
  1349. instanceOf: Element,
  1350. name: 'u',
  1351. priority: 1,
  1352. children: [
  1353. {
  1354. instanceOf: Element,
  1355. name: 'b',
  1356. priority: 2,
  1357. children: [
  1358. { instanceOf: Text, data: 'foobar' }
  1359. ]
  1360. }
  1361. ]
  1362. }
  1363. ]
  1364. } );
  1365. } );
  1366. it( 'merges wrapped nodes #1', () => {
  1367. // <p>[<b>{foo}</b>{bar}<b>{baz}</b>]</p>
  1368. // wrap with <b>
  1369. // <p>[<b>{foobarbaz}</b>]</p>
  1370. const created = create( writer, {
  1371. instanceOf: Element,
  1372. name: 'p',
  1373. rangeStart: 0,
  1374. rangeEnd: 3,
  1375. children: [
  1376. {
  1377. instanceOf: Element,
  1378. name: 'b',
  1379. priority: 1,
  1380. children: [
  1381. { instanceOf: Text, data: 'foo' }
  1382. ]
  1383. },
  1384. { instanceOf: Text, data: 'bar' },
  1385. {
  1386. instanceOf: Element,
  1387. name: 'b',
  1388. priority: 1,
  1389. children: [
  1390. { instanceOf: Text, data: 'baz' }
  1391. ]
  1392. }
  1393. ]
  1394. } );
  1395. const b = new Element( 'b' );
  1396. const newRange = writer.wrap( created.range, b, 1 );
  1397. test( writer, newRange, created.node, {
  1398. instanceOf: Element,
  1399. name: 'p',
  1400. rangeStart: 0,
  1401. rangeEnd: 1,
  1402. children: [
  1403. {
  1404. instanceOf: Element,
  1405. name: 'b',
  1406. priority: 1,
  1407. children: [
  1408. { instanceOf: Text, data: 'foobarbaz' }
  1409. ]
  1410. }
  1411. ]
  1412. } );
  1413. } );
  1414. it( 'merges wrapped nodes #2', () => {
  1415. // <p><b>{foo}</b>[{bar]baz}</p>
  1416. // wrap with <b>
  1417. // <p><b>{foo[bar}</b>]{baz}</p>
  1418. const created = create( writer, {
  1419. instanceOf: Element,
  1420. name: 'p',
  1421. rangeStart: 1,
  1422. children: [
  1423. {
  1424. instanceOf: Element,
  1425. name: 'b',
  1426. priority: 1,
  1427. children: [
  1428. { instanceOf: Text, data: 'foo' }
  1429. ]
  1430. },
  1431. { instanceOf: Text, data: 'barbaz', rangeEnd: 3 }
  1432. ]
  1433. } );
  1434. const newRange = writer.wrap( created.range, new Element( 'b' ), 1 );
  1435. test( writer, newRange, created.node, {
  1436. instanceOf: Element,
  1437. name: 'p',
  1438. rangeEnd: 1,
  1439. children: [
  1440. {
  1441. instanceOf: Element,
  1442. name: 'b',
  1443. priority: 1,
  1444. children: [
  1445. { instanceOf: Text, data: 'foobar', rangeStart: 3 }
  1446. ]
  1447. },
  1448. { instanceOf: Text, data: 'baz' }
  1449. ]
  1450. } );
  1451. } );
  1452. it( 'merges wrapped nodes #3', () => {
  1453. // <p><b>{foobar}</b>[{baz}]</p>
  1454. // wrap with <b>
  1455. // <p><b>{foobar[baz}</b>]</p>
  1456. const created = create( writer, {
  1457. instanceOf: Element,
  1458. name: 'p',
  1459. rangeStart: 1,
  1460. rangeEnd: 2,
  1461. children: [
  1462. {
  1463. instanceOf: Element,
  1464. name: 'b',
  1465. priority: 1,
  1466. children: [
  1467. { instanceOf: Text, data: 'foobar' }
  1468. ]
  1469. },
  1470. { instanceOf: Text, data: 'baz', rangeEnd: 3 }
  1471. ]
  1472. } );
  1473. const newRange = writer.wrap( created.range, new Element( 'b' ), 1 );
  1474. test( writer, newRange, created.node, {
  1475. instanceOf: Element,
  1476. name: 'p',
  1477. rangeEnd: 1,
  1478. children: [
  1479. {
  1480. instanceOf: Element,
  1481. name: 'b',
  1482. priority: 1,
  1483. children: [
  1484. { instanceOf: Text, data: 'foobarbaz', rangeStart: 6 }
  1485. ]
  1486. }
  1487. ]
  1488. } );
  1489. } );
  1490. it( 'merges wrapped nodes #4', () => {
  1491. // <p>[{foo}<i>{bar}</i>]{baz}</p>
  1492. // wrap with <b>
  1493. // <p>[<b>{foo}<i>{bar}</i></b>]{baz}</p>
  1494. const created = create( writer, {
  1495. instanceOf: Element,
  1496. name: 'p',
  1497. rangeStart: 0,
  1498. rangeEnd: 2,
  1499. children: [
  1500. { instanceOf: Text, data: 'foo' },
  1501. {
  1502. instanceOf: Element,
  1503. name: 'i',
  1504. priority: 1,
  1505. children: [
  1506. { instanceOf: Text, data: 'bar' }
  1507. ]
  1508. },
  1509. { instanceOf: Text, data: 'baz' }
  1510. ]
  1511. } );
  1512. const newRange = writer.wrap( created.range, new Element( 'b' ), 1 );
  1513. test( writer, newRange, created.node, {
  1514. instanceOf: Element,
  1515. name: 'p',
  1516. rangeStart: 0,
  1517. rangeEnd: 1,
  1518. children: [
  1519. {
  1520. instanceOf: Element,
  1521. name: 'b',
  1522. priority: 1,
  1523. children: [
  1524. { instanceOf: Text, data: 'foo' },
  1525. {
  1526. instanceOf: Element,
  1527. name: 'i',
  1528. priority: 1,
  1529. children: [
  1530. { instanceOf: Text, data: 'bar' }
  1531. ]
  1532. }
  1533. ]
  1534. },
  1535. { instanceOf: Text, data: 'baz' }
  1536. ]
  1537. } );
  1538. } );
  1539. it( 'merges wrapped nodes #5', () => {
  1540. // <p>[{foo}<i>{bar}</i>{baz}]</p>
  1541. // wrap with <b>, that has higher priority than <i>
  1542. // <p>[<b>{foo}</b><i><b>{bar}</b></i><b>{baz}</b>]</p>
  1543. const created = create( writer, {
  1544. instanceOf: Element,
  1545. name: 'p',
  1546. rangeStart: 0,
  1547. rangeEnd: 3,
  1548. children: [
  1549. { instanceOf: Text, data: 'foo' },
  1550. {
  1551. instanceOf: Element,
  1552. name: 'i',
  1553. priority: 1,
  1554. children: [
  1555. { instanceOf: Text, data: 'bar' }
  1556. ]
  1557. },
  1558. { instanceOf: Text, data: 'baz' }
  1559. ]
  1560. } );
  1561. const newRange = writer.wrap( created.range, new Element( 'b' ), 2 );
  1562. test( writer, newRange, created.node, {
  1563. instanceOf: Element,
  1564. name: 'p',
  1565. rangeStart: 0,
  1566. rangeEnd: 3,
  1567. children: [
  1568. {
  1569. instanceOf: Element,
  1570. name: 'b',
  1571. priority: 2,
  1572. children: [
  1573. { instanceOf: Text, data: 'foo' }
  1574. ]
  1575. },
  1576. {
  1577. instanceOf: Element,
  1578. name: 'i',
  1579. priority: 1,
  1580. children: [
  1581. {
  1582. instanceOf: Element,
  1583. name: 'b',
  1584. priority: 2,
  1585. children: [
  1586. { instanceOf: Text, data: 'bar' }
  1587. ]
  1588. }
  1589. ]
  1590. },
  1591. {
  1592. instanceOf: Element,
  1593. name: 'b',
  1594. priority: 2,
  1595. children: [
  1596. { instanceOf: Text, data: 'baz' }
  1597. ]
  1598. }
  1599. ]
  1600. } );
  1601. } );
  1602. } );
  1603. describe( 'unwrap', () => {
  1604. it( 'should do nothing on collapsed ranges', () => {
  1605. const description = {
  1606. instanceOf: Element,
  1607. name: 'p',
  1608. children: [
  1609. { instanceOf: Text, data: 'foo', rangeStart: 1, rangeEnd: 1 }
  1610. ]
  1611. };
  1612. const created = create( writer, description );
  1613. const newRange = writer.unwrap( created.range, new Element( 'b' ), 1 );
  1614. test( writer, newRange, created.node, description );
  1615. } );
  1616. it( 'should do nothing on single text node', () => {
  1617. // <p>[{foobar}]</p>
  1618. const description = {
  1619. instanceOf: Element,
  1620. name: 'p',
  1621. rangeStart: 0,
  1622. rangeEnd: 1,
  1623. children: [
  1624. { instanceOf: Text, data: 'foobar' }
  1625. ]
  1626. };
  1627. const created = create( writer, description );
  1628. const b = new Element( 'b' );
  1629. writer.setPriority( b, 1 );
  1630. const newRange = writer.unwrap( created.range, b );
  1631. test( writer, newRange, created.node, description );
  1632. } );
  1633. it( 'should throw error when range placed in two containers', () => {
  1634. const container1 = new Element( 'p' );
  1635. const container2 = new Element( 'p' );
  1636. const range = new Range(
  1637. new Position( container1, 0 ),
  1638. new Position( container2, 1 )
  1639. );
  1640. const b = new Element( 'b' );
  1641. expect( () => {
  1642. writer.unwrap( range, b, 1 );
  1643. } ).to.throw( CKEditorError, 'treeview-writer-unwrap-invalid-range' );
  1644. } );
  1645. it( 'should unwrap single node', () => {
  1646. // <p>[<b>{foobar}</b>]<p> -> <p>[{foobar}]</p>
  1647. const created = create( writer, {
  1648. instanceOf: Element,
  1649. name: 'p',
  1650. rangeStart: 0,
  1651. rangeEnd: 1,
  1652. children: [
  1653. {
  1654. instanceOf: Element,
  1655. priority: 1,
  1656. name: 'b',
  1657. children: [
  1658. { instanceOf: Text, data: 'foobar' }
  1659. ]
  1660. }
  1661. ]
  1662. } );
  1663. const b = new Element( 'b' );
  1664. writer.setPriority( b, 1 );
  1665. const newRange = writer.unwrap( created.range, b );
  1666. test( writer, newRange, created.node, {
  1667. instanceOf: Element,
  1668. name: 'p',
  1669. rangeStart: 0,
  1670. rangeEnd: 1,
  1671. children: [
  1672. { instanceOf: Text, data: 'foobar' }
  1673. ]
  1674. } );
  1675. } );
  1676. it( 'should not unwrap attributes with different priorities #1', () => {
  1677. // <p>[<b>{foobar}</b>]<p> -> <p>[<b>{foobar}</b>]</p>
  1678. // Unwrapped with <b> but using different priority.
  1679. const description = {
  1680. instanceOf: Element,
  1681. name: 'p',
  1682. rangeStart: 0,
  1683. rangeEnd: 1,
  1684. children: [
  1685. {
  1686. instanceOf: Element,
  1687. priority: 1,
  1688. name: 'b',
  1689. children: [
  1690. { instanceOf: Text, data: 'foobar' }
  1691. ]
  1692. }
  1693. ]
  1694. };
  1695. const created = create( writer, description );
  1696. const b = new Element( 'b' );
  1697. writer.setPriority( b, 2 );
  1698. const newRange = writer.unwrap( created.range, b );
  1699. test( writer, newRange, created.node, description );
  1700. } );
  1701. it( 'should not unwrap attributes with different priorities #2', () => {
  1702. // <p>[<b>{foo}</b><b>{bar}</b><b>{baz}</b>]<p> -> <p>[{foo}<b>bar</b>{baz}]</p>
  1703. // <b> around `bar` has different priority than others.
  1704. const created = create( writer, {
  1705. instanceOf: Element,
  1706. name: 'p',
  1707. rangeStart: 0,
  1708. rangeEnd: 3,
  1709. children: [
  1710. {
  1711. instanceOf: Element,
  1712. priority: 2,
  1713. name: 'b',
  1714. children: [
  1715. { instanceOf: Text, data: 'foo' }
  1716. ]
  1717. },
  1718. {
  1719. instanceOf: Element,
  1720. priority: 1,
  1721. name: 'b',
  1722. children: [
  1723. { instanceOf: Text, data: 'bar' }
  1724. ]
  1725. },
  1726. {
  1727. instanceOf: Element,
  1728. priority: 2,
  1729. name: 'b',
  1730. children: [
  1731. { instanceOf: Text, data: 'baz' }
  1732. ]
  1733. }
  1734. ]
  1735. } );
  1736. const b = new Element( 'b' );
  1737. writer.setPriority( b, 2 );
  1738. const newRange = writer.unwrap( created.range, b );
  1739. test( writer, newRange, created.node, {
  1740. instanceOf: Element,
  1741. name: 'p',
  1742. rangeStart: 0,
  1743. rangeEnd: 3,
  1744. children: [
  1745. { instanceOf: Text, data: 'foo' },
  1746. {
  1747. instanceOf: Element,
  1748. priority: 1,
  1749. name: 'b',
  1750. children: [
  1751. { instanceOf: Text, data: 'bar' }
  1752. ]
  1753. },
  1754. { instanceOf: Text, data: 'baz' }
  1755. ]
  1756. } );
  1757. } );
  1758. it( 'should unwrap part of the node', () => {
  1759. // <p>[{baz}<b>{foo]bar}</b><p> -> <p>[{bazfoo}]<b>{bar}</b></p>
  1760. const created = create( writer, {
  1761. instanceOf: Element,
  1762. name: 'p',
  1763. rangeStart: 0,
  1764. children: [
  1765. { instanceOf: Text, data: 'baz' },
  1766. {
  1767. instanceOf: Element,
  1768. name: 'b',
  1769. priority: 1,
  1770. children: [
  1771. { instanceOf: Text, data: 'foobar', rangeEnd: 3 }
  1772. ]
  1773. }
  1774. ]
  1775. } );
  1776. const b = new Element( 'b' );
  1777. writer.setPriority( b, 1 );
  1778. const newRange = writer.unwrap( created.range, b );
  1779. test( writer, newRange, created.node, {
  1780. instanceOf: Element,
  1781. name: 'p',
  1782. rangeStart: 0,
  1783. rangeEnd: 1,
  1784. children: [
  1785. { instanceOf: Text, data: 'bazfoo' },
  1786. {
  1787. instanceOf: Element,
  1788. name: 'b',
  1789. priority: 1,
  1790. children: [
  1791. { instanceOf: Text, data: 'bar' }
  1792. ]
  1793. }
  1794. ]
  1795. } );
  1796. } );
  1797. it( 'should unwrap nested attributes', () => {
  1798. // <p>[<u><b>{foobar}</b></u>]</p> -> <p>[<u>{foobar}</u>]</p>
  1799. const created = create( writer, {
  1800. instanceOf: Element,
  1801. name: 'p',
  1802. rangeStart: 0,
  1803. rangeEnd: 1,
  1804. children: [
  1805. {
  1806. instanceOf: Element,
  1807. name: 'u',
  1808. priority: 1,
  1809. children: [
  1810. {
  1811. instanceOf: Element,
  1812. name: 'b',
  1813. priority: 1,
  1814. children: [
  1815. { instanceOf: Text, data: 'foobar' }
  1816. ]
  1817. }
  1818. ]
  1819. }
  1820. ]
  1821. } );
  1822. const b = new Element( 'b' );
  1823. writer.setPriority( b, 1 );
  1824. const newRange = writer.unwrap( created.range, b );
  1825. test( writer, newRange, created.node, {
  1826. instanceOf: Element,
  1827. name: 'p',
  1828. rangeStart: 0,
  1829. rangeEnd: 1,
  1830. children: [
  1831. {
  1832. instanceOf: Element,
  1833. name: 'u',
  1834. priority: 1,
  1835. children: [
  1836. { instanceOf: Text, data: 'foobar' }
  1837. ]
  1838. }
  1839. ]
  1840. } );
  1841. } );
  1842. it( 'should merge unwrapped nodes #1', () => {
  1843. // <p>{foo}[<b>{bar}</b>]{bom}</p> -> <p>{foo[bar]bom}</p>
  1844. const created = create( writer, {
  1845. instanceOf: Element,
  1846. name: 'p',
  1847. rangeStart: 1,
  1848. rangeEnd: 2,
  1849. children: [
  1850. { instanceOf: Text, data: 'foo' },
  1851. {
  1852. instanceOf: Element,
  1853. name: 'b',
  1854. priority: 1,
  1855. children: [
  1856. { instanceOf: Text, data: 'bar' }
  1857. ]
  1858. },
  1859. { instanceOf: Text, data: 'bom' }
  1860. ]
  1861. } );
  1862. const b = new Element( 'b' );
  1863. writer.setPriority( b, 1 );
  1864. const newRange = writer.unwrap( created.range, b );
  1865. test( writer, newRange, created.node, {
  1866. instanceOf: Element,
  1867. name: 'p',
  1868. children: [
  1869. {
  1870. instanceOf: Text,
  1871. data: 'foobarbom',
  1872. rangeStart: 3,
  1873. rangeEnd: 6
  1874. }
  1875. ]
  1876. } );
  1877. } );
  1878. it( 'should merge unwrapped nodes #2', () => {
  1879. // <p>{foo}<u>{bar}</u>[<b><u>{bazqux}</u></b>]</p> -> <p>{foo}<u>{bar[bazqux}</u>]</p>
  1880. const created = create( writer, {
  1881. instanceOf: Element,
  1882. name: 'p',
  1883. rangeStart: 2,
  1884. rangeEnd: 3,
  1885. children: [
  1886. { instanceOf: Text, data: 'foo' },
  1887. {
  1888. instanceOf: Element,
  1889. name: 'u',
  1890. priority: 1,
  1891. children: [
  1892. { instanceOf: Text, data: 'bar' }
  1893. ]
  1894. },
  1895. {
  1896. instanceOf: Element,
  1897. name: 'b',
  1898. priority: 1,
  1899. children: [
  1900. {
  1901. instanceOf: Element,
  1902. name: 'u',
  1903. priority: 1,
  1904. children: [
  1905. { instanceOf: Text, data: 'bazqux' }
  1906. ]
  1907. }
  1908. ]
  1909. }
  1910. ]
  1911. } );
  1912. const b = new Element( 'b' );
  1913. writer.setPriority( b, 1 );
  1914. const newRange = writer.unwrap( created.range, b );
  1915. test( writer, newRange, created.node, {
  1916. instanceOf: Element,
  1917. name: 'p',
  1918. rangeEnd: 2,
  1919. children: [
  1920. {
  1921. instanceOf: Text,
  1922. data: 'foo'
  1923. },
  1924. {
  1925. instanceOf: Element,
  1926. name: 'u',
  1927. priority: 1,
  1928. children: [
  1929. { instanceOf: Text, data: 'barbazqux', rangeStart: 3 }
  1930. ]
  1931. }
  1932. ]
  1933. } );
  1934. } );
  1935. it( 'should merge unwrapped nodes #3', () => {
  1936. // <p>{foo}<u>{bar}</u>[<b><u>{baz]qux}</u></b></p> -> <p>{foo}<u>{bar[baz}</u>]<b><u>{qux}</u></b></p>
  1937. const created = create( writer, {
  1938. instanceOf: Element,
  1939. name: 'p',
  1940. rangeStart: 2,
  1941. children: [
  1942. { instanceOf: Text, data: 'foo' },
  1943. {
  1944. instanceOf: Element,
  1945. name: 'u',
  1946. priority: 1,
  1947. children: [
  1948. { instanceOf: Text, data: 'bar' }
  1949. ]
  1950. },
  1951. {
  1952. instanceOf: Element,
  1953. name: 'b',
  1954. priority: 1,
  1955. children: [
  1956. {
  1957. instanceOf: Element,
  1958. name: 'u',
  1959. priority: 1,
  1960. children: [
  1961. { instanceOf: Text, data: 'bazqux', rangeEnd: 3 }
  1962. ]
  1963. }
  1964. ]
  1965. }
  1966. ]
  1967. } );
  1968. const b = new Element( 'b' );
  1969. writer.setPriority( b, 1 );
  1970. const newRange = writer.unwrap( created.range, b );
  1971. test( writer, newRange, created.node, {
  1972. instanceOf: Element,
  1973. name: 'p',
  1974. rangeEnd: 2,
  1975. children: [
  1976. {
  1977. instanceOf: Text,
  1978. data: 'foo'
  1979. },
  1980. {
  1981. instanceOf: Element,
  1982. name: 'u',
  1983. priority: 1,
  1984. children: [
  1985. { instanceOf: Text, data: 'barbaz', rangeStart: 3 }
  1986. ]
  1987. },
  1988. {
  1989. instanceOf: Element,
  1990. name: 'b',
  1991. priority: 1,
  1992. children: [
  1993. {
  1994. instanceOf: Element,
  1995. name: 'u',
  1996. priority: 1,
  1997. children: [
  1998. { instanceOf: Text, data: 'qux' }
  1999. ]
  2000. }
  2001. ]
  2002. }
  2003. ]
  2004. } );
  2005. } );
  2006. it( 'should merge unwrapped nodes #4', () => {
  2007. // <p>{foo}<u>{bar}</u>[<b><u>{baz}</u></b>]<u>qux</u></p> -> <p>{foo}<u>{bar[baz]qux}</u></p>
  2008. const created = create( writer, {
  2009. instanceOf: Element,
  2010. name: 'p',
  2011. rangeStart: 2,
  2012. rangeEnd: 3,
  2013. children: [
  2014. { instanceOf: Text, data: 'foo' },
  2015. {
  2016. instanceOf: Element,
  2017. name: 'u',
  2018. priority: 1,
  2019. children: [
  2020. { instanceOf: Text, data: 'bar' }
  2021. ]
  2022. },
  2023. {
  2024. instanceOf: Element,
  2025. name: 'b',
  2026. priority: 1,
  2027. children: [
  2028. {
  2029. instanceOf: Element,
  2030. name: 'u',
  2031. priority: 1,
  2032. children: [
  2033. { instanceOf: Text, data: 'baz' }
  2034. ]
  2035. }
  2036. ]
  2037. },
  2038. {
  2039. instanceOf: Element,
  2040. name: 'u',
  2041. priority: 1,
  2042. children: [
  2043. { instanceOf: Text, data: 'qux' }
  2044. ]
  2045. }
  2046. ]
  2047. } );
  2048. const b = new Element( 'b' );
  2049. writer.setPriority( b, 1 );
  2050. const newRange = writer.unwrap( created.range, b );
  2051. test( writer, newRange, created.node, {
  2052. instanceOf: Element,
  2053. name: 'p',
  2054. children: [
  2055. {
  2056. instanceOf: Text,
  2057. data: 'foo'
  2058. },
  2059. {
  2060. instanceOf: Element,
  2061. name: 'u',
  2062. priority: 1,
  2063. children: [
  2064. { instanceOf: Text, data: 'barbazqux', rangeStart: 3, rangeEnd: 6 }
  2065. ]
  2066. }
  2067. ]
  2068. } );
  2069. } );
  2070. it( 'should merge unwrapped nodes #5', () => {
  2071. // <p>[<b><u>{foo}</u></b><b><u>{bar}</u></b><b><u>{baz}</u></b>]</p> -> <p>[<u>{foobarbaz}</u>]</p>
  2072. const created = create( writer, {
  2073. instanceOf: Element,
  2074. name: 'p',
  2075. rangeStart: 0,
  2076. rangeEnd: 3,
  2077. children: [
  2078. {
  2079. instanceOf: Element,
  2080. name: 'b',
  2081. priority: 1,
  2082. children: [
  2083. {
  2084. instanceOf: Element,
  2085. name: 'u',
  2086. priority: 1,
  2087. children: [
  2088. { instanceOf: Text, data: 'foo' }
  2089. ]
  2090. }
  2091. ]
  2092. },
  2093. {
  2094. instanceOf: Element,
  2095. name: 'b',
  2096. priority: 1,
  2097. children: [
  2098. {
  2099. instanceOf: Element,
  2100. name: 'u',
  2101. priority: 1,
  2102. children: [
  2103. { instanceOf: Text, data: 'bar' }
  2104. ]
  2105. }
  2106. ]
  2107. },
  2108. {
  2109. instanceOf: Element,
  2110. name: 'b',
  2111. priority: 1,
  2112. children: [
  2113. {
  2114. instanceOf: Element,
  2115. name: 'u',
  2116. priority: 1,
  2117. children: [
  2118. { instanceOf: Text, data: 'baz' }
  2119. ]
  2120. }
  2121. ]
  2122. }
  2123. ]
  2124. } );
  2125. const b = new Element( 'b' );
  2126. writer.setPriority( b, 1 );
  2127. const newRange = writer.unwrap( created.range, b );
  2128. test( writer, newRange, created.node, {
  2129. instanceOf: Element,
  2130. name: 'p',
  2131. rangeStart: 0,
  2132. rangeEnd: 1,
  2133. children: [
  2134. {
  2135. instanceOf: Element,
  2136. name: 'u',
  2137. priority: 1,
  2138. children: [
  2139. { instanceOf: Text, data: 'foobarbaz' }
  2140. ]
  2141. }
  2142. ]
  2143. } );
  2144. } );
  2145. it( 'should unwrap mixed ranges #1', () => {
  2146. // <p>[<u><b>{foo}]</b></u></p> -> <p>[<u>{foo}</u>]</p
  2147. const created = create( writer, {
  2148. instanceOf: Element,
  2149. name: 'p',
  2150. rangeStart: 0,
  2151. children: [
  2152. {
  2153. instanceOf: Element,
  2154. name: 'u',
  2155. priority: 1,
  2156. children: [
  2157. {
  2158. instanceOf: Element,
  2159. name: 'b',
  2160. priority: 1,
  2161. rangeEnd: 1,
  2162. children: [
  2163. { instanceOf: Text, data: 'foo' }
  2164. ]
  2165. }
  2166. ]
  2167. }
  2168. ]
  2169. } );
  2170. const b = new Element( 'b' );
  2171. writer.setPriority( b, 1 );
  2172. const newRange = writer.unwrap( created.range, b );
  2173. test( writer, newRange, created.node, {
  2174. instanceOf: Element,
  2175. name: 'p',
  2176. rangeStart: 0,
  2177. rangeEnd: 1,
  2178. children: [
  2179. {
  2180. instanceOf: Element,
  2181. name: 'u',
  2182. priority: 1,
  2183. children: [
  2184. { instanceOf: Text, data: 'foo' }
  2185. ]
  2186. }
  2187. ]
  2188. } );
  2189. } );
  2190. it( 'should unwrap mixed ranges #2', () => {
  2191. // <p>[<u><b>{foo]}</b></u></p> -> <p>[<u>{foo}</u>]</p
  2192. const created = create( writer, {
  2193. instanceOf: Element,
  2194. name: 'p',
  2195. rangeStart: 0,
  2196. children: [
  2197. {
  2198. instanceOf: Element,
  2199. name: 'u',
  2200. priority: 1,
  2201. children: [
  2202. {
  2203. instanceOf: Element,
  2204. name: 'b',
  2205. priority: 1,
  2206. children: [
  2207. { instanceOf: Text, data: 'foo', rangeEnd: 3 }
  2208. ]
  2209. }
  2210. ]
  2211. }
  2212. ]
  2213. } );
  2214. const b = new Element( 'b' );
  2215. writer.setPriority( b, 1 );
  2216. const newRange = writer.unwrap( created.range, b );
  2217. test( writer, newRange, created.node, {
  2218. instanceOf: Element,
  2219. name: 'p',
  2220. rangeStart: 0,
  2221. rangeEnd: 1,
  2222. children: [
  2223. {
  2224. instanceOf: Element,
  2225. name: 'u',
  2226. priority: 1,
  2227. children: [
  2228. { instanceOf: Text, data: 'foo' }
  2229. ]
  2230. }
  2231. ]
  2232. } );
  2233. } );
  2234. } );
  2235. } );