@wordpress/block-library 9.34.1-next.2f1c7c01b.0 → 9.35.1-next.16d95556a.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.
- package/CHANGELOG.md +2 -0
- package/build/block/edit.js +2 -2
- package/build/block/edit.js.map +2 -2
- package/build/block-keyboard-shortcuts/index.js +17 -7
- package/build/block-keyboard-shortcuts/index.js.map +2 -2
- package/build/cover/deprecated.js +15 -3
- package/build/cover/deprecated.js.map +2 -2
- package/build/cover/edit/inspector-controls.js +1 -1
- package/build/cover/edit/inspector-controls.js.map +2 -2
- package/build/cover/transforms.js +10 -2
- package/build/cover/transforms.js.map +2 -2
- package/build/embed/icons.js +2 -2
- package/build/embed/icons.js.map +2 -2
- package/build/embed/variations.js +3 -3
- package/build/embed/variations.js.map +2 -2
- package/build/heading/index.js +3 -1
- package/build/heading/index.js.map +3 -3
- package/build/heading/transforms.js +10 -3
- package/build/heading/transforms.js.map +2 -2
- package/build/heading/variations.js +55 -0
- package/build/heading/variations.js.map +7 -0
- package/build/html/edit.js +54 -44
- package/build/html/edit.js.map +3 -3
- package/build/html/modal.js +328 -0
- package/build/html/modal.js.map +7 -0
- package/build/html/utils.js +72 -0
- package/build/html/utils.js.map +7 -0
- package/build/navigation-link/edit.js +25 -10
- package/build/navigation-link/edit.js.map +2 -2
- package/build/navigation-link/link-ui/index.js +8 -3
- package/build/navigation-link/link-ui/index.js.map +2 -2
- package/build/navigation-link/shared/controls.js +42 -7
- package/build/navigation-link/shared/controls.js.map +2 -2
- package/build/navigation-link/shared/use-entity-binding.js +31 -2
- package/build/navigation-link/shared/use-entity-binding.js.map +3 -3
- package/build/paragraph/block.json +1 -3
- package/build/paragraph/deprecated.js +65 -12
- package/build/paragraph/deprecated.js.map +2 -2
- package/build/paragraph/edit.js +14 -25
- package/build/paragraph/edit.js.map +2 -2
- package/build/paragraph/index.js +3 -1
- package/build/paragraph/index.js.map +3 -3
- package/build/paragraph/save.js +3 -3
- package/build/paragraph/save.js.map +2 -2
- package/build/paragraph/transforms.js +7 -1
- package/build/paragraph/transforms.js.map +2 -2
- package/build/paragraph/variations.js +57 -0
- package/build/paragraph/variations.js.map +7 -0
- package/build/pullquote/block.json +3 -2
- package/build/pullquote/transforms.js +0 -31
- package/build/pullquote/transforms.js.map +2 -2
- package/build/quote/transforms.js +0 -20
- package/build/quote/transforms.js.map +2 -2
- package/build-module/block/edit.js +2 -2
- package/build-module/block/edit.js.map +2 -2
- package/build-module/block-keyboard-shortcuts/index.js +17 -7
- package/build-module/block-keyboard-shortcuts/index.js.map +2 -2
- package/build-module/cover/deprecated.js +15 -3
- package/build-module/cover/deprecated.js.map +2 -2
- package/build-module/cover/edit/inspector-controls.js +1 -1
- package/build-module/cover/edit/inspector-controls.js.map +2 -2
- package/build-module/cover/transforms.js +10 -2
- package/build-module/cover/transforms.js.map +2 -2
- package/build-module/embed/icons.js +2 -2
- package/build-module/embed/icons.js.map +2 -2
- package/build-module/embed/variations.js +3 -3
- package/build-module/embed/variations.js.map +2 -2
- package/build-module/heading/index.js +3 -1
- package/build-module/heading/index.js.map +2 -2
- package/build-module/heading/transforms.js +10 -3
- package/build-module/heading/transforms.js.map +2 -2
- package/build-module/heading/variations.js +34 -0
- package/build-module/heading/variations.js.map +7 -0
- package/build-module/html/edit.js +62 -51
- package/build-module/html/edit.js.map +2 -2
- package/build-module/html/modal.js +304 -0
- package/build-module/html/modal.js.map +7 -0
- package/build-module/html/utils.js +46 -0
- package/build-module/html/utils.js.map +7 -0
- package/build-module/navigation-link/edit.js +25 -10
- package/build-module/navigation-link/edit.js.map +2 -2
- package/build-module/navigation-link/link-ui/index.js +8 -3
- package/build-module/navigation-link/link-ui/index.js.map +2 -2
- package/build-module/navigation-link/shared/controls.js +42 -7
- package/build-module/navigation-link/shared/controls.js.map +2 -2
- package/build-module/navigation-link/shared/use-entity-binding.js +35 -3
- package/build-module/navigation-link/shared/use-entity-binding.js.map +2 -2
- package/build-module/paragraph/block.json +1 -3
- package/build-module/paragraph/deprecated.js +65 -12
- package/build-module/paragraph/deprecated.js.map +2 -2
- package/build-module/paragraph/edit.js +14 -26
- package/build-module/paragraph/edit.js.map +2 -2
- package/build-module/paragraph/index.js +3 -1
- package/build-module/paragraph/index.js.map +2 -2
- package/build-module/paragraph/save.js +3 -3
- package/build-module/paragraph/save.js.map +2 -2
- package/build-module/paragraph/transforms.js +7 -1
- package/build-module/paragraph/transforms.js.map +2 -2
- package/build-module/paragraph/variations.js +36 -0
- package/build-module/paragraph/variations.js.map +7 -0
- package/build-module/pullquote/block.json +3 -2
- package/build-module/pullquote/transforms.js +0 -31
- package/build-module/pullquote/transforms.js.map +2 -2
- package/build-module/quote/transforms.js +0 -20
- package/build-module/quote/transforms.js.map +2 -2
- package/build-style/accordion-heading/style-rtl.css +19 -3
- package/build-style/accordion-heading/style.css +19 -3
- package/build-style/accordion-panel/style-rtl.css +4 -1
- package/build-style/accordion-panel/style.css +4 -1
- package/build-style/common-rtl.css +3 -3
- package/build-style/common.css +3 -3
- package/build-style/editor-rtl.css +62 -21
- package/build-style/editor.css +62 -21
- package/build-style/embed/style-rtl.css +5 -0
- package/build-style/embed/style.css +5 -0
- package/build-style/html/editor-rtl.css +55 -21
- package/build-style/html/editor.css +55 -21
- package/build-style/navigation-link/editor-rtl.css +7 -0
- package/build-style/navigation-link/editor.css +7 -0
- package/build-style/style-rtl.css +31 -7
- package/build-style/style.css +31 -7
- package/package.json +37 -37
- package/src/accordion-heading/style.scss +40 -7
- package/src/accordion-panel/style.scss +6 -1
- package/src/block/edit.js +2 -2
- package/src/block-keyboard-shortcuts/index.js +23 -9
- package/src/common.scss +6 -5
- package/src/cover/deprecated.js +15 -3
- package/src/cover/edit/inspector-controls.js +1 -1
- package/src/cover/transforms.js +10 -2
- package/src/embed/icons.js +2 -4
- package/src/embed/style.scss +6 -0
- package/src/embed/variations.js +3 -3
- package/src/heading/index.js +2 -0
- package/src/heading/test/__snapshots__/transforms.native.js.snap +0 -6
- package/src/heading/test/transforms.native.js +1 -5
- package/src/heading/transforms.js +10 -3
- package/src/heading/variations.js +37 -0
- package/src/html/edit.js +62 -56
- package/src/html/editor.scss +69 -10
- package/src/html/modal.js +290 -0
- package/src/html/test/utils.js +234 -0
- package/src/html/utils.js +75 -0
- package/src/navigation-link/edit.js +44 -13
- package/src/navigation-link/editor.scss +7 -0
- package/src/navigation-link/index.php +65 -2
- package/src/navigation-link/link-ui/index.js +9 -8
- package/src/navigation-link/shared/controls.js +70 -12
- package/src/navigation-link/shared/test/controls.js +5 -0
- package/src/navigation-link/shared/test/use-entity-binding.js +14 -1
- package/src/navigation-link/shared/use-entity-binding.js +57 -9
- package/src/paragraph/block.json +1 -3
- package/src/paragraph/deprecated.js +87 -20
- package/src/paragraph/edit.js +7 -18
- package/src/paragraph/edit.native.js +18 -6
- package/src/paragraph/index.js +2 -0
- package/src/paragraph/save.js +4 -3
- package/src/paragraph/test/__snapshots__/transforms.native.js.snap +0 -6
- package/src/paragraph/test/edit.native.js +5 -5
- package/src/paragraph/test/transforms.native.js +0 -1
- package/src/paragraph/transforms.js +7 -1
- package/src/paragraph/variations.js +39 -0
- package/src/pullquote/block.json +3 -2
- package/src/pullquote/test/__snapshots__/transforms.native.js.snap +5 -5
- package/src/pullquote/test/transforms.native.js +1 -1
- package/src/pullquote/transforms.js +0 -31
- package/src/quote/test/__snapshots__/transforms.native.js.snap +0 -6
- package/src/quote/test/transforms.native.js +1 -5
- package/src/quote/transforms.js +0 -25
- package/src/utils/transformation-categories.native.js +0 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/src/pullquote/test/edit.native.js +0 -73
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { parseContent, serializeContent } from '../utils';
|
|
5
|
+
|
|
6
|
+
describe( 'core/html', () => {
|
|
7
|
+
describe( 'parseContent()', () => {
|
|
8
|
+
it( 'should parse empty content', () => {
|
|
9
|
+
const result = parseContent( '' );
|
|
10
|
+
expect( result ).toEqual( { html: '', css: '', js: '' } );
|
|
11
|
+
} );
|
|
12
|
+
|
|
13
|
+
it( 'should parse whitespace-only content', () => {
|
|
14
|
+
const result = parseContent( ' \n\t ' );
|
|
15
|
+
expect( result ).toEqual( { html: '', css: '', js: '' } );
|
|
16
|
+
} );
|
|
17
|
+
|
|
18
|
+
it( 'should parse HTML-only content', () => {
|
|
19
|
+
const content = '<p>Hello World</p>';
|
|
20
|
+
const result = parseContent( content );
|
|
21
|
+
expect( result ).toEqual( {
|
|
22
|
+
html: '<p>Hello World</p>',
|
|
23
|
+
css: '',
|
|
24
|
+
js: '',
|
|
25
|
+
} );
|
|
26
|
+
} );
|
|
27
|
+
|
|
28
|
+
it( 'should parse CSS-only content', () => {
|
|
29
|
+
const content =
|
|
30
|
+
'<style data-wp-block-html="css">body { color: red; }</style>';
|
|
31
|
+
const result = parseContent( content );
|
|
32
|
+
expect( result ).toEqual( {
|
|
33
|
+
html: '',
|
|
34
|
+
css: 'body { color: red; }',
|
|
35
|
+
js: '',
|
|
36
|
+
} );
|
|
37
|
+
} );
|
|
38
|
+
|
|
39
|
+
it( 'should parse JavaScript-only content', () => {
|
|
40
|
+
const content =
|
|
41
|
+
'<script data-wp-block-html="js">console.log("hello");</script>';
|
|
42
|
+
const result = parseContent( content );
|
|
43
|
+
expect( result ).toEqual( {
|
|
44
|
+
html: '',
|
|
45
|
+
css: '',
|
|
46
|
+
js: 'console.log("hello");',
|
|
47
|
+
} );
|
|
48
|
+
} );
|
|
49
|
+
|
|
50
|
+
it( 'should parse content with all three sections', () => {
|
|
51
|
+
const content = `<style data-wp-block-html="css">
|
|
52
|
+
body { color: red; }
|
|
53
|
+
</style>
|
|
54
|
+
|
|
55
|
+
<script data-wp-block-html="js">
|
|
56
|
+
console.log("hello");
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<p>Hello World</p>`;
|
|
60
|
+
const result = parseContent( content );
|
|
61
|
+
expect( result.css ).toBe( 'body { color: red; }' );
|
|
62
|
+
expect( result.js ).toBe( 'console.log("hello");' );
|
|
63
|
+
expect( result.html ).toBe( '<p>Hello World</p>' );
|
|
64
|
+
} );
|
|
65
|
+
|
|
66
|
+
it( 'should ignore unmarked style tags', () => {
|
|
67
|
+
const content = `<style>body { color: blue; }</style>
|
|
68
|
+
<style data-wp-block-html="css">body { color: red; }</style>
|
|
69
|
+
<p>Test</p>`;
|
|
70
|
+
const result = parseContent( content );
|
|
71
|
+
expect( result.css ).toBe( 'body { color: red; }' );
|
|
72
|
+
expect( result.html ).toContain(
|
|
73
|
+
'<style>body { color: blue; }</style>'
|
|
74
|
+
);
|
|
75
|
+
} );
|
|
76
|
+
|
|
77
|
+
it( 'should ignore unmarked script tags', () => {
|
|
78
|
+
const content = `<script>alert("unmarked");</script>
|
|
79
|
+
<script data-wp-block-html="js">console.log("marked");</script>
|
|
80
|
+
<p>Test</p>`;
|
|
81
|
+
const result = parseContent( content );
|
|
82
|
+
expect( result.js ).toBe( 'console.log("marked");' );
|
|
83
|
+
expect( result.html ).toContain(
|
|
84
|
+
'<script>alert("unmarked");</script>'
|
|
85
|
+
);
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
it( 'should handle multiple marked style tags (takes first)', () => {
|
|
89
|
+
const content = `<style data-wp-block-html="css">first</style>
|
|
90
|
+
<style data-wp-block-html="css">second</style>`;
|
|
91
|
+
const result = parseContent( content );
|
|
92
|
+
expect( result.css ).toBe( 'first' );
|
|
93
|
+
expect( result.html ).toContain( 'second' );
|
|
94
|
+
} );
|
|
95
|
+
|
|
96
|
+
it( 'should handle multiple marked script tags (takes first)', () => {
|
|
97
|
+
const content = `<script data-wp-block-html="js">first</script>
|
|
98
|
+
<script data-wp-block-html="js">second</script>`;
|
|
99
|
+
const result = parseContent( content );
|
|
100
|
+
expect( result.js ).toBe( 'first' );
|
|
101
|
+
expect( result.html ).toContain( 'second' );
|
|
102
|
+
} );
|
|
103
|
+
|
|
104
|
+
it( 'should trim whitespace from extracted sections', () => {
|
|
105
|
+
const content = `<style data-wp-block-html="css">
|
|
106
|
+
|
|
107
|
+
body { color: red; }
|
|
108
|
+
|
|
109
|
+
</style>
|
|
110
|
+
|
|
111
|
+
<script data-wp-block-html="js">
|
|
112
|
+
|
|
113
|
+
console.log("test");
|
|
114
|
+
|
|
115
|
+
</script>
|
|
116
|
+
|
|
117
|
+
<p>Test</p> `;
|
|
118
|
+
const result = parseContent( content );
|
|
119
|
+
expect( result.css ).toBe( 'body { color: red; }' );
|
|
120
|
+
expect( result.js ).toBe( 'console.log("test");' );
|
|
121
|
+
expect( result.html ).toBe( '<p>Test</p>' );
|
|
122
|
+
} );
|
|
123
|
+
|
|
124
|
+
it( 'should handle malformed HTML gracefully', () => {
|
|
125
|
+
const content = '<p>Unclosed tag<div>Test</p>';
|
|
126
|
+
const result = parseContent( content );
|
|
127
|
+
expect( result ).toHaveProperty( 'html' );
|
|
128
|
+
expect( result ).toHaveProperty( 'css' );
|
|
129
|
+
expect( result ).toHaveProperty( 'js' );
|
|
130
|
+
} );
|
|
131
|
+
} );
|
|
132
|
+
|
|
133
|
+
describe( 'serializeContent()', () => {
|
|
134
|
+
it( 'should serialize empty sections', () => {
|
|
135
|
+
const result = serializeContent( { html: '', css: '', js: '' } );
|
|
136
|
+
expect( result ).toBe( '' );
|
|
137
|
+
} );
|
|
138
|
+
|
|
139
|
+
it( 'should serialize HTML-only', () => {
|
|
140
|
+
const result = serializeContent( {
|
|
141
|
+
html: '<p>Hello World</p>',
|
|
142
|
+
css: '',
|
|
143
|
+
js: '',
|
|
144
|
+
} );
|
|
145
|
+
expect( result ).toBe( '<p>Hello World</p>' );
|
|
146
|
+
} );
|
|
147
|
+
|
|
148
|
+
it( 'should serialize CSS-only', () => {
|
|
149
|
+
const result = serializeContent( {
|
|
150
|
+
html: '',
|
|
151
|
+
css: 'body { color: red; }',
|
|
152
|
+
js: '',
|
|
153
|
+
} );
|
|
154
|
+
expect( result ).toBe(
|
|
155
|
+
'<style data-wp-block-html="css">\nbody { color: red; }\n</style>'
|
|
156
|
+
);
|
|
157
|
+
} );
|
|
158
|
+
|
|
159
|
+
it( 'should serialize JavaScript-only', () => {
|
|
160
|
+
const result = serializeContent( {
|
|
161
|
+
html: '',
|
|
162
|
+
css: '',
|
|
163
|
+
js: 'console.log("test");',
|
|
164
|
+
} );
|
|
165
|
+
expect( result ).toBe(
|
|
166
|
+
'<script data-wp-block-html="js">\nconsole.log("test");\n</script>'
|
|
167
|
+
);
|
|
168
|
+
} );
|
|
169
|
+
|
|
170
|
+
it( 'should serialize all three sections in correct order', () => {
|
|
171
|
+
const result = serializeContent( {
|
|
172
|
+
html: '<p>Hello</p>',
|
|
173
|
+
css: 'body { color: red; }',
|
|
174
|
+
js: 'console.log("test");',
|
|
175
|
+
} );
|
|
176
|
+
expect( result ).toBe(
|
|
177
|
+
'<style data-wp-block-html="css">\nbody { color: red; }\n</style>\n\n<script data-wp-block-html="js">\nconsole.log("test");\n</script>\n\n<p>Hello</p>'
|
|
178
|
+
);
|
|
179
|
+
} );
|
|
180
|
+
|
|
181
|
+
it( 'should ignore whitespace-only sections', () => {
|
|
182
|
+
const result = serializeContent( {
|
|
183
|
+
html: '<p>Test</p>',
|
|
184
|
+
css: ' \n ',
|
|
185
|
+
js: '\t\t',
|
|
186
|
+
} );
|
|
187
|
+
expect( result ).toBe( '<p>Test</p>' );
|
|
188
|
+
} );
|
|
189
|
+
|
|
190
|
+
it( 'should handle missing properties gracefully', () => {
|
|
191
|
+
const result = serializeContent( {} );
|
|
192
|
+
expect( result ).toBe( '' );
|
|
193
|
+
} );
|
|
194
|
+
|
|
195
|
+
it( 'should handle undefined values', () => {
|
|
196
|
+
const result = serializeContent( {
|
|
197
|
+
html: undefined,
|
|
198
|
+
css: undefined,
|
|
199
|
+
js: undefined,
|
|
200
|
+
} );
|
|
201
|
+
expect( result ).toBe( '' );
|
|
202
|
+
} );
|
|
203
|
+
} );
|
|
204
|
+
|
|
205
|
+
describe( 'round-trip serialization', () => {
|
|
206
|
+
it( 'should maintain HTML-only content', () => {
|
|
207
|
+
const original = '<p>Hello World</p>';
|
|
208
|
+
const parsed = parseContent( original );
|
|
209
|
+
const serialized = serializeContent( parsed );
|
|
210
|
+
expect( serialized ).toBe( original );
|
|
211
|
+
} );
|
|
212
|
+
|
|
213
|
+
it( 'should maintain content with all sections', () => {
|
|
214
|
+
const original =
|
|
215
|
+
'<style data-wp-block-html="css">\nbody { color: red; }\n</style>\n\n<script data-wp-block-html="js">\nconsole.log("test");\n</script>\n\n<p>Hello</p>';
|
|
216
|
+
const parsed = parseContent( original );
|
|
217
|
+
const serialized = serializeContent( parsed );
|
|
218
|
+
expect( serialized ).toBe( original );
|
|
219
|
+
} );
|
|
220
|
+
|
|
221
|
+
it( 'should maintain complex HTML structures', () => {
|
|
222
|
+
const sections = {
|
|
223
|
+
html: '<div><h1>Title</h1><p>Paragraph</p></div>',
|
|
224
|
+
css: 'h1 { font-size: 2em; }',
|
|
225
|
+
js: 'document.addEventListener("load", () => {});',
|
|
226
|
+
};
|
|
227
|
+
const serialized = serializeContent( sections );
|
|
228
|
+
const parsed = parseContent( serialized );
|
|
229
|
+
expect( parsed.html ).toBe( sections.html );
|
|
230
|
+
expect( parsed.css ).toBe( sections.css );
|
|
231
|
+
expect( parsed.js ).toBe( sections.js );
|
|
232
|
+
} );
|
|
233
|
+
} );
|
|
234
|
+
} );
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses content string into separate HTML, CSS, and JS sections.
|
|
3
|
+
*
|
|
4
|
+
* Extracts CSS from <style data-wp-block-html="css"> tags and
|
|
5
|
+
* JavaScript from <script data-wp-block-html="js"> tags.
|
|
6
|
+
* Everything else is treated as HTML.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} content - The combined content string
|
|
9
|
+
* @return {Object} Object with html, css, and js properties
|
|
10
|
+
*/
|
|
11
|
+
export function parseContent( content = '' ) {
|
|
12
|
+
if ( ! content || ! content.trim() ) {
|
|
13
|
+
return { html: '', css: '', js: '' };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Create a temporary document to parse HTML safely
|
|
17
|
+
const doc = document.implementation.createHTMLDocument( '' );
|
|
18
|
+
doc.body.innerHTML = content;
|
|
19
|
+
|
|
20
|
+
// Extract CSS from marked style tag
|
|
21
|
+
const styleTag = doc.body.querySelector(
|
|
22
|
+
'style[data-wp-block-html="css"]'
|
|
23
|
+
);
|
|
24
|
+
const css = styleTag ? styleTag.textContent.trim() : '';
|
|
25
|
+
if ( styleTag ) {
|
|
26
|
+
styleTag.remove();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Extract JS from marked script tag
|
|
30
|
+
const scriptTag = doc.body.querySelector(
|
|
31
|
+
'script[data-wp-block-html="js"]'
|
|
32
|
+
);
|
|
33
|
+
const js = scriptTag ? scriptTag.textContent.trim() : '';
|
|
34
|
+
if ( scriptTag ) {
|
|
35
|
+
scriptTag.remove();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Everything else is HTML
|
|
39
|
+
const html = doc.body.innerHTML.trim();
|
|
40
|
+
|
|
41
|
+
return { html, css, js };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Serializes HTML, CSS, and JS into a single content string.
|
|
46
|
+
*
|
|
47
|
+
* Creates marked <style> and <script> tags for CSS and JS sections,
|
|
48
|
+
* then appends the HTML content.
|
|
49
|
+
*
|
|
50
|
+
* @param {Object} sections Object with html, css, and js properties
|
|
51
|
+
* @param {string} sections.html HTML content
|
|
52
|
+
* @param {string} sections.css CSS content
|
|
53
|
+
* @param {string} sections.js JavaScript content
|
|
54
|
+
* @return {string} Combined content string
|
|
55
|
+
*/
|
|
56
|
+
export function serializeContent( { html = '', css = '', js = '' } ) {
|
|
57
|
+
const parts = [];
|
|
58
|
+
|
|
59
|
+
// Add CSS if present
|
|
60
|
+
if ( css.trim() ) {
|
|
61
|
+
parts.push( `<style data-wp-block-html="css">\n${ css }\n</style>` );
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Add JS if present
|
|
65
|
+
if ( js.trim() ) {
|
|
66
|
+
parts.push( `<script data-wp-block-html="js">\n${ js }\n</script>` );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Add HTML
|
|
70
|
+
if ( html.trim() ) {
|
|
71
|
+
parts.push( html );
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return parts.join( '\n\n' );
|
|
75
|
+
}
|
|
@@ -97,21 +97,35 @@ const useIsInvalidLink = ( kind, type, id, enabled ) => {
|
|
|
97
97
|
const hasId = Number.isInteger( id );
|
|
98
98
|
const blockEditingMode = useBlockEditingMode();
|
|
99
99
|
|
|
100
|
-
const postStatus = useSelect(
|
|
100
|
+
const { postStatus, isDeleted } = useSelect(
|
|
101
101
|
( select ) => {
|
|
102
102
|
if ( ! isPostType ) {
|
|
103
|
-
return null;
|
|
103
|
+
return { postStatus: null, isDeleted: false };
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
// Fetching the posts status is an "expensive" operation. Especially for sites with large navigations.
|
|
107
107
|
// When the block is rendered in a template or other disabled contexts we can skip this check in order
|
|
108
108
|
// to avoid all these additional requests that don't really add any value in that mode.
|
|
109
109
|
if ( blockEditingMode === 'disabled' || ! enabled ) {
|
|
110
|
-
return null;
|
|
110
|
+
return { postStatus: null, isDeleted: false };
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
const { getEntityRecord } =
|
|
114
|
-
|
|
113
|
+
const { getEntityRecord, hasFinishedResolution } =
|
|
114
|
+
select( coreStore );
|
|
115
|
+
const entityRecord = getEntityRecord( 'postType', type, id );
|
|
116
|
+
const hasResolved = hasFinishedResolution( 'getEntityRecord', [
|
|
117
|
+
'postType',
|
|
118
|
+
type,
|
|
119
|
+
id,
|
|
120
|
+
] );
|
|
121
|
+
|
|
122
|
+
// If resolution has finished and entityRecord is undefined, the entity was deleted.
|
|
123
|
+
const deleted = hasResolved && entityRecord === undefined;
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
postStatus: entityRecord?.status,
|
|
127
|
+
isDeleted: deleted,
|
|
128
|
+
};
|
|
115
129
|
},
|
|
116
130
|
[ isPostType, blockEditingMode, enabled, type, id ]
|
|
117
131
|
);
|
|
@@ -121,11 +135,13 @@ const useIsInvalidLink = ( kind, type, id, enabled ) => {
|
|
|
121
135
|
// 2. It has an id.
|
|
122
136
|
// 3. It's neither null, nor undefined, as valid items might be either of those while loading.
|
|
123
137
|
// If those conditions are met, check if
|
|
124
|
-
// 1. The post status is
|
|
125
|
-
// 2. The
|
|
138
|
+
// 1. The post status is trash (trashed).
|
|
139
|
+
// 2. The entity doesn't exist (deleted).
|
|
126
140
|
// If either of those is true, invalidate.
|
|
127
141
|
const isInvalid =
|
|
128
|
-
isPostType &&
|
|
142
|
+
isPostType &&
|
|
143
|
+
hasId &&
|
|
144
|
+
( isDeleted || ( postStatus && 'trash' === postStatus ) );
|
|
129
145
|
const isDraft = 'draft' === postStatus;
|
|
130
146
|
|
|
131
147
|
return [ isInvalid, isDraft ];
|
|
@@ -248,7 +264,12 @@ export default function NavigationLinkEdit( {
|
|
|
248
264
|
const { getBlocks } = useSelect( blockEditorStore );
|
|
249
265
|
|
|
250
266
|
// URL binding logic
|
|
251
|
-
const {
|
|
267
|
+
const {
|
|
268
|
+
clearBinding,
|
|
269
|
+
createBinding,
|
|
270
|
+
hasUrlBinding,
|
|
271
|
+
isBoundEntityAvailable,
|
|
272
|
+
} = useEntityBinding( {
|
|
252
273
|
clientId,
|
|
253
274
|
attributes,
|
|
254
275
|
} );
|
|
@@ -405,14 +426,23 @@ export default function NavigationLinkEdit( {
|
|
|
405
426
|
}
|
|
406
427
|
);
|
|
407
428
|
|
|
408
|
-
if (
|
|
429
|
+
if (
|
|
430
|
+
! url ||
|
|
431
|
+
isInvalid ||
|
|
432
|
+
isDraft ||
|
|
433
|
+
( hasUrlBinding && ! isBoundEntityAvailable )
|
|
434
|
+
) {
|
|
409
435
|
blockProps.onClick = () => {
|
|
410
436
|
setIsLinkOpen( true );
|
|
411
437
|
};
|
|
412
438
|
}
|
|
413
439
|
|
|
414
440
|
const classes = clsx( 'wp-block-navigation-item__content', {
|
|
415
|
-
'wp-block-navigation-link__placeholder':
|
|
441
|
+
'wp-block-navigation-link__placeholder':
|
|
442
|
+
! url ||
|
|
443
|
+
isInvalid ||
|
|
444
|
+
isDraft ||
|
|
445
|
+
( hasUrlBinding && ! isBoundEntityAvailable ),
|
|
416
446
|
} );
|
|
417
447
|
|
|
418
448
|
const missingText = getMissingText( type );
|
|
@@ -531,9 +561,10 @@ export default function NavigationLinkEdit( {
|
|
|
531
561
|
link={ attributes }
|
|
532
562
|
onClose={ () => {
|
|
533
563
|
setIsLinkOpen( false );
|
|
534
|
-
// If there is no link
|
|
564
|
+
// If there is no link and no binding, remove the auto-inserted block.
|
|
535
565
|
// This avoids empty blocks which can provided a poor UX.
|
|
536
|
-
if (
|
|
566
|
+
// Don't remove if binding exists (even if entity is unavailable) so user can fix it.
|
|
567
|
+
if ( ! url && ! hasUrlBinding ) {
|
|
537
568
|
onReplace( [] );
|
|
538
569
|
} else if ( isNewLink.current ) {
|
|
539
570
|
// If we just created a new link, select it
|
|
@@ -312,11 +312,50 @@ function build_variation_for_navigation_link( $entity, $kind ) {
|
|
|
312
312
|
$title = '';
|
|
313
313
|
$description = '';
|
|
314
314
|
|
|
315
|
+
// Get default labels based on entity type
|
|
316
|
+
$default_labels = null;
|
|
317
|
+
if ( $entity instanceof WP_Post_Type ) {
|
|
318
|
+
$default_labels = WP_Post_Type::get_default_labels();
|
|
319
|
+
} elseif ( $entity instanceof WP_Taxonomy ) {
|
|
320
|
+
$default_labels = WP_Taxonomy::get_default_labels();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Get title and check if it's default
|
|
324
|
+
$is_default_title = false;
|
|
315
325
|
if ( property_exists( $entity->labels, 'item_link' ) ) {
|
|
316
326
|
$title = $entity->labels->item_link;
|
|
327
|
+
if ( isset( $default_labels['item_link'] ) ) {
|
|
328
|
+
$is_default_title = in_array( $title, $default_labels['item_link'], true );
|
|
329
|
+
}
|
|
317
330
|
}
|
|
331
|
+
|
|
332
|
+
// Get description and check if it's default
|
|
333
|
+
$is_default_description = false;
|
|
318
334
|
if ( property_exists( $entity->labels, 'item_link_description' ) ) {
|
|
319
335
|
$description = $entity->labels->item_link_description;
|
|
336
|
+
if ( isset( $default_labels['item_link_description'] ) ) {
|
|
337
|
+
$is_default_description = in_array( $description, $default_labels['item_link_description'], true );
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Calculate singular name once (used for both title and description)
|
|
342
|
+
$singular = isset( $entity->labels->singular_name ) ? $entity->labels->singular_name : ucfirst( $entity->name );
|
|
343
|
+
|
|
344
|
+
// Set default title if needed
|
|
345
|
+
if ( $is_default_title || '' === $title ) {
|
|
346
|
+
/* translators: %s: Singular label of the entity. */
|
|
347
|
+
$title = sprintf( __( '%s link' ), $singular );
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Default description if needed.
|
|
351
|
+
// Use a single space character instead of an empty string to prevent fallback to the
|
|
352
|
+
// block.json default description ("Add a page, link, or another item to your navigation.").
|
|
353
|
+
// An empty string would be treated as missing and trigger the fallback, while a single
|
|
354
|
+
// space appears blank in the UI but prevents the fallback behavior.
|
|
355
|
+
// We avoid generating descriptions like "A link to a %s" to prevent grammatical errors
|
|
356
|
+
// (e.g., "A link to a event" should be "A link to an event").
|
|
357
|
+
if ( $is_default_description || '' === $description ) {
|
|
358
|
+
$description = ' ';
|
|
320
359
|
}
|
|
321
360
|
|
|
322
361
|
$variation = array(
|
|
@@ -368,6 +407,7 @@ function build_variation_for_navigation_link( $entity, $kind ) {
|
|
|
368
407
|
*
|
|
369
408
|
* @param array $variations Array of registered variations for a block type.
|
|
370
409
|
* @param WP_Block_Type $block_type The full block type object.
|
|
410
|
+
* @return array Numerically indexed array of block variations.
|
|
371
411
|
*/
|
|
372
412
|
function block_core_navigation_link_filter_variations( $variations, $block_type ) {
|
|
373
413
|
if ( 'core/navigation-link' !== $block_type->name ) {
|
|
@@ -375,7 +415,28 @@ function block_core_navigation_link_filter_variations( $variations, $block_type
|
|
|
375
415
|
}
|
|
376
416
|
|
|
377
417
|
$generated_variations = block_core_navigation_link_build_variations();
|
|
378
|
-
|
|
418
|
+
|
|
419
|
+
/*
|
|
420
|
+
* IMPORTANT: Order matters for deduplication.
|
|
421
|
+
*
|
|
422
|
+
* The variations returned from this filter are bootstrapped to JavaScript and
|
|
423
|
+
* processed by the block variations reducer. The reducer uses `getUniqueItemsByName()`
|
|
424
|
+
* (packages/blocks/src/store/reducer.js:51-57) which keeps the FIRST variation with
|
|
425
|
+
* a given 'name' and discards later duplicates when processing the array in order.
|
|
426
|
+
*
|
|
427
|
+
* By placing generated variations first in `array_merge()`, the improved
|
|
428
|
+
* labels (e.g., "Product link" instead of generic "Post Link") are processed first
|
|
429
|
+
* and preserved. The generic incoming variations are then discarded as duplicates.
|
|
430
|
+
*
|
|
431
|
+
* Why `array_merge()` instead of manual deduplication?
|
|
432
|
+
* - Both arrays use numeric indices (0, 1, 2...), so `array_merge()` concatenates
|
|
433
|
+
* and re-indexes them sequentially, preserving order
|
|
434
|
+
* - The reducer handles deduplication, so it is not needed here
|
|
435
|
+
* - This keeps the PHP code simple and relies on the established JavaScript behavior
|
|
436
|
+
*
|
|
437
|
+
* See: https://github.com/WordPress/gutenberg/pull/72517
|
|
438
|
+
*/
|
|
439
|
+
return array_merge( $generated_variations, $variations );
|
|
379
440
|
}
|
|
380
441
|
|
|
381
442
|
/**
|
|
@@ -419,7 +480,9 @@ function block_core_navigation_link_build_variations() {
|
|
|
419
480
|
}
|
|
420
481
|
}
|
|
421
482
|
|
|
422
|
-
|
|
483
|
+
$all_variations = array_merge( $built_ins, $variations );
|
|
484
|
+
|
|
485
|
+
return $all_variations;
|
|
423
486
|
}
|
|
424
487
|
|
|
425
488
|
/**
|
|
@@ -26,6 +26,7 @@ import { useInstanceId } from '@wordpress/compose';
|
|
|
26
26
|
*/
|
|
27
27
|
import { LinkUIPageCreator } from './page-creator';
|
|
28
28
|
import LinkUIBlockInserter from './block-inserter';
|
|
29
|
+
import { useEntityBinding } from '../shared/use-entity-binding';
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* Given the Link block's type attribute, return the query params to give to
|
|
@@ -66,7 +67,8 @@ export function getSuggestionsQuery( type, kind ) {
|
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
function UnforwardedLinkUI( props, ref ) {
|
|
69
|
-
const { label, url, opensInNewTab, type, kind, id
|
|
70
|
+
const { label, url, opensInNewTab, type, kind, id } = props.link;
|
|
71
|
+
const { clientId } = props;
|
|
70
72
|
const postType = type || 'page';
|
|
71
73
|
|
|
72
74
|
const [ addingBlock, setAddingBlock ] = useState( false );
|
|
@@ -78,12 +80,11 @@ function UnforwardedLinkUI( props, ref ) {
|
|
|
78
80
|
name: postType,
|
|
79
81
|
} );
|
|
80
82
|
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
!! id;
|
|
83
|
+
// Use the entity binding hook to get binding status
|
|
84
|
+
const { isBoundEntityAvailable } = useEntityBinding( {
|
|
85
|
+
clientId,
|
|
86
|
+
attributes: props.link,
|
|
87
|
+
} );
|
|
87
88
|
|
|
88
89
|
// Memoize link value to avoid overriding the LinkControl's internal state.
|
|
89
90
|
// This is a temporary fix. See https://github.com/WordPress/gutenberg/issues/50976#issuecomment-1568226407.
|
|
@@ -152,7 +153,7 @@ function UnforwardedLinkUI( props, ref ) {
|
|
|
152
153
|
onChange={ props.onChange }
|
|
153
154
|
onRemove={ props.onRemove }
|
|
154
155
|
onCancel={ props.onCancel }
|
|
155
|
-
handleEntities={
|
|
156
|
+
handleEntities={ isBoundEntityAvailable }
|
|
156
157
|
renderControlBottom={ () => {
|
|
157
158
|
// Don't show the tools when there is submitted link (preview state).
|
|
158
159
|
if ( link?.url?.length ) {
|