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

Remove labelled-by attribute from SVG when title attribute is passed #249

Merged
merged 15 commits into from
Aug 14, 2024
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,10 @@ Example result:
<svg
title="Camera"
role="img"
aria-labelledby="svg-inline--title-ajx18rtJBjSu"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 448 512"
>
<title id="svg-inline--title-ajx18rtJBjSu">
<title>
Camera
</title>
<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>
Expand Down
8 changes: 1 addition & 7 deletions src/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,8 @@ public function contents(): string
*/
public function addTitle(string $title): string
{
// generate a random id for the title element
$titleId = 'svg-inline--title-'.Str::random(10);

// create title element
$titleElement = '<title id="'.$titleId.'">'.$title.'</title>';

// add aria-labelledby attribute to svg element
$this->attributes['aria-labelledby'] = $titleId;
$titleElement = '<title>'.$title.'</title>';

// add role attribute to svg element
$this->attributes['role'] = 'img';
Expand Down
3 changes: 1 addition & 2 deletions tests/SvgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function it_can_add_title_tag_if_title_attribute_is_passed()
{
$svg = new Svg('heroicon-s-camera', '<svg></svg>', ['title' => 'Camera']);

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

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

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