-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Insert block template skip link via HTML API, minify CSS, remove JS. #10676
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
Changes from 1 commit
8e7719a
7e29956
908f6a9
d6278d3
9a73e67
a62dffb
43c4b01
ce8885a
cd9e029
a194ca8
6b7841c
0bb3ec4
c340b2c
eff4489
b00b244
1ae2bb2
d179f23
540b526
746e686
45c26b8
c565ff9
ce5b88e
8b83d4e
d066dc4
64ff9d1
64a8b4a
350b57c
b1afb70
3ed9039
61711e2
999acca
0db64ac
b219ac5
9f62b62
357ea8a
65fedcb
3fceee0
4d539a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -301,7 +301,121 @@ function get_the_block_template_html() { | |
|
|
||
| // Wrap block template in .wp-site-blocks to allow for specific descendant styles | ||
| // (e.g. `.wp-site-blocks > *`). | ||
| return '<div class="wp-site-blocks">' . $content . '</div>'; | ||
| $template_html = '<div class="wp-site-blocks">' . $content . '</div>'; | ||
|
|
||
| return _block_template_skip_link_markup( $template_html ); | ||
| } | ||
|
|
||
| /** | ||
| * Inserts the block template skip link into the template HTML. | ||
|
rutviksavsani marked this conversation as resolved.
Outdated
|
||
| * | ||
| * Uses the HTML API to ensure that the main content element has an ID and to | ||
| * inject the skip-link anchor before the block template wrapper. | ||
|
Member
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. as much as I love this, I’m not sure we need to describe in the docblock how the function operates. we can probably clarify the goal, which is well-stated here, and give an illustrative example. /**
* ...
*
* When a `MAIN` element exists in the template, this function will ensure
* that the element contains a `id` attribute and will insert a link to
* that main element at the top of the first `DIV.wp-site-blocks` match.
*
* Example:
*
* // Input.
* <main>
* <nav>...</nav>
* <div class="wp-site-blocks">
* <h2>...
*
* // Output.
* <main id="wp--skip-link--target">
* <nav>...</nav>
* <div class="wp-site-blocks">
* <a href="#wp--skip-link--target" id="wp-skip-link" class="...">
* <h2>...
*
* When the `MAIN` element already contains a non-empty `id` value it will be
* used instead of the default skip-link id. |
||
| * | ||
| * @access private | ||
| * @since 7.0.0 | ||
| * | ||
| * @global string $_wp_current_template_content | ||
| * | ||
| * @param string $template_html Block template markup. | ||
| * @return string Modified markup with skip link when applicable. | ||
| */ | ||
| function _block_template_skip_link_markup( $template_html ) { | ||
|
rutviksavsani marked this conversation as resolved.
Outdated
|
||
| global $_wp_current_template_content; | ||
|
rutviksavsani marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Back-compat for plugins that disable functionality by unhooking this action. | ||
| if ( ! has_action( 'wp_footer', 'the_block_template_skip_link' ) ) { | ||
| return $template_html; | ||
| } | ||
|
Member
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. It seems like we could move this check up into |
||
|
|
||
| // Early exit if not a block theme. | ||
| if ( ! current_theme_supports( 'block-templates' ) ) { | ||
| return $template_html; | ||
| } | ||
|
|
||
| // Early exit if not a block template. | ||
| if ( ! $_wp_current_template_content ) { | ||
| return $template_html; | ||
| } | ||
|
|
||
|
rutviksavsani marked this conversation as resolved.
Outdated
|
||
| // Ensure a skip-link target exists and has an ID. | ||
| $processor = new WP_HTML_Tag_Processor( $template_html ); | ||
| $skip_link_target_id = null; | ||
|
|
||
| // Get the first <main> element. | ||
| while ( $processor->next_tag() ) { | ||
| if ( 'MAIN' !== $processor->get_tag() || $processor->is_tag_closer() ) { | ||
| continue; | ||
| } | ||
|
|
||
| $skip_link_target_id = $processor->get_attribute( 'id' ); | ||
| if ( ! $skip_link_target_id ) { | ||
|
rutviksavsani marked this conversation as resolved.
Outdated
|
||
| $skip_link_target_id = 'wp--skip-link--target'; | ||
| $processor->set_attribute( 'id', $skip_link_target_id ); | ||
| } | ||
|
|
||
| // Only consider the first <main> element. | ||
| break; | ||
| } | ||
|
Member
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. if we’re going to default to a given value, we could set it first and only change if we find one already exists. // Only add skip-links to templates with a MAIN element.
if ( ! $processor->next_tag( 'MAIN' ) ) {
return $template_html;
}
$target_id = $processor->get_attribute( 'id' );
if ( ! is_string( $target_id ) && '' === $target_id ) {
$target_id = 'wp--skip-link--target'
$processor->set_attribute( 'id', $target_id );
}
...Note too that I did not use we can see differentiation between <p id="me">one</p>
<p id=" me">two</p>
<p id="me ">three</p>
<p id=" me ">four</p>
<style>
#me {
border: 1px solid blue;
}
#\20me {
border: 1px solid green;
}
#me\20 {
border: 1px solid red;
}
#\20me\20 {
border: 1px solid orange;
}
</style>In other words, the |
||
|
|
||
| // Early exit if a skip-link target can't be located. | ||
| if ( ! $skip_link_target_id ) { | ||
| return $template_html; | ||
| } | ||
|
|
||
| // Apply any updates from setting the main ID. | ||
| $template_html = $processor->get_updated_html(); | ||
|
|
||
| // If a skip link already exists, don't insert another one. | ||
| $existing = new WP_HTML_Tag_Processor( $template_html ); | ||
| while ( $existing->next_tag() ) { | ||
| if ( 'A' === $existing->get_tag() && 'wp-skip-link' === $existing->get_attribute( 'id' ) ) { | ||
| return $template_html; | ||
| } | ||
| } | ||
|
|
||
|
rutviksavsani marked this conversation as resolved.
Outdated
|
||
| // Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans | ||
| // so that text can be inserted before the current token. | ||
| $inserter = new class( $template_html ) extends WP_HTML_Tag_Processor { | ||
| /** | ||
| * Gets the span for the current token. | ||
| * | ||
| * @return WP_HTML_Span Current token span. | ||
| */ | ||
| private function get_span(): WP_HTML_Span { | ||
| // Note: This call will never fail according to the usage of this class, given it is always called after ::next_tag() is true. | ||
| $this->set_bookmark( 'here' ); | ||
| return $this->bookmarks['here']; | ||
| } | ||
|
|
||
| /** | ||
| * Inserts text before the current token. | ||
| * | ||
| * @param string $text Text to insert. | ||
| */ | ||
| public function insert_before( string $text ) { | ||
| $this->lexical_updates[] = new WP_HTML_Text_Replacement( $this->get_span()->start, 0, $text ); | ||
| } | ||
| }; | ||
|
dmsnell marked this conversation as resolved.
|
||
|
|
||
| while ( $inserter->next_tag() ) { | ||
| if ( $inserter->is_tag_closer() ) { | ||
| continue; | ||
| } | ||
|
|
||
|
rutviksavsani marked this conversation as resolved.
Outdated
|
||
| if ( 'DIV' === $inserter->get_tag() && $inserter->has_class( 'wp-site-blocks' ) ) { | ||
| $skip_link = sprintf( | ||
| '<a class="skip-link screen-reader-text" id="wp-skip-link" href="#%s">%s</a>', | ||
| esc_attr( $skip_link_target_id ), | ||
|
rutviksavsani marked this conversation as resolved.
Outdated
westonruter marked this conversation as resolved.
Outdated
|
||
| /* translators: Hidden accessibility text. Do not use HTML entities ( , etc.). */ | ||
|
rutviksavsani marked this conversation as resolved.
Outdated
|
||
| esc_html__( 'Skip to content' ) | ||
| ); | ||
| $inserter->insert_before( $skip_link ); | ||
| break; | ||
| } | ||
| } | ||
|
dmsnell marked this conversation as resolved.
|
||
|
|
||
| return $inserter->get_updated_html(); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.