Permalink
Browse files

Fixes #2109.

IE < 9, getAttribute returns properties set via `el.attribute = value`.
Other browsers return null for such properties that are not part of the
specs (e.g. `href`, `width`, or `title`).

This tries to normalize the behavior and prevents unwanted "expando"
properties from being returned (e.g. `inject` and other Element
methods).

TL;DR: whitelist any setProperty, and getProperty checks in the
whitelist or if the attribute is an expando (not part of the known
specs).

Fixed a spec with maxlenght property, when it needed to be maxlength.

PASSED: IE6-9; FFx 3-5, 8, 10, Opera 11, Chrome latest, Safari 5
  • Loading branch information...
1 parent 27c958b commit 87158363760361263ef39fb5bc43062e8161e74f @ibolmo ibolmo committed Dec 12, 2011
Showing with 36 additions and 3 deletions.
  1. +25 −2 Source/Element/Element.js
  2. +1 −1 Specs/1.3client/Element/Element.js
  3. +10 −0 Specs/1.4client/Element/Element.js
@@ -590,15 +590,32 @@ if (el.type != 'button') propertySetters.type = function(node, value){
/* getProperty, setProperty */
+/* <ltIE9> */
+var pollutesGetAttribute = (function(div){
+ div.random = 'attribute';
+ return (div.getAttribute('random') == 'attribute');
+})(document.createElement('div'));
+
+alert(pollutesGetAttribute);
+
+if (pollutesGetAttribute) var attributeWhiteList = {};
+/* <ltIE9> */
+
Element.implement({
setProperty: function(name, value){
var setter = propertySetters[name.toLowerCase()];
if (setter){
setter(this, value);
} else {
- if (value == null) this.removeAttribute(name);
- else this.setAttribute(name, value);
+ if (value == null){
+ this.removeAttribute(name);
+ } else {
+ this.setAttribute(name, value);
+ /* <ltIE9> */
+ if (pollutesGetAttribute) attributeWhiteList[name] = true;
+ /* </ltIE9> */
+ }
}
return this;
},
@@ -611,6 +628,12 @@ Element.implement({
getProperty: function(name){
var getter = propertyGetters[name.toLowerCase()];
if (getter) return getter(this);
+ /* <ltIE9> */
+ if (pollutesGetAttribute && !attributeWhiteList[name]){
+ var attr = this.getAttributeNode(name);
+ if (!attr || attr.expando) return null;
+ }
+ /* </ltIE9> */
var result = Slick.getAttribute(this, name);
return (!result && !Slick.hasAttribute(this, name)) ? null : result;
},
@@ -43,7 +43,7 @@ describe('Element.getElementById', function(){
describe('Element.removeProperty', function(){
it('should removeProperty from an Element', function (){
- var readonly = new Element('input', { type: 'text', readonly: 'readonly', maxlenght: 10 });
+ var readonly = new Element('input', { type: 'text', readonly: 'readonly', maxlength: 10 });
readonly.removeProperty('readonly');
readonly.removeProperty('maxlength');
var props = readonly.getProperties('type', 'readonly');
@@ -16,6 +16,16 @@ describe('Element', function(){
expect($(div.firstChild).getProperty('action')).toEqual('s');
});
+ it('should ignore expandos', function(){
+ var div = new Element('div');
+ expect(div.getProperty('inject')).toBeNull();
+ });
+
+ it('should work in collaboration with setProperty', function(){
+ var div = new Element('div', {random: 'attribute'});
+ expect(div.getProperty('random')).toEqual('attribute');
+ });
+
});
describe('Element.set', function(){

0 comments on commit 8715836

Please sign in to comment.