Skip to content

Commit

Permalink
Init rich text values on block attributes parse
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Aug 11, 2023
1 parent 073f0d3 commit bff3746
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
9 changes: 7 additions & 2 deletions packages/block-editor/src/components/rich-text/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import deprecated from '@wordpress/deprecated';
*/
import { getMultilineTag } from './utils';

export const Content = ( { value, tagName: Tag, multiline, ...props } ) => {
export const Content = ( {
value = '',
tagName: Tag,
multiline,
...props
} ) => {
// Handle deprecated `children` and `node` sources.
if ( Array.isArray( value ) ) {
deprecated( 'wp.blockEditor.RichText value prop as children type', {
Expand All @@ -29,7 +34,7 @@ export const Content = ( { value, tagName: Tag, multiline, ...props } ) => {
value = `<${ MultilineTag }></${ MultilineTag }>`;
}

const content = <RawHTML>{ value }</RawHTML>;
const content = <RawHTML>{ value.valueOf() }</RawHTML>;

if ( Tag ) {
const { format, ...restProps } = props;
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@wordpress/i18n": "file:../i18n",
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/shortcode": "file:../shortcode",
"change-case": "^4.1.2",
"colord": "^2.7.0",
Expand Down
14 changes: 12 additions & 2 deletions packages/blocks/src/api/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
*/
export { attr, prop, text, query } from 'hpq';

/**
* WordPress dependencies
*/
import { __unstableCreateRichTextString, create } from '@wordpress/rich-text';

/**
* Internal dependencies
*/
export { matcher as node } from './node';
export { matcher as children } from './children';

export function html( selector, multilineTag ) {
export function html( selector, multilineTag, preserveWhiteSpace ) {
return ( domNode ) => {
let match = domNode;

Expand Down Expand Up @@ -38,6 +43,11 @@ export function html( selector, multilineTag ) {
return value;
}

return match.innerHTML;
return __unstableCreateRichTextString( {
// This is faste because we don't need to parse the HTML string.
value: create( { element: match } ),
multilineTag,
preserveWhiteSpace,
} );
};
}
8 changes: 6 additions & 2 deletions packages/blocks/src/api/parser/get-block-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const toBooleanAttributeMatcher = ( matcher ) =>
export function isOfType( value, type ) {
switch ( type ) {
case 'string':
return typeof value === 'string';
return value instanceof String || typeof value === 'string';

case 'boolean':
return typeof value === 'boolean';
Expand Down Expand Up @@ -208,7 +208,11 @@ export const matcherFromSource = memoize( ( sourceConfig ) => {

return matcher;
case 'html':
return html( sourceConfig.selector, sourceConfig.multiline );
return html(
sourceConfig.selector,
sourceConfig.multiline,
sourceConfig.preserveWhitespace
);
case 'text':
return text( sourceConfig.selector );
case 'children':
Expand Down
1 change: 1 addition & 0 deletions packages/rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { toggleFormat } from './toggle-format';
export { LINE_SEPARATOR as __UNSTABLE_LINE_SEPARATOR } from './special-characters';
export { unregisterFormatType } from './unregister-format-type';
export { createElement as __unstableCreateElement } from './create-element';
export { createRichTextString as __unstableCreateRichTextString } from './value';

export { useAnchorRef } from './component/use-anchor-ref';
export { useAnchor } from './component/use-anchor';
Expand Down

0 comments on commit bff3746

Please sign in to comment.