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

Updates iconSvg to allow for alternative text and adds alt text for multi-site globe icon #16128

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Accessibility
- Improved the accessibility of Checkboxes and Radio Buttons fields that allow custom options. ([#16080](https://github.com/craftcms/cms/pull/16080))
- Improved the accessibility of control panel icons. ([#16128](https://github.com/craftcms/cms/pull/16128))

### Administration
- Added the “Show the ‘URL Suffix’ field” setting to Link fields. ([#15813](https://github.com/craftcms/cms/discussions/15813))
Expand Down
25 changes: 20 additions & 5 deletions src/helpers/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3223,13 +3223,17 @@ public static function siteMenuItems(
*
* @param string $icon
* @param string|null $fallbackLabel
* @param string|null $altText
* @return string
* @since 5.0.0
*/
public static function iconSvg(string $icon, ?string $fallbackLabel = null): string
public static function iconSvg(string $icon, ?string $fallbackLabel = null, ?string $altText = null): string
{
$locale = Craft::$app->getLocale();
$orientation = $locale->getOrientation();
$attributes = [
'focusable' => 'false',
];

// BC support for some legacy icon names
$icon = match ($icon) {
Expand Down Expand Up @@ -3318,11 +3322,22 @@ public static function iconSvg(string $icon, ?string $fallbackLabel = null): str
return self::fallbackIconSvg($fallbackLabel);
}

// Add aria-hidden="true"
if ($altText !== null) {
$attributes['aria'] = ['label' => $altText];
$attributes['role'] = 'img';
} else {
$attributes['aria'] = [
'hidden' => 'true',
'labelledby' => false,
];
}

// Remove title tag
$svg = preg_replace(Html::TITLE_TAG_RE, '', $svg);

// Add attributes for accessibility
try {
$svg = Html::modifyTagAttributes($svg, [
'aria' => ['hidden' => 'true'],
]);
$svg = Html::modifyTagAttributes($svg, $attributes);
} catch (InvalidArgumentException) {
}

Expand Down
7 changes: 6 additions & 1 deletion src/helpers/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
*/
class Html extends \yii\helpers\Html
{
/**
* @since 5.6.0
*/
public const TITLE_TAG_RE = '/<title(\s+([\s\S]*?))?>.*?<\/title>\s*/is';

/**
* @var array List of tag attributes that should be specially handled when their values are of array type.
* In particular, if the value of the `data` attribute is `['name' => 'xyz', 'age' => 13]`, two attributes
Expand Down Expand Up @@ -999,7 +1004,7 @@ public static function sanitizeSvg(string $svg): string
$svg = $sanitizer->sanitize($svg);
// Remove comments, title & desc
$svg = preg_replace('/<!--.*?-->\s*/s', '', $svg);
$svg = preg_replace('/<title>.*?<\/title>\s*/is', '', $svg);
$svg = preg_replace(self::TITLE_TAG_RE, '', $svg);
return preg_replace('/<desc>.*?<\/desc>\s*/is', '', $svg);
}

Expand Down
3 changes: 2 additions & 1 deletion src/templates/_layouts/components/crumbs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
href: crumb.url is defined ? url(crumb.url) : null,
} %}
{% if crumb.icon is defined %}
<span class="cp-icon puny" aria-hidden="true">{{ iconSvg(crumb.icon) }}</span>
{% set iconAltText = crumb.iconAltText ?? null %}
<span class="cp-icon puny">{{ iconSvg(crumb.icon, altText: iconAltText) }}</span>
{% endif %}

<span>{{ crumb.label ?? crumb.html|raw }}</span>
Expand Down
1 change: 1 addition & 0 deletions src/templates/_layouts/elementindex.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
{% set crumbs = (crumbs ?? [])|unshift({
id: 'site-crumb',
icon: 'world',
iconAltText: 'Site'|t('app'),
label: selectedSite.name|t('site'),
menu: {
items: siteMenuItems(selectableSites, selectedSite),
Expand Down