Skip to content

Commit 6d7a1ea

Browse files
committed
[change] Remove createElement host type inference for 'href'
Elements other than 'a' support the 'href' attribute, so the element type should not be inferred from 'href'. Longer term no host element type inference should be performed by 'createElement'. Fix necolas#1958
1 parent 7775b3a commit 6d7a1ea

File tree

11 files changed

+67
-41
lines changed

11 files changed

+67
-41
lines changed

packages/react-native-web/src/exports/Pressable/__tests__/__snapshots__/index-test.js.snap

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ exports[`components/Pressable prop "accessibilityLiveRegion" value is set 1`] =
106106
`;
107107

108108
exports[`components/Pressable prop "accessibilityRole" value alters HTML element 1`] = `
109-
<a
110-
class="css-reset-4rbku5 css-cursor-18t94o4 css-view-1dbjc4n r-cursor-1loqt21 r-touchAction-1otgn73"
111-
role="link"
109+
<article
110+
class="css-view-1dbjc4n r-cursor-1loqt21 r-touchAction-1otgn73"
111+
role="article"
112+
tabindex="0"
112113
/>
113114
`;
114115

@@ -136,6 +137,13 @@ exports[`components/Pressable prop "disabled" 1`] = `
136137
/>
137138
`;
138139

140+
exports[`components/Pressable prop "href" 1`] = `
141+
<a
142+
class="css-reset-4rbku5 css-view-1dbjc4n r-cursor-1loqt21 r-touchAction-1otgn73"
143+
href="#href"
144+
/>
145+
`;
146+
139147
exports[`components/Pressable prop "nativeID" value is set 1`] = `
140148
<div
141149
class="css-view-1dbjc4n r-cursor-1loqt21 r-touchAction-1otgn73"

packages/react-native-web/src/exports/Pressable/__tests__/index-test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('components/Pressable', () => {
3838
});
3939

4040
test('value alters HTML element', () => {
41-
const { container } = render(<Pressable accessibilityRole="link" />);
41+
const { container } = render(<Pressable accessibilityRole="article" />);
4242
expect(container.firstChild).toMatchSnapshot();
4343
});
4444
});
@@ -48,6 +48,11 @@ describe('components/Pressable', () => {
4848
expect(container.firstChild).toMatchSnapshot();
4949
});
5050

51+
test('prop "href"', () => {
52+
const { container } = render(<Pressable href="#href" />);
53+
expect(container.firstChild).toMatchSnapshot();
54+
});
55+
5156
describe('prop "nativeID"', () => {
5257
test('value is set', () => {
5358
const { container } = render(<Pressable nativeID="nativeID" />);

packages/react-native-web/src/exports/Text/__tests__/__snapshots__/index-test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ exports[`components/Text prop "accessibilityLiveRegion" value is set 1`] = `
5151
`;
5252

5353
exports[`components/Text prop "accessibilityRole" value alters HTML element 1`] = `
54-
<a
55-
class="css-reset-4rbku5 css-cursor-18t94o4 css-text-901oao"
54+
<article
55+
class="css-text-901oao"
5656
dir="auto"
57-
role="link"
57+
role="article"
5858
/>
5959
`;
6060

packages/react-native-web/src/exports/Text/__tests__/index-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('components/Text', () => {
5151
});
5252

5353
test('value alters HTML element', () => {
54-
const { container } = render(<Text accessibilityRole="link" />);
54+
const { container } = render(<Text accessibilityRole="article" />);
5555
expect(container.firstChild).toMatchSnapshot();
5656
});
5757
});

packages/react-native-web/src/exports/Text/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const Text: React.AbstractComponent<TextProps, HTMLElement & PlatformMethods> =
114114
}
115115
}
116116

