Skip to content

Commit

Permalink
Inline property name string constants
Browse files Browse the repository at this point in the history
This helps typos but it's unneccessarily clever.
  • Loading branch information
sebmarkbage committed Mar 20, 2023
1 parent 8b4c215 commit 2b64767
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ import {
let didWarnInvalidHydration = false;
let didWarnScriptTags = false;

const DANGEROUSLY_SET_INNER_HTML = 'dangerouslySetInnerHTML';
const SUPPRESS_CONTENT_EDITABLE_WARNING = 'suppressContentEditableWarning';
const SUPPRESS_HYDRATION_WARNING = 'suppressHydrationWarning';
const AUTOFOCUS = 'autoFocus';
const CHILDREN = 'children';
const STYLE = 'style';
const HTML = '__html';

let warnedUnknownTags: {
[key: string]: boolean,
};
Expand Down Expand Up @@ -296,7 +288,7 @@ function setInitialDOMProperties(
}
const nextProp = nextProps[propKey];
switch (propKey) {
case STYLE: {
case 'style': {
if (__DEV__) {
if (nextProp) {
// Freeze the next style object so that we can assume it won't be
Expand All @@ -308,8 +300,8 @@ function setInitialDOMProperties(
setValueForStyles(domElement, nextProp);
break;
}
case DANGEROUSLY_SET_INNER_HTML: {
const nextHtml = nextProp ? nextProp[HTML] : undefined;
case 'dangerouslySetInnerHTML': {
const nextHtml = nextProp ? nextProp.__html : undefined;
if (nextHtml != null) {
if (disableIEWorkarounds) {
domElement.innerHTML = nextHtml;
Expand All @@ -319,7 +311,7 @@ function setInitialDOMProperties(
}
break;
}
case CHILDREN: {
case 'children': {
if (typeof nextProp === 'string') {
// Avoid setting initial textContent when the text is empty. In IE11 setting
// textContent on a <textarea> will cause the placeholder to not
Expand Down Expand Up @@ -348,15 +340,15 @@ function setInitialDOMProperties(
}
break;
}
case SUPPRESS_CONTENT_EDITABLE_WARNING:
case SUPPRESS_HYDRATION_WARNING:
case 'suppressContentEditableWarning':
case 'suppressHydrationWarning':
case 'defaultValue': // Reserved
case 'defaultChecked':
case 'innerHTML': {
// Noop
break;
}
case AUTOFOCUS: {
case 'autoFocus': {
// We polyfill it separately on the client during commit.
// We could have excluded it in the property list instead of
// adding a special case here, but then it wouldn't be emitted
Expand Down Expand Up @@ -728,7 +720,7 @@ export function diffProperties(
continue;
}
switch (propKey) {
case STYLE: {
case 'style': {
const lastStyle = lastProps[propKey];
for (styleName in lastStyle) {
if (lastStyle.hasOwnProperty(styleName)) {
Expand All @@ -740,20 +732,20 @@ export function diffProperties(
}
break;
}
case DANGEROUSLY_SET_INNER_HTML:
case CHILDREN: {
case 'dangerouslySetInnerHTML':
case 'children': {
// Noop. This is handled by the clear text mechanism.
break;
}
case SUPPRESS_CONTENT_EDITABLE_WARNING:
case SUPPRESS_HYDRATION_WARNING:
case 'suppressContentEditableWarning':
case 'suppressHydrationWarning':
case 'defaultValue': // Reserved
case 'defaultChecked':
case 'innerHTML': {
// Noop
break;
}
case AUTOFOCUS: {
case 'autoFocus': {
// Noop. It doesn't work on updates anyway.
break;
}
Expand Down Expand Up @@ -790,7 +782,7 @@ export function diffProperties(
continue;
}
switch (propKey) {
case STYLE: {
case 'style': {
if (__DEV__) {
if (nextProp) {
// Freeze the next style object so that we can assume it won't be
Expand Down Expand Up @@ -835,9 +827,9 @@ export function diffProperties(
}
break;
}
case DANGEROUSLY_SET_INNER_HTML: {
const nextHtml = nextProp ? nextProp[HTML] : undefined;
const lastHtml = lastProp ? lastProp[HTML] : undefined;
case 'dangerouslySetInnerHTML': {
const nextHtml = nextProp ? nextProp.__html : undefined;
const lastHtml = lastProp ? lastProp.__html : undefined;
if (nextHtml != null) {
if (lastHtml !== nextHtml) {
(updatePayload = updatePayload || []).push(propKey, nextHtml);
Expand All @@ -848,7 +840,7 @@ export function diffProperties(
}
break;
}
case CHILDREN: {
case 'children': {
if (typeof nextProp === 'string' || typeof nextProp === 'number') {
(updatePayload = updatePayload || []).push(propKey, '' + nextProp);
}
Expand All @@ -870,15 +862,15 @@ export function diffProperties(
}
break;
}
case SUPPRESS_CONTENT_EDITABLE_WARNING:
case SUPPRESS_HYDRATION_WARNING:
case 'suppressContentEditableWarning':
case 'suppressHydrationWarning':
case 'defaultValue': // Reserved
case 'defaultChecked':
case 'innerHTML': {
// Noop
break;
}
case AUTOFOCUS: {
case 'autoFocus': {
// Noop on updates
break;
}
Expand Down Expand Up @@ -912,9 +904,9 @@ export function diffProperties(
}
if (styleUpdates) {
if (__DEV__) {
validateShorthandPropertyCollisionInDev(styleUpdates, nextProps[STYLE]);
validateShorthandPropertyCollisionInDev(styleUpdates, nextProps.style);
}
(updatePayload = updatePayload || []).push(STYLE, styleUpdates);
(updatePayload = updatePayload || []).push('style', styleUpdates);
}
return updatePayload;
}
Expand Down Expand Up @@ -1000,32 +992,32 @@ function diffHydratedCustomComponent(
}
continue;
}
if (rawProps[SUPPRESS_HYDRATION_WARNING] === true) {
if (rawProps.suppressHydrationWarning === true) {
// Don't bother comparing. We're ignoring all these warnings.
continue;
}
// Validate that the properties correspond to their expected values.
let serverValue;
switch (propKey) {
case CHILDREN: // Checked above already
case SUPPRESS_CONTENT_EDITABLE_WARNING:
case SUPPRESS_HYDRATION_WARNING:
case 'children': // Checked above already
case 'suppressContentEditableWarning':
case 'suppressHydrationWarning':
case 'defaultValue':
case 'defaultChecked':
case 'innerHTML':
// Noop
continue;
case DANGEROUSLY_SET_INNER_HTML:
case 'dangerouslySetInnerHTML':
const serverHTML = domElement.innerHTML;
const nextHtml = nextProp ? nextProp[HTML] : undefined;
const nextHtml = nextProp ? nextProp.__html : undefined;
if (nextHtml != null) {
const expectedHTML = normalizeHTML(domElement, nextHtml);
if (expectedHTML !== serverHTML) {
warnForPropDifference(propKey, serverHTML, expectedHTML);
}
}
continue;
case STYLE:
case 'style':
// $FlowFixMe - Should be inferred as not undefined.
extraAttributeNames.delete(propKey);

Expand Down Expand Up @@ -1103,16 +1095,16 @@ function diffHydratedGenericElement(
}
continue;
}
if (rawProps[SUPPRESS_HYDRATION_WARNING] === true) {
if (rawProps.suppressHydrationWarning === true) {
// Don't bother comparing. We're ignoring all these warnings.
continue;
}
// Validate that the properties correspond to their expected values.
let serverValue;
switch (propKey) {
case CHILDREN: // Checked above already
case SUPPRESS_CONTENT_EDITABLE_WARNING:
case SUPPRESS_HYDRATION_WARNING:
case 'children': // Checked above already
case 'suppressContentEditableWarning':
case 'suppressHydrationWarning':
case 'value': // Controlled attributes are not validated
case 'checked': // TODO: Only ignore them on controlled tags.
case 'selected':
Expand All @@ -1121,17 +1113,17 @@ function diffHydratedGenericElement(
case 'innerHTML':
// Noop
continue;
case DANGEROUSLY_SET_INNER_HTML:
case 'dangerouslySetInnerHTML':
const serverHTML = domElement.innerHTML;
const nextHtml = nextProp ? nextProp[HTML] : undefined;
const nextHtml = nextProp ? nextProp.__html : undefined;
if (nextHtml != null) {
const expectedHTML = normalizeHTML(domElement, nextHtml);
if (expectedHTML !== serverHTML) {
warnForPropDifference(propKey, serverHTML, expectedHTML);
}
}
continue;
case STYLE:
case 'style':
// $FlowFixMe - Should be inferred as not undefined.
extraAttributeNames.delete(propKey);

Expand Down Expand Up @@ -1292,7 +1284,7 @@ export function diffHydratedProperties(
// TODO: Should we use domElement.firstChild.nodeValue to compare?
if (typeof children === 'string' || typeof children === 'number') {
if (domElement.textContent !== '' + children) {
if (rawProps[SUPPRESS_HYDRATION_WARNING] !== true) {
if (rawProps.suppressHydrationWarning !== true) {
checkForUnmatchedText(
domElement.textContent,
children,
Expand All @@ -1301,7 +1293,7 @@ export function diffHydratedProperties(
);
}
if (!isConcurrentMode) {
updatePayload = [CHILDREN, children];
updatePayload = ['children', children];
}
}
}
Expand Down Expand Up @@ -1346,7 +1338,7 @@ export function diffHydratedProperties(
if (
// $FlowFixMe - Should be inferred as not undefined.
extraAttributeNames.size > 0 &&
rawProps[SUPPRESS_HYDRATION_WARNING] !== true
rawProps.suppressHydrationWarning !== true
) {
// $FlowFixMe - Should be inferred as not undefined.
warnForExtraAttributes(extraAttributeNames);
Expand Down

0 comments on commit 2b64767

Please sign in to comment.