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

Blocks: Fix quote block (parsing, serializing and transforming) #1260

Merged
merged 5 commits into from
Jun 27, 2017

Conversation

youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Jun 19, 2017

closes #1255

I've noticed several issues with the quote block:

  • We were dropping the p when parsing the quote content
  • We were serializing double ps
  • Transforming to text block was broken when we moved the text block to single p.

This PR adds a new parsing helper node which is similar to children but includes the wrapper node.

@youknowriad youknowriad added [Feature] Blocks Overall functionality of blocks [Type] Bug An existing feature does not function as intended labels Jun 19, 2017
@youknowriad youknowriad self-assigned this Jun 19, 2017
@youknowriad youknowriad requested review from nb and aduth June 19, 2017 12:59
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>` );
Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -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;
Copy link
Member

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?

Copy link
Contributor Author

@youknowriad youknowriad Jun 21, 2017

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.

Copy link
Contributor Author

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() ),
Copy link
Member

@aduth aduth Jun 21, 2017

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.

@youknowriad youknowriad force-pushed the fix/quote-parse-serialize branch from 9212460 to 3a7a696 Compare June 22, 2017 10:49
@@ -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 ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Why rename?

Copy link
Contributor Author

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.

match = domNode.querySelector( selector );
}

return nodeToReact( match, wp.element.createElement );
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch

@youknowriad youknowriad force-pushed the fix/quote-parse-serialize branch from 3a7a696 to 50abb9b Compare June 22, 2017 13:18
[
"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."
]
{
Copy link
Member

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?

@ellatrix
Copy link
Member

ellatrix commented Jun 22, 2017

I'm having some issues going from a quote => heading. Also, isn't it strange to do such a transformation?

screenshot 2017-06-22 15 24 42

Splits in two ^

@youknowriad
Copy link
Contributor Author

@iseulde The transform to text also splits in two if there are multiple p in the quote block. Yes these are strange

@ellatrix
Copy link
Member

So this is not a bug? I don't understand why it splits right in the middle of the text.

@youknowriad
Copy link
Contributor Author

@iseulde The first paragraph is transformed to a heading while the others stay in the quote.

@ellatrix
Copy link
Member

No, it's not the first one. There was only one.

@ellatrix
Copy link
Member

It's splitting right before the italicised text.

@youknowriad
Copy link
Contributor Author

Oh ok, that's a bug.

@youknowriad
Copy link
Contributor Author

@iseulde MM I can't seem to reproduce the bug here.

@ellatrix
Copy link
Member

@youknowriad Go to the "The Inserter Tool" heading in the demo, switch to quote, then back to heading.

@youknowriad youknowriad force-pushed the fix/quote-parse-serialize branch from 50abb9b to b05fc9f Compare June 26, 2017 08:58
@youknowriad
Copy link
Contributor Author

@iseulde I can reproduce now Thanks.

I accidentally pushed the fix for this bug in master. Rebased here so this should be ok 599970e

@youknowriad
Copy link
Contributor Author

@iseulde Going to merge this if no concerns?

@ellatrix
Copy link
Member

Hm, now quote ==> heading no longer works.

@youknowriad
Copy link
Contributor Author

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'
Copy link
Member

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'
Copy link
Member

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

@ellatrix
Copy link
Member

Good to merge once tests pass.

@youknowriad youknowriad force-pushed the fix/quote-parse-serialize branch from 4d8a843 to fdd76b2 Compare June 27, 2017 11:29
@youknowriad youknowriad merged commit f40484e into master Jun 27, 2017
@youknowriad youknowriad deleted the fix/quote-parse-serialize branch June 27, 2017 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quote Block: Moving to text block adds paragraphs inside paragraphs
3 participants