@wordpress/e2e-tests 9.22.0 → 10.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/package.json +5 -5
  3. package/plugins/animated-image-subsizes.php +10 -0
  4. package/plugins/autosave-failures.php +144 -0
  5. package/plugins/content-security-policy.php +43 -0
  6. package/plugins/entity-actions-api/close-comments.js +49 -0
  7. package/plugins/entity-actions-api.php +106 -0
  8. package/plugins/entity-fields-api/comment-status.css +24 -0
  9. package/plugins/entity-fields-api/comment-status.js +26 -0
  10. package/plugins/entity-fields-api/reading-time.css +21 -0
  11. package/plugins/entity-fields-api/reading-time.js +82 -0
  12. package/plugins/entity-fields-api.php +291 -0
  13. package/plugins/image-picture-wrapper.php +30 -0
  14. package/plugins/interactive-blocks/directive-context/render.php +2 -2
  15. package/plugins/interactive-blocks/directive-html/render.php +19 -7
  16. package/plugins/interactive-blocks/directive-html/view.js +21 -2
  17. package/plugins/interactive-blocks/hydration-timing-async/view.js +1 -1
  18. package/plugins/interactive-blocks/router-html/block.json +19 -0
  19. package/plugins/interactive-blocks/router-html/render.php +64 -0
  20. package/plugins/interactive-blocks/router-html/view.asset.php +9 -0
  21. package/plugins/interactive-blocks/router-html/view.js +56 -0
  22. package/plugins/interactive-blocks/router-html-regions/block.json +17 -0
  23. package/plugins/interactive-blocks/router-html-regions/render.php +77 -0
  24. package/plugins/interactive-blocks/router-html-regions/view.asset.php +15 -0
  25. package/plugins/interactive-blocks/router-html-regions/view.js +36 -0
  26. package/plugins/interactive-blocks/store-tag/render.php +3 -3
  27. package/plugins/interactive-blocks.php +12 -0
  28. package/plugins/react-18-compat-block/editor.js +38 -0
  29. package/plugins/rtc-websocket-provider/src/index.js +1 -1
@@ -0,0 +1,291 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: Gutenberg Test Entity Fields API
4
+ * Plugin URI: https://github.com/WordPress/gutenberg
5
+ * Author: Gutenberg Team
6
+ *
7
+ * Extends the fields of Pages through the Entity fields API, one
8
+ * self-contained function per case. Every server case registers its fields on
9
+ * `wp_entity_fields_api_init`, the action the registry fires the first time
10
+ * its fields are read, after `init`, once the default fields are registered.
11
+ *
12
+ * 1. `add_field_declarative`: a declarative field (`menu_order`), plain data,
13
+ * no JavaScript.
14
+ * 2. `add_field_with_script_module`: a field backed by a script module and a
15
+ * stylesheet (`reading_time`), its value and render coming from
16
+ * `entity-fields-api/reading-time.js`. The same module exports a complete
17
+ * `word_count` field no registration names: the module augments the fields
18
+ * it was registered with, so the entry is ignored and the field is not
19
+ * registered.
20
+ * 3. `update_field`: an update of a default field (`comment_status`), which
21
+ * becomes sortable and hideable, and gets a render from
22
+ * `entity-fields-api/comment-status.js`, keeping the rest of its
23
+ * definition.
24
+ * 4. `replace_field`: a substitute for a default field (`author`),
25
+ * unregistered and registered again as a plain integer with a new label.
26
+ * 5. `add_field_with_data`: a field whose value is data the plugin adds to
27
+ * the Pages REST endpoint (`subtitle`), a REST field backed by post meta,
28
+ * readable in the list and writable from the Quick Edit form, which the
29
+ * function adds the field to.
30
+ *
31
+ * @package gutenberg-test-entity-fields-api
32
+ */
33
+
34
+ /**
35
+ * Case 1: a declarative field (`menu_order`), whose value is a property of
36
+ * the record. No JavaScript, no styles.
37
+ */
38
+ function gutenberg_test_entity_fields_api_add_field_declarative() {
39
+ wp_register_entity_fields(
40
+ 'gutenberg-test-entity-fields-api',
41
+ 'postType',
42
+ 'page',
43
+ array(
44
+ array(
45
+ 'id' => 'menu_order',
46
+ 'type' => 'integer',
47
+ 'label' => 'Order',
48
+ 'description' => 'Position of the page among its siblings.',
49
+ 'enableSorting' => false,
50
+ 'filterBy' => false,
51
+ ),
52
+ )
53
+ );
54
+ }
55
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_add_field_declarative' );
56
+
57
+ /**
58
+ * Case 2: a field (`reading_time`) whose value and render come from a script
59
+ * module, styled by a stylesheet. Two functions:
60
+ *
61
+ * - The assets, on `init` like any plugin asset. The module is registered,
62
+ * not enqueued: Gutenberg adds the modules of the registered fields to the
63
+ * import map of the editor pages. A stylesheet cannot ride along with a
64
+ * script module, so the function enqueues it on every screen where the
65
+ * field can show.
66
+ * - The field, on `wp_entity_fields_api_init`. The action fires whenever the
67
+ * registry is first read, during REST requests too, so its callback
68
+ * registers the field and nothing else.
69
+ *
70
+ * The module also exports a complete `word_count` field, id and label
71
+ * included, that no registration names. A module only
72
+ * augments the fields it was registered with, so the entry is ignored and
73
+ * the field is not registered.
74
+ */
75
+ function gutenberg_test_entity_fields_api_register_reading_time_assets() {
76
+ wp_register_script_module(
77
+ 'gutenberg-test-entity-fields-api/reading-time',
78
+ plugins_url( 'entity-fields-api/reading-time.js', __FILE__ ),
79
+ array(),
80
+ filemtime( __DIR__ . '/entity-fields-api/reading-time.js' )
81
+ );
82
+
83
+ wp_register_style(
84
+ 'gutenberg-test-entity-fields-api-reading-time',
85
+ plugins_url( 'entity-fields-api/reading-time.css', __FILE__ ),
86
+ array(),
87
+ filemtime( __DIR__ . '/entity-fields-api/reading-time.css' )
88
+ );
89
+ $enqueue_style = function () {
90
+ wp_enqueue_style( 'gutenberg-test-entity-fields-api-reading-time' );
91
+ };
92
+ // The post editor and the site editor.
93
+ add_action( 'enqueue_block_editor_assets', $enqueue_style );
94
+ // The extensible site editor renders its own document and exits on
95
+ // `admin_init`, before `enqueue_block_editor_assets` fires, so styles are
96
+ // enqueued from its `{page}_init` action instead.
97
+ add_action( 'site-editor-v2_init', $enqueue_style );
98
+ // The extensible site editor rendered inside the wp-admin chrome.
99
+ add_action( 'site-editor-v2-wp-admin_init', $enqueue_style );
100
+ }
101
+ add_action( 'init', 'gutenberg_test_entity_fields_api_register_reading_time_assets' );
102
+
103
+ function gutenberg_test_entity_fields_api_add_field_with_script_module() {
104
+ wp_register_entity_fields(
105
+ 'gutenberg-test-entity-fields-api',
106
+ 'postType',
107
+ 'page',
108
+ array(
109
+ array(
110
+ 'id' => 'reading_time',
111
+ 'type' => 'integer',
112
+ 'label' => 'Reading time',
113
+ 'enableSorting' => false,
114
+ 'filterBy' => false,
115
+ 'readOnly' => true,
116
+ ),
117
+ ),
118
+ 'gutenberg-test-entity-fields-api/reading-time'
119
+ );
120
+ }
121
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_add_field_with_script_module' );
122
+
123
+ /**
124
+ * Case 3: an update of a default field (`comment_status`). Only the properties
125
+ * given change, and the module applies to the field on top of the modules it
126
+ * has. Hideable, so the field can be shown from the view options.
127
+ *
128
+ * Only a registered field can be updated, so the update must come after the
129
+ * default fields, registered on `wp_entity_fields_api_init` at priority 0: the
130
+ * default priority does. An update before would be refused. The assets go
131
+ * on `init`, split from the field as in case 2.
132
+ */
133
+ function gutenberg_test_entity_fields_api_register_comment_status_assets() {
134
+ wp_register_script_module(
135
+ 'gutenberg-test-entity-fields-api/comment-status',
136
+ plugins_url( 'entity-fields-api/comment-status.js', __FILE__ ),
137
+ array(),
138
+ filemtime( __DIR__ . '/entity-fields-api/comment-status.js' )
139
+ );
140
+
141
+ wp_register_style(
142
+ 'gutenberg-test-entity-fields-api-comment-status',
143
+ plugins_url( 'entity-fields-api/comment-status.css', __FILE__ ),
144
+ array(),
145
+ filemtime( __DIR__ . '/entity-fields-api/comment-status.css' )
146
+ );
147
+ $enqueue_style = function () {
148
+ wp_enqueue_style( 'gutenberg-test-entity-fields-api-comment-status' );
149
+ };
150
+ // The same screens as case 2: see the comments there.
151
+ add_action( 'enqueue_block_editor_assets', $enqueue_style );
152
+ add_action( 'site-editor-v2_init', $enqueue_style );
153
+ add_action( 'site-editor-v2-wp-admin_init', $enqueue_style );
154
+ }
155
+ add_action( 'init', 'gutenberg_test_entity_fields_api_register_comment_status_assets' );
156
+
157
+ function gutenberg_test_entity_fields_api_update_field() {
158
+ wp_update_entity_fields(
159
+ 'gutenberg-test-entity-fields-api',
160
+ 'postType',
161
+ 'page',
162
+ array(
163
+ array(
164
+ 'id' => 'comment_status',
165
+ 'enableSorting' => true,
166
+ 'enableHiding' => true,
167
+ ),
168
+ ),
169
+ 'gutenberg-test-entity-fields-api/comment-status'
170
+ );
171
+ }
172
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_update_field' );
173
+
174
+ /**
175
+ * Case 4: a substitute for a default field (`author`). Unregistering drops
176
+ * its definition and detaches its script modules, so the registration that
177
+ * follows is the whole field. The default priority runs after the default
178
+ * fields: there is nothing to unregister before.
179
+ */
180
+ function gutenberg_test_entity_fields_api_replace_field() {
181
+ wp_unregister_entity_fields( 'postType', 'page', array( 'author' ) );
182
+ wp_register_entity_fields(
183
+ 'gutenberg-test-entity-fields-api',
184
+ 'postType',
185
+ 'page',
186
+ array(
187
+ array(
188
+ 'id' => 'author',
189
+ 'type' => 'integer',
190
+ 'label' => 'Written by',
191
+ 'filterBy' => false,
192
+ ),
193
+ )
194
+ );
195
+ }
196
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_replace_field' );
197
+
198
+ /**
199
+ * Case 5: a field (`subtitle`) whose value is data the plugin adds to the
200
+ * Pages REST endpoint. Three parts:
201
+ *
202
+ * - A REST field reading and writing post meta the plugin owns, which the
203
+ * endpoint does not expose otherwise, so the record of every page carries
204
+ * `subtitle` and a request updating a page can set it. Hooked to
205
+ * `rest_api_init` on its own: a request to the Pages endpoint does not read
206
+ * the fields registry, so `wp_entity_fields_api_init` may never fire during it.
207
+ * - A declarative field like case 1. The field API reads the value from the
208
+ * record and, as the field is not read-only, the Quick Edit form saves the
209
+ * edits with the record, through the REST field.
210
+ * - An entry in the Quick Edit form of Pages. The form lists its fields
211
+ * explicitly in the view configuration of the entity, so a new field is
212
+ * not offered for editing until it is added there. Merging appends the
213
+ * field to the end of the form. Filtered from the moment the plugin
214
+ * loads, whenever the view configuration is read.
215
+ */
216
+ function gutenberg_test_entity_fields_api_add_field_with_data() {
217
+ wp_register_entity_fields(
218
+ 'gutenberg-test-entity-fields-api',
219
+ 'postType',
220
+ 'page',
221
+ array(
222
+ array(
223
+ 'id' => 'subtitle',
224
+ 'type' => 'text',
225
+ 'label' => 'Subtitle',
226
+ 'description' => 'A secondary title shown under the title.',
227
+ 'enableSorting' => false,
228
+ 'filterBy' => false,
229
+ ),
230
+ )
231
+ );
232
+ }
233
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_add_field_with_data' );
234
+
235
+ add_action(
236
+ 'rest_api_init',
237
+ function () {
238
+ register_rest_field(
239
+ 'page',
240
+ 'subtitle',
241
+ array(
242
+ 'schema' => array(
243
+ 'description' => 'A secondary title shown under the title.',
244
+ 'type' => 'string',
245
+ 'context' => array( 'view', 'edit' ),
246
+ ),
247
+ 'get_callback' => function ( $item ) {
248
+ return (string) get_post_meta( $item['id'], '_gutenberg_test_subtitle', true );
249
+ },
250
+ 'update_callback' => function ( $value, $post ) {
251
+ update_post_meta( $post->ID, '_gutenberg_test_subtitle', sanitize_text_field( $value ) );
252
+ return true;
253
+ },
254
+ )
255
+ );
256
+ }
257
+ );
258
+
259
+ add_filter(
260
+ 'get_entity_view_config_posttype_page',
261
+ function ( $data ) {
262
+ return $data->merge(
263
+ array(
264
+ 'form' => array(
265
+ 'fields' => array( 'subtitle' ),
266
+ ),
267
+ ),
268
+ 1
269
+ );
270
+ }
271
+ );
272
+
273
+ /**
274
+ * Registers a submenu rendering the extensible site editor inside the
275
+ * wp-admin chrome, so the `site-editor-v2-wp-admin_init` enqueues above can
276
+ * be exercised. The page has no menu entry of its own.
277
+ */
278
+ function gutenberg_test_entity_fields_api_register_embedded_page() {
279
+ if ( ! function_exists( 'gutenberg_site_editor_v2_wp_admin_render_page' ) ) {
280
+ return;
281
+ }
282
+ add_submenu_page(
283
+ 'themes.php',
284
+ 'Pages (embedded)',
285
+ 'Pages (embedded)',
286
+ 'edit_theme_options',
287
+ 'site-editor-v2-wp-admin',
288
+ 'gutenberg_site_editor_v2_wp_admin_render_page'
289
+ );
290
+ }
291
+ add_action( 'admin_menu', 'gutenberg_test_entity_fields_api_register_embedded_page' );
@@ -0,0 +1,30 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: Gutenberg Test Image Picture Wrapper
4
+ * Plugin URI: https://github.com/WordPress/gutenberg
5
+ * Author: Gutenberg Team
6
+ *
7
+ * @package gutenberg-test-image-picture-wrapper
8
+ */
9
+
10
+ /**
11
+ * Wraps the `img` of an Image block in a `picture` element, the way the Modern
12
+ * Image Formats plugin does when its picture element setting is enabled.
13
+ *
14
+ * @param string $block_content Rendered block content.
15
+ * @param array $block Parsed block.
16
+ * @return string Filtered block content.
17
+ */
18
+ function gutenberg_test_wrap_image_in_picture( $block_content, $block ) {
19
+ if ( 'core/image' !== $block['blockName'] || ! str_contains( $block_content, '<img' ) ) {
20
+ return $block_content;
21
+ }
22
+
23
+ return preg_replace(
24
+ '/<img[^>]+>/',
25
+ '<picture style="display: contents;">$0</picture>',
26
+ $block_content,
27
+ 1
28
+ );
29
+ }
30
+ add_filter( 'render_block', 'gutenberg_test_wrap_image_in_picture', 10, 2 );
@@ -12,7 +12,7 @@
12
12
  >
