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

Code block: preserve newlines #59627

Merged
merged 4 commits into from
Mar 11, 2024
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
8 changes: 7 additions & 1 deletion packages/block-library/src/code/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ export default function save( { attributes } ) {
// To do: `escape` encodes characters in shortcodes and URLs to
// prevent embedding in PHP. Ideally checks for the code block,
// or pre/code tags, should be made on the PHP side?
value={ escape( attributes.content.toString() ) }
value={ escape(
typeof attributes.content === 'string'
? attributes.content
: attributes.content.toHTMLString( {
preserveWhiteSpace: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't preserveNewLine make more sense?

} )
) }
/>
</pre>
);
Expand Down
1 change: 1 addition & 0 deletions packages/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ _Parameters_

- _$1_ `Object`: Named argements.
- _$1.value_ `RichTextValue`: Rich text value.
- _$1.preserveWhiteSpace_ `[boolean]`: Preserves newlines if true.

_Returns_

Expand Down
5 changes: 4 additions & 1 deletion packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ export function useRichText( {
: newRecord.formats;
newRecord = { ...newRecord, formats: newFormats };
if ( typeof value === 'string' ) {
_value.current = toHTMLString( { value: newRecord } );
_value.current = toHTMLString( {
value: newRecord,
preserveWhiteSpace,
} );
} else {
_value.current = new RichTextData( newRecord );
}
Expand Down
7 changes: 5 additions & 2 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@ export class RichTextData {
}
// We could expose `toHTMLElement` at some point as well, but we'd only use
// it internally.
toHTMLString() {
return this.originalHTML || toHTMLString( { value: this.#value } );
toHTMLString( { preserveWhiteSpace } = {} ) {
return (
this.originalHTML ||
toHTMLString( { value: this.#value, preserveWhiteSpace } )
);
}
valueOf() {
return this.toHTMLString();
Expand Down
8 changes: 5 additions & 3 deletions packages/rich-text/src/to-html-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import { toTree } from './to-tree';
/**
* Create an HTML string from a Rich Text value.
*
* @param {Object} $1 Named argements.
* @param {RichTextValue} $1.value Rich text value.
* @param {Object} $1 Named argements.
* @param {RichTextValue} $1.value Rich text value.
* @param {boolean} [$1.preserveWhiteSpace] Preserves newlines if true.
*
* @return {string} HTML string.
*/
export function toHTMLString( { value } ) {
export function toHTMLString( { value, preserveWhiteSpace } ) {
const tree = toTree( {
value,
preserveWhiteSpace,
createEmpty,
append,
getLastChild,
Expand Down
3 changes: 2 additions & 1 deletion packages/rich-text/src/to-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function isEqualUntil( a, b, index ) {

export function toTree( {
value,
preserveWhiteSpace,
createEmpty,
append,
getLastChild,
Expand Down Expand Up @@ -267,7 +268,7 @@ export function toTree( {
}
// Ensure pointer is text node.
pointer = append( getParent( pointer ), '' );
} else if ( character === '\n' ) {
} else if ( ! preserveWhiteSpace && character === '\n' ) {
pointer = append( getParent( pointer ), {
type: 'br',
attributes: isEditableTree
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;img /><br> &lt;br></code></pre>
<pre class="wp-block-code"><code>&lt;img />
&lt;br></code></pre>
<!-- /wp:code -->
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>ading<br><br>Paragra</code></pre>
<pre class="wp-block-code"><code>ading

Paragra</code></pre>
<!-- /wp:code -->
Loading