Skip to content

Commit 81e9a58

Browse files
committed
patch 2: fix image crossorigin, backport facebook#30340
1 parent 89b50d8 commit 81e9a58

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

packages/react-dom/src/client/ReactDOMComponent.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,6 @@ export function setInitialProperties(
525525
listenToNonDelegatedEvent('error', domElement);
526526
props = rawProps;
527527
break;
528-
case 'img':
529528
case 'image':
530529
case 'link':
531530
// We listen to these events in case to ensure emulated bubble
@@ -565,6 +564,68 @@ export function setInitialProperties(
565564
// listeners still fire for the invalid event.
566565
listenToNonDelegatedEvent('invalid', domElement);
567566
break;
567+
// img tags previously were implemented as void elements with non delegated events however Safari (and possibly Firefox)Collapse commentComment on line R1034eps1lon commented on Jul 15, 2024 eps1lonon Jul 15, 2024CollaboratorMore actionsSafari truly is the new IE.React😄React with 😄3gnoff, bthall16 and imagekitioWrite a replyResolve comment
568+
// begin fetching the image as soon as the `src` or `srcSet` property is set and if we set these before other properties
569+
// that can modify the request (such as crossorigin) or the resource fetch (such as sizes) then the browser will load
570+
// the wrong thing or load more than one thing. This implementation ensures src and srcSet are set on the instance last
571+
case 'img': {
572+
listenToNonDelegatedEvent('error', domElement);
573+
listenToNonDelegatedEvent('load', domElement);
574+
// Mostly a port of Void Element logic with special casing to ensure srcset and src are set last
575+
let hasSrc = false;
576+
let hasSrcSet = false;
577+
for (const propKey in rawProps) {
578+
if (!rawProps.hasOwnProperty(propKey)) {
579+
continue;
580+
}
581+
const propValue = rawProps[propKey];
582+
if (propValue == null) {
583+
continue;
584+
}
585+
switch (propKey) {
586+
case 'src':
587+
hasSrc = true;
588+
break;
589+
case 'srcSet':
590+
hasSrcSet = true;
591+
break;
592+
case 'children':
593+
case 'dangerouslySetInnerHTML': {
594+
// TODO: Can we make this a DEV warning to avoid this deny list?
595+
throw new Error(
596+
`${tag} is a void element tag and must neither have \`children\` nor ` +
597+
'use `dangerouslySetInnerHTML`.',
598+
);
599+
}
600+
// defaultChecked and defaultValue are ignored by setProp
601+
default: {
602+
setValueForProperty(
603+
domElement,
604+
propKey,
605+
propValue,
606+
false,
607+
);
608+
}
609+
}
610+
}
611+
if (hasSrcSet) {
612+
setValueForProperty(
613+
domElement,
614+
'srcSet',
615+
rawProps.srcSet,
616+
false
617+
);
618+
}
619+
if (hasSrc) {
620+
setValueForProperty(
621+
domElement,
622+
'src',
623+
rawProps.src,
624+
false,
625+
);
626+
}
627+
return;
628+
}
568629
default:
569630
props = rawProps;
570631
}

0 commit comments

Comments
 (0)