Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Bug 7345; Add support for explicit/relative string values in .css - modif #297

Merged
merged 2 commits into from about 3 years ago

2 participants

Dan Heberden Dave Methvin
Dan Heberden
Owner

Bug 7345; Add support for explicit/relative string values in .css - modified from original pull req by brandonaron #78 - #78

Checks for an equals sign to add/substract values - otherwise, reduces string to +digit or -digit to use.

Dan Heberden danheberden closed this April 04, 2011
Dan Heberden danheberden reopened this April 04, 2011
Dan Heberden
Owner

I opted for the inline testing/filtering of regex vs storing the array as in pull #78 due to the fact taht it was 3-7x faster: http://danheberden.com/share/0d501.png and http://jsperf.com/dh-regex-array-vs-parsing -

danheberden@44a3b58 ( which is part of this branch ) has the refactored code, improved regex and rewritten test cases.

Dave Methvin dmethvin merged commit 44a3b58 into from April 05, 2011
Dave Methvin dmethvin closed this April 05, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
15  src/css.js
@@ -7,6 +7,8 @@ var ralpha = /alpha\([^)]*\)/i,
7 7
 	rupper = /([A-Z]|^ms)/g,
8 8
 	rnumpx = /^-?\d+(?:px)?$/i,
9 9
 	rnum = /^-?\d/,
  10
+	rrelNum = /^[+\-]=/,
  11
+	rrelNumFilter = /[^+\-\.\de]+/g,
10 12
 
11 13
 	cssShow = { position: "absolute", visibility: "hidden", display: "block" },
12 14
 	cssWidth = [ "Left", "Right" ],
@@ -75,20 +77,27 @@ jQuery.extend({
75 77
 		}
76 78
 
77 79
 		// Make sure that we're working with the right name
78  
-		var ret, origName = jQuery.camelCase( name ),
  80
+		var ret, type, origName = jQuery.camelCase( name ),
79 81
 			style = elem.style, hooks = jQuery.cssHooks[ origName ];
80 82
 
81 83
 		name = jQuery.cssProps[ origName ] || origName;
82 84
 
83 85
 		// Check if we're setting a value
84 86
 		if ( value !== undefined ) {
  87
+			type = typeof value;
  88
+
85 89
 			// Make sure that NaN and null values aren't set. See: #7116
86  
-			if ( typeof value === "number" && isNaN( value ) || value == null ) {
  90
+			if ( type === "number" && isNaN( value ) || value == null ) {
87 91
 				return;
88 92
 			}
89 93
 
  94
+			// convert relative number strings (+= or -=) to relative numbers. #7345
  95
+			if ( type === "string" && rrelNum.test( value ) ) {
  96
+				value = +value.replace( rrelNumFilter, '' ) + parseFloat( jQuery.css( elem, name ) );
  97
+			}
  98
+
90 99
 			// If a number was passed in, add 'px' to the (except for certain CSS properties)
91  
-			if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) {
  100
+			if ( type === "number" && !jQuery.cssNumber[ origName ] ) {
92 101
 				value += "px";
93 102
 			}
94 103
 
32  test/unit/css.js
@@ -105,6 +105,38 @@ test("css(String|Hash)", function() {
105 105
 	equals( child[0].style.fontSize, old, "Make sure font-size isn't changed on null." );
106 106
 });
107 107
 
  108
+test("css() explicit and relative values", function() {
  109
+	expect(9);
  110
+	var $elem = jQuery('#nothiddendiv');
  111
+
  112
+	$elem.css({ width: 1, height: 1 });
  113
+	equals( $elem.width(), 1, "Initial css set or width/height works (hash)" );
  114
+
  115
+	$elem.css({ width: "+=9" });
  116
+	equals( $elem.width(), 10, "'+=9' on width (hash)" );
  117
+
  118
+	$elem.css({ width: "-=9" });
  119
+	equals( $elem.width(), 1, "'-=9' on width (hash)" );
  120
+
  121
+	$elem.css({ width: "+=9px" });
  122
+	equals( $elem.width(), 10, "'+=9px' on width (hash)" );
  123
+
  124
+	$elem.css({ width: "-=9px" });
  125
+	equals( $elem.width(), 1, "'-=9px' on width (hash)" );
  126
+
  127
+	$elem.css( "width", "+=9" );
  128
+	equals( $elem.width(), 10, "'+=9' on width (params)" );
  129
+
  130
+	$elem.css( "width", "-=9" ) ;
  131
+	equals( $elem.width(), 1, "'-=9' on width (params)" );
  132
+
  133
+	$elem.css( "width", "+=9px" );
  134
+	equals( $elem.width(), 10, "'+=9px' on width (params)" );
  135
+
  136
+	$elem.css( "width", "-=9px" );
  137
+	equals( $elem.width(), 1, "'-=9px' on width (params)" );
  138
+});
  139
+
108 140
 test("css(String, Object)", function() {
109 141
 	expect(22);
110 142
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.