Skip to content

Commit

Permalink
Strip HTML from Post Title when pasting (#35825)
Browse files Browse the repository at this point in the history
* Strip HTML from the post title field portion of the blocks

* Also strip HTML if the content is a string

Co-authored-by: Ben Dwyer <[email protected]>
  • Loading branch information
getdave and scruffian authored Nov 9, 2022
1 parent 07c948d commit 6d804a9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@wordpress/data": "file:../data",
"@wordpress/date": "file:../date",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
Expand Down
8 changes: 6 additions & 2 deletions packages/editor/src/components/post-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
insert,
} from '@wordpress/rich-text';
import { useMergeRefs } from '@wordpress/compose';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';

/**
* Internal dependencies
Expand Down Expand Up @@ -166,7 +167,7 @@ function PostTitle( _, forwardedRef ) {
( firstBlock.name === 'core/heading' ||
firstBlock.name === 'core/paragraph' )
) {
onUpdate( firstBlock.attributes.content );
onUpdate( stripHTML( firstBlock.attributes.content ) );
onInsertBlockAfter( content.slice( 1 ) );
} else {
onInsertBlockAfter( content );
Expand All @@ -176,7 +177,10 @@ function PostTitle( _, forwardedRef ) {
...create( { html: title } ),
...selection,
};
const newValue = insert( value, create( { html: content } ) );
const newValue = insert(
value,
create( { html: stripHTML( content ) } )
);
onUpdate( toHTMLString( { value: newValue } ) );
setSelection( {
start: newValue.start,
Expand Down

0 comments on commit 6d804a9

Please sign in to comment.