-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Blocks: Fix quote block (parsing, serializing and transforming) #1260
Conversation
const html = '<blockquote><p>A delicious sundae dessert</p></blockquote>'; | ||
const match = parse( html, query.node() ); | ||
|
||
expect( wp.element.renderToString( match ) ).to.equal( `<body>${ html }</body>` ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aduth The extra body
here seemed weird to me. Is this something automatically added by the parse
function or something?
Also in the test above, the children
helper is returning the whole HTML while I expected it to return only the inner p
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The body
comes from hpq assigning innerHTML
of a new document on which to perform the querying:
https://github.com/aduth/hpq/blob/a5bda80/src/index.js#L22
I seem to recall there may be alternatives to consider here to be able to avoid it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, doesn't bother me a lot now, but yeah maybe we should drop it somehow.
66ec2e7
to
75c9f17
Compare
blocks/library/quote/index.js
Outdated
@@ -59,9 +64,23 @@ registerBlockType( 'core/quote', { | |||
{ | |||
type: 'block', | |||
blocks: [ 'core/text' ], | |||
transform: ( { value, citation } ) => { | |||
transform: ( { value, citation, ...attrs } ) => { | |||
const textElement = Array.isArray( value ) ? value[ 0 ] : value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
query
should always return an array. When would the falsey flow occur here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
query
no but IIRC it can occur in the Editable
component if you have a single paragraph. I'll try to validate this tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You were right, it seems like the ternary is not necessary here. I only needed a defaultValue for newly inserted quotes.
|
||
registerBlockType( 'core/quote', { | ||
title: wp.i18n.__( 'Quote' ), | ||
icon: 'format-quote', | ||
category: 'common', | ||
|
||
attributes: { | ||
value: query( 'blockquote > p', children() ), | ||
value: query( 'blockquote > p', node() ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be enough to change this to children( 'p' )
?
Edit: Hmm, no, maybe not.
9212460
to
3a7a696
Compare
@@ -36,11 +36,11 @@ export const html = withKnownMatcherFlag( originalHtml ); | |||
export const text = withKnownMatcherFlag( originalText ); | |||
export const query = withKnownMatcherFlag( originalQuery ); | |||
export const children = withKnownMatcherFlag( ( selector ) => { | |||
return ( node ) => { | |||
let match = node; | |||
return ( domNode ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why rename?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to avoid the lint errors with the newly created node
helper.
blocks/api/query.js
Outdated
match = domNode.querySelector( selector ); | ||
} | ||
|
||
return nodeToReact( match, wp.element.createElement ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we don't want wp.element.createElement
anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch
3a7a696
to
50abb9b
Compare
[ | ||
"The editor will endeavour to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery." | ||
] | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we do a test with multiple children?
@iseulde The transform to text also splits in two if there are multiple |
So this is not a bug? I don't understand why it splits right in the middle of the text. |
@iseulde The first paragraph is transformed to a heading while the others stay in the quote. |
No, it's not the first one. There was only one. |
It's splitting right before the italicised text. |
Oh ok, that's a bug. |
@iseulde MM I can't seem to reproduce the bug here. |
@youknowriad Go to the "The Inserter Tool" heading in the demo, switch to quote, then back to heading. |
50abb9b
to
b05fc9f
Compare
@iseulde Going to merge this if no concerns? |
Hm, now quote ==> heading no longer works. |
Fixed, Let's hope this one is the good one. |
const headingContent = isString( content[ 0 ] ) | ||
? content[ 0 ] | ||
: content[ 0 ].props.children; | ||
const headingContent = isObject( content[ 0 ] ) && content[ 0 ].type === 'p' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it doesn't matter much here, but not sure why we should change it, the string check being faster.
@@ -63,9 +89,13 @@ registerBlockType( 'core/quote', { | |||
blocks: [ 'core/heading' ], | |||
transform: ( { value, citation, ...attrs } ) => { | |||
const isMultiParagraph = Array.isArray( value ) && isObject( value[ 0 ] ) && value[ 0 ].type === 'p'; | |||
const headingElement = isMultiParagraph ? value[ 0 ] : value; | |||
const headingContent = isObject( headingElement ) && value[ 0 ].type === 'p' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly here, sometimes we seem to check for string, other times object. Nitpicking now though. :D
Good to merge once tests pass. |
4d8a843
to
fdd76b2
Compare
closes #1255
I've noticed several issues with the quote block:
p
when parsing the quote contentp
sThis PR adds a new parsing helper
node
which is similar tochildren
but includes the wrapper node.