@@ -1098,6 +1098,71 @@ QUnit.test( "val(select) after form.reset() (Bug #2551)", function( assert ) {
10981098 jQuery ( "#kk" ) . remove ( ) ;
10991099} ) ;
11001100
1101+ QUnit . test ( "select.val(space characters) (gh-2978)" , function ( assert ) {
1102+ assert . expect ( 35 ) ;
1103+
1104+ var $select = jQuery ( "<select/>" ) . appendTo ( "#qunit-fixture" ) ,
1105+ spaces = {
1106+ "\\t" : {
1107+ html : "	" ,
1108+ val : "\t"
1109+ } ,
1110+ "\\n" : {
1111+ html : " " ,
1112+ val : "\n"
1113+ } ,
1114+ "\\r" : {
1115+ html : " " ,
1116+ val : "\r"
1117+ } ,
1118+ "\\f" : "\f" ,
1119+ "space" : " " ,
1120+ "\\u00a0" : "\u00a0" ,
1121+ "\\u1680" : "\u1680"
1122+ } ,
1123+ html = "" ;
1124+ jQuery . each ( spaces , function ( key , obj ) {
1125+ var value = obj . html || obj ;
1126+ html += "<option value='attr" + value + "'></option>" ;
1127+ html += "<option value='at" + value + "tr'></option>" ;
1128+ html += "<option value='" + value + "attr'></option>" ;
1129+ } ) ;
1130+ $select . html ( html ) ;
1131+
1132+ jQuery . each ( spaces , function ( key , obj ) {
1133+ var val = obj . val || obj ;
1134+ $select . val ( "attr" + val ) ;
1135+ assert . equal ( $select . val ( ) , "attr" + val , "Value ending with space character (" + key + ") selected (attr)" ) ;
1136+
1137+ $select . val ( "at" + val + "tr" ) ;
1138+ assert . equal ( $select . val ( ) , "at" + val + "tr" , "Value with space character (" + key + ") in the middle selected (attr)" ) ;
1139+
1140+ $select . val ( val + "attr" ) ;
1141+ assert . equal ( $select . val ( ) , val + "attr" , "Value starting with space character (" + key + ") selected (attr)" ) ;
1142+ } ) ;
1143+
1144+ jQuery . each ( spaces , function ( key , obj ) {
1145+ var value = obj . html || obj ,
1146+ val = obj . val || obj ;
1147+ html = "" ;
1148+ html += "<option>text" + value + "</option>" ;
1149+ html += "<option>te" + value + "xt</option>" ;
1150+ html += "<option>" + value + "text</option>" ;
1151+ $select . html ( html ) ;
1152+
1153+ $select . val ( "text" ) ;
1154+ assert . equal ( $select . val ( ) , "text" , "Value with space character at beginning or end is stripped (" + key + ") selected (text)" ) ;
1155+
1156+ if ( / ^ \\ u / . test ( key ) ) {
1157+ $select . val ( "te" + val + "xt" ) ;
1158+ assert . equal ( $select . val ( ) , "te" + val + "xt" , "Value with non-space whitespace character (" + key + ") in the middle selected (text)" ) ;
1159+ } else {
1160+ $select . val ( "te xt" ) ;
1161+ assert . equal ( $select . val ( ) , "te xt" , "Value with space character (" + key + ") in the middle selected (text)" ) ;
1162+ }
1163+ } ) ;
1164+ } ) ;
1165+
11011166var testAddClass = function ( valueObj , assert ) {
11021167 assert . expect ( 9 ) ;
11031168
@@ -1515,17 +1580,22 @@ QUnit.test( "option value not trimmed when setting via parent select", function(
15151580 assert . equal ( jQuery ( "<select><option> 2</option></select>" ) . val ( "2" ) . val ( ) , "2" ) ;
15161581} ) ;
15171582
1518- QUnit . test ( "Insignificant white space returned for $(option).val() (#14858)" , function ( assert ) {
1519- assert . expect ( 3 ) ;
1583+ QUnit . test ( "Insignificant white space returned for $(option).val() (#14858, gh-2978 )" , function ( assert ) {
1584+ assert . expect ( 16 ) ;
15201585
15211586 var val = jQuery ( "<option></option>" ) . val ( ) ;
15221587 assert . equal ( val . length , 0 , "Empty option should have no value" ) ;
15231588
1524- val = jQuery ( "<option> </option>" ) . val ( ) ;
1525- assert . equal ( val . length , 0 , "insignificant white-space returned for value" ) ;
1589+ jQuery . each ( [ " " , "\n" , "\t" , "\f" , "\r" ] , function ( i , character ) {
1590+ var val = jQuery ( "<option>" + character + "</option>" ) . val ( ) ;
1591+ assert . equal ( val . length , 0 , "insignificant white-space returned for value" ) ;
1592+
1593+ val = jQuery ( "<option>" + character + "test" + character + "</option>" ) . val ( ) ;
1594+ assert . equal ( val . length , 4 , "insignificant white-space returned for value" ) ;
15261595
1527- val = jQuery ( "<option> test </option>" ) . val ( ) ;
1528- assert . equal ( val . length , 4 , "insignificant white-space returned for value" ) ;
1596+ val = jQuery ( "<option>te" + character + "st</option>" ) . val ( ) ;
1597+ assert . equal ( val , "te st" , "Whitespace is collapsed in values" ) ;
1598+ } ) ;
15291599} ) ;
15301600
15311601QUnit . test ( "SVG class manipulation (gh-2199)" , function ( assert ) {
0 commit comments