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

Revert: Rich text: copy tag name on internal paste #54301

Merged
merged 3 commits into from
Sep 18, 2023
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Introduce new sections and organize content to help visitors (and search engines

- **Name:** core/heading
- **Category:** text
- **Supports:** align (full, wide), anchor, className, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight)
- **Supports:** __unstablePasteTextInline, align (full, wide), anchor, className, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight)
- **Attributes:** content, level, placeholder, textAlign

## Home Link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ export function usePasteHandler( props ) {
}

const files = [ ...getFilesFromDataTransfer( clipboardData ) ];
const isInternal = clipboardData.getData( 'rich-text' ) === 'true';

// If the data comes from a rich text instance, we can directly use it
// without filtering the data. The filters are only meant for externally
// pasted content and remove inline styles.
if ( isInternal ) {
const pastedValue = create( {
html,
preserveWhiteSpace,
} );
addActiveFormats( pastedValue, value.activeFormats );
onChange( insert( value, pastedValue ) );
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This change here introduced a regression. Basically if you have a plain link in your paragraph for instance, and you cut the link and paste it over some text to create a real link, the link won't be created.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the regression is caused by the change in order here #55195


if ( pastePlainText ) {
onChange( insert( value, create( { text: plainText } ) ) );
Expand Down Expand Up @@ -188,10 +202,6 @@ export function usePasteHandler( props ) {
mode,
tagName,
preserveWhiteSpace,
// If the data comes from a rich text instance, we can directly
// use it without filtering the data. The filters are only meant
// for externally pasted content and remove inline styles.
disableFilters: !! clipboardData.getData( 'rich-text' ),
} );

if ( typeof content === 'string' ) {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"textTransform": true
}
},
"__unstablePasteTextInline": true,
"__experimentalSlashInserter": true
},
"editorStyle": "wp-block-heading-editor",
Expand Down
1 change: 0 additions & 1 deletion packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ _Parameters_
- _options.mode_ `[string]`: Handle content as blocks or inline content. _ 'AUTO': Decide based on the content passed. _ 'INLINE': Always handle as inline content, and return string. \* 'BLOCKS': Always handle as blocks, and return array of blocks.
- _options.tagName_ `[Array]`: The tag into which content will be inserted.
- _options.preserveWhiteSpace_ `[boolean]`: Whether or not to preserve consequent white space.
- _options.disableFilters_ `[boolean]`: Whether or not to filter non semantic content.

_Returns_

Expand Down
70 changes: 25 additions & 45 deletions packages/blocks/src/api/raw-handling/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getPhrasingContentSchema, removeInvalidHTML } from '@wordpress/dom';
*/
import { htmlToBlocks } from './html-to-blocks';
import { hasBlockSupport } from '../registration';
import { getBlockInnerHTML } from '../serializer';
import parse from '../parser';
import normaliseBlocks from './normalise-blocks';
import specialCommentConverter from './special-comment-converter';
Expand Down Expand Up @@ -65,40 +66,6 @@ function filterInlineHTML( HTML, preserveWhiteSpace ) {
return HTML;
}

/**
* If we're allowed to return inline content, and there is only one inlineable
* block, and the original plain text content does not have any line breaks,
* then treat it as inline paste.
*
* @param {Object} options
* @param {Array} options.blocks
* @param {string} options.plainText
* @param {string} options.mode
*/
function maybeConvertToInline( { blocks, plainText, mode } ) {
if (
mode === 'AUTO' &&
blocks.length === 1 &&
hasBlockSupport( blocks[ 0 ].name, '__unstablePasteTextInline', false )
) {
const trimRegex = /^[\n]+|[\n]+$/g;
// Don't catch line breaks at the start or end.
const trimmedPlainText = plainText.replace( trimRegex, '' );

if (
trimmedPlainText !== '' &&
trimmedPlainText.indexOf( '\n' ) === -1
) {
const target = blocks[ 0 ].innerBlocks.length
? blocks[ 0 ].innerBlocks[ 0 ]
: blocks[ 0 ];
return target.attributes.content;
}
}

return blocks;
}

