feat: introduce Title service

Closes #612

Closes #900
This commit is contained in:
Pawel Kozlowski 2015-03-09 17:39:18 +01:00
parent 7e93c54603
commit 0d1dece7b4
5 changed files with 60 additions and 0 deletions

View File

@ -173,6 +173,10 @@ class BrowserDomAdapter extends DomAdapter {
document.implementation.createHtmlDocument('fakeTitle'); document.implementation.createHtmlDocument('fakeTitle');
HtmlDocument defaultDoc() => document; HtmlDocument defaultDoc() => document;
String getTitle() => document.title;
void setTitle(String newTitle) {
document.title = newTitle;
}
bool elementMatches(n, String selector) => bool elementMatches(n, String selector) =>
n is Element && n.matches(selector); n is Element && n.matches(selector);
bool isTemplateElement(Element el) => bool isTemplateElement(Element el) =>

View File

@ -215,6 +215,12 @@ export class BrowserDomAdapter extends DomAdapter {
defaultDoc() { defaultDoc() {
return document; return document;
} }
getTitle() {
return document.title;
}
setTitle(newTitle:string) {
document.title = newTitle;
}
elementMatches(n, selector:string):boolean { elementMatches(n, selector:string):boolean {
return n instanceof HTMLElement && n.matches(selector); return n instanceof HTMLElement && n.matches(selector);
} }

View File

@ -201,6 +201,12 @@ export class DomAdapter {
defaultDoc() { defaultDoc() {
throw _abstract(); throw _abstract();
} }
getTitle() {
throw _abstract();
}
setTitle(newTitle:string) {
throw _abstract();
}
elementMatches(n, selector:string):boolean { elementMatches(n, selector:string):boolean {
throw _abstract(); throw _abstract();
} }

12
modules/angular2/src/services/title.js vendored Normal file
View File

@ -0,0 +1,12 @@
import {DOM} from 'angular2/src/dom/dom_adapter';
export class Title {
getTitle():string {
return DOM.getTitle();
}
setTitle(newTitle:string) {
DOM.setTitle(newTitle);
}
}

View File

@ -0,0 +1,32 @@
import {ddescribe, describe, it, iit, xit, expect, afterEach} from 'angular2/test_lib';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {Title} from 'angular2/src/services/title';
export function main() {
describe('title service', () => {
var initialTitle = DOM.getTitle();
var titleService = new Title();
afterEach(() => {
DOM.setTitle(initialTitle);
});
it('should allow reading initial title', () => {
expect(titleService.getTitle()).toEqual(initialTitle);
});
it('should set a title on the injected document', () => {
titleService.setTitle('test title');
expect(DOM.getTitle()).toEqual('test title');
expect(titleService.getTitle()).toEqual('test title');
});
it('should reset title to empty string if title not provided', () => {
titleService.setTitle(null);
expect(DOM.getTitle()).toEqual('');
});
});
}