-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add class
& for
aliases for className
and htmlFor
#9379
Comments
Yeah, pre-ES5 those keywords were reserved unconditionally. But it's been a decade since they were usable for property access. |
Well, Solidjs, as an example, doesn't expose these names in JSX. It simply uses |
What's a good use case for |
Same for Qwik we can use |
@annevk cases where you want to override previous changes, or setting an initial set of class names. |
I think this proposal should be considered on its own ignoring what JSX or other libraries are doing. When teaching how to manipulate the DOM this is often something that catches students off guard. IMO it makes sense to add an alias for these properties. It feels more natural to interact with |
Agreed. I didn't mention frameworks or JSX in the proposal for this reason. |
I'd be in favour of the new shorter consistent properties, but for backwards compat likely need to keep the old ones as an alias (assuming that was the implicit intention with making them legacy) |
class
& for
aliases for className
and htmlFor
The proposal is to add aliases, not rename the properties. |
Doh, sorry, it's right there in the OP and I managed to miss it. |
You should clarify your post on whether you are talking about HTML Attributes or |
@hinell added spec links. Although, I thought it would be obvious since |
@jakearchibald |
@hinell, please...we all know that. which is the whole point of this issue, adding aliases for matching properties... |
What about functions like |
Content attribute reflection uses property names which are the same as the content attribute name modulo casing as a rule with only a handful of rare exceptions. These exceptions exist due to syntax constraints in JavaScript that were eliminated 12 years ago. They are now anomalies that easily lead to bugs — there’s no error thrown if you assign to The name |
I know it's technically out of scope but it's good to consider SVG here. SVG has Would it be possible to discuss aligning this time and having SVG also support the |
@mgol I think that's an excellent idea. For others on the thread: in SVG, I'd forgotten about this, but it's definitely caught me out in the past. The Edit: |
This adds for as an alias for htmlFor to be more consistent and intuitive. This is now possible since 'for' is no longer a globally reserved word in Javascript. It also updates one example to match the new 'class' alias for 'className' from the DOM spec. fixes whatwg#9379
I checked httparchive (only sample_data which is 10k pages) for resources with https://docs.google.com/spreadsheets/d/1VoVQBocvtPzBJttHchZo00xXfQJeihTvFUse2Rw-NQo/edit?usp=sharing It's hard to tell with static analysis if it's an element or not, but at least some of them look like they're elements, but also it looks like those generally intended to use j.body.class="notranslate" in https://code.jivosite.com/script/widget/bp3D59t7jk Similarly in GitHub search: https://github.com/search?q=.class%3D+lang%3Ajavascript&type=code , for example: button_li[i].className="current";
pic_li[i].className="current";
}else{
button_li[i].class="but";
pic_li[i].className="pic";
} That said, there's some risk of breaking web content. Pages might set Maybe use counters could be useful here? |
At least in Firefox, we plan to put this behind a preference that's initially only enabled in Nightly. |
Nice! Is this going to be regular reflection even on SVG elements? |
Well, first someone will need to take a look at the data zcorpan provided and investigate a bit more if this is possible at all. Are there some obvious, common cases which this would break? |
I love the idea ... but ...
for a migration / deprecation pattern it'd be better to make If If by overriding Looking forward to update all my libraries that monkey-patch |
The way that I'm currently imagining (and PRing) this is that if ("class" in Element.prototype) {
Object.defineProperty(Element.prototype, "className", {
get: function() {
console.warn("Element.className is deprecated. Use Element.class instead.");
return this.getAttribute("class");
},
set: function(value) {
console.warn("Element.className is deprecated. Use Element.class instead.");
this.setAttribute("class", value);
}
});
} else {
Object.defineProperty(Element.prototype, "class", {
get: function() {
return this.getAttribute("class");
},
set: function(value) {
this.setAttribute("class", value);
}
});
} The exact same would go for |
then my concern is gone, thanks. (unrelated P.S. you don't need that |
I filed https://issues.chromium.org/u/1/issues/367992694 . If that is implemented, then when it's in the Stable release the next httparchive run should have accurate data on which pages set these properties (during page load). We can then query for those pages and sort by rank magnitude and assess compat impact by manually testing pages with a browser build where |
I realize this is a bit early, but I've already prepared a build that implements I'd be open to implement the use counters as well, but I might not get to it until later this week. Also, from searching Github earlier, I found a lot of uses for |
@Psychpsyo nice! Yeah, this change has the potential to make some content do what the author intended, which would be nice indeed. However, the web is big, and there's still a risk of breaking other sites with different expectations, which could negatively impact users and block shipping. So I think it's worthwhile to look for those needles. |
Seems like we need some more data here from the use counters, so dropping agenda+ for now. |
https://dom.spec.whatwg.org/#dom-element-classname
https://html.spec.whatwg.org/multipage/forms.html#dom-label-htmlfor
I assume these were originally given unusual and inconsistent names to avoid reserved words, but it isn't an issue with modern JavaScript.
Could
class
be added to aliasclassName
, andfor
to aliashtmlFor
? The old names would be considered legacy.It's a minor thing, but it's polyfillable, and removes a weird inconsistency on the platform.
The text was updated successfully, but these errors were encountered: