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

Commit

Permalink
Merge pull request #1772 from ckeditor/t/paste-from-office/69
Browse files Browse the repository at this point in the history
Other: Add unwrapElement() method to UpcastWriter.
  • Loading branch information
jodator authored Aug 2, 2019
2 parents 04858be + 61cf87f commit 9e97196
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/view/upcastwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ export default class UpcastWriter {
return false;
}

/**
* Removes given element from view structure and places its children in its position.
* It does nothing if element has no parent.
*
* @param {module:engine/view/element~Element} element Element which will be unwrapped
* @returns {Booolean} Whether element was successfully unwrapped.
*/
unwrapElement( element ) {
const parent = element.parent;

if ( parent ) {
const index = parent.getChildIndex( element );

this.remove( element );
this.insertChild( index, element.getChildren(), parent );
}
}

/**
* Renames element by creating a copy of a given element but with its name changed and then moving contents of the
* old element to the new one.
Expand Down
30 changes: 30 additions & 0 deletions tests/view/upcastwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,36 @@ describe( 'UpcastWriter', () => {
} );
} );

describe( 'unwrapElement', () => {
it( 'should unwrap simple element', () => {
const documentFragment = dataprocessor.toView( '<ul><li><p>foo</p></li></ul>' );
const paragraph = documentFragment.getChild( 0 ).getChild( 0 ).getChild( 0 );

writer.unwrapElement( paragraph );

expect( dataprocessor.toData( documentFragment ) ).to.equal( '<ul><li>foo</li></ul>' );
} );

it( 'should unwrap element with children', () => {
const documentFragment = dataprocessor.toView(
'<p><span style="color:red"><strong>foo</strong><a href="example.com">example</a>bar</span></p>' );
const span = documentFragment.getChild( 0 ).getChild( 0 );

writer.unwrapElement( span );

expect( dataprocessor.toData( documentFragment ) ).to.equal(
'<p><strong>foo</strong><a href="example.com">example</a>bar</p>' );
} );

it( 'should do nothing for elements without parent', () => {
const element = new Element( 'p', null, 'foo' );

writer.unwrapElement( element );

expect( dataprocessor.toData( element ) ).to.equal( '<p>foo</p>' );
} );
} );

describe( 'rename', () => {
it( 'should rename simple element', () => {
const el = view.getChild( 0 ).getChild( 1 );
Expand Down

0 comments on commit 9e97196

Please sign in to comment.