Skip to content

Commit eb8d46d

Browse files
IgorMinarmhevery
authored andcommitted
Differentiate between flags and empty keys in $location.hashSearch
* #foo?key=var&flag&emptyKey= should parse into {key:'val', flag: true, emptyKey: ''} * added docs and spec for parseKeyValue function
1 parent 984acdc commit eb8d46d

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

src/Angular.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,17 @@ function compile(element, existingScope) {
370370
}
371371
/////////////////////////////////////////////////
372372

373-
function parseKeyValue(keyValue) {
373+
/**
374+
* Parses an escaped url query string into key-value pairs.
375+
* @return Object.<(string|boolean)>
376+
*/
377+
function parseKeyValue(/**string*/keyValue) {
374378
var obj = {}, key_value, key;
375379
foreach((keyValue || "").split('&'), function(keyValue){
376380
if (keyValue) {
377381
key_value = keyValue.split('=');
378382
key = unescape(key_value[0]);
379-
obj[key] = key_value[1] ? unescape(key_value[1]) : true;
383+
obj[key] = isDefined(key_value[1]) ? unescape(key_value[1]) : true;
380384
}
381385
});
382386
return obj;

test/AngularSpec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,17 @@ describe('equals', function(){
7979
expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);
8080
expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true);
8181
});
82-
8382
});
83+
84+
describe('parseKeyValue', function() {
85+
it('should parse a string into key-value pairs', function() {
86+
expect(parseKeyValue('')).toEqual({});
87+
expect(parseKeyValue('simple=pair')).toEqual({simple: 'pair'});
88+
expect(parseKeyValue('first=1&second=2')).toEqual({first: '1', second: '2'});
89+
expect(parseKeyValue('escaped%20key=escaped%20value')).
90+
toEqual({'escaped key': 'escaped value'});
91+
expect(parseKeyValue('emptyKey=')).toEqual({emptyKey: ''});
92+
expect(parseKeyValue('flag1&key=value&flag2')).
93+
toEqual({flag1: true, key: 'value', flag2: true});
94+
});
95+
})

test/servicesSpec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,23 @@ describe("service", function(){
7777

7878
describe("$location", function(){
7979
it("should inject $location", function(){
80-
scope.$location.parse('http://host:123/p/a/t/h.html?query=value#path?key=value');
81-
expect(scope.$location.href).toEqual("http://host:123/p/a/t/h.html?query=value#path?key=value");
80+
scope.$location.parse('http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=');
81+
expect(scope.$location.href).
82+
toEqual("http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=");
8283
expect(scope.$location.protocol).toEqual("http");
8384
expect(scope.$location.host).toEqual("host");
8485
expect(scope.$location.port).toEqual("123");
8586
expect(scope.$location.path).toEqual("/p/a/t/h.html");
8687
expect(scope.$location.search).toEqual({query:'value'});
87-
expect(scope.$location.hash).toEqual('path?key=value');
88+
expect(scope.$location.hash).toEqual('path?key=value&flag&key2=');
8889
expect(scope.$location.hashPath).toEqual('path');
89-
expect(scope.$location.hashSearch).toEqual({key:'value'});
90+
expect(scope.$location.hashSearch).toEqual({key: 'value', flag: true, key2: ''});
9091

9192
scope.$location.hashPath = 'page=http://path';
9293
scope.$location.hashSearch = {k:'a=b'};
9394

94-
expect(scope.$location.toString()).toEqual('http://host:123/p/a/t/h.html?query=value#page%3Dhttp%3A//path?k=a%3Db');
95+
expect(scope.$location.toString()).
96+
toEqual('http://host:123/p/a/t/h.html?query=value#page%3Dhttp%3A//path?k=a%3Db');
9597
});
9698

9799
it('should parse file://', function(){

0 commit comments

Comments
 (0)