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

Add unwrapElement method to UpcastWriter #1772

Merged
merged 2 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my curiosity. Why not just unwrap? Other methods from this class (like remove, replace, rename) don't have Element suffix.

Copy link
Contributor Author

@msamsel msamsel Aug 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've followed by this explanation:
ckeditor/ckeditor5-paste-from-office#72 (comment)

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