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

Fix paste attributes after #1902 #2408

Merged
merged 5 commits into from Aug 15, 2017
Merged

Fix paste attributes after #1902 #2408

merged 5 commits into from Aug 15, 2017

Conversation

ellatrix
Copy link
Member

No description provided.

@ellatrix
Copy link
Member Author

@aduth Should all of these attributes have defaults set?

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Alternatively, I'd wondered if we just need to feed in the attributes as source instead of defining their full type, since the full type is essentially a duplicate of what should already exist in the destination block type.

diff --git a/blocks/api/paste.js b/blocks/api/paste.js
index 84285728..76e53d3c 100644
--- a/blocks/api/paste.js
+++ b/blocks/api/paste.js
@@ -1,7 +1,7 @@
 /**
  * External dependencies
  */
-import { find, get, flowRight as compose } from 'lodash';
+import { find, get, flowRight as compose, mapValues } from 'lodash';
 
 /**
  * WordPress dependencies
@@ -96,7 +96,7 @@ export default function( nodes ) {
 
                        const attributes = getSourcedAttributes(
                                node.outerHTML,
-                               transform.attributes,
+                               mapValues( transform.attributes, ( source ) => ( { source } ) ),
                        );
 
                        return createBlock( blockType.name, attributes );

Can we add some unit tests for paste block transforms to capture these issues?

@codecov
Copy link

codecov bot commented Aug 14, 2017

Codecov Report

Merging #2408 into master will increase coverage by 0.54%.
The diff coverage is 50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2408      +/-   ##
==========================================
+ Coverage    25.9%   26.45%   +0.54%     
==========================================
  Files         157      157              
  Lines        4853     4858       +5     
  Branches      822      824       +2     
==========================================
+ Hits         1257     1285      +28     
+ Misses       3035     3017      -18     
+ Partials      561      556       -5
Impacted Files Coverage Δ
blocks/library/image/index.js 15% <ø> (ø) ⬆️
blocks/library/paragraph/index.js 47.05% <ø> (ø) ⬆️
blocks/library/list/index.js 6.97% <0%> (ø) ⬆️
blocks/library/heading/index.js 23.8% <0%> (ø) ⬆️
blocks/api/paste.js 97.36% <100%> (+35.82%) ⬆️
blocks/color-palette/index.js 0% <0%> (ø) ⬆️
blocks/library/cover-text/index.js 36% <0%> (+1%) ⬆️
blocks/api/paste/remove-spans.js 77.77% <0%> (+66.66%) ⬆️
blocks/api/paste/strip-attributes.js 100% <0%> (+100%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 77620b2...c04c554. Read the comment docs.

@ellatrix
Copy link
Member Author

What about cases where they don't map? I guess we should think of it when it pops up then.

@aduth
Copy link
Member

aduth commented Aug 14, 2017

Also, it should be responsibility of createBlock to fill in attribute defaults.

@ellatrix
Copy link
Member Author

Why shouldn't we then just loop through getSourcedAttributes instead of source and see if it returns any attributes? That removes the need entirely for these raw transforms.

@ellatrix
Copy link
Member Author

Maybe this could work if we add required, and then look if that's satisfied.

@ellatrix
Copy link
Member Author

Okay, maybe not, there will always be strange cases we'll need to handle. Let's just add what you suggested.

@@ -45,3 +48,44 @@ describe( 'normaliseToBlockLevelNodes', () => {
} );
} );

describe( 'paste', () => {
registerBlockType( 'test/small', {
Copy link
Member

Choose a reason for hiding this comment

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

Since the block registry is global, we should make sure to reset to its original state after our tests are done.

Example from serializer tests:

afterEach( () => {
getBlockTypes().forEach( block => {
unregisterBlockType( block.name );
} );
} );

@aduth
Copy link
Member

aduth commented Aug 14, 2017

Why shouldn't we then just loop through getSourcedAttributes instead of source and see if it returns any attributes? That removes the need entirely for these raw transforms.

Yeah, in the case of the raw transforms, if we know that the original content is a match, we shouldn't need to redefine attributes and instead just trigger the default parsing of getBlockAttributes.

I think I might have been a little overreaching with the renaming of matcher: to source:. In the case of raw transforms, I think matcher: makes a little more sense for what it's doing (returning boolean, maybe also match / isMatch). What do you think?

@ellatrix
Copy link
Member Author

Sounds good. Changed it.

equal( pastedBlock.attributes.content, '<big>test</big>' );
} );

afterAll( () => {
Copy link
Member

Choose a reason for hiding this comment

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

Minor: Conventionally setup and teardown occurs together before any test cases.

https://facebook.github.io/jest/docs/setup-teardown.html

@ellatrix ellatrix merged commit 39c6bfc into master Aug 15, 2017
@ellatrix ellatrix deleted the fix/paste-matchers branch August 15, 2017 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants