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

Pasting Mixed Tagname HTML Breaks if 'core/heading' Block Has Been Disabled #19036

Open
Kelderic opened this issue Dec 10, 2019 · 3 comments
Open
Labels
[Feature] Raw Handling Related to the ability to convert content to blocks, paste handling, etc [Type] Bug An existing feature does not function as intended

Comments

@Kelderic
Copy link

Kelderic commented Dec 10, 2019

Describe the bug
When using the default 'core/paragraph' and 'core/heading' blocks, I can copy text from another website that includes <p> tags and <h2> tags mixed within the copied content. I can then paste it into Gutenberg and the content is properly split. However, if the 'core/heading' is disabled, (say to replace it with a custom one), then pasting stops working altogether if the pasted content contains mixed text.

To reproduce
Steps to reproduce the behavior:

  1. Disable the 'core/heading' block.
  2. Add custom block using RichText, with <h2> as the wrapper.
  3. Copy the following HTML: <p>First line</p><h2>Second line.</h2><p>Third line</p>
  4. The console will log that the content has been properly converted. But nothing will happen and the paste breaks.

Expected behavior
I would expect the content to be pasted into three blocks. A paragraph, a custom heading, and a paragraph. Or, I'd at least expect a console error message.

Actual behavior
Nothing happens. Pasting just does nothing.

Screenshots
Untitled Project

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser Firefox
  • Version 71.0

Custom block RichText Element:

		        <RichText 
		            tagName="h2"
		            withoutInteractiveFormatting={ true }
		            allowedFormats={ [] }
		            className={ className }
		            value={ attributes.content }
		            onChange={ ( value ) => blockProps.setAttributes( { content: value } ) }
		            style={ { textAlign: attributes.alignment } }
		            placeholder={ 'Write heading...' }
		            keepPlaceholderOnFocus={ true }
		        />

Does there need to be a way to register a custom block as the recipient of certain HTML paste types?

@Kelderic
Copy link
Author

Kelderic commented Dec 11, 2019

The code that I am using to disable the 'core/heading' block is:

// ONLY ALLOW CERTAIN BLOCKS

add_filter( 'allowed_block_types', 'allowed_block_types' );
 
function allowed_block_types( $allowed_blocks ) {
 
	return array(
		'core/block',
		'core/image',
		'core/paragraph',
		'core/list',
		'core/quote',
		'core/video',
		'core/code',
		'core/table',
		'core/separator'
	);
 
}

@Kelderic Kelderic changed the title Pasting Mixed Tagname HTML Breaks if 'core/heading' Block Has Been Replaced Pasting Mixed Tagname HTML Breaks if 'core/heading' Block Has Been Disabled Jan 2, 2020
@talldan talldan added [Feature] Raw Handling Related to the ability to convert content to blocks, paste handling, etc [Type] Bug An existing feature does not function as intended labels Jan 24, 2020
@talldan
Copy link
Contributor

talldan commented Jan 24, 2020

@Kelderic I think this could be considered a bug. Ideally the editor wouldn't just ignore the pasted content even when some blocks are disabled. I think the Classic block or Custom HTML block could be considered fallbacks for content that can't be matched to a block, but the paste was still ignored when I tried with those blocks enabled.

Does there need to be a way to register a custom block as the recipient of certain HTML paste types?

Yep, that's handled using the transform system as a 'raw' transform. Here's how the Heading block handles it:

{
type: 'raw',
selector: 'h1,h2,h3,h4,h5,h6',
schema: ( { phrasingContentSchema, isPaste } ) => {
const schema = {
children: phrasingContentSchema,
attributes: isPaste ? [] : [ 'style' ],
};
return {
h1: schema,
h2: schema,
h3: schema,
h4: schema,
h5: schema,
h6: schema,
};
},
transform( node ) {
const attributes = getBlockAttributes( name, node.outerHTML );
const { textAlign } = node.style || {};
attributes.level = getLevelFromHeadingNodeName( node.nodeName );
if (
textAlign === 'left' ||
textAlign === 'center' ||
textAlign === 'right'
) {
attributes.align = textAlign;
}
return createBlock( name, attributes );
},

Unfortunately there aren't a lot of docs for raw transforms, but the main bit you'd be concerned about is the transform function and creating your block with the right attributes instead of the core/heading block.

@Mamaduka
Copy link
Member

Mamaduka commented Dec 7, 2022

The root cause of the issue is probably the same as for #11245 and #27835.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Raw Handling Related to the ability to convert content to blocks, paste handling, etc [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

3 participants