unwrap.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Writer from '/ckeditor5/engine/treeview/writer.js';
  8. import Element from '/ckeditor5/engine/treeview/element.js';
  9. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  10. import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
  11. import Position from '/ckeditor5/engine/treeview/position.js';
  12. import Range from '/ckeditor5/engine/treeview/range.js';
  13. import Text from '/ckeditor5/engine/treeview/text.js';
  14. import utils from '/tests/engine/treeview/writer/_utils/utils.js';
  15. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  16. describe( 'Writer', () => {
  17. const create = utils.create;
  18. const test = utils.test;
  19. let writer;
  20. beforeEach( () => {
  21. writer = new Writer();
  22. } );
  23. describe( 'unwrap', () => {
  24. it( 'should do nothing on collapsed ranges', () => {
  25. const description = {
  26. instanceOf: ContainerElement,
  27. name: 'p',
  28. children: [
  29. { instanceOf: Text, data: 'foo', rangeStart: 1, rangeEnd: 1 }
  30. ]
  31. };
  32. const created = create( writer, description );
  33. const newRange = writer.unwrap( created.range, new AttributeElement( 'b' ), 1 );
  34. test( writer, newRange, created.node, description );
  35. } );
  36. it( 'should do nothing on single text node', () => {
  37. // <p>[{foobar}]</p>
  38. const description = {
  39. instanceOf: ContainerElement,
  40. name: 'p',
  41. rangeStart: 0,
  42. rangeEnd: 1,
  43. children: [
  44. { instanceOf: Text, data: 'foobar' }
  45. ]
  46. };
  47. const created = create( writer, description );
  48. const b = new AttributeElement( 'b' );
  49. b.priority = 1;
  50. const newRange = writer.unwrap( created.range, b );
  51. test( writer, newRange, created.node, description );
  52. } );
  53. it( 'should throw error when element is not instance of AttributeElement', () => {
  54. const container = new ContainerElement( 'p', null, new AttributeElement( 'b', null, new Text( 'foo' ) ) );
  55. const range = new Range(
  56. new Position( container, 0 ),
  57. new Position( container, 1 )
  58. );
  59. const b = new Element( 'b' );
  60. expect( () => {
  61. writer.unwrap( range, b );
  62. } ).to.throw( CKEditorError, 'treeview-writer-unwrap-invalid-attribute' );
  63. } );
  64. it( 'should throw error when range placed in two containers', () => {
  65. const container1 = new ContainerElement( 'p' );
  66. const container2 = new ContainerElement( 'p' );
  67. const range = new Range(
  68. new Position( container1, 0 ),
  69. new Position( container2, 1 )
  70. );
  71. const b = new AttributeElement( 'b' );
  72. expect( () => {
  73. writer.unwrap( range, b, 1 );
  74. } ).to.throw( CKEditorError, 'treeview-writer-invalid-range-container' );
  75. } );
  76. it( 'should unwrap single node', () => {
  77. // <p>[<b>{foobar}</b>]<p> -> <p>[{foobar}]</p>
  78. const created = create( writer, {
  79. instanceOf: ContainerElement,
  80. name: 'p',
  81. rangeStart: 0,
  82. rangeEnd: 1,
  83. children: [
  84. {
  85. instanceOf: AttributeElement,
  86. name: 'b',
  87. priority: 1,
  88. children: [
  89. { instanceOf: Text, data: 'foobar' }
  90. ]
  91. }
  92. ]
  93. } );
  94. const b = new AttributeElement( 'b' );
  95. b.priority = 1;
  96. const newRange = writer.unwrap( created.range, b );
  97. test( writer, newRange, created.node, {
  98. instanceOf: ContainerElement,
  99. name: 'p',
  100. rangeStart: 0,
  101. rangeEnd: 1,
  102. children: [
  103. { instanceOf: Text, data: 'foobar' }
  104. ]
  105. } );
  106. } );
  107. it( 'should not unwrap attributes with different priorities #1', () => {
  108. // <p>[<b>{foobar}</b>]<p> -> <p>[<b>{foobar}</b>]</p>
  109. // Unwrapped with <b> but using different priority.
  110. const description = {
  111. instanceOf: ContainerElement,
  112. name: 'p',
  113. rangeStart: 0,
  114. rangeEnd: 1,
  115. children: [
  116. {
  117. instanceOf: AttributeElement,
  118. name: 'b',
  119. priority: 1,
  120. children: [
  121. { instanceOf: Text, data: 'foobar' }
  122. ]
  123. }
  124. ]
  125. };
  126. const created = create( writer, description );
  127. const b = new AttributeElement( 'b' );
  128. b.priority = 2;
  129. const newRange = writer.unwrap( created.range, b );
  130. test( writer, newRange, created.node, description );
  131. } );
  132. it( 'should not unwrap attributes with different priorities #2', () => {
  133. // <p>[<b>{foo}</b><b>{bar}</b><b>{baz}</b>]<p> -> <p>[{foo}<b>bar</b>{baz}]</p>
  134. // <b> around `bar` has different priority than others.
  135. const created = create( writer, {
  136. instanceOf: ContainerElement,
  137. name: 'p',
  138. rangeStart: 0,
  139. rangeEnd: 3,
  140. children: [
  141. {
  142. instanceOf: AttributeElement,
  143. name: 'b',
  144. priority: 2,
  145. children: [
  146. { instanceOf: Text, data: 'foo' }
  147. ]
  148. },
  149. {
  150. instanceOf: AttributeElement,
  151. name: 'b',
  152. priority: 1,
  153. children: [
  154. { instanceOf: Text, data: 'bar' }
  155. ]
  156. },
  157. {
  158. instanceOf: AttributeElement,
  159. name: 'b',
  160. priority: 2,
  161. children: [
  162. { instanceOf: Text, data: 'baz' }
  163. ]
  164. }
  165. ]
  166. } );
  167. const b = new AttributeElement( 'b' );
  168. b.priority = 2;
  169. const newRange = writer.unwrap( created.range, b );
  170. test( writer, newRange, created.node, {
  171. instanceOf: ContainerElement,
  172. name: 'p',
  173. rangeStart: 0,
  174. rangeEnd: 3,
  175. children: [
  176. { instanceOf: Text, data: 'foo' },
  177. {
  178. instanceOf: AttributeElement,
  179. name: 'b',
  180. priority: 1,
  181. children: [
  182. { instanceOf: Text, data: 'bar' }
  183. ]
  184. },
  185. { instanceOf: Text, data: 'baz' }
  186. ]
  187. } );
  188. } );
  189. it( 'should unwrap part of the node', () => {
  190. // <p>[{baz}<b>{foo]bar}</b><p> -> <p>[{bazfoo}]<b>{bar}</b></p>
  191. const created = create( writer, {
  192. instanceOf: ContainerElement,
  193. name: 'p',
  194. rangeStart: 0,
  195. children: [
  196. { instanceOf: Text, data: 'baz' },
  197. {
  198. instanceOf: AttributeElement,
  199. name: 'b',
  200. priority: 1,
  201. children: [
  202. { instanceOf: Text, data: 'foobar', rangeEnd: 3 }
  203. ]
  204. }
  205. ]
  206. } );
  207. const b = new AttributeElement( 'b' );
  208. b.priority = 1;
  209. const newRange = writer.unwrap( created.range, b );
  210. test( writer, newRange, created.node, {
  211. instanceOf: ContainerElement,
  212. name: 'p',
  213. rangeStart: 0,
  214. rangeEnd: 1,
  215. children: [
  216. { instanceOf: Text, data: 'bazfoo' },
  217. {
  218. instanceOf: AttributeElement,
  219. name: 'b',
  220. priority: 1,
  221. children: [
  222. { instanceOf: Text, data: 'bar' }
  223. ]
  224. }
  225. ]
  226. } );
  227. } );
  228. it( 'should unwrap nested attributes', () => {
  229. // <p>[<u><b>{foobar}</b></u>]</p> -> <p>[<u>{foobar}</u>]</p>
  230. const created = create( writer, {
  231. instanceOf: ContainerElement,
  232. name: 'p',
  233. rangeStart: 0,
  234. rangeEnd: 1,
  235. children: [
  236. {
  237. instanceOf: AttributeElement,
  238. name: 'u',
  239. priority: 1,
  240. children: [
  241. {
  242. instanceOf: AttributeElement,
  243. name: 'b',
  244. priority: 1,
  245. children: [
  246. { instanceOf: Text, data: 'foobar' }
  247. ]
  248. }
  249. ]
  250. }
  251. ]
  252. } );
  253. const b = new AttributeElement( 'b' );
  254. b.priority = 1;
  255. const newRange = writer.unwrap( created.range, b );
  256. test( writer, newRange, created.node, {
  257. instanceOf: ContainerElement,
  258. name: 'p',
  259. rangeStart: 0,
  260. rangeEnd: 1,
  261. children: [
  262. {
  263. instanceOf: AttributeElement,
  264. name: 'u',
  265. priority: 1,
  266. children: [
  267. { instanceOf: Text, data: 'foobar' }
  268. ]
  269. }
  270. ]
  271. } );
  272. } );
  273. it( 'should merge unwrapped nodes #1', () => {
  274. // <p>{foo}[<b>{bar}</b>]{bom}</p> -> <p>{foo[bar]bom}</p>
  275. const created = create( writer, {
  276. instanceOf: ContainerElement,
  277. name: 'p',
  278. rangeStart: 1,
  279. rangeEnd: 2,
  280. children: [
  281. { instanceOf: Text, data: 'foo' },
  282. {
  283. instanceOf: AttributeElement,
  284. name: 'b',
  285. priority: 1,
  286. children: [
  287. { instanceOf: Text, data: 'bar' }
  288. ]
  289. },
  290. { instanceOf: Text, data: 'bom' }
  291. ]
  292. } );
  293. const b = new AttributeElement( 'b' );
  294. b.priority = 1;
  295. const newRange = writer.unwrap( created.range, b );
  296. test( writer, newRange, created.node, {
  297. instanceOf: ContainerElement,
  298. name: 'p',
  299. children: [
  300. {
  301. instanceOf: Text,
  302. data: 'foobarbom',
  303. rangeStart: 3,
  304. rangeEnd: 6
  305. }
  306. ]
  307. } );
  308. } );
  309. it( 'should merge unwrapped nodes #2', () => {
  310. // <p>{foo}<u>{bar}</u>[<b><u>{bazqux}</u></b>]</p> -> <p>{foo}<u>{bar[bazqux}</u>]</p>
  311. const created = create( writer, {
  312. instanceOf: ContainerElement,
  313. name: 'p',
  314. rangeStart: 2,
  315. rangeEnd: 3,
  316. children: [
  317. { instanceOf: Text, data: 'foo' },
  318. {
  319. instanceOf: AttributeElement,
  320. name: 'u',
  321. priority: 1,
  322. children: [
  323. { instanceOf: Text, data: 'bar' }
  324. ]
  325. },
  326. {
  327. instanceOf: AttributeElement,
  328. name: 'b',
  329. priority: 1,
  330. children: [
  331. {
  332. instanceOf: AttributeElement,
  333. name: 'u',
  334. priority: 1,
  335. children: [
  336. { instanceOf: Text, data: 'bazqux' }
  337. ]
  338. }
  339. ]
  340. }
  341. ]
  342. } );
  343. const b = new AttributeElement( 'b' );
  344. b.priority = 1;
  345. const newRange = writer.unwrap( created.range, b );
  346. test( writer, newRange, created.node, {
  347. instanceOf: ContainerElement,
  348. name: 'p',
  349. rangeEnd: 2,
  350. children: [
  351. {
  352. instanceOf: Text,
  353. data: 'foo'
  354. },
  355. {
  356. instanceOf: AttributeElement,
  357. name: 'u',
  358. priority: 1,
  359. children: [
  360. { instanceOf: Text, data: 'barbazqux', rangeStart: 3 }
  361. ]
  362. }
  363. ]
  364. } );
  365. } );
  366. it( 'should merge unwrapped nodes #3', () => {
  367. // <p>{foo}<u>{bar}</u>[<b><u>{baz]qux}</u></b></p> -> <p>{foo}<u>{bar[baz}</u>]<b><u>{qux}</u></b></p>
  368. const created = create( writer, {
  369. instanceOf: ContainerElement,
  370. name: 'p',
  371. rangeStart: 2,
  372. children: [
  373. { instanceOf: Text, data: 'foo' },
  374. {
  375. instanceOf: AttributeElement,
  376. name: 'u',
  377. priority: 1,
  378. children: [
  379. { instanceOf: Text, data: 'bar' }
  380. ]
  381. },
  382. {
  383. instanceOf: AttributeElement,
  384. name: 'b',
  385. priority: 1,
  386. children: [
  387. {
  388. instanceOf: AttributeElement,
  389. name: 'u',
  390. priority: 1,
  391. children: [
  392. { instanceOf: Text, data: 'bazqux', rangeEnd: 3 }
  393. ]
  394. }
  395. ]
  396. }
  397. ]
  398. } );
  399. const b = new AttributeElement( 'b' );
  400. b.priority = 1;
  401. const newRange = writer.unwrap( created.range, b );
  402. test( writer, newRange, created.node, {
  403. instanceOf: ContainerElement,
  404. name: 'p',
  405. rangeEnd: 2,
  406. children: [
  407. {
  408. instanceOf: Text,
  409. data: 'foo'
  410. },
  411. {
  412. instanceOf: AttributeElement,
  413. name: 'u',
  414. priority: 1,
  415. children: [
  416. { instanceOf: Text, data: 'barbaz', rangeStart: 3 }
  417. ]
  418. },
  419. {
  420. instanceOf: AttributeElement,
  421. name: 'b',
  422. priority: 1,
  423. children: [
  424. {
  425. instanceOf: AttributeElement,
  426. name: 'u',
  427. priority: 1,
  428. children: [
  429. { instanceOf: Text, data: 'qux' }
  430. ]
  431. }
  432. ]
  433. }
  434. ]
  435. } );
  436. } );
  437. it( 'should merge unwrapped nodes #4', () => {
  438. // <p>{foo}<u>{bar}</u>[<b><u>{baz}</u></b>]<u>qux</u></p> -> <p>{foo}<u>{bar[baz]qux}</u></p>
  439. const created = create( writer, {
  440. instanceOf: ContainerElement,
  441. name: 'p',
  442. rangeStart: 2,
  443. rangeEnd: 3,
  444. children: [
  445. { instanceOf: Text, data: 'foo' },
  446. {
  447. instanceOf: AttributeElement,
  448. name: 'u',
  449. priority: 1,
  450. children: [
  451. { instanceOf: Text, data: 'bar' }
  452. ]
  453. },
  454. {
  455. instanceOf: AttributeElement,
  456. name: 'b',
  457. priority: 1,
  458. children: [
  459. {
  460. instanceOf: AttributeElement,
  461. name: 'u',
  462. priority: 1,
  463. children: [
  464. { instanceOf: Text, data: 'baz' }
  465. ]
  466. }
  467. ]
  468. },
  469. {
  470. instanceOf: AttributeElement,
  471. name: 'u',
  472. priority: 1,
  473. children: [
  474. { instanceOf: Text, data: 'qux' }
  475. ]
  476. }
  477. ]
  478. } );
  479. const b = new AttributeElement( 'b' );
  480. b.priority = 1;
  481. const newRange = writer.unwrap( created.range, b );
  482. test( writer, newRange, created.node, {
  483. instanceOf: ContainerElement,
  484. name: 'p',
  485. children: [
  486. {
  487. instanceOf: Text,
  488. data: 'foo'
  489. },
  490. {
  491. instanceOf: AttributeElement,
  492. name: 'u',
  493. priority: 1,
  494. children: [
  495. { instanceOf: Text, data: 'barbazqux', rangeStart: 3, rangeEnd: 6 }
  496. ]
  497. }
  498. ]
  499. } );
  500. } );
  501. it( 'should merge unwrapped nodes #5', () => {
  502. // <p>[<b><u>{foo}</u></b><b><u>{bar}</u></b><b><u>{baz}</u></b>]</p> -> <p>[<u>{foobarbaz}</u>]</p>
  503. const created = create( writer, {
  504. instanceOf: ContainerElement,
  505. name: 'p',
  506. rangeStart: 0,
  507. rangeEnd: 3,
  508. children: [
  509. {
  510. instanceOf: AttributeElement,
  511. name: 'b',
  512. priority: 1,
  513. children: [
  514. {
  515. instanceOf: AttributeElement,
  516. name: 'u',
  517. priority: 1,
  518. children: [
  519. { instanceOf: Text, data: 'foo' }
  520. ]
  521. }
  522. ]
  523. },
  524. {
  525. instanceOf: AttributeElement,
  526. name: 'b',
  527. priority: 1,
  528. children: [
  529. {
  530. instanceOf: AttributeElement,
  531. name: 'u',
  532. priority: 1,
  533. children: [
  534. { instanceOf: Text, data: 'bar' }
  535. ]
  536. }
  537. ]
  538. },
  539. {
  540. instanceOf: AttributeElement,
  541. name: 'b',
  542. priority: 1,
  543. children: [
  544. {
  545. instanceOf: AttributeElement,
  546. name: 'u',
  547. priority: 1,
  548. children: [
  549. { instanceOf: Text, data: 'baz' }
  550. ]
  551. }
  552. ]
  553. }
  554. ]
  555. } );
  556. const b = new AttributeElement( 'b' );
  557. b.priority = 1;
  558. const newRange = writer.unwrap( created.range, b );
  559. test( writer, newRange, created.node, {
  560. instanceOf: ContainerElement,
  561. name: 'p',
  562. rangeStart: 0,
  563. rangeEnd: 1,
  564. children: [
  565. {
  566. instanceOf: AttributeElement,
  567. name: 'u',
  568. priority: 1,
  569. children: [
  570. { instanceOf: Text, data: 'foobarbaz' }
  571. ]
  572. }
  573. ]
  574. } );
  575. } );
  576. it( 'should unwrap mixed ranges #1', () => {
  577. // <p>[<u><b>{foo}]</b></u></p> -> <p>[<u>{foo}</u>]</p
  578. const created = create( writer, {
  579. instanceOf: ContainerElement,
  580. name: 'p',
  581. rangeStart: 0,
  582. children: [
  583. {
  584. instanceOf: AttributeElement,
  585. name: 'u',
  586. priority: 1,
  587. children: [
  588. {
  589. instanceOf: AttributeElement,
  590. name: 'b',
  591. priority: 1,
  592. rangeEnd: 1,
  593. children: [
  594. { instanceOf: Text, data: 'foo' }
  595. ]
  596. }
  597. ]
  598. }
  599. ]
  600. } );
  601. const b = new AttributeElement( 'b' );
  602. b.priority = 1;
  603. const newRange = writer.unwrap( created.range, b );
  604. test( writer, newRange, created.node, {
  605. instanceOf: ContainerElement,
  606. name: 'p',
  607. rangeStart: 0,
  608. rangeEnd: 1,
  609. children: [
  610. {
  611. instanceOf: AttributeElement,
  612. name: 'u',
  613. priority: 1,
  614. children: [
  615. { instanceOf: Text, data: 'foo' }
  616. ]
  617. }
  618. ]
  619. } );
  620. } );
  621. it( 'should unwrap mixed ranges #2', () => {
  622. // <p>[<u><b>{foo]}</b></u></p> -> <p>[<u>{foo}</u>]</p
  623. const created = create( writer, {
  624. instanceOf: ContainerElement,
  625. name: 'p',
  626. rangeStart: 0,
  627. children: [
  628. {
  629. instanceOf: AttributeElement,
  630. name: 'u',
  631. priority: 1,
  632. children: [
  633. {
  634. instanceOf: AttributeElement,
  635. name: 'b',
  636. priority: 1,
  637. children: [
  638. { instanceOf: Text, data: 'foo', rangeEnd: 3 }
  639. ]
  640. }
  641. ]
  642. }
  643. ]
  644. } );
  645. const b = new AttributeElement( 'b' );
  646. b.priority = 1;
  647. const newRange = writer.unwrap( created.range, b );
  648. test( writer, newRange, created.node, {
  649. instanceOf: ContainerElement,
  650. name: 'p',
  651. rangeStart: 0,
  652. rangeEnd: 1,
  653. children: [
  654. {
  655. instanceOf: AttributeElement,
  656. name: 'u',
  657. priority: 1,
  658. children: [
  659. { instanceOf: Text, data: 'foo' }
  660. ]
  661. }
  662. ]
  663. } );
  664. } );
  665. } );
  666. } );