From 447cc44b8ec52a21e11fa119ad25899df87aa5fe Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Thu, 30 Jan 2020 06:10:45 -0600 Subject: [PATCH 1/2] Put and <base> in the <head> instead of <body> This also means renderHTML() is no longer needed. --- .../Detail/DetailWebViewController.swift | 4 ++- Mac/MainWindow/Detail/page.html | 2 ++ .../Article Rendering/ArticleRenderer.swift | 33 +++++++------------ Shared/Article Rendering/main.js | 7 ++++ iOS/Article/WebViewController.swift | 4 ++- iOS/Resources/page.html | 2 ++ 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Mac/MainWindow/Detail/DetailWebViewController.swift b/Mac/MainWindow/Detail/DetailWebViewController.swift index bf047ea5b..942f4c0ff 100644 --- a/Mac/MainWindow/Detail/DetailWebViewController.swift +++ b/Mac/MainWindow/Detail/DetailWebViewController.swift @@ -199,6 +199,8 @@ extension DetailWebViewController: WKNavigationDelegate { struct TemplateData: Codable { let style: String let body: String + let title: String + let baseURL: String } private extension DetailWebViewController { @@ -227,7 +229,7 @@ private extension DetailWebViewController { rendering = ArticleRenderer.articleHTML(article: article, extractedArticle: extractedArticle, style: style) } - let templateData = TemplateData(style: rendering.style, body: rendering.html) + let templateData = TemplateData(style: rendering.style, body: rendering.html, title: rendering.title, baseURL: rendering.baseURL) let encoder = JSONEncoder() var render = "error();" diff --git a/Mac/MainWindow/Detail/page.html b/Mac/MainWindow/Detail/page.html index b9cac098b..0db4ccf7f 100644 --- a/Mac/MainWindow/Detail/page.html +++ b/Mac/MainWindow/Detail/page.html @@ -1,5 +1,7 @@ <html> <head> + <title> + diff --git a/Shared/Article Rendering/ArticleRenderer.swift b/Shared/Article Rendering/ArticleRenderer.swift index e19adec0a..25a510907 100644 --- a/Shared/Article Rendering/ArticleRenderer.swift +++ b/Shared/Article Rendering/ArticleRenderer.swift @@ -13,7 +13,7 @@ import Account struct ArticleRenderer { - typealias Rendering = (style: String, html: String) + typealias Rendering = (style: String, html: String, title: String, baseURL: String) typealias Page = (url: URL, baseURL: URL) static var imageIconScheme = "nnwImageIcon" @@ -49,27 +49,27 @@ struct ArticleRenderer { static func articleHTML(article: Article, extractedArticle: ExtractedArticle? = nil, style: ArticleStyle) -> Rendering { let renderer = ArticleRenderer(article: article, extractedArticle: extractedArticle, style: style) - return (renderer.styleString(), renderer.articleHTML) + return (renderer.styleString(), renderer.articleHTML, renderer.title, renderer.baseURL ?? "") } static func multipleSelectionHTML(style: ArticleStyle) -> Rendering { let renderer = ArticleRenderer(article: nil, extractedArticle: nil, style: style) - return (renderer.styleString(), renderer.multipleSelectionHTML) + return (renderer.styleString(), renderer.multipleSelectionHTML, renderer.title, renderer.baseURL ?? "") } static func loadingHTML(style: ArticleStyle) -> Rendering { let renderer = ArticleRenderer(article: nil, extractedArticle: nil, style: style) - return (renderer.styleString(), renderer.loadingHTML) + return (renderer.styleString(), renderer.loadingHTML, renderer.title, renderer.baseURL ?? "") } static func noSelectionHTML(style: ArticleStyle) -> Rendering { let renderer = ArticleRenderer(article: nil, extractedArticle: nil, style: style) - return (renderer.styleString(), renderer.noSelectionHTML) + return (renderer.styleString(), renderer.noSelectionHTML, renderer.title, renderer.baseURL ?? "") } static func noContentHTML(style: ArticleStyle) -> Rendering { let renderer = ArticleRenderer(article: nil, extractedArticle: nil, style: style) - return (renderer.styleString(), renderer.noContentHTML) + return (renderer.styleString(), renderer.noContentHTML, renderer.title, renderer.baseURL ?? "") } } @@ -79,26 +79,26 @@ private extension ArticleRenderer { private var articleHTML: String { let body = try! MacroProcessor.renderedText(withTemplate: template(), substitutions: articleSubstitutions()) - return renderHTML(withBody: body) + return body } private var multipleSelectionHTML: String { let body = "

Multiple selection

" - return renderHTML(withBody: body) + return body } private var loadingHTML: String { let body = "

Loading...

" - return renderHTML(withBody: body) + return body } private var noSelectionHTML: String { let body = "

No selection

" - return renderHTML(withBody: body) + return body } private var noContentHTML: String { - return renderHTML(withBody: "") + return "" } static var defaultStyleSheet: String = { @@ -225,17 +225,6 @@ private extension ArticleRenderer { return dateFormatter.string(from: date) } - func renderHTML(withBody body: String) -> String { - var s = "" - if let baseURL = baseURL { - s += ("") - } - s += title.htmlBySurroundingWithTag("title") - - s += body - return s - } - } // MARK: - Article extension diff --git a/Shared/Article Rendering/main.js b/Shared/Article Rendering/main.js index ddfcf3e18..c99a7ece4 100644 --- a/Shared/Article Rendering/main.js +++ b/Shared/Article Rendering/main.js @@ -90,6 +90,13 @@ function error() { function render(data, scrollY) { document.getElementsByTagName("style")[0].innerHTML = data.style; + + let title = document.getElementsByTagName("title")[0]; + title.textContent = data.title + + let base = document.getElementsByTagName("base")[0]; + base.href = data.baseURL + document.body.innerHTML = data.body; window.scrollTo(0, scrollY); diff --git a/iOS/Article/WebViewController.swift b/iOS/Article/WebViewController.swift index b85f1dd40..71a7c37a1 100644 --- a/iOS/Article/WebViewController.swift +++ b/iOS/Article/WebViewController.swift @@ -437,6 +437,8 @@ extension WebViewController: UIScrollViewDelegate { private struct TemplateData: Codable { let style: String let body: String + let title: String + let baseURL: String } private struct ImageClickMessage: Codable { @@ -478,7 +480,7 @@ private extension WebViewController { rendering = ArticleRenderer.noSelectionHTML(style: style) } - let templateData = TemplateData(style: rendering.style, body: rendering.html) + let templateData = TemplateData(style: rendering.style, body: rendering.html, title: rendering.title, baseURL: rendering.baseURL) let encoder = JSONEncoder() var render = "error();" diff --git a/iOS/Resources/page.html b/iOS/Resources/page.html index fae517747..f130430ea 100644 --- a/iOS/Resources/page.html +++ b/iOS/Resources/page.html @@ -1,5 +1,7 @@ + + - - + + + - - - + + + diff --git a/iOS/Resources/page.html b/iOS/Resources/page.html index f130430ea..22bc5881c 100644 --- a/iOS/Resources/page.html +++ b/iOS/Resources/page.html @@ -1,17 +1,17 @@ - + - - - + + + - - - + + +