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

Block API: Add attribute-sourced block attributes on server #8000

Draft
wants to merge 141 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
141 commits
Select commit Hold shift + click to select a range
0e8c4fb
WIP class skeleton
sirreal Nov 19, 2024
2d3d283
Document class
sirreal Nov 20, 2024
40222d3
Do not support namespaced selectors
sirreal Nov 21, 2024
6092642
Flesh out stuff
sirreal Nov 22, 2024
3e3b2b2
Starting to actually parse
sirreal Nov 22, 2024
967557f
Add ident tests
sirreal Nov 22, 2024
2ec1db3
Fix ident non-ascii bug
sirreal Nov 22, 2024
ee2c7ce
Use class after defined
sirreal Nov 22, 2024
0f708ba
Fix some char stuff
sirreal Nov 22, 2024
3cb455d
Improve tests
sirreal Nov 22, 2024
5609e50
Housekeeping
sirreal Nov 22, 2024
4f25bc2
Require new file in WP
sirreal Nov 22, 2024
943293f
Fix offset type
sirreal Nov 22, 2024
24c9744
Add more tests and invalid tests
sirreal Nov 22, 2024
a7c10b9
Fix wrong offset var usage
sirreal Nov 22, 2024
dd718b7
comment tweak
sirreal Nov 22, 2024
5884aca
Implement codepoint escape with strspn
sirreal Nov 22, 2024
a9a077f
Test with UPPER HEX
sirreal Nov 22, 2024
5f53e0a
Add ID tests
sirreal Nov 25, 2024
effbbbe
Improve tests
sirreal Nov 25, 2024
62ec5bb
Add class selector tests
sirreal Nov 25, 2024
153f009
Add class selector
sirreal Nov 25, 2024
fcc6401
Simplify id selector parse
sirreal Nov 25, 2024
21c67e5
Improve ident tests
sirreal Nov 25, 2024
728d798
Add type selector tests
sirreal Nov 25, 2024
e1e8e09
Add docs and remove unreachable line
sirreal Nov 25, 2024
13ac3c1
Add type selector class
sirreal Nov 25, 2024
a3c25e8
Add attribute selector tests
sirreal Nov 25, 2024
ad5c600
improve attr tests
sirreal Nov 25, 2024
6758704
Fix expectation argument order
sirreal Nov 25, 2024
e97842c
Add test and fix is_ident
sirreal Nov 25, 2024
ef00856
Add parse_string stub
sirreal Nov 26, 2024
463e799
Add attribute selector parsing
sirreal Nov 26, 2024
0f5b28c
Fix test expectations
sirreal Nov 26, 2024
f4a491a
More and improved attribute tests
sirreal Nov 26, 2024
b680b1b
Implement parse_string
sirreal Nov 26, 2024
e7da05f
Add string parse tests
sirreal Nov 26, 2024
d5e7e60
Remove covers annotations
sirreal Nov 26, 2024
08187c6
Remove unused line
sirreal Nov 26, 2024
5a5066c
Improve tests for 100% coverage on parse methods
sirreal Nov 26, 2024
2f8bd19
Improve documentation
sirreal Nov 26, 2024
8b0ac55
Fix parse return type and return annotations
sirreal Nov 26, 2024
dffcac6
Update documentation links and grammar
sirreal Nov 27, 2024
9f81744
Update documentation and class name
sirreal Nov 27, 2024
d4c6f38
Add selector class
sirreal Nov 27, 2024
6432056
Implement complex selector
sirreal Nov 27, 2024
5c746cd
Working and tested
sirreal Nov 27, 2024
501102a
Selector parsing should allow cap I,S modifier
sirreal Nov 28, 2024
f98fbb3
CSS Add matches to selector classes
sirreal Nov 28, 2024
c8f16e1
Match is successful on _any_ match in selector list
sirreal Nov 28, 2024
c689c9c
PICKME: Add is_quirks_mode method to processor
sirreal Nov 28, 2024
1221efa
ID matches depend on quirks mode
sirreal Nov 28, 2024
e5e94b1
has_class may return null, coerce to bool
sirreal Nov 28, 2024
1e888ba
Update docs to only allow subclass selectors in final complex selecto…
sirreal Nov 28, 2024
dd4fcb0
Restrict complex selectors to only allow subclass selectors in final …
sirreal Nov 28, 2024
256c55a
Work on complex selector handling
sirreal Nov 28, 2024
465cc36
Implement descendent selector matching
sirreal Nov 28, 2024
467d45d
Add null check for subclass selectors
sirreal Nov 29, 2024
44bfc64
CSS selector reformat ternaries
sirreal Nov 29, 2024
ca4531c
Implement ~= attribute matching
sirreal Nov 29, 2024
489db93
CSS fix return type
sirreal Nov 29, 2024
e57a211
Fix static analysis problems
sirreal Nov 29, 2024
509e648
Fix and annotate things (static analysis)
sirreal Nov 29, 2024
58c1698
update tests
sirreal Nov 29, 2024
c9b9145
Id attribute must be a string to match id selector
sirreal Nov 29, 2024
e5cac63
Coerce boolean attributes to ""
sirreal Nov 29, 2024
2bafae9
Fix a few more static analysis things
sirreal Nov 29, 2024
8fe57e3
Add select method
sirreal Nov 28, 2024
ab2fe0d
Unify parsing under single class
sirreal Dec 3, 2024
6a6969f
Rename files to align with class name
sirreal Dec 3, 2024
27ca891
Add html processor select test suite
sirreal Dec 3, 2024
9ff2769
Fix select types
sirreal Dec 3, 2024
d1a276b
Update class doc
sirreal Dec 4, 2024
4909b56
Improve select_ method arguments, docs, implementation
sirreal Dec 4, 2024
1d45225
Split classes into their own files
sirreal Dec 4, 2024
0b277b4
Remove redundant see phpdoc annotations
sirreal Dec 4, 2024
0c53c42
Fix docs and return type on select_all
sirreal Dec 4, 2024
d966e9a
Improve html select test docs
sirreal Dec 4, 2024
5201ba9
Add select support to tag processor
sirreal Dec 4, 2024
2036a83
Simplify whitspace splitting function
sirreal Dec 4, 2024
3421a4e
Remove unreachable code
sirreal Dec 4, 2024
784b2d9
Add a lot of selector integration tests
sirreal Dec 4, 2024
4d4c5fe
Extract normalize input method
sirreal Dec 4, 2024
dbc37fc
tests
sirreal Dec 4, 2024
d241f31
Add nonfinal subclass selector test
sirreal Dec 4, 2024
663070b
Fix logic bug in child selector exploration
sirreal Dec 5, 2024
5478af9
Improve selector integration tests
sirreal Dec 5, 2024
4f6bf94
Try abstract class instead of interface
sirreal Dec 5, 2024
fe07dfd
Revert "Try abstract class instead of interface"
sirreal Dec 5, 2024
143e092
Clean up and document attribute selector
sirreal Dec 5, 2024
32ee2a7
Update ticket number in tests
sirreal Dec 5, 2024
5922494
Improve some types
sirreal Dec 5, 2024
e492aa6
Fix and improve string token parsing
sirreal Dec 5, 2024
81c6758
Update attribute selector tests
sirreal Dec 5, 2024
7bccf3e
Revert "Update attribute selector tests"
sirreal Dec 5, 2024
3949cc5
Improve some complex selector match tests
sirreal Dec 5, 2024
c696889
Add and use matches_tag type selector method
sirreal Dec 9, 2024
c193551
Improve complex selector structure
sirreal Dec 9, 2024
9dd8114
Rework structure of complex_selector class
sirreal Dec 9, 2024
b134308
Improve documentation
sirreal Dec 9, 2024
94c06ef
Document complex selector class
sirreal Dec 9, 2024
f46fced
Document matches functions
sirreal Dec 9, 2024
1bacfd7
Simplify condition in compound::matches
sirreal Dec 9, 2024
a274ea0
Change class require order
sirreal Dec 9, 2024
12a0a99
Annotate matches processor argument type
sirreal Dec 9, 2024
0e2b34a
Document class selector and update class_name property
sirreal Dec 9, 2024
dea1029
Document ID selector class, rename id property
sirreal Dec 9, 2024
d268f4c
Document type selector class and rename type property
sirreal Dec 9, 2024
d89fbd9
Document compound selector
sirreal Dec 9, 2024
8ced3aa
Improve attribute selector docs and types
sirreal Dec 9, 2024
ca1a129
Update matches docs
sirreal Dec 9, 2024
71fd62a
Document complex selector class
sirreal Dec 9, 2024
4a3e084
Merge branch 'trunk' into html-api/add-css-selector-parser
sirreal Dec 9, 2024
25dbb19
PHP < 7.4 does not like this annotation
sirreal Dec 9, 2024
70cf7f7
Update since annotations to 6.8.0
sirreal Dec 9, 2024
355c9a2
Update attr-modifier to match selectors grammar
sirreal Dec 9, 2024
7ef67c1
Merge branch 'trunk' into html-api/add-css-selector-parser
sirreal Dec 10, 2024
abb4d25
Merge branch 'trunk' into html-api/add-css-selector-parser
sirreal Dec 11, 2024
9ac05b4
Merge branch 'trunk' into html-api/add-css-selector-parser
sirreal Dec 11, 2024
3206e0b
Move parsing back to selector classes
sirreal Dec 11, 2024
46646b5
Update tests for class parsing
sirreal Dec 11, 2024
f217eb0
Use whitepsace chars constant
sirreal Dec 11, 2024
6154742
parse_whitespace should be protected
sirreal Dec 11, 2024
577b3a3
Update interface to abstract class require
sirreal Dec 11, 2024
5ea93ab
Document base class
sirreal Dec 11, 2024
adfebdf
Invert and comment confusing compound selector condition
sirreal Dec 11, 2024
db469e6
Use switch in compound selector parsing
sirreal Dec 11, 2024
400263a
Fix up some todo-s
sirreal Dec 11, 2024
483a819
Make most selector constructors private
sirreal Dec 11, 2024
1f64168
Fix test class implementation of abstract class
sirreal Dec 11, 2024
3bfb8a1
Remove php 8+ ?static return types
sirreal Dec 11, 2024
8d2aef2
Fix typo in Exception class name
sirreal Dec 11, 2024
33b8333
Remove ?static return type from test
sirreal Dec 11, 2024
fd4ec39
Merge branch 'html-api/add-css-selector-parser' into html-api/css-sel…
sirreal Dec 12, 2024
0ebfbb7
Proof of concept adding attribute-sourced attrs.
sirreal Dec 12, 2024
e4b1753
Better implementation in parser
sirreal Dec 12, 2024
28b34dd
Data providers do not have ticket numbers
sirreal Dec 12, 2024
e105add
remove "block_thing" function 😅
sirreal Dec 13, 2024
51343ec
Move to attributes calculation
sirreal Dec 13, 2024
f708d7f
Improve method name/signature, add tests
sirreal Dec 16, 2024
5ea6a8e
Include innerHTML in tests parsed block mocks
sirreal Dec 16, 2024
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
81 changes: 81 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,87 @@ public function prepare_attributes_for_render( $attributes ) {
return $attributes;
}

