- Notifications
You must be signed in to change notification settings - Fork48.9k
Filter certain DOM attributes (e.g. src) if value is empty string#18513
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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
codesandbox-cibot commentedApr 6, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
This pull request is automatically built and testable inCodeSandbox. To see build info of the built libraries, clickhere or the icon next to each commit SHA. Latest deployment of this branch, based on commit2399b82:
|
sizebot commentedApr 6, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Details of bundled changes.Comparing:3278d24...2399b82 react-dom
ReactDOM: size: 🔺+0.3%, gzip: 🔺+0.3% Size changes (experimental) |
sizebot commentedApr 6, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Details of bundled changes.Comparing:3278d24...2399b82 react-dom
Size changes (stable) |
Is |
sebmarkbage commentedApr 6, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
SSR should probably just work, in the sense that it just removes it from the HTML, since it calls this.
You could add some tests to the server integration tests. Note sure if that causes problems in practice but I can't think of one other than maybe |
) { | ||
return true; | ||
} | ||
} |
sebmarkbageApr 6, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This should move below line 178 here into thepropertyInfo !== null
check so we only do that once.
We also probably should not filter this for isCustomComponentTag since custom elements could have all kinds of semantics where this matters. We only try to fix the built-ins.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This should move below line 178 here into the propertyInfo !== null check so we only do that once.
Oh, yeah. Duh.
We also probably should not filter this for isCustomComponentTag
Moving it to where we already checkpropertyInfo
will prevent it from applying to custom component tags. 👍
Should this be accompanied with a warning too since this is likely a bug and you should be setting the value to undefined or null if you want this behavior? |
Seems like a reasonable approach that's inline with sanitizeURL. |
bvaughn commentedApr 7, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I don't think so.
Based on my own testing (Chrome, Firefox, Edge) adding
Great. I was hoping that was the case, and tests seemed to confirm.
Seems reasonable to add a warning. Seems odd to suggest " How about:
|
…pty stringsThis prevents e.g. <img src=""> from making an unnecessar HTTP request for certain browsers.
06d07c0
tof8ac921
Compare
What if there's another <basehref="something/else/"/><basehref=""/> vs <basehref="something/else/"/><base/> |
bvaughn commentedApr 7, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Same behavior. The empty one does not modify the Edit Is it even valid to have multiple ones on the same page? Looks like the browser only pays attention to the first one, regardless. |
bvaughn commentedApr 7, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
The spec says:
|
JeromeDeLeon commentedApr 7, 2020
What about |
The warning should probably recommend not rendering the element at all as the primary recommendation and only null if that doesn’t work (i.e. if someone is too lazy to refactor). |
I could see someone using |
bvaughn commentedApr 7, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Yeah, that sounds like kind of a long and confusing warning 😉 I can change the rec. to "don't render at all" though I guess.
Yeah... that's the use case that seems most likely I guess. They could always use
@JeromeDeLeon No. This is something different. |
I dunno. Is this warning better?
|
koenpunt commentedApr 7, 2020
I'm wondering why it would be necessary to filter these? |
Zeko369 commentedApr 7, 2020
@koenpunt to prevent unnecessary network requests, for example |
@bvaughn We were recently going through React warnings with@rachelnabors and she mentioned “no-op” is unfamiliar jargon. We’ll need to take a pass over them. But for now, I suggest:
|
bvaughn commentedApr 7, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I appreciate the re-worked error message,@gaearon! These things are difficult to write 😅 That being said, I think the middle sentence of the one you mentioned is too I think this makes sense for every case exceptmaybe |
Maybe, but can we leave this wording for |
Sure. |
Treat `<a href="" />` the same with and without`enableFilterEmptyStringAttributesDOM`in#18513 we started to warn andignore for empty `href` and `src` props since it usually hinted at amistake. However, for anchor tags there's a valid use case since `<ahref=""></a>` will by spec render a link to the current page. It couldbe used to reload the page without having to rely on browseraffordances.The implementation for Fizz is in the spirit of#21153. I gated the fork behindthe flag so that the fork is DCE'd when the flag is off.
Treat `<a href="" />` the same with and without`enableFilterEmptyStringAttributesDOM`in#18513 we started to warn andignore for empty `href` and `src` props since it usually hinted at amistake. However, for anchor tags there's a valid use case since `<ahref=""></a>` will by spec render a link to the current page. It couldbe used to reload the page without having to rely on browseraffordances.The implementation for Fizz is in the spirit of#21153. I gated the fork behindthe flag so that the fork is DCE'd when the flag is off.DiffTrain build for [f3ce87a](f3ce87a)
Treat `<a href="" />` the same with and without`enableFilterEmptyStringAttributesDOM`in#18513 we started to warn andignore for empty `href` and `src` props since it usually hinted at amistake. However, for anchor tags there's a valid use case since `<ahref=""></a>` will by spec render a link to the current page. It couldbe used to reload the page without having to rely on browseraffordances.The implementation for Fizz is in the spirit of#21153. I gated the fork behindthe flag so that the fork is DCE'd when the flag is off.
…28124)Treat `<a href="" />` the same with and without`enableFilterEmptyStringAttributesDOM`infacebook#18513 we started to warn andignore for empty `href` and `src` props since it usually hinted at amistake. However, for anchor tags there's a valid use case since `<ahref=""></a>` will by spec render a link to the current page. It couldbe used to reload the page without having to rely on browseraffordances.The implementation for Fizz is in the spirit offacebook#21153. I gated the fork behindthe flag so that the fork is DCE'd when the flag is off.
Treat `<a href="" />` the same with and without`enableFilterEmptyStringAttributesDOM`infacebook/react#18513 we started to warn andignore for empty `href` and `src` props since it usually hinted at amistake. However, for anchor tags there's a valid use case since `<ahref=""></a>` will by spec render a link to the current page. It couldbe used to reload the page without having to rely on browseraffordances.The implementation for Fizz is in the spirit offacebook/react#21153. I gated the fork behindthe flag so that the fork is DCE'd when the flag is off.DiffTrain build for commitfacebook/react@f3ce87a.
Uh oh!
There was an error while loading.Please reload this page.
This is a breaking change!
So I have put it behind a new feature flag,
enableFilterEmptyStringAttributesDOM
.Scope
This flag affects the following attributes/elements:
action
form
formAction
button
,input
href
a
,area
,base
,link
src
audio
,embed
,iframe
,img
,input
,script
,source,
,track
,video
Behavior before
<img src={emptyStringValue} />
<img src="">
Behavior after
<img src={emptyStringValue} />
<img>
Implementation
I am not very familiar with the details of the DOM render, but I think it makes the most sense to add this to the
DOMProperty
file. It seems to fit with the other filtering logic we have there.This approach has a possible downside, since the logic only applies toattributes and does not consider the element type. (It's possible we may want to make an exception for some of the impacted elements I listed above?)
Loosely related to PR#15047