Skip to content

Commit c020867

Browse files
committed
Proper handling of special attributes in jqlite
1 parent 3074a52 commit c020867

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/Angular.js

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

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

src/jqLite.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function getStyle(element) {
3232
return current;
3333
}
3434

35+
//TODO: delete me! dead code?
3536
if (msie) {
3637
extend(JQLite.prototype, {
3738
text: function(value) {
@@ -174,6 +175,8 @@ var JQLitePrototype = JQLite.prototype = {
174175
// these functions return self on setter and
175176
// value on get.
176177
//////////////////////////////////////////
178+
var SPECIAL_ATTR = makeMap("multiple,selected,checked,disabled,readonly");
179+
177180
forEach({
178181
data: JQLiteData,
179182

@@ -200,7 +203,13 @@ forEach({
200203
},
201204

202205
attr: function(element, name, value){
203-
if (isDefined(value)) {
206+
if (SPECIAL_ATTR[name]) {
207+
if (isDefined(value)) {
208+
element[name] = !!value;
209+
} else {
210+
return element[name];
211+
}
212+
} else if (isDefined(value)) {
204213
element.setAttribute(name, value);
205214
} else if (element.getAttribute) {
206215
// 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)