/**
* Gets attributes from a parsed block.
*
* This method will return an array of the block attributes that includes:
* - Block attributes serialized in the comment delimiters from the initial parse.
* - Sourced attributes from the "attribute" source.
*
* This method does not perform and validation of attribute types or default values.
*
* @since TBD
*
* @param array $parsed_block Original parsed array representation of block.
* @return array Block attributes.
*/
public function get_attributes_from_parsed_block( $parsed_block ) {
$attributes = isset( $parsed_block['attrs'] ) ?
$parsed_block['attrs'] :
array();

// If there are no attribute definitions for the block type, skip
// processing and return verbatim.
if ( ! isset( $this->attributes ) ) {
return $attributes;
}

// Sourced attributes are extracted from the block's HTML. If there's no
// HTML, there's nothing to do.
if ( '' === $parsed_block['innerHTML'] ) {
return $attributes;
}

foreach ( $this->attributes as $attribute_name => $attribute_definition ) {
if (
! isset( $attribute_definition['source'] ) ||
'attribute' !== $attribute_definition['source'] ||
! ( isset( $attribute_definition['attribute'] ) && is_string( $attribute_definition['attribute'] ) ) ||
! isset( $attribute_definition['selector'] ) ||

// @todo what to do if it's in serialized attributes already? Skip for now.
isset( $attributes[ $attribute_name ] )
) {
continue;
}

$processor = WP_HTML_Processor::create_fragment( $parsed_block['innerHTML'] );
if ( null === $processor ) {
continue;
}

$selector = $attribute_definition['selector'];

// This is a workaround for a known unsupported selector in a core block.
if ( 'a:not([download])' === $selector ) {
$selector = 'a';
}

foreach ( $processor->select_all( $selector ) as $_ ) {
// This is a workaround for a known unsupported selector in a core block.
if (
'a:not([download])' === $attribute_definition['selector'] &&
null !== $processor->get_attribute( 'download' )
) {
continue;
}

$value = $processor->get_attribute( $attribute_definition['attribute'] );

// @todo Should matched selectors without the attribute include null?
// if ( null === $value ) {
// continue 2;
// }

// @todo another function validates value types, is that sufficient?
$attributes[ $attribute_name ] = $value;

}
}

return $attributes;
}

/**
* Sets block type properties.
*
Expand Down
12 changes: 7 additions & 5 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,14 @@ public function __construct( $block, $available_context = array(), $registry = n
*/
public function __get( $name ) {
if ( 'attributes' === $name ) {
$this->attributes = isset( $this->parsed_block['attrs'] ) ?
$this->parsed_block['attrs'] :
array();

if ( ! is_null( $this->block_type ) ) {
$this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes );
$this->attributes = $this->block_type->prepare_attributes_for_render(
$this->block_type->get_attributes_from_parsed_block( $this->parsed_block )
);
} else {
$this->attributes = isset( $this->parsed_block['attrs'] ) ?
$this->parsed_block['attrs'] :
array();
}

return $this->attributes;
Expand Down
Loading
Loading