Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed empty quote block issue on copy paste #6160

Closed
wants to merge 1 commit into from
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
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 );
} );
} );