Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged

Conversation

bvaughn
Copy link
Contributor

@bvaughnbvaughn commentedApr 6, 2020
edited
Loading

This is a breaking change!

So I have put it behind a new feature flag,enableFilterEmptyStringAttributesDOM.

Scope

This flag affects the following attributes/elements:

AttributeElements
actionform
formActionbutton,input
hrefa,area,base,link
srcaudio,embed,iframe,img,input,script,source,,track,video

Behavior before

ReactHTML
<img src={emptyStringValue} /><img src="">

Behavior after

ReactHTML
<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 theDOMProperty 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

@facebook-github-botfacebook-github-bot added CLA Signed React Core TeamOpened by a member of the React Core Team labelsApr 6, 2020
@codesandbox-ci
Copy link

codesandbox-cibot commentedApr 6, 2020
edited
Loading

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:

SandboxSource
happy-pond-7x32rConfiguration

@sizebot
Copy link

sizebot commentedApr 6, 2020
edited
Loading

Details of bundled changes.

Comparing:3278d24...2399b82

react-dom

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-dom-server.browser.development.js+0.3%+0.1%156.14 KB156.59 KB39.56 KB39.59 KBUMD_DEV
ReactTestUtils-dev.js+0.2%+0.1%64.85 KB64.95 KB17.48 KB17.5 KBFB_WWW_DEV
react-dom-server.browser.production.min.js🔺+0.3%🔺+0.3%23.32 KB23.39 KB8.57 KB8.6 KBUMD_PROD
react-dom.profiling.min.js+0.1%0.0%127.64 KB127.71 KB39.75 KB39.77 KBNODE_PROFILING
ReactDOM-dev.js+0.1%+0.1%972.33 KB973.75 KB215.42 KB215.64 KBFB_WWW_DEV
react-dom-unstable-fizz.browser.production.min.js0.0%🔺+0.1%1.2 KB1.2 KB706 B707 BUMD_PROD
ReactDOM-prod.js🔺+0.1%🔺+0.1%407.39 KB407.79 KB73.57 KB73.65 KBFB_WWW_PROD
ReactDOM-profiling.js+0.1%+0.1%418.2 KB418.61 KB75.4 KB75.48 KBFB_WWW_PROFILING
react-dom-unstable-fizz.browser.production.min.js0.0%-0.2%1.02 KB1.02 KB619 B618 BNODE_PROD
react-dom-test-utils.development.js0.0%0.0%75.14 KB75.14 KB20.12 KB20.12 KBUMD_DEV
ReactDOMTesting-dev.js0.0%0.0%925.85 KB926.29 KB206.06 KB206.1 KBFB_WWW_DEV
ReactDOMTesting-prod.js0.0%0.0%392.11 KB392.27 KB71.34 KB71.38 KBFB_WWW_PROD
ReactDOMTesting-profiling.js0.0%0.0%392.11 KB392.27 KB71.34 KB71.38 KBFB_WWW_PROFILING
react-dom-server.node.development.js+0.3%+0.1%148.15 KB148.58 KB39.07 KB39.1 KBNODE_DEV
react-dom-test-utils.production.min.js0.0%-0.0%13.1 KB13.1 KB4.81 KB4.81 KBNODE_PROD
react-dom-server.node.production.min.js🔺+0.3%🔺+0.3%23.62 KB23.69 KB8.7 KB8.72 KBNODE_PROD
react-dom-server.browser.development.js+0.3%+0.1%146.94 KB147.36 KB38.81 KB38.84 KBNODE_DEV
react-dom-server.browser.production.min.js🔺+0.3%🔺+0.3%23.21 KB23.28 KB8.54 KB8.57 KBNODE_PROD
react-dom.development.js0.0%0.0%931.11 KB931.57 KB203.13 KB203.17 KBUMD_DEV
react-dom-unstable-native-dependencies.development.js0.0%0.0%56.11 KB56.11 KB13.85 KB13.85 KBUMD_DEV
react-dom.production.min.js🔺+0.1%🔺+0.1%123.47 KB123.54 KB39.28 KB39.31 KBUMD_PROD
ReactDOMForked-dev.js+0.1%+0.1%972.33 KB973.75 KB215.42 KB215.64 KBFB_WWW_DEV
ReactDOMServer-dev.js+0.9%+0.6%153.06 KB154.48 KB38.75 KB38.97 KBFB_WWW_DEV
react-dom.profiling.min.js+0.1%+0.1%127.29 KB127.36 KB40.56 KB40.6 KBUMD_PROFILING
ReactDOMForked-prod.js🔺+0.1%🔺+0.1%407.39 KB407.79 KB73.57 KB73.65 KBFB_WWW_PROD
ReactDOMServer-prod.js🔺+0.9%🔺+0.7%52.11 KB52.56 KB12.6 KB12.68 KBFB_WWW_PROD
react-dom.development.js0.0%0.0%886.59 KB887.01 KB200.64 KB200.68 KBNODE_DEV
ReactDOMForked-profiling.js+0.1%+0.1%418.2 KB418.61 KB75.4 KB75.48 KBFB_WWW_PROFILING
react-dom-unstable-native-dependencies.development.js0.0%0.0%53.21 KB53.21 KB13.65 KB13.65 KBNODE_DEV
react-dom.production.min.js🔺+0.1%🔺+0.1%123.66 KB123.73 KB38.51 KB38.53 KBNODE_PROD
react-dom-unstable-fizz.node.production.min.js0.0%-0.1%1.17 KB1.17 KB669 B668 BNODE_PROD

