From 0d1dece7b43958c46e4adae2bf31056e22b68c77 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Mon, 9 Mar 2015 17:39:18 +0100 Subject: [PATCH] feat: introduce Title service Closes #612 Closes #900 --- modules/angular2/src/dom/browser_adapter.dart | 4 +++ modules/angular2/src/dom/browser_adapter.es6 | 6 ++++ modules/angular2/src/dom/dom_adapter.js | 6 ++++ modules/angular2/src/services/title.js | 12 +++++++ modules/angular2/test/services/title_spec.js | 32 +++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 modules/angular2/src/services/title.js create mode 100644 modules/angular2/test/services/title_spec.js diff --git a/modules/angular2/src/dom/browser_adapter.dart b/modules/angular2/src/dom/browser_adapter.dart index 1be8cbaf4e..77dc59f31c 100644 --- a/modules/angular2/src/dom/browser_adapter.dart +++ b/modules/angular2/src/dom/browser_adapter.dart @@ -173,6 +173,10 @@ class BrowserDomAdapter extends DomAdapter { document.implementation.createHtmlDocument('fakeTitle'); HtmlDocument defaultDoc() => document; + String getTitle() => document.title; + void setTitle(String newTitle) { + document.title = newTitle; + } bool elementMatches(n, String selector) => n is Element && n.matches(selector); bool isTemplateElement(Element el) => diff --git a/modules/angular2/src/dom/browser_adapter.es6 b/modules/angular2/src/dom/browser_adapter.es6 index 144e6de256..3b45ed406b 100644 --- a/modules/angular2/src/dom/browser_adapter.es6 +++ b/modules/angular2/src/dom/browser_adapter.es6 @@ -215,6 +215,12 @@ export class BrowserDomAdapter extends DomAdapter { defaultDoc() { return document; } + getTitle() { + return document.title; + } + setTitle(newTitle:string) { + document.title = newTitle; + } elementMatches(n, selector:string):boolean { return n instanceof HTMLElement && n.matches(selector); } diff --git a/modules/angular2/src/dom/dom_adapter.js b/modules/angular2/src/dom/dom_adapter.js index c43c4abe40..3aa7b7f83f 100644 --- a/modules/angular2/src/dom/dom_adapter.js +++ b/modules/angular2/src/dom/dom_adapter.js @@ -201,6 +201,12 @@ export class DomAdapter { defaultDoc() { throw _abstract(); } + getTitle() { + throw _abstract(); + } + setTitle(newTitle:string) { + throw _abstract(); + } elementMatches(n, selector:string):boolean { throw _abstract(); } diff --git a/modules/angular2/src/services/title.js b/modules/angular2/src/services/title.js new file mode 100644 index 0000000000..a5d77949b3 --- /dev/null +++ b/modules/angular2/src/services/title.js @@ -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); + } +} diff --git a/modules/angular2/test/services/title_spec.js b/modules/angular2/test/services/title_spec.js new file mode 100644 index 0000000000..ba2397be46 --- /dev/null +++ b/modules/angular2/test/services/title_spec.js @@ -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(''); + }); + + }); +}