Skip to content
Closed
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
16 changes: 10 additions & 6 deletions blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function createBlock( name, blockAttributes = {}, innerBlocks = [] ) {

// Ensure attributes contains only values defined by block type, and merge
// default values for missing attributes.
const attributes = reduce( blockType.attributes, ( result, source, key ) => {
let attributes = reduce( blockType.attributes, ( result, source, key ) => {
const value = blockAttributes[ key ];
if ( undefined !== value ) {
result[ key ] = value;
Expand All @@ -54,6 +54,8 @@ export function createBlock( name, blockAttributes = {}, innerBlocks = [] ) {
return result;
}, {} );

attributes = applyFilters( 'blocks.createBlock.attributes', attributes, name, blockAttributes, innerBlocks );

// Blocks are stored with a unique ID, the assigned type name,
// and the block attributes.
return {
Expand Down Expand Up @@ -207,12 +209,14 @@ export function switchToBlockType( blocks, name ) {
return null;
}

let transformationResults;
if ( transformation.isMultiBlock ) {
transformationResults = transformation.transform( blocksArray.map( ( currentBlock ) => currentBlock.attributes ) );
} else {
transformationResults = transformation.transform( firstBlock.attributes );
const transformationArray = transformation.isMultiBlock ? blocksArray : [ firstBlock ];
let sourceBlocksAttributes = transformationArray.map( ( block ) => {
return applyFilters( 'blocks.switchToBlockType.sourceAttributes', block.attributes, sourceName );
} );
if ( ! transformation.isMultiBlock ) {
sourceBlocksAttributes = first( sourceBlocksAttributes );
}
let transformationResults = transformation.transform( sourceBlocksAttributes );

// Ensure that the transformation function returned an object or an array
// of objects.
Expand Down
38 changes: 0 additions & 38 deletions blocks/api/matchers.js

This file was deleted.

53 changes: 36 additions & 17 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* External dependencies
*/
import { parse as hpqParse } from 'hpq';
import { parse as hpqParse, attr, prop, html, text, query } from 'hpq';
import { mapValues, omit } from 'lodash';

/**
* WordPress dependencies
*/
import { autop } from '@wordpress/autop';
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies
Expand All @@ -17,7 +18,6 @@ import { getBlockType, getUnknownTypeHandlerName } from './registration';
import { createBlock } from './factory';
import { isValidBlock } from './validation';
import { getCommentDelimitedContent } from './serializer';
import { attr, prop, html, text, query, node, children } from './matchers';

/**
* Returns value coerced to the specified JSON schema type string.
Expand Down Expand Up @@ -75,10 +75,6 @@ export function matcherFromSource( sourceConfig ) {
return html( sourceConfig.selector );
case 'text':
return text( sourceConfig.selector );
case 'children':
return children( sourceConfig.selector );
case 'node':
return node( sourceConfig.selector );
case 'query':
const subMatchers = mapValues( sourceConfig.query, matcherFromSource );
return query( sourceConfig.selector, subMatchers );
Expand Down Expand Up @@ -118,7 +114,15 @@ export function getBlockAttribute( attributeKey, attributeSchema, innerHTML, com
break;
}

return value === undefined ? attributeSchema.default : asType( value, attributeSchema.type );
value = applyFilters( 'blocks.getBlockAttribute.source', value, attributeSchema, innerHTML, commentAttributes );

if ( value === undefined ) {
value = attributeSchema.default;
} else {
value = asType( value, attributeSchema.type );
}

return applyFilters( 'blocks.getBlockAttribute', value, attributeSchema, innerHTML, commentAttributes );
}

/**
Expand All @@ -142,14 +146,15 @@ export function getBlockAttributes( blockType, innerHTML, attributes ) {
* Attempt to parse the innerHTML using using a supplied `deprecated`
* definition.
*
* @param {?Object} blockType Block type.
* @param {string} innerHTML Raw block content.
* @param {?Object} attributes Known block attributes (from delimiters).
* @param {string} name Block name.
* @param {Object} attributes Known block attributes (from delimiters).
* @param {string} innerHTML Raw block content.
*
* @return {Object} Block attributes.
* @return {?Object} Migrated block attributes, if possible.
*/
export function getAttributesFromDeprecatedVersion( blockType, innerHTML, attributes ) {
if ( ! blockType.deprecated ) {
export function getAttributesFromDeprecatedVersion( name, attributes, innerHTML ) {
const blockType = getBlockType( name );
if ( ! blockType || ! blockType.deprecated ) {
return;
}

Expand All @@ -164,11 +169,25 @@ export function getAttributesFromDeprecatedVersion( blockType, innerHTML, attrib
// Try to validate the parsed block using this same deprecated version.
// Ignore this version if the the validation fails.
const deprecatedBlockAttributes = getBlockAttributes( deprecatedBlockType, innerHTML, attributes );
const migratedBlockAttributes = deprecatedBlockType.migrate ? deprecatedBlockType.migrate( deprecatedBlockAttributes ) : deprecatedBlockAttributes;
const isValid = isValidBlock( innerHTML, deprecatedBlockType, deprecatedBlockAttributes );
if ( isValid ) {
return migratedBlockAttributes;
if ( ! isValid ) {
continue;
}

let migratedBlockAttributes = deprecatedBlockAttributes;
if ( deprecatedBlockType.migrate ) {
migratedBlockAttributes = deprecatedBlockType.migrate( deprecatedBlockAttributes );
}

migratedBlockAttributes = applyFilters(
'blocks.getAttributesFromDeprecatedVersion.migratedBlockAttributes',
migratedBlockAttributes,
name,
attributes,
innerHTML
);

return migratedBlockAttributes;
} catch ( error ) {
// ignore error, it means this deprecated version is invalid
}
Expand Down Expand Up @@ -257,7 +276,7 @@ export function createBlockWithFallback( blockNode ) {
// invalidating content written in previous formats.
if ( ! block.isValid ) {
const attributesParsedWithDeprecatedVersion = getAttributesFromDeprecatedVersion(
blockType, innerHTML, attributes
name, attributes, innerHTML
);

if ( attributesParsedWithDeprecatedVersion ) {
Expand Down
4 changes: 2 additions & 2 deletions blocks/api/raw-handling/test/integration/evernote-out.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- wp:paragraph -->
<p>This is a <em>paragraph</em>.
<br/>This is a <a href="https://w.org">link</a>.
<br/></p>
<br>This is a <a href="https://w.org">link</a>.
<br></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
Expand Down
6 changes: 3 additions & 3 deletions blocks/api/raw-handling/test/integration/google-docs-out.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!-- wp:paragraph -->
<p>This is a <strong>title</strong><br/></p>
<p>This is a <strong>title</strong><br></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>This is a <em>heading</em></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is a <strong>paragraph</strong> with a <a href="https://w.org">link</a>.<br/></p>
<p>This is a <strong>paragraph</strong> with a <a href="https://w.org">link</a>.<br></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
Expand Down Expand Up @@ -56,7 +56,7 @@ <h2>This is a <em>heading</em></h2>
<!-- /wp:separator -->

<!-- wp:paragraph -->
<p>An image:<br/></p>
<p>An image:<br></p>
<!-- /wp:paragraph -->

<!-- wp:image -->
Expand Down
38 changes: 19 additions & 19 deletions blocks/api/raw-handling/test/integration/ms-word-online-out.html
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
<!-- wp:paragraph -->
<p>This is a <em>heading</em> </p>
<p>This is a <em>heading</em>&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is a <strong>paragraph </strong>with a <a href="https://w.org/">link</a>. </p>
<p>This is a <strong>paragraph </strong>with a <a href="https://w.org/">link</a>.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul>
<li>A </li>
<li>Bulleted </li>
<li>Indented </li>
<li>List </li>
<li>A&nbsp;</li>
<li>Bulleted&nbsp;</li>
<li>Indented&nbsp;</li>
<li>List&nbsp;</li>
</ul>
<!-- /wp:list -->

<!-- wp:list -->
<ol>
<li>One </li>
<li>Two </li>
<li>Three </li>
<li>One&nbsp;</li>
<li>Two&nbsp;</li>
<li>Three&nbsp;</li>
</ol>
<!-- /wp:list -->

<!-- wp:table -->
<table class="wp-block-table">
<tbody>
<tr>
<td>One </td>
<td>Two </td>
<td>Three </td>
<td>One&nbsp;</td>
<td>Two&nbsp;</td>
<td>Three&nbsp;</td>
</tr>
<tr>
<td>1 </td>
<td>2 </td>
<td>3 </td>
<td>1&nbsp;</td>
<td>2&nbsp;</td>
<td>3&nbsp;</td>
</tr>
<tr>
<td>I </td>
<td>II </td>
<td>III </td>
<td>I&nbsp;</td>
<td>II&nbsp;</td>
<td>III&nbsp;</td>
</tr>
</tbody>
</table>
<!-- /wp:table -->

<!-- wp:paragraph -->
<p>An image: </p>
<p>An image:&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
Expand Down
4 changes: 2 additions & 2 deletions blocks/api/raw-handling/test/integration/plain-out.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- wp:paragraph -->
<p>test<br/>test</p>
<p>test<br>test</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>test<br/></p>
<p>test<br></p>
<!-- /wp:paragraph -->
14 changes: 12 additions & 2 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isEmpty, reduce, isObject, castArray, startsWith } from 'lodash';
import { isEmpty, reduce, isPlainObject, castArray, startsWith } from 'lodash';
import { html as beautifyHtml } from 'js-beautify';
import isEqualShallow from 'is-equal-shallow';

Expand Down Expand Up @@ -50,9 +50,19 @@ export function getSaveElement( blockType, attributes, innerBlocks = [] ) {
save = instance.render.bind( instance );
}

/**
* Filters the attributes of a block before generating serialized result.
* Avoid mutating the attribute value, as it should only be effective for
* the duration of the serialization.
*
* @param {Object} attributes Block attributes.
* @param {WPBlockType} blockType Block type definition.
*/
attributes = applyFilters( 'blocks.getSaveElement.attributes', attributes, blockType.name, innerBlocks );

let element = save( { attributes, innerBlocks } );

if ( isObject( element ) && hasFilter( 'blocks.getSaveContent.extraProps' ) ) {
if ( isPlainObject( element ) && hasFilter( 'blocks.getSaveContent.extraProps' ) ) {
/**
* Filters the props applied to the block save result element.
*
Expand Down
50 changes: 0 additions & 50 deletions blocks/api/test/matchers.js

This file was deleted.

Loading