unwrap.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 DowncastWriter from '../../../src/view/downcastwriter';
  6. import Element from '../../../src/view/element';
  7. import ContainerElement from '../../../src/view/containerelement';
  8. import AttributeElement from '../../../src/view/attributeelement';
  9. import EmptyElement from '../../../src/view/emptyelement';
  10. import UIElement from '../../../src/view/uielement';
  11. import RawElement from '../../../src/view/rawelement';
  12. import Position from '../../../src/view/position';
  13. import Range from '../../../src/view/range';
  14. import Text from '../../../src/view/text';
  15. import { stringify, parse } from '../../../src/dev-utils/view';
  16. import Document from '../../../src/view/document';
  17. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  18. import { StylesProcessor } from '../../../src/view/stylesmap';
  19. describe( 'DowncastWriter', () => {
  20. describe( 'unwrap()', () => {
  21. let writer, document;
  22. // Executes test using `parse` and `stringify` utils functions.
  23. //
  24. // @param {String} input
  25. // @param {String} unwrapAttribute
  26. // @param {String} expected
  27. function testUnwrap( input, unwrapAttribute, expected ) {
  28. const { view, selection } = parse( input );
  29. const newRange = writer.unwrap( selection.getFirstRange(), parse( unwrapAttribute ) );
  30. expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
  31. }
  32. beforeEach( () => {
  33. document = new Document( new StylesProcessor() );
  34. writer = new DowncastWriter( document );
  35. } );
  36. it( 'should do nothing on collapsed ranges', () => {
  37. testUnwrap(
  38. '<container:p>f{}oo</container:p>',
  39. '<attribute:b view-priority="10"></attribute:b>',
  40. '<container:p>f{}oo</container:p>'
  41. );
  42. } );
  43. it( 'should do nothing on single text node', () => {
  44. testUnwrap(
  45. '<container:p>[foobar]</container:p>',
  46. '<attribute:b view-priority="1"></attribute:b>',
  47. '<container:p>[foobar]</container:p>'
  48. );
  49. } );
  50. it( 'should throw error when element is not instance of AttributeElement', () => {
  51. const container = new ContainerElement( document, 'p', null, new AttributeElement( document, 'b', null, new Text( 'foo' ) ) );
  52. const range = new Range(
  53. new Position( container, 0 ),
  54. new Position( container, 1 )
  55. );
  56. const b = new Element( document, 'b' );
  57. expectToThrowCKEditorError( () => {
  58. writer.unwrap( range, b );
  59. }, 'view-writer-unwrap-invalid-attribute', document );
  60. } );
  61. it( 'should throw error when range placed in two containers', () => {
  62. const container1 = new ContainerElement( document, 'p' );
  63. const container2 = new ContainerElement( document, 'p' );
  64. const range = new Range(
  65. new Position( container1, 0 ),
  66. new Position( container2, 1 )
  67. );
  68. const b = new AttributeElement( document, 'b' );
  69. expectToThrowCKEditorError( () => {
  70. writer.unwrap( range, b );
  71. }, 'view-writer-invalid-range-container', document );
  72. } );
  73. it( 'should throw when range has no parent container', () => {
  74. const el = new AttributeElement( document, 'b' );
  75. const b = new AttributeElement( document, 'b' );
  76. expectToThrowCKEditorError( () => {
  77. writer.unwrap( Range._createFromParentsAndOffsets( el, 0, el, 0 ), b );
  78. }, 'view-writer-invalid-range-container', document );
  79. } );
  80. it( 'should unwrap single node', () => {
  81. testUnwrap(
  82. '<container:p>[<attribute:b view-priority="1">foobar</attribute:b>]</container:p>',
  83. '<attribute:b view-priority="1"></attribute:b>',
  84. '<container:p>[foobar]</container:p>'
  85. );
  86. } );
  87. it( 'should not unwrap attributes with different priorities #1', () => {
  88. testUnwrap(
  89. '<container:p>[<attribute:b view-priority="1">foobar</attribute:b>]</container:p>',
  90. '<attribute:b view-priority="2"></attribute:b>',
  91. '<container:p>[<attribute:b view-priority="1">foobar</attribute:b>]</container:p>'
  92. );
  93. } );
  94. it( 'should not unwrap attributes with different priorities #2', () => {
  95. testUnwrap(
  96. '<container:p>' +
  97. '[' +
  98. '<attribute:b view-priority="2">foo</attribute:b>' +
  99. '<attribute:b view-priority="1">bar</attribute:b>' +
  100. '<attribute:b view-priority="2">baz</attribute:b>' +
  101. ']' +
  102. '</container:p>',
  103. '<attribute:b view-priority="2"></attribute:b>',
  104. '<container:p>[foo<attribute:b view-priority="1">bar</attribute:b>baz]</container:p>'
  105. );
  106. } );
  107. it( 'should unwrap part of the node', () => {
  108. testUnwrap(
  109. '<container:p>[baz<attribute:b view-priority="1">foo}bar</attribute:b></container:p>',
  110. '<attribute:b view-priority="1"></attribute:b>',
  111. '<container:p>[bazfoo]<attribute:b view-priority="1">bar</attribute:b></container:p>'
  112. );
  113. } );
  114. it( 'should support unicode', () => {
  115. testUnwrap(
  116. '<container:p>[நிலை<attribute:b view-priority="1">க்}கு</attribute:b></container:p>',
  117. '<attribute:b view-priority="1"></attribute:b>',
  118. '<container:p>[நிலைக்]<attribute:b view-priority="1">கு</attribute:b></container:p>'
  119. );
  120. } );
  121. it( 'should unwrap nested attributes', () => {
  122. testUnwrap(
  123. '<container:p>' +
  124. '[<attribute:u view-priority="1"><attribute:b view-priority="1">foobar</attribute:b></attribute:u>]' +
  125. '</container:p>',
  126. '<attribute:b view-priority="1"></attribute:b>',
  127. '<container:p>[<attribute:u view-priority="1">foobar</attribute:u>]</container:p>'
  128. );
  129. } );
  130. it( 'should unwrap a part of a nested attribute', () => {
  131. testUnwrap(
  132. '<container:p>' +
  133. '<attribute:u view-priority="1"><attribute:b view-priority="1">fo{ob}ar</attribute:b></attribute:u>' +
  134. '</container:p>',
  135. '<attribute:b view-priority="1"></attribute:b>',
  136. '<container:p>' +
  137. '<attribute:u view-priority="1">' +
  138. '<attribute:b view-priority="1">fo</attribute:b>' +
  139. '[ob]' +
  140. '<attribute:b view-priority="1">ar</attribute:b>' +
  141. '</attribute:u>' +
  142. '</container:p>'
  143. );
  144. } );
  145. it( 'should merge unwrapped nodes #1', () => {
  146. testUnwrap(
  147. '<container:p>foo[<attribute:b view-priority="1">bar</attribute:b>]baz</container:p>',
  148. '<attribute:b view-priority="1"></attribute:b>',
  149. '<container:p>foo{bar}baz</container:p>'
  150. );
  151. } );
  152. it( 'should merge unwrapped nodes #2', () => {
  153. const input = '<container:p>' +
  154. 'foo' +
  155. '<attribute:u view-priority="1">bar</attribute:u>' +
  156. '[' +
  157. '<attribute:b view-priority="1">' +
  158. '<attribute:u view-priority="1">bazqux</attribute:u>' +
  159. '</attribute:b>' +
  160. ']' +
  161. '</container:p>';
  162. const attribute = '<attribute:b view-priority="1"></attribute:b>';
  163. const result = '<container:p>foo<attribute:u view-priority="1">bar{bazqux</attribute:u>]</container:p>';
  164. testUnwrap( input, attribute, result );
  165. } );
  166. it( 'should merge unwrapped nodes #3', () => {
  167. const input = '<container:p>' +
  168. 'foo' +
  169. '<attribute:u view-priority="1">bar</attribute:u>' +
  170. '[' +
  171. '<attribute:b view-priority="1">' +
  172. '<attribute:u view-priority="1">baz}qux</attribute:u>' +
  173. '</attribute:b>' +
  174. '</container:p>';
  175. const attribute = '<attribute:b view-priority="1"></attribute:b>';
  176. const result = '<container:p>' +
  177. 'foo' +
  178. '<attribute:u view-priority="1">bar{baz</attribute:u>]' +
  179. '<attribute:b view-priority="1">' +
  180. '<attribute:u view-priority="1">qux</attribute:u>' +
  181. '</attribute:b>' +
  182. '</container:p>';
  183. testUnwrap( input, attribute, result );
  184. } );
  185. it( 'should merge unwrapped nodes #4', () => {
  186. const input = '<container:p>' +
  187. 'foo' +
  188. '<attribute:u view-priority="1">bar</attribute:u>' +
  189. '[' +
  190. '<attribute:b view-priority="1">' +
  191. '<attribute:u view-priority="1">baz</attribute:u>' +
  192. '</attribute:b>' +
  193. ']' +
  194. '<attribute:u view-priority="1">qux</attribute:u>' +
  195. '</container:p>';
  196. const attribute = '<attribute:b view-priority="1"></attribute:b>';
  197. const result = '<container:p>' +
  198. 'foo' +
  199. '<attribute:u view-priority="1">bar{baz}qux</attribute:u>' +
  200. '</container:p>';
  201. testUnwrap( input, attribute, result );
  202. } );
  203. it( 'should merge unwrapped nodes #5', () => {
  204. const input = '<container:p>' +
  205. '[' +
  206. '<attribute:b view-priority="1"><attribute:u view-priority="1">foo</attribute:u></attribute:b>' +
  207. '<attribute:b view-priority="1"><attribute:u view-priority="1">bar</attribute:u></attribute:b>' +
  208. '<attribute:b view-priority="1"><attribute:u view-priority="1">baz</attribute:u></attribute:b>' +
  209. ']' +
  210. '</container:p>';
  211. const attribute = '<attribute:b view-priority="1"></attribute:b>';
  212. const result = '<container:p>[<attribute:u view-priority="1">foobarbaz</attribute:u>]</container:p>';
  213. testUnwrap( input, attribute, result );
  214. } );
  215. it( 'should unwrap mixed ranges #1', () => {
  216. const input = '<container:p>' +
  217. '[' +
  218. '<attribute:u view-priority="1">' +
  219. '<attribute:b view-priority="1">foo]</attribute:b>' +
  220. '</attribute:u>' +
  221. '</container:p>';
  222. const attribute = '<attribute:b view-priority="1"></attribute:b>';
  223. const result = '<container:p>[<attribute:u view-priority="1">foo</attribute:u>]</container:p>';
  224. testUnwrap( input, attribute, result );
  225. } );
  226. it( 'should unwrap mixed ranges #2', () => {
  227. testUnwrap(
  228. '<container:p>' +
  229. '[<attribute:u view-priority="1"><attribute:b view-priority="1">foo}</attribute:b></attribute:u>' +
  230. '</container:p>',
  231. '<attribute:b view-priority="1"></attribute:b>',
  232. '<container:p>[<attribute:u view-priority="1">foo</attribute:u>]</container:p>'
  233. );
  234. } );
  235. it( 'should unwrap single element by removing matching attributes', () => {
  236. testUnwrap(
  237. '<container:p>[<attribute:b view-priority="1" foo="bar" baz="qux">test</attribute:b>]</container:p>',
  238. '<attribute:b view-priority="1" baz="qux"></attribute:b>',
  239. '<container:p>[<attribute:b view-priority="1" foo="bar">test</attribute:b>]</container:p>'
  240. );
  241. } );
  242. it( 'should not unwrap single element when attributes are different', () => {
  243. testUnwrap(
  244. '<container:p>[<attribute:b view-priority="1" baz="qux" foo="bar">test</attribute:b>]</container:p>',
  245. '<attribute:b view-priority="1" baz="qux" test="true"></attribute:b>',
  246. '<container:p>[<attribute:b view-priority="1" baz="qux" foo="bar">test</attribute:b>]</container:p>'
  247. );
  248. } );
  249. it( 'should unwrap single element by removing matching classes', () => {
  250. testUnwrap(
  251. '<container:p>[<attribute:b view-priority="1" class="bar baz foo">test</attribute:b>]</container:p>',
  252. '<attribute:b view-priority="1" class="baz foo"></attribute:b>',
  253. '<container:p>[<attribute:b view-priority="1" class="bar">test</attribute:b>]</container:p>'
  254. );
  255. } );
  256. it( 'should not unwrap single element when classes are different', () => {
  257. testUnwrap(
  258. '<container:p>[<attribute:b view-priority="1" class="bar baz foo">test</attribute:b>]</container:p>',
  259. '<attribute:b view-priority="1" class="baz foo qux"></attribute:b>',
  260. '<container:p>[<attribute:b view-priority="1" class="bar baz foo">test</attribute:b>]</container:p>'
  261. );
  262. } );
  263. it( 'should unwrap single element by removing matching styles', () => {
  264. testUnwrap(
  265. '<container:p>' +
  266. '[<attribute:b view-priority="1" style="color:red;position:absolute;top:10px;">test</attribute:b>]' +
  267. '</container:p>',
  268. '<attribute:b view-priority="1" style="position: absolute;"></attribute:b>',
  269. '<container:p>[<attribute:b view-priority="1" style="color:red;top:10px">test</attribute:b>]</container:p>'
  270. );
  271. } );
  272. it( 'should not unwrap single element when styles are different', () => {
  273. testUnwrap(
  274. '<container:p>' +
  275. '[<attribute:b view-priority="1" style="color:red;position:absolute;top:10px">test</attribute:b>]' +
  276. '</container:p>',
  277. '<attribute:b view-priority="1" style="position: relative;"></attribute:b>',
  278. '<container:p>' +
  279. '[<attribute:b view-priority="1" style="color:red;position:absolute;top:10px">test</attribute:b>]' +
  280. '</container:p>'
  281. );
  282. } );
  283. it( 'should partially unwrap part of a node', () => {
  284. testUnwrap(
  285. '<container:p>' +
  286. '[<attribute:b view-priority="1" baz="qux" foo="bar">foo}bar</attribute:b>' +
  287. '</container:p>',
  288. '<attribute:b view-priority="1" foo="bar"></attribute:b>',
  289. '<container:p>' +
  290. '[<attribute:b view-priority="1" baz="qux">foo</attribute:b>]' +
  291. '<attribute:b view-priority="1" baz="qux" foo="bar">bar</attribute:b>' +
  292. '</container:p>'
  293. );
  294. } );
  295. it( 'should partially unwrap a nested attribute', () => {
  296. testUnwrap(
  297. '<container:p>' +
  298. '[<attribute:i view-priority="1">' +
  299. '<attribute:b view-priority="1" style="color:red;position:absolute;top:10px;">test</attribute:b>' +
  300. '</attribute:i>]' +
  301. '</container:p>',
  302. '<attribute:b view-priority="1" style="position: absolute;"></attribute:b>',
  303. '<container:p>' +
  304. '[<attribute:i view-priority="1">' +
  305. '<attribute:b view-priority="1" style="color:red;top:10px">test</attribute:b>' +
  306. '</attribute:i>]' +
  307. '</container:p>'
  308. );
  309. } );
  310. it( 'should partially unwrap a part of a nested attribute', () => {
  311. testUnwrap(
  312. '<container:p>' +
  313. '<attribute:i view-priority="1">' +
  314. '<attribute:b view-priority="1" style="color:red;position:absolute;top:10px;">t{es}t</attribute:b>' +
  315. '</attribute:i>' +
  316. '</container:p>',
  317. '<attribute:b view-priority="1" style="position: absolute;"></attribute:b>',
  318. '<container:p>' +
  319. '<attribute:i view-priority="1">' +
  320. '<attribute:b view-priority="1" style="color:red;position:absolute;top:10px">t</attribute:b>' +
  321. '[<attribute:b view-priority="1" style="color:red;top:10px">es</attribute:b>]' +
  322. '<attribute:b view-priority="1" style="color:red;position:absolute;top:10px">t</attribute:b>' +
  323. '</attribute:i>' +
  324. '</container:p>'
  325. );
  326. } );
  327. it( 'should be merged after being partially unwrapped', () => {
  328. testUnwrap(
  329. '<container:p>' +
  330. '<attribute:b view-priority="1" baz="qux">xyz</attribute:b>' +
  331. '[<attribute:b view-priority="1" baz="qux" foo="bar">foo}bar</attribute:b>' +
  332. '</container:p>',
  333. '<attribute:b view-priority="1" foo="bar"></attribute:b>',
  334. '<container:p>' +
  335. '<attribute:b view-priority="1" baz="qux">xyz{foo</attribute:b>]' +
  336. '<attribute:b view-priority="1" baz="qux" foo="bar">bar</attribute:b>' +
  337. '</container:p>'
  338. );
  339. } );
  340. it( 'should unwrap single node in document fragment', () => {
  341. testUnwrap(
  342. '<container:p>[<attribute:b view-priority="1">foobar</attribute:b>]</container:p>',
  343. '<attribute:b view-priority="1"></attribute:b>',
  344. '<container:p>[foobar]</container:p>'
  345. );
  346. } );
  347. it( 'should unwrap EmptyElement', () => {
  348. testUnwrap(
  349. '<container:p>[<attribute:b><empty:img></empty:img></attribute:b>]</container:p>',
  350. '<attribute:b></attribute:b>',
  351. '<container:p>[<empty:img></empty:img>]</container:p>'
  352. );
  353. } );
  354. it( 'should throw if range is inside EmptyElement', () => {
  355. const empty = new EmptyElement( document, 'img' );
  356. const attribute = new AttributeElement( document, 'b' );
  357. const container = new ContainerElement( document, 'p', null, [ empty, attribute ] );
  358. const range = Range._createFromParentsAndOffsets( empty, 0, container, 2 );
  359. expectToThrowCKEditorError( () => {
  360. writer.unwrap( range, attribute );
  361. }, 'view-writer-cannot-break-empty-element', document );
  362. } );
  363. it( 'should unwrap UIElement', () => {
  364. testUnwrap(
  365. '<container:p>[<attribute:b><ui:span></ui:span></attribute:b>]</container:p>',
  366. '<attribute:b></attribute:b>',
  367. '<container:p>[<ui:span></ui:span>]</container:p>'
  368. );
  369. } );
  370. it( 'should throw if range is placed inside UIElement', () => {
  371. const uiElement = new UIElement( document, 'span' );
  372. const attribute = new AttributeElement( document, 'b' );
  373. const container = new ContainerElement( document, 'p', null, [ uiElement, attribute ] );
  374. const range = Range._createFromParentsAndOffsets( uiElement, 0, container, 2 );
  375. expectToThrowCKEditorError( () => {
  376. writer.unwrap( range, attribute );
  377. }, 'view-writer-cannot-break-ui-element', document );
  378. } );
  379. it( 'should unwrap a RawElement', () => {
  380. testUnwrap(
  381. '<container:p>[<attribute:b><raw:span></raw:span></attribute:b>]</container:p>',
  382. '<attribute:b></attribute:b>',
  383. '<container:p>[<raw:span></raw:span>]</container:p>'
  384. );
  385. } );
  386. it( 'should throw if a range is placed inside a RawElement', () => {
  387. const rawElement = new RawElement( document, 'span' );
  388. const attribute = new AttributeElement( document, 'b' );
  389. const container = new ContainerElement( document, 'p', null, [ rawElement, attribute ] );
  390. const range = Range._createFromParentsAndOffsets( rawElement, 0, container, 2 );
  391. expectToThrowCKEditorError( () => {
  392. writer.unwrap( range, attribute );
  393. }, 'view-writer-cannot-break-raw-element', document );
  394. } );
  395. it( 'should unwrap if both elements have same id', () => {
  396. const unwrapper = writer.createAttributeElement( 'span', null, { id: 'foo' } );
  397. const attribute = writer.createAttributeElement( 'span', null, { id: 'foo' } );
  398. const container = writer.createContainerElement( 'div' );
  399. writer.insert( writer.createPositionAt( container, 0 ), attribute );
  400. writer.unwrap( Range._createOn( attribute ), unwrapper );
  401. expect( stringify( container, null, { showType: false, showPriority: false } ) ).to.equal( '<div></div>' );
  402. } );
  403. it( 'should always unwrap whole element if both elements have same id', () => {
  404. const unwrapper = writer.createAttributeElement( 'b', null, { id: 'foo' } );
  405. const attribute = writer.createAttributeElement( 'span', { foo: 'foo' }, { id: 'foo' } );
  406. const container = writer.createContainerElement( 'div' );
  407. writer.insert( writer.createPositionAt( container, 0 ), attribute );
  408. writer.unwrap( writer.createRangeOn( attribute ), unwrapper );
  409. expect( stringify( container, null, { showType: false, showPriority: false } ) ).to.equal( '<div></div>' );
  410. } );
  411. // Below are tests for elements with different ids.
  412. // Only partial unwrapping is tested because elements with different ids are never similar.
  413. // This means that unwrapping of whole element is not possible in this case.
  414. it( 'should not unwrap matching attributes if the element to unwrap has id', () => {
  415. const unwrapper = writer.createAttributeElement( 'span', { foo: 'foo' } );
  416. const attribute = writer.createAttributeElement( 'span', { foo: 'foo', bar: 'bar' }, { id: 'id' } );
  417. const container = writer.createContainerElement( 'div' );
  418. writer.insert( writer.createPositionAt( container, 0 ), attribute );
  419. writer.unwrap( writer.createRangeOn( attribute ), unwrapper );
  420. const view = stringify( container, null, { showType: false, showPriority: false } );
  421. expect( view ).to.equal( '<div><span bar="bar" foo="foo"></span></div>' );
  422. } );
  423. it( 'should not unwrap matching attributes if the unwrapping element has id', () => {
  424. const unwrapper = writer.createAttributeElement( 'span', { foo: 'foo' }, { id: 'id' } );
  425. const attribute = writer.createAttributeElement( 'span', { foo: 'foo', bar: 'bar' } );
  426. const container = writer.createContainerElement( 'div' );
  427. writer.insert( writer.createPositionAt( container, 0 ), attribute );
  428. writer.unwrap( writer.createRangeOn( attribute ), unwrapper );
  429. const view = stringify( container, null, { showType: false, showPriority: false } );
  430. expect( view ).to.equal( '<div><span bar="bar" foo="foo"></span></div>' );
  431. } );
  432. it( 'should not unwrap matching attributes if the elements have different id', () => {
  433. const unwrapper = writer.createAttributeElement( 'span', { foo: 'foo' }, { id: 'a' } );
  434. const attribute = writer.createAttributeElement( 'span', { foo: 'foo', bar: 'bar' }, { id: 'b' } );
  435. const container = writer.createContainerElement( 'div' );
  436. writer.insert( writer.createPositionAt( container, 0 ), attribute );
  437. writer.unwrap( writer.createRangeOn( attribute ), unwrapper );
  438. const view = stringify( container, null, { showType: false, showPriority: false } );
  439. expect( view ).to.equal( '<div><span bar="bar" foo="foo"></span></div>' );
  440. } );
  441. } );
  442. } );