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

Filter certain DOM attributes (e.g. src) if value is empty string #18513

Merged
merged 4 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
87 changes: 87 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,93 @@ describe('ReactDOMComponent', () => {
expect(node.hasAttribute('data-foo')).toBe(false);
});

if (ReactFeatureFlags.enableFilterEmptyStringAttributesDOM) {
it('should not add an empty src attribute', () => {
const container = document.createElement('div');
expect(() => ReactDOM.render(<img src="" />, container)).toErrorDev(
'Invalid value "" (empty string) for attribute `src`.',
);
const node = container.firstChild;
expect(node.hasAttribute('src')).toBe(false);

ReactDOM.render(<img src="abc" />, container);
expect(node.hasAttribute('src')).toBe(true);

expect(() => ReactDOM.render(<img src="" />, container)).toErrorDev(
'Invalid value "" (empty string) for attribute `src`.',
);
expect(node.hasAttribute('src')).toBe(false);
});

it('should not add an empty href attribute', () => {
const container = document.createElement('div');
expect(() => ReactDOM.render(<link href="" />, container)).toErrorDev(
'Invalid value "" (empty string) for attribute `href`.',
);
const node = container.firstChild;
expect(node.hasAttribute('href')).toBe(false);

ReactDOM.render(<link href="abc" />, container);
expect(node.hasAttribute('href')).toBe(true);

expect(() => ReactDOM.render(<link href="" />, container)).toErrorDev(
'Invalid value "" (empty string) for attribute `href`.',
);
expect(node.hasAttribute('href')).toBe(false);
});

it('should not add an empty action attribute', () => {
const container = document.createElement('div');
expect(() => ReactDOM.render(<form action="" />, container)).toErrorDev(
'Invalid value "" (empty string) for attribute `action`.',
);
const node = container.firstChild;
expect(node.hasAttribute('action')).toBe(false);

ReactDOM.render(<form action="abc" />, container);
expect(node.hasAttribute('action')).toBe(true);

expect(() => ReactDOM.render(<form action="" />, container)).toErrorDev(
'Invalid value "" (empty string) for attribute `action`.',
);
expect(node.hasAttribute('action')).toBe(false);
});

it('should not add an empty formAction attribute', () => {
const container = document.createElement('div');
expect(() =>
ReactDOM.render(<button formAction="" />, container),
).toErrorDev(
'Invalid value "" (empty string) for attribute `formAction`.',
);
const node = container.firstChild;
expect(node.hasAttribute('formAction')).toBe(false);

ReactDOM.render(<button formAction="abc" />, container);
expect(node.hasAttribute('formAction')).toBe(true);

expect(() =>
ReactDOM.render(<button formAction="" />, container),
).toErrorDev(
'Invalid value "" (empty string) for attribute `formAction`.',
);
expect(node.hasAttribute('formAction')).toBe(false);
});

it('should not filter attributes for custom elements', () => {
const container = document.createElement('div');
ReactDOM.render(
<some-custom-element action="" formAction="" href="" src="" />,
container,
);
const node = container.firstChild;
expect(node.hasAttribute('action')).toBe(true);
expect(node.hasAttribute('formAction')).toBe(true);
expect(node.hasAttribute('href')).toBe(true);
expect(node.hasAttribute('src')).toBe(true);
});
}

