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

.css(), the values like +=10% aren't ignored anymore (like .animate()) #2011

Closed
wants to merge 2 commits into from

Conversation

mr21
Copy link
Contributor

@mr21 mr21 commented Jan 13, 2015

Hi :)

Actually when we make:

$element.animate({width: "+=10%"}, 500);
it's work fine, the value is converted into percentage BUT if we write:

$element.css("width", "+=10%");
We don't have the same behavior...
The .css() function doesn't look the unity, it's "px" every time.

The deletion of my two old commit has closed my last pull request... and even when I make another new commit I can't reopen it, sorry for that doublon.

So, @gibson042 told me to see this part: https://github.com/jquery/jquery/blob/master/src/effects.js#L38-L60
Before, I didn't understand I could make a new CSS change on the element with $.style just for make the calcul conversion about the different CSS unities.
When I understand this, I understand the code BUT I didn't understand (again) why it's necessary to make the do{}while() loop, because every time this loop is passed only one time for each call.

@gibson042
Copy link
Member

Thanks for the followup! I think you will see some problems if you run the effects test suite against this code. Also, I think more thought needs to go into handling extreme values (zero, very small, very large)—which is the reason for do...while in effects.js.

And finally, this new functionality should come with unit tests so we can ensure its correct behavior on all platforms into the future.

@mr21
Copy link
Contributor Author

mr21 commented Jan 14, 2015

Hi @gibson042 :)

I forgot to check the division by zero, shame on me... I have update my commit.

I didn't understand what you mean by very small value it's between 0 and 1 pixel?
Like 0.5pt == 0.666px ?

It make sens for me only if we could multiply a value like when we multiply a float casted in int:
((int)2.5) * 100 == 200 instead of 2.5 * 100 == 250
I mean the tiny difference less than one pixel would increase with the multiplication. But we cannot multiply anyway.

@gibson042
Copy link
Member

"Very small" in the sense of "risking problems from rounding errors". The easiest way to get them is mixing "%" and non-"%", e.g. between width: 0.2% and 7em.

And again, all of this should have unit tests.

@mr21
Copy link
Contributor Author

mr21 commented Jan 15, 2015

Here I put several examples with the current jQuery
http://jsfiddle.net/Mr21/o4qbt6sf/

And here it's the same but with my changes (without the loop).
http://jsfiddle.net/Mr21/78yb124y/

The width of the blue bars are hard-fixed with the correct value and never move.
The width of the red and the green bars are also initialised with a specific values but I modify it to reach to the blue value:
But the red bars is modified with .css("width", "+=10px");
But the green bars is modified with .animate({width: "+=10px"}, 500);

I wrote your example (0.2% to 7em) at the end of list.
But I cannot find any example who show me the need of the do...while();

@gibson042
Copy link
Member

You're still dealing with smallish values, but dimensions can get arbitrarily large (remember, "%" is always relative)—the loop is a guarantee that we can establish an accurate scale even for extreme values.

All of which, of course, is completely beside the point while this PR is not adding unit tests for new functionality and is failing existing effects tests.

@mr21
Copy link
Contributor Author

mr21 commented Jan 15, 2015

I saw these failing tests, I will extract the loop in the common function this evening.
Is there a particular norme for adding some new unit tests?

@gibson042
Copy link
Member

Best judgment, and trying to preserve the surrounding style. In this case, I'd like to see a new test immediately after the existing relative-value test in test/unit/css.js that looks something like:

test( "css() non-px relative values (gh-1711)", <N>, function() {
    
});

and covers various non-px scenarios.

@mr21
Copy link
Contributor Author

mr21 commented Jan 18, 2015

I made a new commit (all tests are passing).
But it was a little complicated to find something in common finally.
Tell me if you think it's okay.

And another thing: why not make an alias on:
.css(prop, value) or
.css({prop: value}) to call directly:
.animate({prop: value}, 0);
And on the animate side we would find a if to check if we launch or not a real animation.

I think it can be a great save a code, no?

@mr21
Copy link
Contributor Author

mr21 commented Jan 21, 2015

