mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
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
This commit is contained in:
parent
984acdc627
commit
eb8d46d380
3 changed files with 26 additions and 8 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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});
|
||||
});
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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(){
|
||||
|
|
|
|||
Loading…
Reference in a new issue