From 7e6481bcd758f8ea333aa0d05b83091da54cc5ad Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 24 Jul 2007 17:31:23 +0000 Subject: [PATCH] prototype: Make Element#readAttribute work for cloned elements in IE. Closes #8481. --- CHANGELOG | 2 ++ src/dom.js | 77 +++++++++++++++++++++++++++++----------------- test/unit/dom.html | 15 +++++++-- 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 90fdd7d..6505636 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. diff --git a/src/dom.js b/src/dom.js index 0a8b455..df6e9a9 100644 --- a/src/dom.js +++ b/src/dom.js @@ -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) { diff --git a/test/unit/dom.html b/test/unit/dom.html index 1f72677..124fac7 100644 --- a/test/unit/dom.html +++ b/test/unit/dom.html @@ -228,7 +228,8 @@
- +
+
@@ -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'));