@@ -1095,6 +1095,71 @@ QUnit.test( "val(select) after form.reset() (Bug #2551)", function( assert ) {
10951095 jQuery ( "#kk" ) . remove ( ) ;
10961096} ) ;
10971097
1098+ QUnit . test ( "select.val(space characters) (gh-2978)" , function ( assert ) {
1099+ assert . expect ( 35 ) ;
1100+
1101+ var $select = jQuery ( "<select/>" ) . appendTo ( "#qunit-fixture" ) ,
1102+ spaces = {
1103+ "\\t" : {
1104+ html : "	" ,
1105+ val : "\t"
1106+ } ,
1107+ "\\n" : {
1108+ html : " " ,
1109+ val : "\n"
1110+ } ,
1111+ "\\r" : {
1112+ html : " " ,
1113+ val : "\r"
1114+ } ,
1115+ "\\f" : "\f" ,
1116+ "space" : " " ,
1117+ "\\u00a0" : "\u00a0" ,
1118+ "\\u1680" : "\u1680"
1119+ } ,
1120+ html = "" ;
1121+ jQuery . each ( spaces , function ( key , obj ) {
1122+ var value = obj . html || obj ;
1123+ html += "<option value='attr" + value + "'></option>" ;
1124+ html += "<option value='at" + value + "tr'></option>" ;
1125+ html += "<option value='" + value + "attr'></option>" ;
1126+ } ) ;
1127+ $select . html ( html ) ;
1128+
1129+ jQuery . each ( spaces , function ( key , obj ) {
1130+ var val = obj . val || obj ;
1131+ $select . val ( "attr" + val ) ;
1132+ assert . equal ( $select . val ( ) , "attr" + val , "Value ending with space character (" + key + ") selected (attr)" ) ;
1133+
1134+ $select . val ( "at" + val + "tr" ) ;
1135+ assert . equal ( $select . val ( ) , "at" + val + "tr" , "Value with space character (" + key + ") in the middle selected (attr)" ) ;
1136+
1137+ $select . val ( val + "attr" ) ;
1138+ assert . equal ( $select . val ( ) , val + "attr" , "Value starting with space character (" + key + ") selected (attr)" ) ;
1139+ } ) ;
1140+
1141+ jQuery . each ( spaces , function ( key , obj ) {
1142+ var value = obj . html || obj ,
1143+ val = obj . val || obj ;
1144+ html = "" ;
1145+ html += "<option>text" + value + "</option>" ;
1146+ html += "<option>te" + value + "xt</option>" ;
1147+ html += "<option>" + value + "text</option>" ;
1148+ $select . html ( html ) ;
1149+
1150+ $select . val ( "text" ) ;
1151+ assert . equal ( $select . val ( ) , "text" , "Value with space character at beginning or end is stripped (" + key + ") selected (text)" ) ;
1152+
1153+ if ( / ^ \\ u / . test ( key ) ) {
1154+ $select . val ( "te" + val + "xt" ) ;
1155+ assert . equal ( $select . val ( ) , "te" + val + "xt" , "Value with non-space whitespace character (" + key + ") in the middle selected (text)" ) ;
1156+ } else {
1157+ $select . val ( "te xt" ) ;
1158+ assert . equal ( $select . val ( ) , "te xt" , "Value with space character (" + key + ") in the middle selected (text)" ) ;
1159+ }
1160+ } ) ;
1161+ } ) ;
1162+
10981163var testAddClass = function ( valueObj , assert ) {
10991164 assert . expect ( 9 ) ;
11001165
@@ -1511,17 +1576,22 @@ QUnit.test( "option value not trimmed when setting via parent select", function(
15111576 assert . equal ( jQuery ( "<select><option> 2</option></select>" ) . val ( "2" ) . val ( ) , "2" ) ;
15121577} ) ;
15131578
1514- QUnit . test ( "Insignificant white space returned for $(option).val() (#14858)" , function ( assert ) {
1515- assert . expect ( 3 ) ;
1579+ QUnit . test ( "Insignificant white space returned for $(option).val() (#14858, gh-2978 )" , function ( assert ) {
1580+ assert . expect ( 16 ) ;
15161581
15171582 var val = jQuery ( "<option></option>" ) . val ( ) ;
15181583 assert . equal ( val . length , 0 , "Empty option should have no value" ) ;
15191584
1520- val = jQuery ( "<option> </option>" ) . val ( ) ;
1521- assert . equal ( val . length , 0 , "insignificant white-space returned for value" ) ;
1585+ jQuery . each ( [ " " , "\n" , "\t" , "\f" , "\r" ] , function ( i , character ) {
1586+ var val = jQuery ( "<option>" + character + "</option>" ) . val ( ) ;
1587+ assert . equal ( val . length , 0 , "insignificant white-space returned for value" ) ;
1588+
1589+ val = jQuery ( "<option>" + character + "test" + character + "</option>" ) . val ( ) ;
1590+ assert . equal ( val . length , 4 , "insignificant white-space returned for value" ) ;
15221591
1523- val = jQuery ( "<option> test </option>" ) . val ( ) ;
1524- assert . equal ( val . length , 4 , "insignificant white-space returned for value" ) ;
1592+ val = jQuery ( "<option>te" + character + "st</option>" ) . val ( ) ;
1593+ assert . equal ( val , "te st" , "Whitespace is collapsed in values" ) ;
1594+ } ) ;
15251595} ) ;
15261596
15271597QUnit . test ( "SVG class manipulation (gh-2199)" , function ( assert ) {
0 commit comments