Bug Tracker

Ticket #2930: normalized-attr.diff

File normalized-attr.diff, 2.8 kB (added by flesler, 8 months ago)

Normalizes all to undefined (change and tests)

  • src/core.js

     
    10571057                elem.parentNode.selectedIndex; 
    10581058 
    10591059            // If applicable, access the attribute via the DOM 0 way 
    1060             if ( notxml && !special && name in elem ) { 
     1060            if ( name in elem && notxml && !special ) { 
    10611061                if ( set ){ 
    10621062                    // We can't allow the type property to be changed (since it causes problems in IE) 
    10631063                    if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode ) 
     
    10801080                // convert the value to a string (all browsers do this but IE) see #1070 
    10811081                elem.setAttribute( name, "" + value ); 
    10821082 
    1083             if ( msie && special && notxml ) 
    1084                 return elem.getAttribute( name, 2 ); 
     1083            var attr = msie && notxml && special 
     1084                    // Some attributes require a special call on IE 
     1085                    ? elem.getAttribute( name, 2 ) 
     1086                    : elem.getAttribute( name ); 
    10851087 
    1086             return elem.getAttribute( name ); 
     1088            // Non-existent attributes return null, we normalize to undefined 
     1089            return attr === null ? undefined : attr; 
    10871090 
    10881091        } 
    10891092 
  • test/unit/core.js

     
    320320}); 
    321321 
    322322test("attr(String)", function() { 
    323     expect(20); 
     323    expect(26); 
    324324    equals( $('#text1').attr('value'), "Test", 'Check for value attribute' ); 
    325325    equals( $('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' ); 
    326326    equals( $('#text1').attr('type'), "text", 'Check for type attribute' ); 
     
    343343     
    344344    $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path 
    345345    equals( $('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' ); 
     346     
     347    // Enabled since [5574] 
     348    var body = document.body, $body = $(body); 
     349     
     350    ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' ); 
     351    ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' ); 
     352     
     353    body.setAttribute('foo', 'baz');     
     354    equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' ); 
     355     
     356    body.foo = 'bar'; 
     357    equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' ); 
     358     
     359    $body.attr('foo','cool'); 
     360    equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' ); 
     361     
     362    body.foo = undefined; 
     363    ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' ); 
     364     
     365    body.removeAttribute('foo'); // Cleanup 
    346366}); 
    347367 
    348368if ( !isLocal ) {