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

Support "for", "class", "http-equiv", and "accept-charset" #10169

Closed
wants to merge 3 commits into from

Conversation

nhunzaker
Copy link
Contributor

This commit updates the HTML property config and UnknownPropertyHook developer warnings such that is no longer required to specify "className" instead of "class", "htmlFor" instead of "for", and so on.

Both className and class, htmlFor and for, are supported. I do not want this to be a breaking change.

When both are specified within props, React provides the warning:

"className and class were listed as properties on <div />. Both write to the same attribute; use one or the other."

This required updating some tests that were dependent using className vs class for developer warnings.

This feature is implemented separately from #7311 but there should be no functional conflict.

Related issues: #5926, #4331

@syranide
Copy link
Contributor

Are we intending to discourage and eventually deprecate the old className and htmlFor then? Just having both for the sake of it doesn't seem like good thing.

@gaearon
Copy link
Collaborator

gaearon commented Jul 13, 2017

Yes, I think we want to transition to just class and for. It’s going to be super churny so IMO it’s fine to support both for a couple of majors. Maybe the warning when you specify both should nudge you to use the newer one.

@syranide
Copy link
Contributor

syranide commented Jul 13, 2017

@gaearon 👍 On another note, I'm curious, what's the plan with dropping the whitelist then? From what I understand this has been the plan all along in the minds of some, but this seems like definitively moving towards keeping the whitelist or otherwise there will be churn going back.

This commit updates the HTML property config and UnknownPropertyHook
developer warnings such that is no longer required to specify
"className" instead of "class", "htmlFor" instead of "for", and so
on.

Both forms are supported. When both are specified within props, React
provides the warning:

"className and class were listed as properties on <div />. Both write
to the same attribute; use one or the other."
@nhunzaker
Copy link
Contributor Author

Ack. Lint fixed.

@nhunzaker
Copy link
Contributor Author

@gaearon I can update the warning to recommend using the attribute name, but I think we should only recommend it when both are specified. What do you think?

@gaearon
Copy link
Collaborator

gaearon commented Jul 13, 2017

I agree.

@gaearon
Copy link
Collaborator

gaearon commented Jul 13, 2017

this seems like definitively moving towards keeping the whitelist

Could you elaborate on that? I’m not super familiar with this code. From my understanding additions to whitelist are temporary to calm down the warnings, but #7311 would make it possible to later remove them. Do I misunderstand this?

@syranide
Copy link
Contributor

syranide commented Jul 13, 2017

@gaearon Since way back there has been a discussion towards doing away with HTMLDOMPropertyConfig entirely, all props would then be passed through as-is. If that is still on the table then it seems logical to understand the outcome of that effort before doing this. Because this PR now follows the attribute naming convention for these props, whereas IMHO it makes more sense to use the property naming convention then (htmlFor). Perhaps that discussion has changed but the devs seemed rather vocal towards going down this path in the past.

@syranide
Copy link
Contributor

@gaearon Perhaps I should've read the linked PR more first :) but yeah, if the intention is to follow the attribute naming convention it makes sense to do this. If the idea is to follow the property naming convention it doesn't make sense to do this IMHO.

@nhunzaker
Copy link
Contributor Author

nhunzaker commented Jul 13, 2017

@syranide I'm in favor of following the attribute route, keeping the authoring as similar to static HTML as possible. All attributes that we require the property name for, we convert into the attribute name to write as an attribute anyway.

For me, I would like the roadmap to be:

  1. Allow all attributes we presently require to be specified by their property names. Move users towards the attribute names
  2. With Allow custom attributes by default #7311, allow all custom attributes, providing an option to keep the old behavior for legacy concerns.
  3. Remove the feature flag, allow all attributes "as is". Remove the "className" and "htmlFor" helper warnings.
  4. Trim down the HTML property config as much as we possibly can.
  5. Eliminate all conditions that are roughly "can we write this attribute to the element"
  6. Figure out how to remove the property configs, or drastically minimize them.

@nhunzaker
Copy link
Contributor Author

@gaearon have to shift gears, but I can update that warning later today.

After some discussion in
facebook#10169 (comment),
we have elected to move forward with using attribute names as the
standard way of defining React props on html fields.

This commit updates the warning associated with instances where a
property _and_ attribute name are given such that it recommends using
the attribute.
expect(container.firstChild.className).toBe('cleric');
expectDev(console.error.calls.count()).toBe(1);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: class and className were listed as properties on a <div />. Use "class".\n in div (at **)',
Copy link
Contributor Author

@nhunzaker nhunzaker Jul 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon how's this sound? too direct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I've just added quotes around class and className for consistency.

@Chudesnov
Copy link

Chudesnov commented Jul 14, 2017

This being on the roadmap raises several questions for me:

  • Will we someday also switch to onclick, autofocus or even make attributes case-insensitive?
  • Is the ultimate goal to make JSX in React 100% compatible with HTML?

@aweary
Copy link
Contributor

aweary commented Jul 14, 2017

@Chudesnov

Will we someday also switch to onclick, autofocus or even make attributes case-insensitive?

That's a good question, I think it would make sense to adopt one strategy or the other. Having a mix of attribute and property names doesn't seem acceptable IMO. If we implement this change I think it follows that we would eventually implement the same change for all attributes.

Is the ultimate goal to make JSX in React 100% compatible with HTML?

At least with the current spec, JSX can't be 100% compatible with HTML due to other differences, like self-closing tags. But that doesn't mean we can't make it more compatible.

@ghost
Copy link

ghost commented Jul 16, 2017

Please add support for string styles -

<div style="margin-left: 10px; display: inline-block ....">{text}</div>

This would be last thing which limits jsx from using html inside render function

@gaearon
Copy link
Collaborator

gaearon commented Jul 26, 2017

Some discussion on this: https://twitter.com/dan_abramov/status/890191815567175680

@nhunzaker
Copy link
Contributor Author

Oof. I have a lot of tweets to read.

@bvaughn bvaughn mentioned this pull request Aug 1, 2017
@gaearon
Copy link
Collaborator

gaearon commented Aug 4, 2017

In the end we discussed against doing this, at least for now.

I previously thought class is only an issue in IE8, but it doesn’t work even with modern features in contexts where you’d expect it to:

const {class, someOtherProp} = this.props; // breaks

If we allowed both, it would put third party components in an awkward situation where they are expected to support both. It gets brittle and confusing.

@gaearon gaearon closed this Aug 4, 2017
@phiresky
Copy link

phiresky commented Aug 4, 2017

In the end we discussed against doing this

Noooo

I guess it makes sense, but I looked forward to this.

@gaearon
Copy link
Collaborator

gaearon commented Aug 4, 2017

I wanted to do it, but it doesn’t make as much sense to me now.
We probably should just better articulate why we don’t do it. It wasn’t obvious to me before.

@KernelDeimos
Copy link

I realize this is like 6 years old but if anyone finds this in the future (probably from the same StackOverflow link) and happens to know better... could you tell me why they didn't do something like this?

function createElement(tag, attributes, ...children) {
    attributes.className = attributes['class'] =
        attributes.className || attributes['class'];
    // ...
}

This seems to dispute @gaearon's claim

[...] would put third party components in an awkward situation where they are expected to support both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants