mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-04 21:24:42 +00:00
bug($cookie): set on app base path rather the current path.
This commit is contained in:
parent
7f0eb15161
commit
d0159454df
2 changed files with 65 additions and 48 deletions
|
|
@ -17,12 +17,11 @@
|
||||||
/**
|
/**
|
||||||
* @param {object} window The global window object.
|
* @param {object} window The global window object.
|
||||||
* @param {object} document jQuery wrapped document.
|
* @param {object} document jQuery wrapped document.
|
||||||
* @param {object} body jQuery wrapped document.body.
|
|
||||||
* @param {function()} XHR XMLHttpRequest constructor.
|
* @param {function()} XHR XMLHttpRequest constructor.
|
||||||
* @param {object} $log console.log or an object with the same interface.
|
* @param {object} $log console.log or an object with the same interface.
|
||||||
* @param {object} $sniffer $sniffer service
|
* @param {object} $sniffer $sniffer service
|
||||||
*/
|
*/
|
||||||
function Browser(window, document, body, $log, $sniffer) {
|
function Browser(window, document, $log, $sniffer) {
|
||||||
var self = this,
|
var self = this,
|
||||||
rawDocument = document[0],
|
rawDocument = document[0],
|
||||||
location = window.location,
|
location = window.location,
|
||||||
|
|
@ -221,11 +220,27 @@ function Browser(window, document, body, $log, $sniffer) {
|
||||||
return callback;
|
return callback;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////
|
||||||
|
// Misc API
|
||||||
|
//////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns current <base href>
|
||||||
|
* (always relative - without domain)
|
||||||
|
*
|
||||||
|
* @returns {string=}
|
||||||
|
*/
|
||||||
|
self.baseHref = function() {
|
||||||
|
var href = document.find('base').attr('href');
|
||||||
|
return href ? href.replace(/^https?\:\/\/[^\/]*/, '') : href;
|
||||||
|
};
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////
|
||||||
// Cookies API
|
// Cookies API
|
||||||
//////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////
|
||||||
var lastCookies = {};
|
var lastCookies = {};
|
||||||
var lastCookieString = '';
|
var lastCookieString = '';
|
||||||
|
var cookiePath = self.baseHref();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc method
|
* @ngdoc method
|
||||||
|
|
@ -253,12 +268,10 @@ function Browser(window, document, body, $log, $sniffer) {
|
||||||
|
|
||||||
if (name) {
|
if (name) {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
rawDocument.cookie = escape(name) + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
rawDocument.cookie = escape(name) + "=;path=" + cookiePath + ";expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
||||||
} else {
|
} else {
|
||||||
if (isString(value)) {
|
if (isString(value)) {
|
||||||
rawDocument.cookie = escape(name) + '=' + escape(value);
|
cookieLength = (rawDocument.cookie = escape(name) + '=' + escape(value) + ';path=' + cookiePath).length + 1;
|
||||||
|
|
||||||
cookieLength = name.length + value.length + 1;
|
|
||||||
if (cookieLength > 4096) {
|
if (cookieLength > 4096) {
|
||||||
$log.warn("Cookie '"+ name +"' possibly not set or overflowed because it was too large ("+
|
$log.warn("Cookie '"+ name +"' possibly not set or overflowed because it was too large ("+
|
||||||
cookieLength + " > 4096 bytes)!");
|
cookieLength + " > 4096 bytes)!");
|
||||||
|
|
@ -338,26 +351,11 @@ function Browser(window, document, body, $log, $sniffer) {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////
|
|
||||||
// Misc API
|
|
||||||
//////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns current <base href>
|
|
||||||
* (always relative - without domain)
|
|
||||||
*
|
|
||||||
* @returns {string=}
|
|
||||||
*/
|
|
||||||
self.baseHref = function() {
|
|
||||||
var href = document.find('base').attr('href');
|
|
||||||
return href ? href.replace(/^https?\:\/\/[^\/]*/, '') : href;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function $BrowserProvider(){
|
function $BrowserProvider(){
|
||||||
this.$get = ['$window', '$log', '$sniffer', '$document',
|
this.$get = ['$window', '$log', '$sniffer', '$document',
|
||||||
function( $window, $log, $sniffer, $document){
|
function( $window, $log, $sniffer, $document){
|
||||||
return new Browser($window, $document, $document.find('body'), $log, $sniffer);
|
return new Browser($window, $document, $log, $sniffer);
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,15 +46,39 @@ function MockWindow() {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function MockDocument() {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
this[0] = window.document
|
||||||
|
this.basePath = '/';
|
||||||
|
|
||||||
|
this.find = function(name) {
|
||||||
|
if (name == 'base') {
|
||||||
|
return {
|
||||||
|
attr: function(name){
|
||||||
|
if (name == 'href') {
|
||||||
|
return self.basePath;
|
||||||
|
} else {
|
||||||
|
throw new Error(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe('browser', function() {
|
describe('browser', function() {
|
||||||
|
|
||||||
var browser, fakeWindow, logs, scripts, removedScripts, sniffer;
|
var browser, fakeWindow, fakeDocument, logs, scripts, removedScripts, sniffer;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
scripts = [];
|
scripts = [];
|
||||||
removedScripts = [];
|
removedScripts = [];
|
||||||
sniffer = {history: true, hashchange: true};
|
sniffer = {history: true, hashchange: true};
|
||||||
fakeWindow = new MockWindow();
|
fakeWindow = new MockWindow();
|
||||||
|
fakeDocument = new MockDocument();
|
||||||
|
|
||||||
var fakeBody = [{appendChild: function(node){scripts.push(node);},
|
var fakeBody = [{appendChild: function(node){scripts.push(node);},
|
||||||
removeChild: function(node){removedScripts.push(node);}}];
|
removeChild: function(node){removedScripts.push(node);}}];
|
||||||
|
|
@ -66,7 +90,7 @@ describe('browser', function() {
|
||||||
info: function() { logs.info.push(slice.call(arguments)); },
|
info: function() { logs.info.push(slice.call(arguments)); },
|
||||||
error: function() { logs.error.push(slice.call(arguments)); }};
|
error: function() { logs.error.push(slice.call(arguments)); }};
|
||||||
|
|
||||||
browser = new Browser(fakeWindow, jqLite(window.document), fakeBody, fakeLog, sniffer);
|
browser = new Browser(fakeWindow, fakeDocument, fakeLog, sniffer);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should contain cookie cruncher', function() {
|
it('should contain cookie cruncher', function() {
|
||||||
|
|
@ -137,12 +161,17 @@ describe('browser', function() {
|
||||||
|
|
||||||
function deleteAllCookies() {
|
function deleteAllCookies() {
|
||||||
var cookies = document.cookie.split(";");
|
var cookies = document.cookie.split(";");
|
||||||
|
var path = location.pathname;
|
||||||
|
|
||||||
for (var i = 0; i < cookies.length; i++) {
|
for (var i = 0; i < cookies.length; i++) {
|
||||||
var cookie = cookies[i];
|
var cookie = cookies[i];
|
||||||
var eqPos = cookie.indexOf("=");
|
var eqPos = cookie.indexOf("=");
|
||||||
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
|
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
|
||||||
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
var parts = path.split('/');
|
||||||
|
while (parts.length) {
|
||||||
|
document.cookie = name + "=;path=" + (parts.join('/') || '/') + ";expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
||||||
|
parts.pop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -171,7 +200,7 @@ describe('browser', function() {
|
||||||
describe('remove via cookies(cookieName, undefined)', function() {
|
describe('remove via cookies(cookieName, undefined)', function() {
|
||||||
|
|
||||||
it('should remove a cookie when it is present', function() {
|
it('should remove a cookie when it is present', function() {
|
||||||
document.cookie = 'foo=bar';
|
document.cookie = 'foo=bar;path=/';
|
||||||
|
|
||||||
browser.cookies('foo', undefined);
|
browser.cookies('foo', undefined);
|
||||||
|
|
||||||
|
|
@ -198,7 +227,7 @@ describe('browser', function() {
|
||||||
|
|
||||||
|
|
||||||
it('should overwrite an existing unsynced cookie', function() {
|
it('should overwrite an existing unsynced cookie', function() {
|
||||||
document.cookie = "cookie=new";
|
document.cookie = "cookie=new;path=/";
|
||||||
|
|
||||||
var oldVal = browser.cookies('cookie', 'newer');
|
var oldVal = browser.cookies('cookie', 'newer');
|
||||||
|
|
||||||
|
|
@ -220,7 +249,7 @@ describe('browser', function() {
|
||||||
it('should log warnings when 4kb per cookie storage limit is reached', function() {
|
it('should log warnings when 4kb per cookie storage limit is reached', function() {
|
||||||
var i, longVal = '', cookieStr;
|
var i, longVal = '', cookieStr;
|
||||||
|
|
||||||
for(i=0; i<4091; i++) {
|
for(i=0; i<4083; i++) {
|
||||||
longVal += '+';
|
longVal += '+';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -286,13 +315,13 @@ describe('browser', function() {
|
||||||
|
|
||||||
|
|
||||||
it ('should return a value for an existing cookie', function() {
|
it ('should return a value for an existing cookie', function() {
|
||||||
document.cookie = "foo=bar=baz";
|
document.cookie = "foo=bar=baz;path=/";
|
||||||
expect(browser.cookies().foo).toEqual('bar=baz');
|
expect(browser.cookies().foo).toEqual('bar=baz');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it ('should unescape cookie values that were escaped by puts', function() {
|
it ('should unescape cookie values that were escaped by puts', function() {
|
||||||
document.cookie = "cookie2%3Dbar%3Bbaz=val%3Due";
|
document.cookie = "cookie2%3Dbar%3Bbaz=val%3Due;path=/";
|
||||||
expect(browser.cookies()['cookie2=bar;baz']).toEqual('val=ue');
|
expect(browser.cookies()['cookie2=bar;baz']).toEqual('val=ue');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -308,8 +337,8 @@ describe('browser', function() {
|
||||||
describe('getAll via cookies()', function() {
|
describe('getAll via cookies()', function() {
|
||||||
|
|
||||||
it('should return cookies as hash', function() {
|
it('should return cookies as hash', function() {
|
||||||
document.cookie = "foo1=bar1";
|
document.cookie = "foo1=bar1;path=/";
|
||||||
document.cookie = "foo2=bar2";
|
document.cookie = "foo2=bar2;path=/";
|
||||||
expect(browser.cookies()).toEqual({'foo1':'bar1', 'foo2':'bar2'});
|
expect(browser.cookies()).toEqual({'foo1':'bar1', 'foo2':'bar2'});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -324,13 +353,13 @@ describe('browser', function() {
|
||||||
browser.cookies('oatmealCookie', 'drool');
|
browser.cookies('oatmealCookie', 'drool');
|
||||||
expect(browser.cookies()).toEqual({'oatmealCookie':'drool'});
|
expect(browser.cookies()).toEqual({'oatmealCookie':'drool'});
|
||||||
|
|
||||||
document.cookie = 'oatmealCookie=changed';
|
document.cookie = 'oatmealCookie=changed;path=/';
|
||||||
expect(browser.cookies().oatmealCookie).toEqual('changed');
|
expect(browser.cookies().oatmealCookie).toEqual('changed');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should initialize cookie cache with existing cookies', function() {
|
it('should initialize cookie cache with existing cookies', function() {
|
||||||
document.cookie = "existingCookie=existingValue";
|
document.cookie = "existingCookie=existingValue;path=/";
|
||||||
expect(browser.cookies()).toEqual({'existingCookie':'existingValue'});
|
expect(browser.cookies()).toEqual({'existingCookie':'existingValue'});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -530,35 +559,25 @@ describe('browser', function() {
|
||||||
describe('baseHref', function() {
|
describe('baseHref', function() {
|
||||||
var jqDocHead;
|
var jqDocHead;
|
||||||
|
|
||||||
function setDocumentBaseHrefTo(href) {
|
|
||||||
clearDocumentBaseHref();
|
|
||||||
jqDocHead.append('<base href="' + href +'" />');
|
|
||||||
}
|
|
||||||
|
|
||||||
function clearDocumentBaseHref() {
|
|
||||||
jqDocHead.find('base').remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
jqDocHead = jqLite(document).find('head');
|
jqDocHead = jqLite(document).find('head');
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(clearDocumentBaseHref);
|
|
||||||
|
|
||||||
it('should return value from <base href>', function() {
|
it('should return value from <base href>', function() {
|
||||||
setDocumentBaseHrefTo('/base/path/');
|
fakeDocument.basePath = '/base/path/';
|
||||||
expect(browser.baseHref()).toEqual('/base/path/');
|
expect(browser.baseHref()).toEqual('/base/path/');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return undefined if no <base href>', function() {
|
it('should return undefined if no <base href>', function() {
|
||||||
|
fakeDocument.basePath = undefined;
|
||||||
expect(browser.baseHref()).toBeUndefined();
|
expect(browser.baseHref()).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should remove domain from <base href>', function() {
|
it('should remove domain from <base href>', function() {
|
||||||
setDocumentBaseHrefTo('http://host.com/base/path/');
|
fakeDocument.basePath = 'http://host.com/base/path/';
|
||||||
expect(browser.baseHref()).toEqual('/base/path/');
|
expect(browser.baseHref()).toEqual('/base/path/');
|
||||||
|
|
||||||
setDocumentBaseHrefTo('http://host.com/base/path/index.html');
|
fakeDocument.basePath = 'http://host.com/base/path/index.html';
|
||||||
expect(browser.baseHref()).toEqual('/base/path/index.html');
|
expect(browser.baseHref()).toEqual('/base/path/index.html');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue