Skip to content
Permalink
Browse files
Only set height/width if it's a non-negative number (don't set it to 0).
  • Loading branch information
jeresig committed Sep 9, 2010
1 parent cb3a9c1 commit 8b7015987cbd24c79f328bcf9260a6596a785bf5
Showing 1 changed file with 5 additions and 1 deletion.
@@ -123,7 +123,11 @@ jQuery.each(["height", "width"], function( i, name ) {

set: function( elem, value ) {
// ignore negative width and height values #1599
return Math.max( parseFloat(value), 0 ) + "px";
value = parseFloat(value);

if ( value >= 0 ) {
return value + "px";
}
}
};
});

3 comments on commit 8b70159

@staabm
Copy link
Contributor

@staabm staabm commented on 8b70159 Sep 10, 2010

Choose a reason for hiding this comment

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

You also set it to 0....

@jeresig
Copy link
Member Author

@jeresig jeresig commented on 8b70159 Sep 10, 2010

Choose a reason for hiding this comment

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

Actually, that's not the case. In the current version of jQuery we do:

    // ignore negative width and height values #1599
    if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
        value = undefined;
    }

Which effectively sets no value. We're matching that functionality (as of this commit).

@GarrettS
Copy link

@GarrettS GarrettS commented on 8b70159 Sep 10, 2010

Choose a reason for hiding this comment

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

The problem is not internet explorer. The problem itself is the attempt to assign a property value that is invalid or unparseable. The error is not limited to height or width (or other properties that accept length that is not negative (borderWidth, fontSize)).

This feature was discussed on comp.lang.javascript fairly recently in "Sencha Touch--Support 2 browsers in just 228K!" and also later in "isColor()?". For another example, when making transitional adjustments to style (animation), assigning "100" for fontWeight works because "100" is a valid value, but "101" is not a valid value, and so can be expected to thrown the error specified in DOM 2 Style.

Validating only two properties is limited but trying to validate every possible input value for every property would be impractical.

Please sign in to comment.