diff options
| -rw-r--r-- | src/Angular.js | 8 | ||||
| -rw-r--r-- | test/AngularSpec.js | 14 | ||||
| -rw-r--r-- | test/servicesSpec.js | 12 | 
3 files changed, 26 insertions, 8 deletions
diff --git a/src/Angular.js b/src/Angular.js index 2bd21de4..3e53c755 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -370,13 +370,17 @@ function compile(element, existingScope) {  }  ///////////////////////////////////////////////// -function parseKeyValue(keyValue) { +/** + * Parses an escaped url query string into key-value pairs. + * @return Object.<(string|boolean)> + */ +function parseKeyValue(/**string*/keyValue) {    var obj = {}, key_value, key;    foreach((keyValue || "").split('&'), function(keyValue){      if (keyValue) {        key_value = keyValue.split('=');        key = unescape(key_value[0]); -      obj[key] = key_value[1] ? unescape(key_value[1]) : true; +      obj[key] = isDefined(key_value[1]) ? unescape(key_value[1]) : true;      }    });    return obj; diff --git a/test/AngularSpec.js b/test/AngularSpec.js index b4e90175..725e8ea0 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -79,5 +79,17 @@ describe('equals', function(){      expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);      expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true);    }); -  }); + +describe('parseKeyValue', function() { +  it('should parse a string into key-value pairs', function() { +    expect(parseKeyValue('')).toEqual({}); +    expect(parseKeyValue('simple=pair')).toEqual({simple: 'pair'}); +    expect(parseKeyValue('first=1&second=2')).toEqual({first: '1', second: '2'}); +    expect(parseKeyValue('escaped%20key=escaped%20value')). +      toEqual({'escaped key': 'escaped value'}); +    expect(parseKeyValue('emptyKey=')).toEqual({emptyKey: ''}); +    expect(parseKeyValue('flag1&key=value&flag2')). +      toEqual({flag1: true, key: 'value', flag2: true}); +  }); +}) diff --git a/test/servicesSpec.js b/test/servicesSpec.js index f40b8f5e..04aebce7 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -77,21 +77,23 @@ describe("service", function(){    describe("$location", function(){      it("should inject $location", function(){ -      scope.$location.parse('http://host:123/p/a/t/h.html?query=value#path?key=value'); -      expect(scope.$location.href).toEqual("http://host:123/p/a/t/h.html?query=value#path?key=value"); +      scope.$location.parse('http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2='); +      expect(scope.$location.href). +        toEqual("http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=");        expect(scope.$location.protocol).toEqual("http");        expect(scope.$location.host).toEqual("host");        expect(scope.$location.port).toEqual("123");        expect(scope.$location.path).toEqual("/p/a/t/h.html");        expect(scope.$location.search).toEqual({query:'value'}); -      expect(scope.$location.hash).toEqual('path?key=value'); +      expect(scope.$location.hash).toEqual('path?key=value&flag&key2=');        expect(scope.$location.hashPath).toEqual('path'); -      expect(scope.$location.hashSearch).toEqual({key:'value'}); +      expect(scope.$location.hashSearch).toEqual({key: 'value', flag: true, key2: ''});        scope.$location.hashPath = 'page=http://path';        scope.$location.hashSearch = {k:'a=b'}; -      expect(scope.$location.toString()).toEqual('http://host:123/p/a/t/h.html?query=value#page%3Dhttp%3A//path?k=a%3Db'); +      expect(scope.$location.toString()). +        toEqual('http://host:123/p/a/t/h.html?query=value#page%3Dhttp%3A//path?k=a%3Db');      });      it('should parse file://', function(){  | 
