diff --git a/packages/block-library/src/gallery/save.js b/packages/block-library/src/gallery/save.js
index 263055c0681e9..a77a0761cb83e 100644
--- a/packages/block-library/src/gallery/save.js
+++ b/packages/block-library/src/gallery/save.js
@@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
+import { __unstableStripHTML } from '@wordpress/dom';
/**
* Internal dependencies
@@ -40,10 +41,20 @@ export default function save( { attributes } ) {
break;
}
+ // image.caption comes from a RichText component, so it can contain
+ // HTML that we need to strip. In GB 9.2, image.alt would fall back to
+ // image.caption without stripping the HTML, so we also run it through
+ // __unstableStripHTML here.
+ let alt = image.alt || image.caption;
+
+ if ( alt && alt !== __unstableStripHTML( alt ) ) {
+ alt = __unstableStripHTML( alt );
+ }
+
const img = (
+
+
diff --git a/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.json b/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.json
new file mode 100644
index 0000000000000..f4f3f53741bff
--- /dev/null
+++ b/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.json
@@ -0,0 +1,31 @@
+[
+ {
+ "clientId": "_clientId_0",
+ "name": "core/gallery",
+ "isValid": true,
+ "attributes": {
+ "images": [
+ {
+ "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
+ "alt": "Caption with a link",
+ "caption": "Caption with a link"
+ },
+ {
+ "url": "data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=",
+ "alt": "Caption without a link",
+ "caption": "Caption without a link"
+ }
+ ],
+ "ids": [
+ null,
+ null
+ ],
+ "caption": "",
+ "imageCrop": true,
+ "linkTo": "none",
+ "sizeSlug": "large"
+ },
+ "innerBlocks": [],
+ "originalContent": "\n\t\n\t\t- \n\t\t\t\n\t\t\t\t\n\t\t\t\tCaption with a link\n\t\t\t\n\t\t
\n\t\t- \n\t\t\t\n\t\t\t\t\n\t\t\t\tCaption without a link\n\t\t\t\n\t\t
\n\t
\n"
+ }
+]
diff --git a/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.parsed.json b/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.parsed.json
new file mode 100644
index 0000000000000..303050649e0c9
--- /dev/null
+++ b/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.parsed.json
@@ -0,0 +1,25 @@
+[
+ {
+ "blockName": "core/gallery",
+ "attrs": {
+ "ids": [
+ null,
+ null
+ ]
+ },
+ "innerBlocks": [],
+ "innerHTML": "\n\n\t\n\t\t- \n\t\t\t\n\t\t\t\t\n\t\t\t\tCaption with a link\n\t\t\t\n\t\t
\n\t\t- \n\t\t\t\n\t\t\t\t\n\t\t\t\tCaption without a link\n\t\t\t\n\t\t
\n\t
\n\n",
+ "innerContent": [
+ "\n\n\t\n\t\t- \n\t\t\t\n\t\t\t\t\n\t\t\t\tCaption with a link\n\t\t\t\n\t\t
\n\t\t- \n\t\t\t\n\t\t\t\t\n\t\t\t\tCaption without a link\n\t\t\t\n\t\t
\n\t
\n\n"
+ ]
+ },
+ {
+ "blockName": null,
+ "attrs": {},
+ "innerBlocks": [],
+ "innerHTML": "\n",
+ "innerContent": [
+ "\n"
+ ]
+ }
+]
diff --git a/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.serialized.html b/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.serialized.html
new file mode 100644
index 0000000000000..9148ad41cfa2f
--- /dev/null
+++ b/packages/e2e-tests/fixtures/blocks/core__gallery__image-with-caption.serialized.html
@@ -0,0 +1,3 @@
+
+- Caption with a link
- Caption without a link
+