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

Add inlineStyle matcher for sourcing block attributes #10074

Closed
wants to merge 1 commit into from
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
20 changes: 20 additions & 0 deletions docs/block-api/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ _Example_: Extract child nodes from a paragraph of rich text.
// }
```

### `styleProperty`

Use `inlineStyle` to extract a value from the inline style declaration of the matched element.

_Example_: Extract the width value from the an inline style

```js
{
width: {
type: 'string',
source: 'inlineStyle',
selector: 'div',
styleProperty: 'width'
}
}
// {
// "width": "20px"
// }
```

### `query`

Use `query` to extract an array of values from markup. Entries of the array are determined by the selector argument, where each matched element within the block will have an entry structured corresponding to the second argument, an object of attribute sources.
Expand Down
6 changes: 6 additions & 0 deletions packages/blocks/src/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ export function matcherFromSource( sourceConfig ) {
return children( sourceConfig.selector );
case 'node':
return node( sourceConfig.selector );
case 'inlineStyle':
return flow( [
prop( sourceConfig.selector, `style.${ sourceConfig.styleProperty }` ),
( value ) => value !== '' ? value : undefined,
] );
case 'query':
const subMatchers = mapValues( sourceConfig.query, matcherFromSource );
return query( sourceConfig.selector, subMatchers );
Expand Down Expand Up @@ -164,6 +169,7 @@ export function getBlockAttribute( attributeKey, attributeSchema, innerHTML, com
case 'text':
case 'children':
case 'node':
case 'inlineStyle':
case 'query':
case 'tag':
value = parseWithAttributeSchema( innerHTML, attributeSchema );
Expand Down
32 changes: 30 additions & 2 deletions packages/blocks/src/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe( 'block parser', () => {
} );

describe( 'parseWithAttributeSchema', () => {
it( 'should return the matcher’s attribute value', () => {
it( 'should return the matcher’s text content value', () => {
const value = parseWithAttributeSchema(
'<div>chicken</div>',
{
Expand All @@ -149,6 +149,19 @@ describe( 'block parser', () => {
expect( value ).toBe( 'chicken' );
} );

it( 'should return the matcher’s style property value', () => {
const value = parseWithAttributeSchema(
'<div style="width: 10px">chicken</div>',
{
type: 'string',
source: 'inlineStyle',
selector: 'div',
styleProperty: 'width',
},
);
expect( value ).toBe( '10px' );
} );

it( 'should return the matcher’s string attribute value', () => {
const value = parseWithAttributeSchema(
'<audio src="#" loop>',
Expand Down Expand Up @@ -216,7 +229,7 @@ describe( 'block parser', () => {
expect( value ).toBe( 10 );
} );

it( "should return the matcher's attribute value", () => {
it( "should return the matcher's text content value", () => {
const value = getBlockAttribute(
'content',
{
Expand All @@ -230,6 +243,21 @@ describe( 'block parser', () => {
expect( value ).toBe( 'chicken' );
} );

it( "returns the matcher's style property value", () => {
const value = getBlockAttribute(
'content',
{
type: 'string',
source: 'inlineStyle',
selector: 'div',
styleProperty: 'width',
},
'<div style="width:10px">chicken</div>',
{}
);
expect( value ).toBe( '10px' );
} );

it( 'should return undefined for meta attributes', () => {
const value = getBlockAttribute(
'content',
Expand Down