Skip to content

Commit 8f787ba

Browse files
aarishgilanidriesvintsjdmghost
authored
Remove labelled-by attribute from SVG when title attribute is passed (#249)
* Add title tag to SVG's if title attribute is set * Update src/Svg.php * Pass tile directly to the method as param * Better variable name for title element * Update regular expression, consider <svg> may be passed with not attributes. * Svg class tests to ensure aria attribute and title element are added correctly. * Add role as img: WCAG best practice * Update tests to check for role attribute * Update README.md - Add Accessibility section * Update README.md * Include usage example * Remove aria-labelledby attribute * Update tests: Remove checks for id and aria-labelledby * Update docs: Examples without aria-labelledby --------- Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Nicky <[email protected]>
1 parent ce2c875 commit 8f787ba

File tree

3 files changed

+3
-11
lines changed

3 files changed

+3
-11
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,10 @@ Example result:
568568
<svg
569569
title="Camera"
570570
role="img"
571-
aria-labelledby="svg-inline--title-ajx18rtJBjSu"
572571
xmlns="http://www.w3.org/2000/svg"
573572
viewBox="0 0 448 512"
574573
>
575-
<title id="svg-inline--title-ajx18rtJBjSu">
574+
<title>
576575
Camera
577576
</title>
578577
<path fill="currentColor" d="M438.6 278.6c12.5-12.5 12.5-32.8 0-45.3l-160-160c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L338.8 224 32 224c-17.7 0-32 14.3-32 32s14.3 32 32 32l306.7 0L233.4 393.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l160-160z"></path>

src/Svg.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,8 @@ public function contents(): string
4343
*/
4444
public function addTitle(string $title): string
4545
{
46-
// generate a random id for the title element
47-
$titleId = 'svg-inline--title-'.Str::random(10);
48-
4946
// create title element
50-
$titleElement = '<title id="'.$titleId.'">'.$title.'</title>';
51-
52-
// add aria-labelledby attribute to svg element
53-
$this->attributes['aria-labelledby'] = $titleId;
47+
$titleElement = '<title>'.$title.'</title>';
5448

5549
// add role attribute to svg element
5650
$this->attributes['role'] = 'img';

tests/SvgTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function it_can_add_title_tag_if_title_attribute_is_passed()
205205
{
206206
$svg = new Svg('heroicon-s-camera', '<svg></svg>', ['title' => 'Camera']);
207207

208-
$this->assertStringContainsString('><title id="svg-inline--title-', $svg->toHtml());
208+
$this->assertStringContainsString('><title', $svg->toHtml());
209209
$this->assertStringContainsString('</title></svg>', $svg->toHtml());
210210
}
211211

@@ -214,7 +214,6 @@ public function it_can_add_aria_labelledby_and_role_attributes_if_title_attribut
214214
{
215215
$svg = new Svg('heroicon-s-camera', '<svg></svg>', ['title' => 'Camera']);
216216

217-
$this->assertStringContainsString('aria-labelledby="svg-inline--title-', $svg->toHtml());
218217
$this->assertStringContainsString('role="img">', $svg->toHtml());
219218
}
220219
}

0 commit comments

Comments
 (0)