@@ -285,7 +286,7 @@ jQuery.extend({

// If a number was passed in, add 'px' (except for certain CSS properties)
if ( type === "number" && !jQuery.cssNumber[ origName ] ) {
value += "px";
value += ret && ret[3] ? ret[3] : "px";
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick but you should have spaces inside the square brackets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, thanks (it's done) :)

"../var/rfxnum"
], function( jQuery, rfxnum ) {

function cssCastUnit( elem, prop, parts, tween ) {
Copy link
Member

Choose a reason for hiding this comment

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

Variables here could have better names, especially now that this code is a fully-fledged function serving two purposes. I recommend something like:

  • cssCastUnit → adjustCSS
  • parts → value
  • getCSS → currentValue
  • target → initial
  • start → recasted
  • end → adjusted

@gibson042
Copy link
Member

👏 @mr21; this is looking great!

@mr21
Copy link
Contributor Author

mr21 commented Feb 4, 2015

All is done @gibson042 :)

I found a probleme with line-height, this property is set in the cssNumber object for Don't automatically add "px" to these possibly-unitless properties.
And if we not pass any unit to line-height by default it's em.
So I modified some code again here: https://github.com/jquery/jquery/pull/2011/files#diff-414c8f59bff0d1b63680b64763d8c529R288.

Thanks for all these indications!

@gibson042
Copy link
Member

I found a probleme with line-height, this property is set in the cssNumber object

Yep, that's exactly the kind of thing I was hoping to uncover.

Thanks for all these indications!

Thank you; I will try to look it over again today. Also, squashing everything into a single commit makes it hard to see changes over the course of this pull request... in the future, please add new commits and fast-forward instead of force-pushing replacements.

@mr21
Copy link
Contributor Author

mr21 commented Feb 4, 2015

Understood 👍

], function( jQuery, pnum, access, rmargin, rnumnonpx, cssExpand, isHidden,
getStyles, curCSS, defaultDisplay, addGetHookIf, support, dataPriv ) {
], function( jQuery, pnum, rfxnum, access, rmargin, rnumnonpx, cssExpand, isHidden,
getStyles, curCSS, adjustCSS, defaultDisplay, addGetHookIf, support, dataPriv ) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's keep the "r" variables together (e.g., rfxnum before or after rnumnonpx).

unit = value && value[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ),
// Starting value computation is required for potential unit mismatches
recasted = ( jQuery.cssNumber[ prop ] || unit !== "px" && +initial ) &&
rfxnum.exec( jQuery.css( elem, prop ) );
Copy link
Member

Choose a reason for hiding this comment

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

Seeing it all together, maybe a couple more renames are in order:

  • valuevalueParts
  • recastedinitialInUnit

@gibson042
Copy link
Member

Almost there! Thanks again, @mr21.

@mr21
Copy link
Contributor Author

mr21 commented Feb 6, 2015

All is done (in a new commit this time).

@gibson042
Copy link
Member

Just one more thing before this lands (and sorry I didn't check earlier)... make sure to sign our CLA with the same email address you use for GitHub commits.

@mr21
Copy link
Contributor Author

mr21 commented Feb 8, 2015

Done :)

@timmywil
Copy link
Member

timmywil commented Feb 8, 2015

Thanks @mr21! 👍

@mr21
Copy link
Contributor Author

mr21 commented Feb 20, 2015

Great ^^
So now, do I have to merge my two commits?

@gibson042
Copy link
Member

Nope; just leave this as-is and I expect to merge within the next seven days (and definitely before the 3.0 beta). Thanks again!

@gibson042 gibson042 closed this in 9b03f6d Mar 9, 2015
gibson042 pushed a commit that referenced this pull request Mar 9, 2015
Fixes gh-1711
Closes gh-2011

(cherry picked from commit 9b03f6d)

Conflicts:
	src/css.js
	src/effects.js
@gibson042
Copy link
Member

Thanks again, @mr21!

@mr21
Copy link
Contributor Author

mr21 commented Mar 9, 2015

You're welcome 😄

markelog pushed a commit that referenced this pull request Nov 10, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants