-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 supports: Restore root DOMDocument save #25028
Conversation
Size Change: 0 B Total Size: 1.2 MB ℹ️ View Unchanged
|
This is no longer a simple revert. The regression should be addressed by this PR. The whitespace cleanup should be maintained here, and an additional test to cover the regression is introduced. |
It will be fragile to match exact strings to be removed from a saved DOMDocument. It's already been observed that this introduces extraneous newlines and other document normalization could change the strings we expect to match. Instead of matching some HTML strings to remove, concatenate the children of the document body element in order to produce the expected HTML. Noted in #25028 (comment)
add0f99
to
0a92f0d
Compare
Addressed comments and rebased to fix conflicts. |
I'll investigate the test failures… |
lib/block-supports/index.php
Outdated
@@ -71,8 +77,8 @@ function gutenberg_apply_block_supports( $block_content, $block ) { | |||
return $block_content; | |||
} | |||
|
|||
$xpath = new DOMXPath( $dom ); | |||
$block_root = $xpath->query( '/html/body/*' )[0]; | |||
$body_element = $dom->getElementByID( $body_id ); |
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 think it'd be sufficient to
$body_element = $dom->getElementByID( $body_id ); | |
$body_element = $dom->getElementsByTagName( 'body' )[0]; |
(which means requiring that no other <body />
be prepended, which I think is a fair assumption).
Also fine if you wanna play it even safer though 👍
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 don't like going through the NodeList to get the first element, but I don't feel strongly.
I've implemented an alternative in b252259e2c52ecff711f812f48d026db700f85b0 that traverses simple, known path to the body.
0a92f0d
to
1f69bff
Compare
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.
Looking good, thanks. One non-blocking note here.
Feel free to merge once green!
It will be fragile to match exact strings to be removed from a saved DOMDocument. It's already been observed that this introduces extraneous newlines and other document normalization could change the strings we expect to match. Instead of matching some HTML strings to remove, concatenate the children of the document body element in order to produce the expected HTML. Noted in #25028 (comment)
b252259
to
de01660
Compare
This will also fix #25117 |
In some versions of PHP (< 7.3) the `DOMDocument::saveHtml( $node )` method would format HTML introducing whitespace that could result in different rendered results in the browser. Avoid using the `DOMDocument::saveHtml( $node )` to ensure consistent behavior with supported PHP versions. Mentioned here: #25028 (comment)
…25240) Get DOM root and extract substring contents In some versions of PHP (< 7.3) the `DOMDocument::saveHtml( $node )` method would format HTML introducing whitespace that could result in different rendered results in the browser. Avoid using the `DOMDocument::saveHtml( $node )` to ensure consistent behavior with supported PHP versions. Mentioned here: #25028 (comment)
Fix a regression from #25020 which was found to remove anything outside of the block root that may have been appended or prepended. This fix is achieved by concatenating all the childNodes in the DOMDocument body. See #25026 for additional details on the regression.
Add a test to ensure that appended HTML is preserved.
The regression from #25020 exposes an issue I'll raise in another PR with some failing tests. HTML appended to the block content works well. However, if additional output comes before the block content then then
gutenberg_apply_block_supports
will misbehave because it expects the block content to be the first element in the constructed DOM body:gutenberg/lib/block-supports/index.php
Line 75 in 7ee00ad
See #25053 for discussion around the expected behavior of
gutenberg_apply_block_supports
.Fixes #25117