From 63fac139ae14493a252385b5ad49be141e6b9e0c Mon Sep 17 00:00:00 2001 From: Fernando Souza Date: Fri, 6 Jan 2017 19:57:01 -0300 Subject: [PATCH] Uses title HTML tag whenever is possible to update the page title. Closes #185 (#186) * Uses title HTML tag whenever is possible to update the page title. Closes #185 * Saves the unescaped title string --- src/app/App.js | 9 ++++++++- src/screen/HtmlScreen.js | 2 +- test/screen/HtmlScreen.js | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/app/App.js b/src/app/App.js index 7b73bb4..a2b7397 100644 --- a/src/app/App.js +++ b/src/app/App.js @@ -1106,7 +1106,14 @@ class App extends EventEmitter { } else { globals.window.history.pushState(state, title, path); } - globals.document.title = title; + + let titleNode = globals.document.querySelector('title'); + if (titleNode) { + titleNode.innerHTML = title; + } + else { + globals.document.title = title; + } } } diff --git a/src/screen/HtmlScreen.js b/src/screen/HtmlScreen.js index f230826..b30f4ee 100644 --- a/src/screen/HtmlScreen.js +++ b/src/screen/HtmlScreen.js @@ -307,7 +307,7 @@ class HtmlScreen extends RequestScreen { resolveTitleFromVirtualDocument() { var title = this.virtualDocument.querySelector(this.titleSelector); if (title) { - this.setTitle(title.innerHTML.trim()); + this.setTitle(title.textContent.trim()); } } diff --git a/test/screen/HtmlScreen.js b/test/screen/HtmlScreen.js index c87c4be..f003395 100644 --- a/test/screen/HtmlScreen.js +++ b/test/screen/HtmlScreen.js @@ -307,6 +307,25 @@ describe('HtmlScreen', function() { this.requests[0].respond(200, null, ''); }); + it('should have correct title', (done) => { + var screen = new HtmlScreen(); + screen.allocateVirtualDocumentForContent('left'); + screen.resolveTitleFromVirtualDocument(); + screen.flip([]).then(() => { + assert.strictEqual('left', screen.getTitle()); + done(); + }); + }); + + it('should have correct title when the title contains html entities', (done) => { + var screen = new HtmlScreen(); + screen.allocateVirtualDocumentForContent('left & right'); + screen.resolveTitleFromVirtualDocument(); + screen.flip([]).then(() => { + assert.strictEqual('left & right', screen.getTitle()); + done(); + }); + }); }); function enterDocumentSurfaceElement(surfaceId, opt_content) {