mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
feat($browser): add $browser.baseHref()
This method abstracts <base href="" /> in document.head - returns the value. If absolute href set, it converts the href to relative.
This commit is contained in:
parent
d0f459c56f
commit
91ccb4ba6e
3 changed files with 52 additions and 0 deletions
|
|
@ -509,4 +509,15 @@ function Browser(window, document, body, XHR, $log, $sniffer) {
|
|||
|
||||
return script;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
5
src/angular-mocks.js
vendored
5
src/angular-mocks.js
vendored
|
|
@ -302,6 +302,11 @@ function MockBrowser() {
|
|||
self.deferredFns.shift().fn();
|
||||
}
|
||||
};
|
||||
|
||||
self.$$baseHref = '';
|
||||
self.baseHref = function() {
|
||||
return this.$$baseHref;
|
||||
};
|
||||
}
|
||||
MockBrowser.prototype = {
|
||||
|
||||
|
|
|
|||
|
|
@ -691,4 +691,40 @@ describe('browser', function(){
|
|||
expect(script).toBe(scripts[0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('baseHref', function() {
|
||||
var jqDocHead;
|
||||
|
||||
function setDocumentBaseHrefTo(href) {
|
||||
clearDocumentBaseHref();
|
||||
jqDocHead.append('<base href="' + href +'" />');
|
||||
}
|
||||
|
||||
function clearDocumentBaseHref() {
|
||||
jqDocHead.find('base').remove();
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
jqDocHead = jqLite(document).find('head');
|
||||
});
|
||||
|
||||
afterEach(clearDocumentBaseHref);
|
||||
|
||||
it('should return value from <base href>', function() {
|
||||
setDocumentBaseHrefTo('/base/path/');
|
||||
expect(browser.baseHref()).toEqual('/base/path/');
|
||||
});
|
||||
|
||||
it('should return undefined if no <base href>', function() {
|
||||
expect(browser.baseHref()).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should remove domain from <base href>', function() {
|
||||
setDocumentBaseHrefTo('http://host.com/base/path/');
|
||||
expect(browser.baseHref()).toEqual('/base/path/');
|
||||
|
||||
setDocumentBaseHrefTo('http://host.com/base/path/index.html');
|
||||
expect(browser.baseHref()).toEqual('/base/path/index.html');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue