Skip to content
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

Strip HTML from gallery image alt text #26574

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/block-library/src/gallery/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { __unstableStripHTML } from '@wordpress/dom';

/**
* Internal dependencies
Expand Down Expand Up @@ -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 );
}
WunderBart marked this conversation as resolved.
Show resolved Hide resolved

const img = (
<img
src={ image.url }
alt={ image.alt !== '' ? image.alt : image.caption }
alt={ alt }
data-id={ image.id }
data-full-url={ image.fullUrl }
data-link={ image.link }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- wp:core/gallery {"ids":[null,null]} -->
<figure class="wp-block-gallery columns-2 is-cropped">
<ul class="blocks-gallery-grid">
<li class="blocks-gallery-item">
<figure>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="Caption with a link" />
<figcaption class="blocks-gallery-item__caption">Caption with a <a href="#">link</a></figcaption>
</figure>
</li>
<li class="blocks-gallery-item">
<figure>
<img src="data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=" alt="Caption without a link" />
<figcaption class="blocks-gallery-item__caption">Caption without a link</figcaption>
</figure>
</li>
</ul>
</figure>
<!-- /wp:core/gallery -->
Original file line number Diff line number Diff line change
@@ -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 <a href=\"#\">link</a>"
},
{
"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": "<figure class=\"wp-block-gallery columns-2 is-cropped\">\n\t<ul class=\"blocks-gallery-grid\">\n\t\t<li class=\"blocks-gallery-item\">\n\t\t\t<figure>\n\t\t\t\t<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"Caption with a link\" />\n\t\t\t\t<figcaption class=\"blocks-gallery-item__caption\">Caption with a <a href=\"#\">link</a></figcaption>\n\t\t\t</figure>\n\t\t</li>\n\t\t<li class=\"blocks-gallery-item\">\n\t\t\t<figure>\n\t\t\t\t<img src=\"data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=\" alt=\"Caption without a link\" />\n\t\t\t\t<figcaption class=\"blocks-gallery-item__caption\">Caption without a link</figcaption>\n\t\t\t</figure>\n\t\t</li>\n\t</ul>\n</figure>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"blockName": "core/gallery",
"attrs": {
"ids": [
null,
null
]
},
"innerBlocks": [],
"innerHTML": "\n<figure class=\"wp-block-gallery columns-2 is-cropped\">\n\t<ul class=\"blocks-gallery-grid\">\n\t\t<li class=\"blocks-gallery-item\">\n\t\t\t<figure>\n\t\t\t\t<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"Caption with a link\" />\n\t\t\t\t<figcaption class=\"blocks-gallery-item__caption\">Caption with a <a href=\"#\">link</a></figcaption>\n\t\t\t</figure>\n\t\t</li>\n\t\t<li class=\"blocks-gallery-item\">\n\t\t\t<figure>\n\t\t\t\t<img src=\"data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=\" alt=\"Caption without a link\" />\n\t\t\t\t<figcaption class=\"blocks-gallery-item__caption\">Caption without a link</figcaption>\n\t\t\t</figure>\n\t\t</li>\n\t</ul>\n</figure>\n",
"innerContent": [
"\n<figure class=\"wp-block-gallery columns-2 is-cropped\">\n\t<ul class=\"blocks-gallery-grid\">\n\t\t<li class=\"blocks-gallery-item\">\n\t\t\t<figure>\n\t\t\t\t<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"Caption with a link\" />\n\t\t\t\t<figcaption class=\"blocks-gallery-item__caption\">Caption with a <a href=\"#\">link</a></figcaption>\n\t\t\t</figure>\n\t\t</li>\n\t\t<li class=\"blocks-gallery-item\">\n\t\t\t<figure>\n\t\t\t\t<img src=\"data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=\" alt=\"Caption without a link\" />\n\t\t\t\t<figcaption class=\"blocks-gallery-item__caption\">Caption without a link</figcaption>\n\t\t\t</figure>\n\t\t</li>\n\t</ul>\n</figure>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:gallery {"ids":[null,null],"linkTo":"none"} -->
<figure class="wp-block-gallery columns-2 is-cropped"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="Caption with a link"/><figcaption class="blocks-gallery-item__caption">Caption with a <a href="#">link</a></figcaption></figure></li><li class="blocks-gallery-item"><figure><img src="data:image/jpeg;base64,/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=" alt="Caption without a link"/><figcaption class="blocks-gallery-item__caption">Caption without a link</figcaption></figure></li></ul></figure>
<!-- /wp:gallery -->