it('should apply React-specific aliases to HTML elements', () => {
const container = document.createElement('div');
ReactDOM.render(<form acceptCharset="foo" />, container);
Expand Down
36 changes: 35 additions & 1 deletion packages/react-dom/src/shared/DOMProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
* @flow
*/

import {enableDeprecatedFlareAPI} from 'shared/ReactFeatureFlags';
import {
enableDeprecatedFlareAPI,
enableFilterEmptyStringAttributesDOM,
} from 'shared/ReactFeatureFlags';

type PropertyType = 0 | 1 | 2 | 3 | 4 | 5 | 6;

Expand Down Expand Up @@ -52,6 +55,7 @@ export type PropertyInfo = {|
+propertyName: string,
+type: PropertyType,
+sanitizeURL: boolean,
+removeEmptyString: boolean,
|};

/* eslint-disable max-len */
Expand Down Expand Up @@ -163,6 +167,19 @@ export function shouldRemoveAttribute(
return false;
}
if (propertyInfo !== null) {
if (enableFilterEmptyStringAttributesDOM) {
if (propertyInfo.removeEmptyString && value === '') {
if (__DEV__) {
console.error(
'Invalid value "" (empty string) for attribute `%s`. ' +
'Use `null` instead to indicate an empty value.',
name,
);
}
return true;
}
}

switch (propertyInfo.type) {
case BOOLEAN:
return !value;
Expand All @@ -188,6 +205,7 @@ function PropertyInfoRecord(
attributeName: string,
attributeNamespace: string | null,
sanitizeURL: boolean,
removeEmptyString: boolean,
) {
this.acceptsBooleans =
type === BOOLEANISH_STRING ||
Expand All @@ -199,6 +217,7 @@ function PropertyInfoRecord(
this.propertyName = name;
this.type = type;
this.sanitizeURL = sanitizeURL;
this.removeEmptyString = removeEmptyString;
}

// When adding attributes to this list, be sure to also add them to
Expand Down Expand Up @@ -232,6 +251,7 @@ reservedProps.forEach(name => {
name, // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -250,6 +270,7 @@ reservedProps.forEach(name => {
attributeName, // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -264,6 +285,7 @@ reservedProps.forEach(name => {
name.toLowerCase(), // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -284,6 +306,7 @@ reservedProps.forEach(name => {
name, // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand Down Expand Up @@ -322,6 +345,7 @@ reservedProps.forEach(name => {
name.toLowerCase(), // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -346,6 +370,7 @@ reservedProps.forEach(name => {
name, // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -366,6 +391,7 @@ reservedProps.forEach(name => {
name, // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -387,6 +413,7 @@ reservedProps.forEach(name => {
name, // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -399,6 +426,7 @@ reservedProps.forEach(name => {
name.toLowerCase(), // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand Down Expand Up @@ -497,6 +525,7 @@ const capitalize = token => token[1].toUpperCase();
attributeName,
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -521,6 +550,7 @@ const capitalize = token => token[1].toUpperCase();
attributeName,
'http://www.w3.org/1999/xlink',
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -542,6 +572,7 @@ const capitalize = token => token[1].toUpperCase();
attributeName,
'http://www.w3.org/XML/1998/namespace',
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -556,6 +587,7 @@ const capitalize = token => token[1].toUpperCase();
attributeName.toLowerCase(), // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

Expand All @@ -569,6 +601,7 @@ properties[xlinkHref] = new PropertyInfoRecord(
'xlink:href',
'http://www.w3.org/1999/xlink',
true, // sanitizeURL
false, // removeEmptyString
);

['src', 'href', 'action', 'formAction'].forEach(attributeName => {
Expand All @@ -579,5 +612,6 @@ properties[xlinkHref] = new PropertyInfoRecord(
attributeName.toLowerCase(), // attributeName
null, // attributeNamespace
true, // sanitizeURL
true, // removeEmptyString
);
});
4 changes: 4 additions & 0 deletions packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* @flow strict
*/

// Filter certain DOM attributes (e.g. src, href) if their values are empty strings.
// This prevents e.g. <img src=""> from making an unnecessar HTTP request for certain browsers.
export const enableFilterEmptyStringAttributesDOM = false;

// Helps identify side effects in render-phase lifecycle hooks and setState
// reducers by double invoking them in Strict Mode.
export const debugRenderPhaseSideEffectsForStrictMode = __DEV__;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.native-fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const enableModernEventSystem = false;
export const warnAboutSpreadingKeyToJSX = false;
export const enableComponentStackLocations = false;
export const enableLegacyFBSupport = false;
export const enableFilterEmptyStringAttributesDOM = false;

// Internal-only attempt to debug a React Native issue. See D20130868.
export const throwEarlyForMysteriousError = true;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.native-oss.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const enableModernEventSystem = false;
export const warnAboutSpreadingKeyToJSX = false;
export const enableComponentStackLocations = false;
export const enableLegacyFBSupport = false;
export const enableFilterEmptyStringAttributesDOM = false;

// Internal-only attempt to debug a React Native issue. See D20130868.
export const throwEarlyForMysteriousError = false;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.test-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const enableModernEventSystem = false;
export const warnAboutSpreadingKeyToJSX = false;
export const enableComponentStackLocations = false;
export const enableLegacyFBSupport = false;
export const enableFilterEmptyStringAttributesDOM = false;

// Internal-only attempt to debug a React Native issue. See D20130868.
export const throwEarlyForMysteriousError = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const enableModernEventSystem = false;
export const warnAboutSpreadingKeyToJSX = false;
export const enableComponentStackLocations = false;
export const enableLegacyFBSupport = false;
export const enableFilterEmptyStringAttributesDOM = false;

// Internal-only attempt to debug a React Native issue. See D20130868.
export const throwEarlyForMysteriousError = false;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const enableModernEventSystem = false;
export const warnAboutSpreadingKeyToJSX = false;
export const enableComponentStackLocations = false;
export const enableLegacyFBSupport = false;
export const enableFilterEmptyStringAttributesDOM = false;

// Internal-only attempt to debug a React Native issue. See D20130868.
export const throwEarlyForMysteriousError = false;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.testing.www.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const enableModernEventSystem = false;
export const warnAboutSpreadingKeyToJSX = false;
export const enableComponentStackLocations = false;
export const enableLegacyFBSupport = !__EXPERIMENTAL__;
export const enableFilterEmptyStringAttributesDOM = false;

// Internal-only attempt to debug a React Native issue. See D20130868.
export const throwEarlyForMysteriousError = false;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.www-dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const warnAboutSpreadingKeyToJSX = __VARIANT__;
export const enableComponentStackLocations = __VARIANT__;
export const disableModulePatternComponents = __VARIANT__;
export const disableInputAttributeSyncing = __VARIANT__;
export const enableFilterEmptyStringAttributesDOM = __VARIANT__;

// These are already tested in both modes using the build type dimension,
// so we don't need to use __VARIANT__ to get extra coverage.
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.www.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const {
enableComponentStackLocations,
replayFailedUnitOfWorkWithInvokeGuardedCallback,
enableModernEventSystem,
enableFilterEmptyStringAttributesDOM,
} = dynamicFeatureFlags;

// On WWW, __EXPERIMENTAL__ is used for a new modern build.
Expand Down