Skip to content

Commit f76a7f6

Browse files
committed
Pattern: Experiment with using template slots for content replacement
1 parent c1238e4 commit f76a7f6

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

lib/experimental/pattern.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Temporary compatibility shims for pattern APIs present in Gutenberg.
4+
*
5+
* @package gutenberg
6+
*/
7+
8+
register_block_pattern(
9+
'gutenberg/get-in-touch',
10+
array(
11+
'title' => esc_html__( 'Get In Touch', 'default' ),
12+
'categories' => array( 'call-to-action' ),
13+
'content' => implode(
14+
'',
15+
array(
16+
'<!-- wp:paragraph {"fontSize":"huge"} -->',
17+
'<p class="has-huge-font-size"></$wp:template-slot name="getInTouch"></p>',
18+
'<!-- /wp:paragraph -->',
19+
'<!-- wp:columns -->',
20+
'<div class="wp-block-columns"><!-- wp:column -->',
21+
'<div class="wp-block-column"><!-- wp:paragraph -->',
22+
'<p>' . esc_html__( '20 Cooper Avenue', 'default' ) . '<br>' . esc_html__( 'New York, New York 10023', 'default' ) . '</p>',
23+
'<!-- /wp:paragraph --></div>',
24+
'<!-- /wp:column -->',
25+
'<!-- wp:column -->',
26+
'<div class="wp-block-column"><!-- wp:paragraph -->',
27+
'<p>' . esc_html__( '(555) 555-5555', 'default' ) . '<br><a href="mailto:[email protected]">' . esc_html__( '[email protected]', 'default' ) . '</a></p>',
28+
'<!-- /wp:paragraph --></div>',
29+
'<!-- /wp:column --></div>',
30+
'<!-- /wp:columns -->',
31+
'<!-- wp:buttons -->',
32+
'<div class="wp-block-buttons"><!-- wp:button {"backgroundColor":"dark-gray"} -->',
33+
'<div class="wp-block-button"><a class="wp-block-button__link has-dark-gray-background-color has-background">' . esc_html__( 'Contact Us', 'default' ) . '</a></div>',
34+
'<!-- /wp:button --></div>',
35+
'<!-- /wp:buttons -->',
36+
'<!-- wp:template-fill {"name":"getInTouch"} -->',
37+
esc_html__( 'Get In Touch', 'default' ),
38+
'<!-- /wp:template-fill -->',
39+
)
40+
),
41+
)
42+
);

lib/load.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function gutenberg_is_experiment_enabled( $name ) {
103103
require __DIR__ . '/experimental/kses.php';
104104
require __DIR__ . '/experimental/l10n.php';
105105
require __DIR__ . '/experimental/navigation-fallback.php';
106+
require __DIR__ . '/experimental/pattern.php';
106107
if ( gutenberg_is_experiment_enabled( 'gutenberg-interactivity-api-core-blocks' ) ) {
107108
require __DIR__ . '/experimental/interactivity-api/script-loader.php';
108109
require __DIR__ . '/experimental/interactivity-api/blocks.php';

packages/blocks/src/api/validation/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,18 @@ export function getNextNonWhitespaceToken( tokens ) {
573573
* @return {Object[]|null} Array of valid tokenized HTML elements, or null on error
574574
*/
575575
function getHTMLTokens( html, logger = createLogger() ) {
576+
let temp = html;
576577
try {
577-
return new Tokenizer( new DecodeEntityParser() ).tokenize( html );
578+
// Quick way to ensure that the template slot is correctly validated as HTML comment.
579+
if ( temp.includes( '</$wp:template-slot' ) ) {
580+
temp = temp.replace(
581+
/<\/(\$wp\:template-slot[^>]*)>/gi,
582+
'<!-- $1 -->'
583+
);
584+
}
585+
return new Tokenizer( new DecodeEntityParser() ).tokenize( temp );
578586
} catch ( e ) {
579-
logger.warning( 'Malformed HTML detected: %s', html );
587+
logger.warning( 'Malformed HTML detected: %s', temp );
580588
}
581589

582590
return null;

0 commit comments

Comments
 (0)