117-
const component = hasTextAncestor ? 'span' : 'div';
117+
let component = hasTextAncestor ? 'span' : 'div';
118118
const supportedProps = pickProps(props);
119119
supportedProps.classList = classList;
120120
supportedProps.dir = dir;
@@ -124,16 +124,19 @@ const Text: React.AbstractComponent<TextProps, HTMLElement & PlatformMethods> =
124124
}
125125
supportedProps.onClick = handleClick;
126126
supportedProps.style = style;
127-
if (props.href != null && hrefAttrs != null) {
128-
const { download, rel, target } = hrefAttrs;
129-
if (download != null) {
130-
supportedProps.download = download;
131-
}
132-
if (rel != null) {
133-
supportedProps.rel = rel;
134-
}
135-
if (typeof target === 'string') {
136-
supportedProps.target = target.charAt(0) !== '_' ? '_' + target : target;
127+
if (props.href != null) {
128+
component = 'a';
129+
if (hrefAttrs != null) {
130+
const { download, rel, target } = hrefAttrs;
131+
if (download != null) {
132+
supportedProps.download = download;
133+
}
134+
if (rel != null) {
135+
supportedProps.rel = rel;
136+
}
137+
if (typeof target === 'string') {
138+
supportedProps.target = target.charAt(0) !== '_' ? '_' + target : target;
139+
}
137140
}
138141
}
139142

packages/react-native-web/src/exports/View/__tests__/__snapshots__/index-test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ exports[`components/View prop "accessibilityLiveRegion" value is set 1`] = `
4646
`;
4747

4848
exports[`components/View prop "accessibilityRole" value alters HTML element 1`] = `
49-
<a
50-
class="css-reset-4rbku5 css-cursor-18t94o4 css-view-1dbjc4n"
51-
role="link"
49+
<article
50+
class="css-view-1dbjc4n"
51+
role="article"
5252
/>
5353
`;
5454

packages/react-native-web/src/exports/View/__tests__/index-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('components/View', () => {
7979
});
8080

8181
test('value alters HTML element', () => {
82-
const { container } = render(<View accessibilityRole="link" />);
82+
const { container } = render(<View accessibilityRole="article" />);
8383
expect(container.firstChild).toMatchSnapshot();
8484
});
8585
});

packages/react-native-web/src/exports/View/index.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,25 @@ const View: React.AbstractComponent<ViewProps, HTMLElement & PlatformMethods> =
9797
onStartShouldSetResponderCapture
9898
});
9999

100+
let component = 'div';
100101
const style = StyleSheet.compose(hasTextAncestor && styles.inline, props.style);
101102

102103
const supportedProps = pickProps(props);
103104
supportedProps.classList = classList;
104105
supportedProps.style = style;
105-
if (props.href != null && hrefAttrs != null) {
106-
const { download, rel, target } = hrefAttrs;
107-
if (download != null) {
108-
supportedProps.download = download;
109-
}
110-
if (rel != null) {
111-
supportedProps.rel = rel;
112-
}
113-
if (typeof target === 'string') {
114-
supportedProps.target = target.charAt(0) !== '_' ? '_' + target : target;
106+
if (props.href != null) {
107+
component = 'a';
108+
if (hrefAttrs != null) {
109+
const { download, rel, target } = hrefAttrs;
110+
if (download != null) {
111+
supportedProps.download = download;
112+
}
113+
if (rel != null) {
114+
supportedProps.rel = rel;
115+
}
116+
if (typeof target === 'string') {
117+
supportedProps.target = target.charAt(0) !== '_' ? '_' + target : target;
118+
}
115119
}
116120
}
117121

@@ -120,7 +124,7 @@ const View: React.AbstractComponent<ViewProps, HTMLElement & PlatformMethods> =
120124

121125
supportedProps.ref = setRef;
122126

123-
return createElement('div', supportedProps);
127+
return createElement(component, supportedProps);
124128
}
125129
);
126130

packages/react-native-web/src/exports/createElement/__tests__/__snapshots__/index-test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@
33
exports[`exports/createElement renders different DOM elements 1`] = `<span />`;
44

55
exports[`exports/createElement renders different DOM elements 2`] = `<main />`;
6+
7+
exports[`exports/createElement renders different DOM elements 3`] = `
8+
<svg>
9+
<image
10+
href="#href"
11+
/>
12+
</svg>
13+
`;

packages/react-native-web/src/exports/createElement/__tests__/index-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ describe('exports/createElement', () => {
1818
expect(container.firstChild).toMatchSnapshot();
1919
({ container } = render(createElement('main')));
2020
expect(container.firstChild).toMatchSnapshot();
21+
({ container } = render(
22+
createElement('svg', { children: createElement('image', { href: '#href' }) })
23+
));
24+
expect(container.firstChild).toMatchSnapshot();
2125
});
2226

2327
describe('prop "accessibilityRole"', () => {
2428
test('string component type', () => {
2529
const { container } = render(createElement('span', { accessibilityRole: 'link' }));
26-
expect(container.firstChild.nodeName).toBe('A');
30+
expect(container.firstChild.nodeName).toBe('SPAN');
2731
});
2832

2933
test('function component type', () => {

0 commit comments

Comments
 (0)