From 043653a28292a23838ada7618879e48ac5a5aadf Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Mon, 23 Feb 2009 20:20:21 -0600 Subject: [PATCH] Make sure `getAttribute` is used without flag when accessing the "type" attribute of an iframe (IE throws error otherwise). [#118 state:resolved] (Zekid, kangax) --- CHANGELOG | 2 ++ src/dom/dom.js | 49 +++++++++++++++++++++++++++++++------------ test/unit/dom_test.js | 5 +++++ 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e011e29..77bb59d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Make sure `getAttribute` is used without flag when accessing the "type" attribute of an iframe (IE throws error otherwise). [#118 state:resolved] (Zekid, kangax) + * String#gsub should escape RegExp metacharacters when the first argument is a string. [#469 state:resolved] (michael, kangax) * Fix order of replacement in String#unescapeHTML [#544 state:resolved] (SWeini, kangax) diff --git a/src/dom/dom.js b/src/dom/dom.js index 7e0f0bd..2e78d4f 100644 --- a/src/dom/dom.js +++ b/src/dom/dom.js @@ -274,20 +274,43 @@ Element.Methods = { return id; }, - readAttribute: function(element, name) { - element = $(element); - if (Prototype.Browser.IE) { - var t = Element._attributeTranslations.read; - if (t.values[name]) return t.values[name](element, name); - if (t.names[name]) name = t.names[name]; - if (name.include(':')) { - return (!element.attributes || !element.attributes[name]) ? null : - element.attributes[name].value; - } - } + readAttribute: (function(){ - return element.getAttribute(name); - }, + var iframeGetAttributeThrowsError = (function(){ + var el = document.createElement('iframe'), + isBuggy = false; + + document.documentElement.appendChild(el); + try { + el.getAttribute('type', 2); + } catch(e) { + isBuggy = true; + } + document.documentElement.removeChild(el); + el = null; + return isBuggy; + })(); + + return function(element, name) { + element = $(element); + // check boolean first, to get out of expression faster + if (iframeGetAttributeThrowsError && + name === 'type' && + element.tagName.toUpperCase() == 'IFRAME') { + return element.getAttribute('type'); + } + if (Prototype.Browser.IE) { + var t = Element._attributeTranslations.read; + if (t.values[name]) return t.values[name](element, name); + if (t.names[name]) name = t.names[name]; + if (name.include(':')) { + return (!element.attributes || !element.attributes[name]) ? null : + element.attributes[name].value; + } + } + return element.getAttribute(name); + } + })(), writeAttribute: function(element, name, value) { element = $(element); diff --git a/test/unit/dom_test.js b/test/unit/dom_test.js index 4e02c5f..bfbee38 100644 --- a/test/unit/dom_test.js +++ b/test/unit/dom_test.js @@ -933,6 +933,11 @@ new Test.Unit.Runner({ var table = $('write_attribute_table'); this.assertEqual('4', table.readAttribute('cellspacing')); this.assertEqual('6', table.readAttribute('cellpadding')); + + var el = document.createElement('iframe'); + document.body.appendChild(el); + alert(Element.readAttribute(el, 'type')); + document.body.removeChild(el); }, testElementWriteAttribute: function() {