ReactDOM: size: 🔺+0.3%, gzip: 🔺+0.3%

Size changes (experimental)

Generated by 🚫dangerJS against2399b82

@sizebot
Copy link

sizebot commentedApr 6, 2020
edited
Loading

Details of bundled changes.

Comparing:3278d24...2399b82

react-dom

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-dom.production.min.js🔺+0.1%0.0%119.51 KB119.58 KB37.36 KB37.38 KBNODE_PROD
react-dom-test-utils.development.js0.0%0.0%75.12 KB75.12 KB20.11 KB20.11 KBUMD_DEV
ReactDOMTesting-profiling.js0.0%0.0%404.08 KB404.25 KB73.13 KB73.17 KBFB_WWW_PROFILING
react-dom-server.browser.production.min.js🔺+0.3%🔺+0.3%22.75 KB22.82 KB8.46 KB8.49 KBNODE_PROD
react-dom.profiling.min.js+0.1%+0.1%123.37 KB123.44 KB38.53 KB38.55 KBNODE_PROFILING
react-dom-server.node.development.js+0.3%+0.1%146.64 KB147.06 KB38.85 KB38.89 KBNODE_DEV
react-dom-server.node.production.min.js🔺+0.3%🔺+0.3%23.16 KB23.23 KB8.63 KB8.65 KBNODE_PROD
ReactDOMForked-dev.js+0.1%+0.1%1000.21 KB1001.63 KB221.43 KB221.66 KBFB_WWW_DEV
ReactDOMForked-prod.js🔺+0.1%🔺+0.1%420.45 KB420.85 KB75.47 KB75.54 KBFB_WWW_PROD
react-dom.development.js+0.1%0.0%901.28 KB901.74 KB197.85 KB197.89 KBUMD_DEV
ReactDOMForked-profiling.js+0.1%+0.1%431.33 KB431.73 KB77.34 KB77.41 KBFB_WWW_PROFILING
react-dom-server.browser.development.js+0.3%+0.1%154.55 KB155 KB39.36 KB39.38 KBUMD_DEV
react-dom-unstable-fizz.node.production.min.js0.0%-0.2%1.16 KB1.16 KB661 B660 BNODE_PROD
react-dom.production.min.js🔺+0.1%🔺+0.1%119.39 KB119.46 KB38.17 KB38.19 KBUMD_PROD
react-dom-server.browser.production.min.js🔺+0.3%🔺+0.4%22.85 KB22.92 KB8.48 KB8.52 KBUMD_PROD
react-dom.profiling.min.js+0.1%0.0%123.11 KB123.18 KB39.38 KB39.4 KBUMD_PROFILING
ReactDOMTesting-dev.js0.0%0.0%952.39 KB952.84 KB211.66 KB211.71 KBFB_WWW_DEV
react-dom.development.js0.0%0.0%858 KB858.42 KB195.38 KB195.42 KBNODE_DEV
ReactDOMTesting-prod.js0.0%0.0%404.08 KB404.25 KB73.13 KB73.17 KBFB_WWW_PROD
react-dom-server.browser.development.js+0.3%+0.1%145.42 KB145.85 KB38.6 KB38.63 KBNODE_DEV
ReactDOM-dev.js+0.1%+0.1%1000.21 KB1001.63 KB221.43 KB221.66 KBFB_WWW_DEV
ReactDOMServer-dev.js+0.9%+0.6%156.56 KB157.98 KB39.68 KB39.92 KBFB_WWW_DEV
ReactDOM-prod.js🔺+0.1%🔺+0.1%420.45 KB420.85 KB75.47 KB75.54 KBFB_WWW_PROD
ReactDOMServer-prod.js🔺+0.8%🔺+0.6%52.85 KB53.29 KB12.76 KB12.84 KBFB_WWW_PROD
react-dom-unstable-fizz.browser.development.js0.0%+0.1%4.42 KB4.42 KB1.54 KB1.54 KBNODE_DEV
ReactDOM-profiling.js+0.1%+0.1%431.33 KB431.73 KB77.34 KB77.41 KBFB_WWW_PROFILING
react-dom-test-utils.production.min.js0.0%-0.0%13.09 KB13.09 KB4.8 KB4.8 KBNODE_PROD
react-dom-unstable-native-dependencies.development.js0.0%0.0%56.1 KB56.1 KB13.84 KB13.84 KBUMD_DEV
ReactTestUtils-dev.js+0.2%+0.1%64.85 KB64.95 KB17.47 KB17.5 KBFB_WWW_DEV
react-dom-unstable-native-dependencies.development.js0.0%0.0%53.2 KB53.2 KB13.64 KB13.65 KBNODE_DEV

