Skip to content

Commit

Permalink
Block API: Ensure backwards compatibility for block matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 20, 2017
1 parent 7d79f94 commit 8252a6b
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 31 deletions.
4 changes: 4 additions & 0 deletions blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ export {
getBlockTypes,
hasBlockSupport,
} from './registration';

// Deprecated matchers
import * as source from './source';
export { source };
38 changes: 38 additions & 0 deletions blocks/api/matchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { createElement } from '@wordpress/element';

/**
* External dependencies
*/
import { nodeListToReact, nodeToReact } from 'dom-react';
export { attr, prop, html, text, query } from 'hpq';

export const children = ( selector ) => {
return ( domNode ) => {
let match = domNode;

if ( selector ) {
match = domNode.querySelector( selector );
}

if ( match ) {
return nodeListToReact( match.childNodes || [], createElement );
}

return [];
};
};

export const node = ( selector ) => {
return ( domNode ) => {
let match = domNode;

if ( selector ) {
match = domNode.querySelector( selector );
}

return nodeToReact( match, createElement );
};
};
2 changes: 1 addition & 1 deletion blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 './source';
import { attr, prop, html, text, query, node, children } from './matchers';

/**
* Returns value coerced to the specified JSON schema type string
Expand Down
13 changes: 12 additions & 1 deletion blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* External dependencies
*/
import { get, isFunction, some } from 'lodash';
import { get, isFunction, some, mapValues } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -119,6 +119,17 @@ export function registerBlockType( name, settings ) {
...settings,
};

// Resolve deprecated attributes
settings.attributes = mapValues( settings.attributes, ( attribute ) => {
if ( isFunction( attribute.source ) ) {
return {
...attribute,
...attribute.source(),
};
}
return attribute;
} );

settings = applyFilters( 'registerBlockType', settings, name );

return blocks[ name ] = settings;
Expand Down
82 changes: 55 additions & 27 deletions blocks/api/source.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,66 @@
/**
* WordPress dependencies
*/
import { createElement } from '@wordpress/element';
function warnAboutDeprecatedMatcher() {
// eslint-disable-next-line no-console
console.warn(
'Attributes matchers are deprecated and they will be removed in a future version of Gutenberg. ' +
'Please update your attributes definition https://wordpress.org/gutenberg/handbook/reference/attributes/'
);
}

/**
* External dependencies
*/
import { nodeListToReact, nodeToReact } from 'dom-react';
export { attr, prop, html, text, query } from 'hpq';

export const children = ( selector ) => {
return ( domNode ) => {
let match = domNode;
export const attr = ( selector, attribute ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'attribute',
attribute: attribute === undefined ? selector : attribute,
selector: attribute === undefined ? undefined : selector,
};
};

if ( selector ) {
match = domNode.querySelector( selector );
}
export const prop = ( selector, property ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'property',
property: property === undefined ? selector : property,
selector: property === undefined ? undefined : selector,
};
};

if ( match ) {
return nodeListToReact( match.childNodes || [], createElement );
}
export const html = ( selector ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'html',
selector,
};
};

return [];
export const text = ( selector ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'text',
selector,
};
};

export const node = ( selector ) => {
return ( domNode ) => {
let match = domNode;
export const query = ( selector, subMatchers ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'query',
selector,
query: subMatchers,
};
};

if ( selector ) {
match = domNode.querySelector( selector );
}
export const children = ( selector ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'children',
selector,
};
};

return nodeToReact( match, createElement );
export const node = ( selector ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'node',
selector,
};
};
4 changes: 2 additions & 2 deletions blocks/api/test/source.js → blocks/api/test/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { renderToString } from '@wordpress/element';
/**
* Internal dependencies
*/
import * as sources from '../source';
import * as sources from '../matchers';

describe( 'sources', () => {
describe( 'matchers', () => {
describe( 'children()', () => {
it( 'should return a source function', () => {
const source = sources.children();
Expand Down
1 change: 1 addition & 0 deletions blocks/library/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class ButtonBlock extends Component {
</span>,
focus && (
<form
key="form-link"
className="blocks-button__inline-link"
onSubmit={ ( event ) => event.preventDefault() }>
<Dashicon icon="admin-links" />
Expand Down

0 comments on commit 8252a6b

Please sign in to comment.