/**
* Converts an HTML string to known blocks. Strips everything else.
*
Expand All @@ -112,7 +79,6 @@ function maybeConvertToInline( { blocks, plainText, mode } ) {
* @param {Array} [options.tagName] The tag into which content will be inserted.
* @param {boolean} [options.preserveWhiteSpace] Whether or not to preserve consequent white space.
*
* @param {boolean} [options.disableFilters] Whether or not to filter non semantic content.
* @return {Array|string} A list of blocks or a string, depending on `handlerMode`.
*/
export function pasteHandler( {
Expand All @@ -121,7 +87,6 @@ export function pasteHandler( {
mode = 'AUTO',
tagName,
preserveWhiteSpace,
disableFilters,
} ) {
// First of all, strip any meta tags.
HTML = HTML.replace( /<meta[^>]+>/g, '' );
Expand Down Expand Up @@ -156,14 +121,6 @@ export function pasteHandler( {
HTML = HTML.normalize();
}

if ( disableFilters ) {
return maybeConvertToInline( {
blocks: htmlToBlocks( normaliseBlocks( HTML ), pasteHandler ),
plainText,
mode,
} );
}

// Parse Markdown (and encoded HTML) if:
// * There is a plain text version.
// * There is no HTML version, or it has no formatting.
Expand Down Expand Up @@ -262,5 +219,28 @@ export function pasteHandler( {
.flat()
.filter( Boolean );

return maybeConvertToInline( { blocks, plainText, mode } );
// If we're allowed to return inline content, and there is only one
// inlineable block, and the original plain text content does not have any
// line breaks, then treat it as inline paste.
if (
mode === 'AUTO' &&
blocks.length === 1 &&
hasBlockSupport( blocks[ 0 ].name, '__unstablePasteTextInline', false )
) {
const trimRegex = /^[\n]+|[\n]+$/g;
// Don't catch line breaks at the start or end.
const trimmedPlainText = plainText.replace( trimRegex, '' );

if (
trimmedPlainText !== '' &&
trimmedPlainText.indexOf( '\n' ) === -1
) {
return removeInvalidHTML(
getBlockInnerHTML( blocks[ 0 ] ),
phrasingContentSchema
).replace( trimRegex, '' );
}
}

return blocks;
}
9 changes: 1 addition & 8 deletions packages/rich-text/src/component/use-copy-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,10 @@ export function useCopyHandler( props ) {

const selectedRecord = slice( record.current );
const plainText = getTextContent( selectedRecord );
const tagName = element.tagName.toLowerCase();

let html = toHTMLString( {
const html = toHTMLString( {
value: selectedRecord,
preserveWhiteSpace,
} );

if ( tagName && tagName !== 'span' && tagName !== 'div' ) {
html = `<${ tagName }>${ html }</${ tagName }>`;
}

event.clipboardData.setData( 'text/plain', plainText );
event.clipboardData.setData( 'text/html', html );
event.clipboardData.setData( 'rich-text', 'true' );
Expand Down
27 changes: 1 addition & 26 deletions test/e2e/specs/editor/various/rich-text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,11 +656,7 @@ test.describe( 'RichText', () => {
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: '1' },
},
{
name: 'core/list-item',
attributes: { content: '2' },
attributes: { content: '1<br>2' },
},
],
},
Expand Down Expand Up @@ -802,25 +798,4 @@ test.describe( 'RichText', () => {
},
] );
} );

test( 'should copy/paste heading', async ( {
page,
editor,
pageUtils,
} ) => {
await editor.insertBlock( { name: 'core/heading' } );
await page.keyboard.type( 'Heading' );
await pageUtils.pressKeys( 'primary+a' );
await pageUtils.pressKeys( 'primary+c' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'Enter' );
await pageUtils.pressKeys( 'primary+v' );

expect( await editor.getBlocks() ).toMatchObject(
Array( 2 ).fill( {
name: 'core/heading',
attributes: { content: 'Heading' },
} )
);
} );
} );
6 changes: 2 additions & 4 deletions test/integration/blocks-raw-handling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,9 @@ describe( 'Blocks raw handling', () => {
HTML: '<h1>FOO</h1>',
plainText: 'FOO\n',
mode: 'AUTO',
} )
.map( getBlockContent )
.join( '' );
} );

expect( filtered ).toBe( '<h1 class="wp-block-heading">FOO</h1>' );
expect( filtered ).toBe( 'FOO' );
expect( console ).toHaveLogged();
} );

Expand Down
Loading