prototype: Make Element#readAttribute work for cloned elements in IE. Closes #8481.

This commit is contained in:
Sam Stephenson 2007-07-24 17:31:23 +00:00
parent ff45622e39
commit 7e6481bcd7
3 changed files with 62 additions and 32 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Make Element#readAttribute work for cloned elements in IE. Closes #8481. [chem, Tobie Langel]
* Template enhancements. Closes #8166. [Christophe Porteneuve]
- Added String#interpolate as a shortcut for new Template(...).evaluate(...).
- If you pass String#interpolate or Template#evaluate an object with a toTemplateReplacements() method, the return value of that method will be used as the replacement object.

View File

@ -236,12 +236,9 @@ Element.Methods = {
readAttribute: function(element, name) {
element = $(element);
if (Prototype.Browser.IE) {
if (!element.attributes) return null;
var t = Element._attributeTranslations.read;
if (t.values[name]) return t.values[name](element, name);
if (t.names[name]) name = t.names[name];
var attribute = element.attributes[name];
return attribute ? attribute.nodeValue : null;
if (t.names[name]) name = t.names[name];
}
return element.getAttribute(name);
},
@ -750,21 +747,17 @@ else if (Prototype.Browser.IE) {
Element._attributeTranslations = {
read: {
names: {
colspan: "colSpan",
rowspan: "rowSpan",
valign: "vAlign",
datetime: "dateTime",
accesskey: "accessKey",
tabindex: "tabIndex",
enctype: "encType",
maxlength: "maxLength",
readonly: "readOnly",
longdesc: "longDesc"
'class': 'className',
'for': 'htmlFor'
},
values: {
_getAttr: function(element, attribute) {
return element.getAttribute(attribute, 2);
},
_getEv: function(element, attribute) {
var attribute = element.getAttribute(attribute);
return attribute ? attribute.toString().slice(23, -2) : null;
},
_flag: function(element, attribute) {
return $(element).hasAttribute(attribute) ? attribute : null;
},
@ -772,8 +765,7 @@ else if (Prototype.Browser.IE) {
return element.style.cssText.toLowerCase();
},
title: function(element) {
var node = element.getAttributeNode('title');
return node.specified ? node.nodeValue : null;
return element.title;
}
}
}
@ -781,9 +773,18 @@ else if (Prototype.Browser.IE) {
Element._attributeTranslations.write = {
names: Object.extend({
'class': 'className',
'for': 'htmlFor'
}, Element._attributeTranslations.read.names),
colspan: 'colSpan',
rowspan: 'rowSpan',
valign: 'vAlign',
datetime: 'dateTime',
accesskey: 'accessKey',
tabindex: 'tabIndex',
enctype: 'encType',
maxlength: 'maxLength',
readonly: 'readOnly',
longdesc: 'longDesc'
}, Element._attributeTranslations.read.names),
values: {
checked: function(element, value) {
element.checked = !!value;
@ -795,17 +796,35 @@ else if (Prototype.Browser.IE) {
}
};
(function() {
Object.extend(this, {
href: this._getAttr,
src: this._getAttr,
type: this._getAttr,
disabled: this._flag,
checked: this._flag,
readonly: this._flag,
multiple: this._flag
(function(v) {
Object.extend(v, {
href: v._getAttr,
src: v._getAttr,
type: v._getAttr,
disabled: v._flag,
checked: v._flag,
readonly: v._flag,
multiple: v._flag,
onload: v._getEv,
onunload: v._getEv,
onclick: v._getEv,
ondblclick: v._getEv,
onmousedown: v._getEv,
onmouseup: v._getEv,
onmouseover: v._getEv,
onmousemove: v._getEv,
onmouseout: v._getEv,
onfocus: v._getEv,
onblur: v._getEv,
onkeypress: v._getEv,
onkeydown: v._getEv,
onkeyup: v._getEv,
onsubmit: v._getEv,
onreset: v._getEv,
onselect: v._getEv,
onchange: v._getEv
});
}).call(Element._attributeTranslations.read.values);
})(Element._attributeTranslations.read.values);
}
else if (Prototype.Browser.Gecko) {

View File

@ -228,7 +228,8 @@
<div foo="2"></div>
</div>
<a id="attributes_with_issues_1" href="test.html" accesskey="L" tabindex="50" title="a link"></a>
<div id="cloned_element_attributes_issue" foo="original"></div>
<a id="attributes_with_issues_1" href="test.html" accesskey="L" tabindex="50" title="a link" onclick="alert('hello world');"></a>
<a id="attributes_with_issues_2" href="" accesskey="" tabindex="" title=""></a>
<a id="attributes_with_issues_3"></a>
<form id="attributes_with_issues_form" method="post" action="blah">
@ -1048,11 +1049,16 @@
testElementReadAttribute: function() {with(this) {
assertEqual('test.html' , $('attributes_with_issues_1').readAttribute('href'));
assertEqual('L' , $('attributes_with_issues_1').readAttribute('accesskey'));
assertEqual('50' , $('attributes_with_issues_1').readAttribute('tabindex'));
assertEqual('a link' , $('attributes_with_issues_1').readAttribute('title'));
$('cloned_element_attributes_issue').readAttribute('foo')
var clone = $('cloned_element_attributes_issue').cloneNode(true);
clone.writeAttribute('foo', 'cloned');
assertEqual('cloned', clone.readAttribute('foo'));
assertEqual('original', $('cloned_element_attributes_issue').readAttribute('foo'));
['href', 'accesskey', 'accesskey', 'title'].each(function(attr){
assertEqual('' , $('attributes_with_issues_2').readAttribute(attr));
});
@ -1061,6 +1067,9 @@
assertEqual(attr, $('attributes_with_issues_'+attr).readAttribute(attr));
});
assertEqual("alert('hello world');", $('attributes_with_issues_1').readAttribute('onclick'));
assertNull($('attributes_with_issues_1').readAttribute('onmouseover'));
assertEqual('date', $('attributes_with_issues_type').readAttribute('type'));
assertEqual('text', $('attributes_with_issues_readonly').readAttribute('type'));