Size changes (stable)

Generated by 🚫dangerJS against2399b82

@sebmarkbage
Copy link
Collaborator

Is<base href="" /> useful?

@sebmarkbage
Copy link
Collaborator

sebmarkbage commentedApr 6, 2020
edited
Loading

SSR should probably just work, in the sense that it just removes it from the HTML, since it calls this.

if(shouldRemoveAttribute(name,value,propertyInfo,false)){

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<base href="" />.

bvaughn reacted with thumbs up emoji

) {
return true;
}
}
Copy link
Collaborator

@sebmarkbagesebmarkbageApr 6, 2020
edited
Loading

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.

Copy link
ContributorAuthor

@bvaughnbvaughnApr 7, 2020
edited
Loading

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. 👍

@sebmarkbage
Copy link
Collaborator

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?

@sebmarkbage
Copy link
Collaborator

Seems like a reasonable approach that's inline with sanitizeURL.

@bvaughn
Copy link
ContributorAuthor

bvaughn commentedApr 7, 2020
edited
Loading

Is<base href="" /> useful?

I don't think so.

Mozilla says:

If the document contains no<base> elements,baseURI defaults tolocation.href.

Based on my own testing (Chrome, Firefox, Edge) adding<base href=""> does not affect thedocument.baseURI value (still the defaultlocation.href).

SSR should probably just work, in the sense that it just removes it from the HTML, since it calls this.

Great. I was hoping that was the case, and tests seemed to confirm.

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 reasonable to add a warning.

Seems odd to suggest "null orundefined" rather than"" when we treat them the same. (undefined seems at least as likely to be a potential mistake as""?)

How about:

Invalid value "" (empty string) for attribute%s. Usenull instead to indicate an empty value.

