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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 105 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Expand Up @@ -445,6 +445,111 @@ 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(
'An empty string ("") was passed to the src attribute. ' +
'This may cause the browser to download the whole page again over the network. ' +
'To fix this, either do not render the element at all ' +
'or pass null to src instead of an empty string.',
);
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(
'An empty string ("") was passed to the src attribute. ' +
'This may cause the browser to download the whole page again over the network. ' +
'To fix this, either do not render the element at all ' +
'or pass null to src instead of an empty string.',
);
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(
'An empty string ("") was passed to the href attribute. ' +
'To fix this, either do not render the element at all ' +
'or pass null to href instead of an empty string.',
);
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(
'An empty string ("") was passed to the href attribute. ' +
'To fix this, either do not render the element at all ' +
'or pass null to href instead of an empty string.',
);
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(
'An empty string ("") was passed to the action attribute. ' +
'To fix this, either do not render the element at all ' +
'or pass null to action instead of an empty string.',
);
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(
'An empty string ("") was passed to the action attribute. ' +
'To fix this, either do not render the element at all ' +
'or pass null to action instead of an empty string.',
);
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(
'An empty string ("") was passed to the formAction attribute. ' +
'To fix this, either do not render the element at all ' +
'or pass null to formAction instead of an empty string.',
);
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(
'An empty string ("") was passed to the formAction attribute. ' +
'To fix this, either do not render the element at all ' +
'or pass null to formAction instead of an empty string.',
);
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
49 changes: 48 additions & 1 deletion packages/react-dom/src/shared/DOMProperty.js
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,32 @@ export function shouldRemoveAttribute(
return false;
}
if (propertyInfo !== null) {
if (enableFilterEmptyStringAttributesDOM) {
if (propertyInfo.removeEmptyString && value === '') {
if (__DEV__) {
if (name === 'src') {
console.error(
'An empty string ("") was passed to the %s attribute. ' +
'This may cause the browser to download the whole page again over the network. ' +
'To fix this, either do not render the element at all ' +
'or pass null to %s instead of an empty string.',
name,
name,
);
} else {
console.error(
'An empty string ("") was passed to the %s attribute. ' +
'To fix this, either do not render the element at all ' +
'or pass null to %s instead of an empty string.',
name,
name,
);
}
}
return true;
}
}

switch (propertyInfo.type) {
case BOOLEAN:
return !value;
Expand All @@ -188,6 +218,7 @@ function PropertyInfoRecord(
attributeName: string,
attributeNamespace: string | null,
sanitizeURL: boolean,
removeEmptyString: boolean,
) {
this.acceptsBooleans =
type === BOOLEANISH_STRING ||
Expand All @@ -199,6 +230,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 +264,7 @@ reservedProps.forEach(name => {
name, // attributeName
null, // attributeNamespace
false, // sanitizeURL
false, // removeEmptyString
);
});

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

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

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

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

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

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

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

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

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

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

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

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

Expand All @@ -569,6 +614,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 +625,6 @@ properties[xlinkHref] = new PropertyInfoRecord(
attributeName.toLowerCase(), // attributeName
null, // attributeNamespace
true, // sanitizeURL
true, // removeEmptyString
);
});
4 changes: 4 additions & 0 deletions packages/shared/ReactFeatureFlags.js
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
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
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
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
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
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
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
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
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