Skip to content

Commit

Permalink
WordPress#5946: Fixed empty quote block issue on copy paste
Browse files Browse the repository at this point in the history
Signed-off-by: Faishal Saiyed <[email protected]>
  • Loading branch information
faishal committed Apr 13, 2018
1 parent 0c5aa0d commit 111a23e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions blocks/api/raw-handling/blockquote-normaliser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
* Internal dependencies
*/
import normaliseBlocks from './normalise-blocks';
import { isInlineWrapper } from './utils';

/**
* Browser dependencies
*/
const { ELEMENT_NODE } = window.Node;

function replace( node, tagName ) {
const newNode = document.createElement( tagName );

while ( node.firstChild ) {
newNode.appendChild( node.firstChild );
}

node.parentNode.replaceChild( newNode, node );
}

export default function( node ) {
if ( node.nodeType !== ELEMENT_NODE ) {
return;
Expand All @@ -17,5 +28,14 @@ export default function( node ) {
return;
}

Array.from( node.childNodes ).forEach( ( childNode ) => {
// Quote component only handle p tag inside blockquote tag
// normaliseBlocks will not wrap any inline wrapper tag with p so we have to do it manually.
// If child node is inline wrapper node and not paragraph then convert it to paragraph.
if ( isInlineWrapper( childNode ) && node.nodeName.toLowerCase() !== 'p' ) {
replace( childNode, 'p' );
}
} );

node.innerHTML = normaliseBlocks( node.innerHTML );
}
18 changes: 18 additions & 0 deletions blocks/api/raw-handling/test/blockquote-normaliser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,22 @@ describe( 'blockquoteNormaliser', () => {
const output = '<blockquote><p>test</p></blockquote>';
equal( deepFilterHTML( input, [ blockquoteNormaliser ] ), output );
} );

it( 'should normalise blockquote containing inline wrapper tag', () => {
const input = '<blockquote><h2>test</h2></blockquote>';
const output = '<blockquote><p>test</p></blockquote>';
equal( deepFilterHTML( input, [ blockquoteNormaliser ] ), output );
} );

it( 'should normalise blockquote containing multiple inline tags', () => {
const input = '<blockquote><p>test</p><h1>test2</h1></blockquote>';
const output = '<blockquote><p>test</p><p>test2</p></blockquote>';
equal( deepFilterHTML( input, [ blockquoteNormaliser ] ), output );
} );

it( 'should normalise blockquote containing multiple inline tags and caption', () => {
const input = '<blockquote><h1>test</h1><cite>cite</cite></blockquote>';
const output = '<blockquote><p>test</p><cite>cite</cite></blockquote>';
equal( deepFilterHTML( input, [ blockquoteNormaliser ] ), output );
} );
} );

0 comments on commit 111a23e

Please sign in to comment.