Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8481: readAttribute.diff

File readAttribute.diff, 6.7 kB (added by Tobie, 1 year ago)
  • test/unit/dom.html

    old new  
    228228  <div foo="2"></div> 
    229229</div> 
    230230 
    231 <a id="attributes_with_issues_1" href="test.html" accesskey="L" tabindex="50" title="a link"></a> 
     231<div id="cloned_element_attributes_issue" foo="original"></div> 
     232<a id="attributes_with_issues_1" href="test.html" accesskey="L" tabindex="50" title="a link" onclick="alert('hello world');"></a> 
    232233<a id="attributes_with_issues_2" href="" accesskey="" tabindex="" title=""></a> 
    233234<a id="attributes_with_issues_3"></a> 
    234235<form id="attributes_with_issues_form" method="post" action="blah"> 
     
    10381039     
    10391040    testElementReadAttribute: function() {with(this) { 
    10401041      assertEqual('test.html' , $('attributes_with_issues_1').readAttribute('href')); 
    1041        
    10421042      assertEqual('L' , $('attributes_with_issues_1').readAttribute('accesskey')); 
    10431043      assertEqual('50' , $('attributes_with_issues_1').readAttribute('tabindex')); 
    10441044      assertEqual('a link' , $('attributes_with_issues_1').readAttribute('title')); 
    1045  
     1045       
     1046      $('cloned_element_attributes_issue').readAttribute('foo') 
     1047      var clone = $('cloned_element_attributes_issue').cloneNode(true); 
     1048      clone.writeAttribute('foo', 'cloned'); 
     1049      assertEqual('cloned', clone.readAttribute('foo')); 
     1050      assertEqual('original', $('cloned_element_attributes_issue').readAttribute('foo')); 
     1051       
    10461052      ['href', 'accesskey', 'accesskey', 'title'].each(function(attr){ 
    10471053        assertEqual('' , $('attributes_with_issues_2').readAttribute(attr)); 
    10481054      }); 
     
    10511057        assertEqual(attr, $('attributes_with_issues_'+attr).readAttribute(attr)); 
    10521058      }); 
    10531059       
     1060      assertEqual("alert('hello world');", $('attributes_with_issues_1').readAttribute('onclick')); 
     1061      assertNull($('attributes_with_issues_1').readAttribute('onmouseover')); 
     1062      
    10541063      assertEqual('date', $('attributes_with_issues_type').readAttribute('type')); 
    10551064      assertEqual('text', $('attributes_with_issues_readonly').readAttribute('type')); 
    10561065       
  • src/dom.js

    old new  
    231231  readAttribute: function(element, name) { 
    232232    element = $(element); 
    233233    if (Prototype.Browser.IE) { 
    234       if (!element.attributes) return null; 
    235234      var t = Element._attributeTranslations.read; 
    236235      if (t.values[name]) return t.values[name](element, name); 
    237       if (t.names[name])  name = t.names[name]; 
    238       var attribute = element.attributes[name]; 
    239       return attribute ? attribute.nodeValue : null; 
     236      if (t.names[name]) name = t.names[name]; 
    240237    }     
    241238    return element.getAttribute(name); 
    242239  }, 
     
    743740  Element._attributeTranslations = { 
    744741    read: { 
    745742      names: { 
    746         colspan:   "colSpan", 
    747         rowspan:   "rowSpan", 
    748         valign:    "vAlign", 
    749         datetime:  "dateTime", 
    750         accesskey: "accessKey", 
    751         tabindex:  "tabIndex", 
    752         enctype:   "encType", 
    753         maxlength: "maxLength", 
    754         readonly:  "readOnly", 
    755         longdesc:  "longDesc" 
     743        'class': 'className', 
     744        'for':   'htmlFor' 
    756745      }, 
    757746      values: { 
    758747        _getAttr: function(element, attribute) { 
    759748          return element.getAttribute(attribute, 2); 
    760749        }, 
     750        _getEv: function(element, attribute) { 
     751          var attribute = element.getAttribute(attribute); 
     752          return attribute ? attribute.toString().slice(23, -2) : null; 
     753        }, 
    761754        _flag: function(element, attribute) { 
    762755          return $(element).hasAttribute(attribute) ? attribute : null; 
    763756        }, 
     
    765758          return element.style.cssText.toLowerCase(); 
    766759        }, 
    767760        title: function(element) { 
    768           var node = element.getAttributeNode('title'); 
    769           return node.specified ? node.nodeValue : null; 
     761          return element.title; 
    770762        } 
    771763      } 
    772764    } 
     
    774766   
    775767  Element._attributeTranslations.write = { 
    776768    names: Object.extend({ 
    777         'class': 'className', 
    778         'for':   'htmlFor'     
    779       }, Element._attributeTranslations.read.names), 
     769      colspan:   'colSpan', 
     770      rowspan:   'rowSpan', 
     771      valign:    'vAlign', 
     772      datetime:  'dateTime', 
     773      accesskey: 'accessKey', 
     774      tabindex:  'tabIndex', 
     775      enctype:   'encType', 
     776      maxlength: 'maxLength', 
     777      readonly:  'readOnly', 
     778      longdesc:  'longDesc' 
     779    }, Element._attributeTranslations.read.names), 
     780     
    780781    values: { 
    781782      checked: function(element, value) { 
    782783        element.checked = !!value; 
     
    788789    } 
    789790  }; 
    790791   
    791   (function() { 
    792     Object.extend(this, { 
    793       href: this._getAttr, 
    794       src:  this._getAttr, 
    795       type: this._getAttr, 
    796       disabled: this._flag, 
    797       checked:  this._flag, 
    798       readonly: this._flag, 
    799       multiple: this._flag 
     792  (function(v) { 
     793    Object.extend(v, { 
     794      href: v._getAttr, 
     795      src:  v._getAttr, 
     796      type: v._getAttr, 
     797      disabled: v._flag, 
     798      checked:  v._flag, 
     799      readonly: v._flag, 
     800      multiple: v._flag, 
     801      onload:      v._getEv, 
     802      onunload:    v._getEv, 
     803      onclick:     v._getEv, 
     804      ondblclick:  v._getEv, 
     805      onmousedown: v._getEv, 
     806      onmouseup:   v._getEv, 
     807      onmouseover: v._getEv, 
     808      onmousemove: v._getEv, 
     809      onmouseout:  v._getEv, 
     810      onfocus:     v._getEv, 
     811      onblur:      v._getEv, 
     812      onkeypress:  v._getEv, 
     813      onkeydown:   v._getEv, 
     814      onkeyup:     v._getEv, 
     815      onsubmit:    v._getEv, 
     816      onreset:     v._getEv, 
     817      onselect:    v._getEv, 
     818      onchange:    v._getEv 
    800819    }); 
    801   }).call(Element._attributeTranslations.read.values); 
     820  })(Element._attributeTranslations.read.values); 
    802821} 
    803822 
    804823else if (Prototype.Browser.Gecko) { 
  • CHANGELOG

    old new  
    11*SVN* 
    22 
     3* Make Element#readAttribute work for cloned elements in IE. Closes #8481. [chem, Tobie Langel] 
     4 
    35* The action for Form#request defaults to the current URL if the "action" attribute is empty. (This is what most of the major browsers do.) Fixes #8483. [Tomas, Mislav Marohnić] 
    46 
    57* In form serialization, change the way submit buttons are handled. Previously all submit buttons were serialized; now Prototype serializes only the first one. Change Form#serialize and Form.serializeElements to accept a params hash. With the "hash: false" option, a serialized string is returned instead of the hash data object. With the "submit: 'foo'" option, only the submit button with the name "foo" is serialized.  References #5031.  [Mislav Marohnić]