13
13
  <pre
14
14
  data-testid="parent context"
15
- data-wp-bind--children="state.renderContext"
15
+ data-wp-text="state.renderContext"
16
16
  >
17
17
  <!-- rendered during hydration -->
18
18
  </pre>
@@ -62,7 +62,7 @@
62
62
  >
63
63
  <pre
64
64
  data-testid="child context"
65
- data-wp-bind--children="state.renderContext"
65
+ data-wp-text="state.renderContext"
66
66
  >
67
67
  <!-- rendered during hydration -->
68
68
  </pre>
@@ -4,12 +4,31 @@
4
4
  *
5
5
  * @package gutenberg-test-interactive-blocks
6
6
  */
7
+
8
+ wp_interactivity_state(
9
+ 'directive-html',
10
+ array(
11
+ 'serverHtml' => array( 'wpServerHtml' => true ),
12
+ )
13
+ );
7
14
  ?>
8
15
 
9
16
  <div data-wp-interactive="directive-html">
17
+ <span data-wp-text="state.hydration" data-testid="hydration">not hydrated</span>
18
+ <button data-wp-on--click="actions.setTrustedHtml" data-testid="set trusted html">
19
+ Set Trusted HTML
20
+ </button>
21
+
10
22
  <div data-wp-html="state.html" data-testid="show state html">