…pty stringsThis prevents e.g. <img src=""> from making an unnecessar HTTP request for certain browsers.
@bvaughnbvaughnforce-pushed thedom-empty-string-attribute-filter branch from06d07c0 tof8ac921CompareApril 7, 2020 00:45
@bvaughnbvaughn marked this pull request as ready for reviewApril 7, 2020 00:46
@sebmarkbage
Copy link
Collaborator

Based on my own testing (Chrome, Firefox, Edge) adding does not affect the document.baseURI value (still the default location.href).

What if there's another<base href="somethingelse" /> before it?

<basehref="something/else/"/><basehref=""/>

vs

<basehref="something/else/"/><base/>

@bvaughn
Copy link
ContributorAuthor

bvaughn commentedApr 7, 2020
edited
Loading

What if there's another<base href="somethingelse" /> before it?

Same behavior. The empty one does not modify thedocument.baseURI value.

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
Copy link
ContributorAuthor

bvaughn commentedApr 7, 2020
edited
Loading

The spec says:

There must be no more than onebase element per document.

crutchcorn reacted with heart emoji

@JeromeDeLeon
Copy link

What aboutclass attribute? Can it be added?

@sebmarkbage
Copy link
Collaborator

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).

@sebmarkbage
Copy link
Collaborator

I could see someone using<a href=“”> for a reload link or open this page in a new tab.

@bvaughn
Copy link
ContributorAuthor

bvaughn commentedApr 7, 2020
edited
Loading

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).

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.

I could see someone using<a href=“”> for a reload link or open this page in a new tab.

Yeah... that's the use case that seems most likely I guess. They could always use<a href=“#”> instead?

What aboutclass attribute? Can it be added?

@JeromeDeLeon No. This is something different.

@bvaughn
Copy link
ContributorAuthor

I dunno. Is this warning better?

Invalid value "" (empty string) for attribute%s. This will be a no-op. Either do not render the element at all or usenull instead to indicate an empty value.

@koenpunt
Copy link

I'm wondering why it would be necessary to filter these?

@Zeko369
Copy link

@koenpunt to prevent unnecessary network requests, for example<img src=""/> would make a GET request to the current url

@gaearon
Copy link
Collaborator

@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:

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.

eps1lon, Zeko369, vz01d, and kripod reacted with thumbs up emoji

@bvaughn
Copy link
ContributorAuthor

bvaughn commentedApr 7, 2020
edited
Loading

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 tooimg+src specific. Happy to use the first and third sentence though.

I think this makes sense for every case exceptmaybe<a href="">. (I'm still a little uncertain about that one.)

@gaearon
Copy link
Collaborator

I think the middle sentence of the one you mentioned is too img+src specific

Maybe, but can we leave this wording forsrc then? The reason I'm asking is because it looks like we're just being pedantic. It's not obvious from the warning just how bad it is.

@bvaughn
Copy link
ContributorAuthor

Sure.

@bvaughnbvaughn merged commitdc49ea1 intofacebook:masterApr 7, 2020
@bvaughnbvaughn deleted the dom-empty-string-attribute-filter branchApril 7, 2020 16:52
eps1lon added a commit that referenced this pull requestJan 30, 2024
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.
github-actionsbot pushed a commit that referenced this pull requestJan 30, 2024
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)
gaearon pushed a commit that referenced this pull requestFeb 3, 2024
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.
EdisonVan pushed a commit to EdisonVan/react that referenced this pull requestApr 15, 2024
…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.
Akshato07 pushed a commit to Akshato07/-Luffy that referenced this pull requestFeb 20, 2025
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.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@sebmarkbagesebmarkbagesebmarkbage approved these changes

@gaearongaearonAwaiting requested review from gaearon

@trueadmtrueadmAwaiting requested review from trueadm

Assignees
No one assigned
Labels
CLA SignedReact Core TeamOpened by a member of the React Core Team
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

8 participants
@bvaughn@sizebot@sebmarkbage@JeromeDeLeon@koenpunt@Zeko369@gaearon@facebook-github-bot

[8]ページ先頭

©2009-2025 Movatter.jp