From 8b9edc4e712f3adbd9f9c503dfc5e4d627f763cd Mon Sep 17 00:00:00 2001
From: Jeff Wen <3297859+sinchang@users.noreply.github.com>
Date: Wed, 28 Sep 2022 14:55:10 +0800
Subject: [PATCH] feat(a11y): add attribute role="img" to the svg element
(#750)
---
packages/babel-preset/src/index.test.ts | 24 ++--
packages/babel-preset/src/index.ts | 7 +-
.../cli/src/__snapshots__/index.test.ts.snap | 132 +++++++++++++++---
.../cli/src/__snapshots__/util.test.ts.snap | 9 +-
.../src/__snapshots__/transform.test.ts.snap | 109 +++++++++++++--
packages/plugin-jsx/src/index.test.ts | 8 +-
.../src/__snapshots__/index.test.ts.snap | 9 +-
.../src/__snapshots__/index.test.ts.snap | 15 +-
8 files changed, 253 insertions(+), 60 deletions(-)
diff --git a/packages/babel-preset/src/index.test.ts b/packages/babel-preset/src/index.test.ts
index 8460c9fe..18945f50 100644
--- a/packages/babel-preset/src/index.test.ts
+++ b/packages/babel-preset/src/index.test.ts
@@ -28,7 +28,7 @@ describe('preset', () => {
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
- const SvgComponent = () => ;
+ const SvgComponent = () => ;
export default SvgComponent;"
`)
@@ -45,7 +45,7 @@ describe('preset', () => {
const SvgComponent = ({
title,
titleId
- }) => ;
+ }) => ;
export default SvgComponent;"
`)
@@ -62,7 +62,7 @@ describe('preset', () => {
const SvgComponent = ({
title,
titleId
- }) => ;
+ }) => ;
export default SvgComponent;"
`)
@@ -77,7 +77,7 @@ describe('preset', () => {
const SvgComponent = ({
title,
titleId
- }) => ;
+ }) => ;
export default SvgComponent;"
`)
@@ -94,7 +94,7 @@ describe('preset', () => {
const SvgComponent = ({
desc,
descId
- }) => ;
+ }) => ;
export default SvgComponent;"
`)
@@ -111,7 +111,7 @@ describe('preset', () => {
const SvgComponent = ({
desc,
descId
- }) => ;
+ }) => ;
export default SvgComponent;"
`)
@@ -126,7 +126,7 @@ describe('preset', () => {
const SvgComponent = ({
desc,
descId
- }) => ;
+ }) => ;
export default SvgComponent;"
`)
@@ -146,7 +146,7 @@ describe('preset', () => {
titleId,
desc,
descId
- }) => ;
+ }) => ;
export default SvgComponent;"
`)
@@ -163,7 +163,7 @@ describe('preset', () => {
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
- const SvgComponent = () => ;
+ const SvgComponent = () => ;
export default SvgComponent;"
`)
@@ -179,7 +179,7 @@ describe('preset', () => {
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
- const SvgComponent = props => ;
+ const SvgComponent = props => ;
export default SvgComponent;"
`)
@@ -195,7 +195,7 @@ describe('preset', () => {
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
- const SvgComponent = props => ;
+ const SvgComponent = props => ;
export default SvgComponent;"
`)
@@ -213,7 +213,7 @@ describe('preset', () => {
"import * as React from \\"react\\";
import Svg from \\"react-native-svg\\";
- const SvgComponent = props => ;
+ const SvgComponent = props => ;
export default SvgComponent;"
`)
diff --git a/packages/babel-preset/src/index.ts b/packages/babel-preset/src/index.ts
index c77b2396..7a5b1b39 100644
--- a/packages/babel-preset/src/index.ts
+++ b/packages/babel-preset/src/index.ts
@@ -49,7 +49,12 @@ function replaceMapToValues(replaceMap: { [key: string]: string }): Value[] {
const plugin = (_: ConfigAPI, opts: Options) => {
let toRemoveAttributes = ['version']
- let toAddAttributes: Attribute[] = []
+ let toAddAttributes: Attribute[] = [
+ {
+ name: 'role',
+ value: 'img',
+ },
+ ]
if (opts.svgProps) {
toAddAttributes = [...toAddAttributes, ...propsToAttributes(opts.svgProps)]
diff --git a/packages/cli/src/__snapshots__/index.test.ts.snap b/packages/cli/src/__snapshots__/index.test.ts.snap
index f05c6526..8bbce21b 100644
--- a/packages/cli/src/__snapshots__/index.test.ts.snap
+++ b/packages/cli/src/__snapshots__/index.test.ts.snap
@@ -10,7 +10,7 @@ exports[`cli should not override config with cli defaults 1`] = `
"import * as React from 'react'
const SvgFile = () => (
-