11
23
  <p data-testid="fallback">Fallback content</p>
12
24
  </div>
25
+ <div data-wp-html="state.rejected" data-testid="rejected html"><p>Rejected fallback</p></div>
26
+ <div data-wp-html="state.serverHtml" data-testid="server html"><p>Server-rendered HTML</p></div>
27
+ <button data-wp-on--click="actions.setServerHtml" data-testid="set server html">Set Server HTML</button>
28
+ <ul data-testid="created items">
29
+ <template data-wp-each="state.list"><li><div data-wp-html="state.empty"></div></li></template>
30
+ </ul>
31
+ <button data-wp-on--click="actions.setRejected" data-testid="set rejected html">Set Rejected HTML</button>
13
32
 
14
33
  <button data-wp-on--click="actions.setHtml" data-testid="set html">
15
34
  Set HTML
@@ -22,13 +41,6 @@
22
41
  Set Loading
23
42
  </button>
24
43
 
25
- <button
26
- data-wp-on--click="actions.setPlainString"
27
- data-testid="set plain string"
28
- >
29
- Set Plain String
30
- </button>
31
-
32
44
  <div data-wp-html--suffix="state.html" data-testid="ignores suffixes">
33
45
  <span data-testid="suffix-fallback">Suffix fallback</span>
34
46
  </div>
