diff options
| author | Misko Hevery | 2011-04-21 16:32:05 -0700 | 
|---|---|---|
| committer | Misko Hevery | 2011-06-08 13:49:11 -0700 | 
| commit | cc9f1fdf38bb179166212382b33e78255e669e73 (patch) | |
| tree | cb9881186ce11f18c985e93e75a20b4601851f60 | |
| parent | f243c6aeda4c7dc87dac495dfb8942f4c8ba675e (diff) | |
| download | angular.js-cc9f1fdf38bb179166212382b33e78255e669e73.tar.bz2 | |
Proper handling of special attributes in jqlite
| -rw-r--r-- | src/Angular.js | 13 | ||||
| -rw-r--r-- | src/jqLite.js | 11 | ||||
| -rw-r--r-- | src/sanitizer.js | 11 | ||||
| -rw-r--r-- | 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 @@ -413,6 +413,19 @@ function isElement(node) {  }  /** + * @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   * @param html raw (unsafe) html 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 @@ -187,17 +187,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   * @returns {string} A string with decoded entities. 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('<select>'); +      expect(select.attr('multiple')).toEqual(false); +      expect(jqLite('<select multiple>').attr('multiple')).toEqual(true); +      expect(jqLite('<select multiple="">').attr('multiple')).toEqual(true); +      expect(jqLite('<select multiple="x">').attr('multiple')).toEqual(true); + +      select.attr('multiple', false); +      expect(select.attr('multiple')).toEqual(false); + +      select.attr('multiple', true); +      expect(select.attr('multiple')).toEqual(true); +    }); +    }); | 
