-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Documentation: Initial matchers documentation #1499
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,7 +127,7 @@ Registers a new block provided a unique name and an object defining its behavior | |
|
||
- `title: string` - A human-readable [localized](https://codex.wordpress.org/I18n_for_WordPress_Developers#Handling_JavaScript_files) label for the block. Shown in the block picker. | ||
- `icon: string | WPElement | Function` - Slug of the [Dashicon](https://developer.wordpress.org/resource/dashicons/#awards) to be shown in the control's button, or an element (or function returning an element) if you choose to render your own SVG. | ||
- `attributes: Object | Function` - An object of [matchers](http://github.com/aduth/hpq) or a function which, when passed the raw content of the block, returns block attributes as an object. When defined as an object of matchers, the attributes object is generated with values corresponding to the shape of the matcher object keys. | ||
- `attributes: Object | Function` - An object of [matchers](#matchers) or a function which, when passed the raw content of the block, returns block attributes as an object. When defined as an object of matchers, the attributes object is generated with values corresponding to the shape of the matcher object keys. | ||
- `category: string` - Slug of the block's category. The category is used to organize the blocks in the block inserter. | ||
- `edit( { attributes: Object, setAttributes: Function } ): WPElement` - Returns an element describing the markup of a block to be shown in the editor. A block can update its own state in response to events using the `setAttributes` function, passing an object of properties to be applied as a partial update. | ||
- `save( { attributes: Object } ): WPElement | String` - Returns an element describing the markup of a block to be saved in the published content. This function is called before save and when switching to an editor's HTML view. | ||
|
@@ -219,3 +219,35 @@ function edit( props ) { | |
} ); | ||
} | ||
``` | ||
|
||
## Matchers | ||
|
||
Given the block's HTML content as a string, attribute matchers extract the block attributes values into an Object. The matchers are made available throught the `wp.blocks.query` variable as superset of [HPQ](http://github.com/aduth/hpq)'s API. | ||
|
||
`attr( selector: ?string, name: string ): Function` | ||
|
||
Generates a matcher function, returning an attribute by name if the attribute exists. If no selector is passed, returns attribute of the root element. | ||
|
||
`prop( selector: ?string, name: string ): Function` | ||
|
||
Generates a matcher function, returning an attribute by property if the attribute exists. If no selector is passed, returns property of the root element. | ||
|
||
`html( selector: ?string ): Function` | ||
|
||
Convenience for `prop( selector, 'innerHTML' )` . | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe: Use when you want to get the raw html within the block as a single attribute. Convenience for |
||
|
||
`text( selector: ?string ): Function` | ||
|
||
Convenience for `prop( selector, 'textContent' )` . | ||
|
||
`query( selector: string, matchers: Object | Function )` | ||
|
||
Creates a new matching context by first finding elements matching selector using [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) before then running another `parse` on `matchers` scoped to the matched elements. | ||
|
||
`node( selector: ?string ): Function` | ||
|
||
Generates a matcher function, converting the HTML markup to a WPElement (The [Editable](#Editable) component expects a WPElement for its `value` prop). If no selector is passed, returns WPElement of the root element. | ||
|
||
`children( selector: ?string ): Function` | ||
|
||
Similar to the `node` matcher but converts only the children of the matched node into a WPElement. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit confusing. Can we explain it simpler? If an attributes exists where? Maybe we can change the phrasing to be more about: "Use
attr
when you want to extract y from x`.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd appreciate some help phrasing here. I'm not very good at that 😅