@@ -1,8 +1,21 @@
1
1
  import { store, asDangerousHTML } from '@wordpress/interactivity';
2
2
 
3
+ const policy = window.trustedTypes?.createPolicy( 'directive-html', {
4
+ createHTML: ( html ) => html,
5
+ } );
6
+
3
7
  const { state } = store( 'directive-html', {
4
8
  state: {
5
9
  html: null,
10
+ // Matches the server-rendered fallback, so it renders before the
11
+ // rejected value replaces it.
12
+ rejected: asDangerousHTML(
13
+ policy?.createHTML( '<p>Rejected fallback</p>' ) ??
14
+ '<p>Rejected fallback</p>'
15
+ ),
16
+ empty: null,
17
+ list: [ 'a', 'b' ],
18
+ hydration: 'hydrated',
6
19
  },
7
20
  actions: {
8
21
  setHtml() {
@@ -13,8 +26,14 @@ const { state } = store( 'directive-html', {
13
26
  setLoading() {
14
27
  state.html = null;
15
28
  },
16
- setPlainString() {
17
- state.html = '<strong>Not rendered</strong>';
29
+ setTrustedHtml() {
30
+ state.html = asDangerousHTML( policy.createHTML( '<p>Late</p>' ) );
31
+ },
32
+ setRejected() {
33
+ state.rejected = asDangerousHTML( '<b>x</b>' );
34
+ },
35
+ setServerHtml() {
36
+ state.serverHtml = asDangerousHTML( '<p>Client HTML</p>' );
18
37
  },
19
38
  },
20
39
  } );
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This module dynamically imports @wordpress/interactivity on DOMContentLoaded.
2
+ * This module dynamically imports `@wordpress/interactivity` on DOMContentLoaded.
3
3
  *
4
4
  * This simulates a lazy-loading scenario where the Interactivity API is not
5
5
  * statically imported. The test verifies that hydration still occurs even
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "https://schemas.wp.org/trunk/block.json",
3
+ "apiVersion": 3,
4
+ "name": "test/router-html",
5
+ "title": "E2E Interactivity tests - router HTML",
6
+ "category": "text",
7
+ "icon": "heart",
8
+ "attributes": {
9
+ "page": { "type": "string" },
10
+ "value": { "type": "string", "default": "null" },
11
+ "next": { "type": "string" }
12
+ },
13
+ "supports": {
14
+ "interactivity": true
15
+ },
16
+ "textdomain": "e2e-interactivity",
17
+ "viewScriptModule": "file:./view.js",
18
+ "render": "file:./render.php"
19
+ }
@@ -0,0 +1,64 @@
1
+ <?php
2
+ /**
3
+ * HTML for testing HTML elements kept across router navigation.
4
+ *
5
+ * @package gutenberg-test-interactive-blocks
6
+ */
7
+
8
+ $attributes = get_defined_vars()['attributes'];
9
+ $page = $attributes['page'];
10
+
11
+ if ( 'pending' === $attributes['value'] ) {
12
+ wp_interactivity_state(
13
+ 'router-html',
14
+ array(
15
+ /**
16
+ * Supplies the server-only derived HTML placeholder.
17
+ *
18
+ * @return null The client implementation is deferred.
19
+ */
20
+ 'late' => function () {
21
+ return null;
22
+ },
23
+ )
24
+ );
25
+ add_filter(
26
+ 'script_module_data_@wordpress/interactivity',
27
+ /**
28
+ * Declares a derived value whose client implementation loads later.
29
+ *
30
+ * @param array|null $data Serialized Interactivity API server data.
31
+ * @return array Server data with the pending HTML getter declaration.
32
+ */
33
+ function ( $data ) {
34
+ $data = $data ?? array();
35
+ $data['derivedStateClosures']['router-html'][] = 'state.late';
36
+ return $data;
37
+ }
38
+ );
39
+ }
40
+ ?>
41
+
42
+ <div data-wp-interactive="router-html">
43
+ <div
44
+ data-wp-interactive="router-html"
45
+ data-wp-router-region="router-html-kept"
46
+ <?php
47
+ echo wp_interactivity_data_wp_context(
48
+ array(
49
+ 'value' => $attributes['value'],
50
+ 'resolved' => false,
51
+ )
52
+ );
53
+ ?>
54
+ >
55
+ <p data-testid="page-content">content from <?php echo esc_html( $page ); ?></p>
56
+ <div data-testid="kept-html" data-wp-html="state.html"><i><?php echo 'one' === $page ? 'one' : 'two'; ?></i></div>
57
+ <div data-testid="never-html" data-wp-html="state.never"><i><?php echo 'one' === $page ? 'never one' : 'never two'; ?></i></div>
58
+ <p data-testid="value" data-wp-text="state.value">not hydrated</p>
59
+ <button data-wp-on--click="actions.resolve">Resolve HTML</button>
60
+ <?php if ( ! empty( $attributes['next'] ) ) : ?>
61
+ <a href="<?php echo esc_url( $attributes['next'] ); ?>" data-wp-on--click="actions.navigate">Next</a>
62
+ <?php endif; ?>
63
+ </div>
64
+ </div>
@@ -0,0 +1,9 @@
1
+ <?php return array(
2
+ 'dependencies' => array(
3
+ '@wordpress/interactivity',
4
+ array(
5
+ 'id' => '@wordpress/interactivity-router',
6
+ 'import' => 'dynamic',
7
+ ),
8
+ ),
9
+ );
@@ -0,0 +1,56 @@
1
+ import {
2
+ asDangerousHTML,
3
+ getContext,
4
+ getServerContext,
5
+ store,
6
+ withSyncEvent,
7
+ } from '@wordpress/interactivity';
8
+
9
+ /** HTML tokens selected by each page's server context. */
10
+ const tokens = {
11
+ /** Initial or matching destination HTML. */
12
+ T: asDangerousHTML( '<p>T</p>' ),
13
+ /** Differing or late destination HTML. */
14
+ U: asDangerousHTML( '<p>U</p>' ),
15
+ };
16
+
17
+ const { state } = store( 'router-html', {
18
+ state: {
19
+ /** Always empty, including on a kept node. */
20
+ never: null,
21
+ /** Returns the destination token, null, or a pending derived value. */
22
+ get html() {
23
+ if ( getServerContext().value === 'pending' ) {
24
+ return state.late;
25
+ }
26
+ return getContext().resolved
27
+ ? tokens.U
28
+ : ( tokens[ getServerContext().value ] ?? null );
29
+ },
30
+ /** Reports the destination's own serialized value selection. */
31
+ get value() {
32
+ return getServerContext().value;
33
+ },
34
+ },
35
+ actions: {
36
+ /** Navigates using the router without a document reload. */
37
+ navigate: withSyncEvent( function* ( event ) {
38
+ event.preventDefault();
39
+ const { actions } =
40
+ yield import( '@wordpress/interactivity-router' );
41
+ yield actions.navigate( event.target.href );
42
+ } ),
43
+ /** Resolves null HTML or registers the pending derived getter. */
44
+ resolve() {
45
+ getContext().resolved = true;
46
+ store( 'router-html', {
47
+ state: {
48
+ /** Supplies a late token after the pending destination loads. */
49
+ get late() {
50
+ return tokens.U;
51
+ },
52
+ },
53
+ } );
54
+ },
55
+ },
56
+ } );
@@ -0,0 +1,17 @@
1
+ {
2
+ "$schema": "https://schemas.wp.org/trunk/block.json",
3
+ "apiVersion": 3,
4
+ "name": "test/router-html-regions",
5
+ "title": "E2E Interactivity tests - HTML router regions",
6
+ "category": "text",
7
+ "supports": { "interactivity": true },
8
+ "attributes": {
9
+ "letter": { "type": "string", "default": "A" },
10
+ "next": { "type": "string", "default": "" },
11
+ "mode": { "type": "string", "default": "fallback" },
12
+ "before": { "type": "boolean", "default": false },
13
+ "copy": { "type": "boolean", "default": true }
14
+ },
15
+ "viewScriptModule": "file:./view.js",
16
+ "render": "file:./render.php"
17
+ }
@@ -0,0 +1,77 @@
1
+ <?php
2
+ /**
3
+ * Markup for navigation around inert HTML content.
4
+ *
5
+ * @package gutenberg-test-interactive-blocks
6
+ */
7
+
8
+ // Block rendering supplies attributes; standalone inclusion uses defaults.
9
+ $fixture_attributes = get_defined_vars()['attributes'] ?? array();
10
+ $letter = $fixture_attributes['letter'] ?? 'A';
11
+ $mode = $fixture_attributes['mode'] ?? 'fallback';
12
+ $has_copy = $fixture_attributes['copy'] ?? true;
13
+ $copy_before = $fixture_attributes['before'] ?? false;
14
+ $region_value = 'attach' === $mode ? wp_json_encode(
15
+ array(
16
+ 'id' => 'html/o',
17
+ 'attachTo' => 'body',
18
+ )
19
+ ) : 'html/r';
20
+
21
+ // Derived server state keeps fallback content without defining a client value.
22
+ wp_interactivity_state(
23
+ 'router-html-regions',
24
+ array(
25
+ /**
26
+ * Computes the fallback's pending server value.
27
+ *
28
+ * @return null The client getter is intentionally unavailable.
29
+ */
30
+ 'pending' => static function () {
31
+ return null;
32
+ },
33
+ )
34
+ );
35
+
36
+ add_filter(
37
+ 'script_module_data_@wordpress/interactivity',
38
+ /**
39
+ * Declares the server getter referenced by the HTML directive.
40
+ *
41
+ * @param array|null $data Serialized runtime data.
42
+ * @return array Runtime data with the pending getter declaration.
43
+ */
44
+ static function ( $data ) {
45
+ $data = $data ?? array();
46
+ $data['derivedStateClosures']['router-html-regions'][] = 'state.pending';
47
+ return $data;
48
+ }
49
+ );
50
+
51
+ $copy_markup = $has_copy ? sprintf(
52
+ '<div data-testid="html-owner" data-wp-html="state.pending"><div data-testid="copy" data-wp-interactive="router-html-regions" data-wp-router-region="%s" data-wp-init="actions.mark"><p>copy</p></div></div>',
53
+ esc_attr( $region_value )
54
+ ) : '';
55
+ ?>
56
+ <div data-wp-interactive="router-html-regions">
57
+ <a data-testid="next" data-wp-on--click="actions.navigate" href="<?php echo esc_url( $fixture_attributes['next'] ?? '' ); ?>">Next</a>
58
+ <output data-testid="marks" data-wp-text="state.marks">0</output>
59
+ <?php
60
+ if ( $copy_before && 'fallback' === $mode ) {
61
+ echo $copy_markup;
62
+ }
63
+ ?>
64
+ <?php if ( 'attach' !== $mode || 'B' === $letter ) : ?>
65
+ <div data-testid="real" data-wp-interactive="router-html-regions" data-wp-router-region="<?php echo esc_attr( $region_value ); ?>">
66
+ <p><?php echo esc_html( 'attach' === $mode ? 'overlay ' . $letter : $letter ); ?></p>
67
+ </div>
68
+ <?php endif; ?>
69
+ <?php
70
+ if ( ! $copy_before && 'inserted' !== $mode ) {
71
+ echo $copy_markup;
72
+ }
73
+ ?>
74
+ <?php if ( 'inserted' === $mode && 'A' === $letter ) : ?>
75
+ <div data-testid="html-owner" data-wp-html="state.inserted"></div>
76
+ <?php endif; ?>
77
+ </div>