Skip to content

Commit cc9f1fd

Browse files
committed
Proper handling of special attributes in jqlite
1 parent f243c6a commit cc9f1fd

4 files changed

Lines changed: 39 additions & 13 deletions

File tree

src/Angular.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,19 @@ function isElement(node) {
412412
|| (node.bind && node.find)); // we have a bind and find method part of jQuery API
413413
}
414414

415+
/**
416+
* @param str 'key1,key2,...'
417+
* @returns {object} in the form of {key1:true, key2:true, ...}
418+
*/
419+
function makeMap(str){
420+
var obj = {}, items = str.split(","), i;
421+
for ( i = 0; i < items.length; i++ )
422+
obj[ items[i] ] = true;
423+
return obj;
424+
}
425+
426+
427+
415428
/**
416429
* HTML class which is the only class which can be used in ng:bind to inline HTML for security reasons.
417430
* @constructor

src/jqLite.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function getStyle(element) {
8484
return current;
8585
}
8686

87+
//TODO: delete me! dead code?
8788
if (msie) {
8889
extend(JQLite.prototype, {
8990
text: function(value) {
@@ -226,6 +227,8 @@ var JQLitePrototype = JQLite.prototype = {
226227
// these functions return self on setter and
227228
// value on get.
228229
//////////////////////////////////////////
230+
var SPECIAL_ATTR = makeMap("multiple,selected,checked,disabled,readonly");
231+
229232
forEach({
230233
data: JQLiteData,
231234

@@ -252,7 +255,13 @@ forEach({
252255
},
253256

254257
attr: function(element, name, value){
255-
if (isDefined(value)) {
258+
if (SPECIAL_ATTR[name]) {
259+
if (isDefined(value)) {
260+
element[name] = !!value;
261+
} else {
262+
return element[name];
263+
}
264+
} else if (isDefined(value)) {
256265
element.setAttribute(name, value);
257266
} else if (element.getAttribute) {
258267
// the extra argument "2" is to get the right thing for a.href in IE, see jQuery code

src/sanitizer.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,6 @@ function htmlParser( html, handler ) {
186186
}
187187
}
188188

189-
/**
190-
* @param str 'key1,key2,...'
191-
* @returns {object} in the form of {key1:true, key2:true, ...}
192-
*/
193-
function makeMap(str){
194-
var obj = {}, items = str.split(","), i;
195-
for ( i = 0; i < items.length; i++ )
196-
obj[ items[i] ] = true;
197-
return obj;
198-
}
199-
200189
/**
201190
* decodes all entities into regular string
202191
* @param value

test/jqLiteSpec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('jqLite', function(){
128128

129129

130130
describe('attr', function(){
131-
it('shoul read wirite and remove attr', function(){
131+
it('shoul read write and remove attr', function(){
132132
var selector = jqLite([a, b]);
133133

134134
expect(selector.attr('prop', 'value')).toEqual(selector);
@@ -147,6 +147,21 @@ describe('jqLite', function(){
147147
expect(jqLite(a).attr('prop')).toBeFalsy();
148148
expect(jqLite(b).attr('prop')).toBeFalsy();
149149
});
150+
151+
it('should read special attributes as boolean', function(){
152+
var select = jqLite('<select>');
153+
expect(select.attr('multiple')).toEqual(false);
154+
expect(jqLite('<select multiple>').attr('multiple')).toEqual(true);
155+
expect(jqLite('<select multiple="">').attr('multiple')).toEqual(true);
156+
expect(jqLite('<select multiple="x">').attr('multiple')).toEqual(true);
157+
158+
select.attr('multiple', false);
159+
expect(select.attr('multiple')).toEqual(false);
160+
161+
select.attr('multiple', true);
162+
expect(select.attr('multiple')).toEqual(true);
163+
});
164+
150165
});
151166

152167

0 commit comments

Comments
 (0)