From cc9f1fdf38bb179166212382b33e78255e669e73 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 21 Apr 2011 16:32:05 -0700 Subject: Proper handling of special attributes in jqlite --- src/Angular.js | 13 +++++++++++++ src/jqLite.js | 11 ++++++++++- src/sanitizer.js | 11 ----------- test/jqLiteSpec.js | 17 ++++++++++++++++- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index de6f2e5b..7249fb69 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -412,6 +412,19 @@ function isElement(node) { || (node.bind && node.find)); // we have a bind and find method part of jQuery API } +/** + * @param str 'key1,key2,...' + * @returns {object} in the form of {key1:true, key2:true, ...} + */ +function makeMap(str){ + var obj = {}, items = str.split(","), i; + for ( i = 0; i < items.length; i++ ) + obj[ items[i] ] = true; + return obj; +} + + + /** * HTML class which is the only class which can be used in ng:bind to inline HTML for security reasons. * @constructor diff --git a/src/jqLite.js b/src/jqLite.js index c86c265a..68cda632 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -84,6 +84,7 @@ function getStyle(element) { return current; } +//TODO: delete me! dead code? if (msie) { extend(JQLite.prototype, { text: function(value) { @@ -226,6 +227,8 @@ var JQLitePrototype = JQLite.prototype = { // these functions return self on setter and // value on get. ////////////////////////////////////////// +var SPECIAL_ATTR = makeMap("multiple,selected,checked,disabled,readonly"); + forEach({ data: JQLiteData, @@ -252,7 +255,13 @@ forEach({ }, attr: function(element, name, value){ - if (isDefined(value)) { + if (SPECIAL_ATTR[name]) { + if (isDefined(value)) { + element[name] = !!value; + } else { + return element[name]; + } + } else if (isDefined(value)) { element.setAttribute(name, value); } else if (element.getAttribute) { // the extra argument "2" is to get the right thing for a.href in IE, see jQuery code diff --git a/src/sanitizer.js b/src/sanitizer.js index 7bd26455..2cf126dc 100644 --- a/src/sanitizer.js +++ b/src/sanitizer.js @@ -186,17 +186,6 @@ function htmlParser( html, handler ) { } } -/** - * @param str 'key1,key2,...' - * @returns {object} in the form of {key1:true, key2:true, ...} - */ -function makeMap(str){ - var obj = {}, items = str.split(","), i; - for ( i = 0; i < items.length; i++ ) - obj[ items[i] ] = true; - return obj; -} - /** * decodes all entities into regular string * @param value diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 3486fbbb..5aec0cff 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -128,7 +128,7 @@ describe('jqLite', function(){ describe('attr', function(){ - it('shoul read wirite and remove attr', function(){ + it('shoul read write and remove attr', function(){ var selector = jqLite([a, b]); expect(selector.attr('prop', 'value')).toEqual(selector); @@ -147,6 +147,21 @@ describe('jqLite', function(){ expect(jqLite(a).attr('prop')).toBeFalsy(); expect(jqLite(b).attr('prop')).toBeFalsy(); }); + + it('should read special attributes as boolean', function(){ + var select = jqLite('').attr('multiple')).toEqual(true); + expect(jqLite('').attr('multiple')).toEqual(true); + + select.attr('multiple', false); + expect(select.attr('multiple')).toEqual(false); + + select.attr('multiple', true); + expect(select.attr('multiple')).toEqual(true); + }); + }); -- cgit v1.2.3