Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 9e97196

Browse files
authored
Merge pull request #1772 from ckeditor/t/paste-from-office/69
Other: Add unwrapElement() method to UpcastWriter.
2 parents 04858be + 61cf87f commit 9e97196

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/view/upcastwriter.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,24 @@ export default class UpcastWriter {
172172
return false;
173173
}
174174

175+
/**
176+
* Removes given element from view structure and places its children in its position.
177+
* It does nothing if element has no parent.
178+
*
179+
* @param {module:engine/view/element~Element} element Element which will be unwrapped
180+
* @returns {Booolean} Whether element was successfully unwrapped.
181+
*/
182+
unwrapElement( element ) {
183+
const parent = element.parent;
184+
185+
if ( parent ) {
186+
const index = parent.getChildIndex( element );
187+
188+
this.remove( element );
189+
this.insertChild( index, element.getChildren(), parent );
190+
}
191+
}
192+
175193
/**
176194
* Renames element by creating a copy of a given element but with its name changed and then moving contents of the
177195
* old element to the new one.

tests/view/upcastwriter.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,36 @@ describe( 'UpcastWriter', () => {
328328
} );
329329
} );
330330

331+
describe( 'unwrapElement', () => {
332+
it( 'should unwrap simple element', () => {
333+
const documentFragment = dataprocessor.toView( '<ul><li><p>foo</p></li></ul>' );
334+
const paragraph = documentFragment.getChild( 0 ).getChild( 0 ).getChild( 0 );
335+
336+
writer.unwrapElement( paragraph );
337+
338+
expect( dataprocessor.toData( documentFragment ) ).to.equal( '<ul><li>foo</li></ul>' );
339+
} );
340+
341+
it( 'should unwrap element with children', () => {
342+
const documentFragment = dataprocessor.toView(
343+
'<p><span style="color:red"><strong>foo</strong><a href="example.com">example</a>bar</span></p>' );
344+
const span = documentFragment.getChild( 0 ).getChild( 0 );
345+
346+
writer.unwrapElement( span );
347+
348+
expect( dataprocessor.toData( documentFragment ) ).to.equal(
349+
'<p><strong>foo</strong><a href="example.com">example</a>bar</p>' );
350+
} );
351+
352+
it( 'should do nothing for elements without parent', () => {
353+
const element = new Element( 'p', null, 'foo' );
354+
355+
writer.unwrapElement( element );
356+
357+
expect( dataprocessor.toData( element ) ).to.equal( '<p>foo</p>' );
358+
} );
359+
} );
360+
331361
describe( 'rename', () => {
332362
it( 'should rename simple element', () => {
333363
const el = view.getChild( 0 ).getChild( 1 );

0 commit comments

Comments
 (0)