-
Notifications
You must be signed in to change notification settings - Fork 20.6k
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
CSS: Don't automatically add "px" to properties with a few exceptions #4055
Conversation
a1c0a1e
to
096afcf
Compare
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.
LGTM, just the comment change
src/css.js
Outdated
@@ -247,7 +231,7 @@ jQuery.extend( { | |||
|
|||
// If a number was passed in, add the unit (except for certain CSS properties) |
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.
Comment should be changed as well, something like If the value is a number, add
px for certain CSS properties
.
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.
Nice catch; changed.
src/css/var/isAutoPx.js
Outdated
// |-+-| Border |-+-+-| Bottom |-+-|-+-| |-+-END | ||
// | \ Padding / \ Left / | \ Height / | ||
// | | | ||
// BEGIN -| /---------\ | |
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 does recognize some strings that are not currently CSS properties (like PaddingLeftWidth) but it's safe to assume they will never be CSS properties. Besides it only comes into play if a number is passed in.
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.
Hmm, border
is a little special here in that it differs from margin
and padding
. I could try extracting it but we'll end up with more than +1 byte then. ;)
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.
There are also others non-existing values like topWidth
. That's not ideal but the important part is the list is finite and not that large.
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.
I worry it also matches min
& max
- there are no such things in CSS but I wouldn't be surprised if they were added; it'd be worth to exclude them.
cd5c18a
to
b15c36b
Compare
@dmethvin I updated the regexp (again). It now adds |
55af87f
to
9dcc840
Compare
Nice! |
|
||
// Starting value computation is required for potential unit mismatches | ||
initialInUnit = ( jQuery.cssNumber[ prop ] || unit !== "px" && +initial ) && | ||
initialInUnit = elem.nodeType && |
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.
Without this change, effects tests were failing on non-element objects on an a
property. I've noticed they were already failing if a
was changed to anything from jQuery.cssNumber
and this PR makes almost everything "like from jQuery.cssNumber
".
The nodeType
check should most likely already be there, perhaps we should add it for 3.4.0 as well?
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.
Oh, ugh. Put another way, we only want to add "px" to property values if they're on DOM elements. I still wish we hadn't supported animating plain objects,.
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.
I extracted this change to PR #4061.
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.
#4601 landed, this PR rebased so this particular change is no longer a part of this PR.
Without this change animating properties from jQuery.cssNumber on non-elements throws an error. Ref jquerygh-4055
src/css/var/isAutoPx.js
Outdated
// The first test is used to ensure that: | ||
// 1. The prop starts with a lowercase letter (as we uppercase it for the second regex). | ||
// 2. The prop is not empty. | ||
return /^[a-z]$/.test( prop[ 0 ] ) && |
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.
Having a function is fine, but we should continue with the convention of creating regexes once outside of functions.
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.
It would be nice to see explicit tests covering "px" appending for these 14 + 6 + 10 properties and non-appending for other properties (both real ones like "line-height" and similarly-named fakes like "MarginLeftWidth" and "BorderTopHeight").
src/css/var/isAutoPx.js
Outdated
// The first test is used to ensure that: | ||
// 1. The prop starts with a lowercase letter (as we uppercase it for the second regex). | ||
// 2. The prop is not empty. | ||
return /^[a-z]$/.test( prop[ 0 ] ) && |
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.
Small savings here by replacing /^[a-z]$/.test( prop[ 0 ] )
with /^[a-z]/.test( prop )
.
src/css/var/isAutoPx.js
Outdated
// 1. The prop starts with a lowercase letter (as we uppercase it for the second regex). | ||
// 2. The prop is not empty. | ||
return /^[a-z]$/.test( prop[ 0 ] ) && | ||
/^(?:((?:Margin|Padding)?(?:Top|Right|Bottom|Left)?)|(?:Min|Max)?(?:Width|Height)|Border(?:Top|Right|Bottom|Left)?(?:Width)?)$/.test( prop[ 0 ].toUpperCase() + prop.substr( 1 ) ); |
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.
Moderate savings here by replacing prop.substr( 1 )
with prop.slice( 1 )
, and small savings by changing the order of alternatives. Suggested gzip-friendly representation:
/^(?:Border(?:Top|Right|Bottom|Left)?(?:Width|)|(?:Margin|Padding)?(?:Top|Right|Bottom|Left)?|(?:Min|Max)?(?:Width|Height))$/.test( prop[ 0 ].toUpperCase() + prop.slice( 1 ) )
I intend to write the test for all matched props (I'll take the list from #4053) and add a few negative ones to the props from outside of the set. I just haven't gotten to it yet. Thanks for the size reduction tips, I'll try them! |
PR updated, tests were added, all remarks by @timmywil & @gibson042 addressed. Thanks to @gibson042's clever remarks we're now down from +6 bytes to -9. 😲 |
Most of these were added but it's hard to test fake ones as the browser throws them away so there's no observable effect. |
@mgol yes, that addressed my concern. |
03e9dba
to
b3313af
Compare
d3458b9
to
ad41ce9
Compare
Good news, suddenly it got down to -45 bytes compared to current |
Summary
Don't automatically add "px" to properties with a few exceptions
Fixes gh-2795
Ref gh-4009
This is an alternative to PR #4053.
Const over #4053:
it matches more than we theoretically want (the list in WIP CSS: Don't automatically add "px" to properties with a few exceptions #4053 is precise), e.g. non-existent props liketop-width
Pros over #4053:
1 byte6 bytes instead of 120Checklist