Skip to content
Closed
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
9 changes: 7 additions & 2 deletions packages/dom/src/dom/clean-node-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ export default function cleanNodeList( nodeList, doc, schema, inline ) {
} = schema[ tag ];

// If the node is empty and it's supposed to have children,
// remove the node.
// remove the node. For phrasing content, unwrap instead to
// preserve any whitespace content (e.g. <b> </b> between words).
if ( children && ! allowEmpty && isEmpty( node ) ) {
remove( node );
if ( isPhrasingContent( node ) ) {
unwrap( node );
} else {
remove( node );
}
return;
}

Expand Down
59 changes: 59 additions & 0 deletions packages/dom/src/dom/test/clean-node-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Internal dependencies
*/
import removeInvalidHTML from '../remove-invalid-html';

describe( 'cleanNodeList', () => {
it( 'should preserve whitespace when unwrapping empty phrasing elements', () => {
const schema = {
p: { children: { '#text': {} } },
};
// Whitespace inside <b> should be preserved when <b> is unwrapped.
const input = '<p>some text<b> </b>more text</p>';
const output = '<p>some text more text</p>';
expect( removeInvalidHTML( input, schema, false ) ).toEqual( output );
} );

it( 'should preserve whitespace in various phrasing elements', () => {
const schema = {
p: { children: { '#text': {} } },
};
// Test with different phrasing elements.
expect(
removeInvalidHTML(
'<p>hello<strong> </strong>world</p>',
schema,
false
)
).toEqual( '<p>hello world</p>' );
expect(
removeInvalidHTML( '<p>hello<em> </em>world</p>', schema, false )
).toEqual( '<p>hello world</p>' );
expect(
removeInvalidHTML( '<p>hello<sup> </sup>world</p>', schema, false )
).toEqual( '<p>hello world</p>' );
} );

it( 'should remove truly empty elements without whitespace', () => {
const schema = {
p: { children: { '#text': {} } },
};
const input = '<p>some text<b></b>more text</p>';
const output = '<p>some textmore text</p>';
expect( removeInvalidHTML( input, schema, false ) ).toEqual( output );
} );

it( 'should remove empty block elements entirely (not unwrap)', () => {
const schema = {
figure: {
children: {
img: {},
},
},
};
// Block elements like figure should be removed, not unwrapped.
const input = '<figure> </figure>';
const output = '';
expect( removeInvalidHTML( input, schema, false ) ).toEqual( output );
} );
} );
Loading