Put <title> and <base> in the <head> instead of <body>

This also means renderHTML() is no longer needed.
This commit is contained in:
Nate Weaver 2020-01-30 06:10:45 -06:00
parent dc4850b777
commit 447cc44b8e
6 changed files with 28 additions and 24 deletions

View File

@ -199,6 +199,8 @@ extension DetailWebViewController: WKNavigationDelegate {
struct TemplateData: Codable { struct TemplateData: Codable {
let style: String let style: String
let body: String let body: String
let title: String
let baseURL: String
} }
private extension DetailWebViewController { private extension DetailWebViewController {
@ -227,7 +229,7 @@ private extension DetailWebViewController {
rendering = ArticleRenderer.articleHTML(article: article, extractedArticle: extractedArticle, style: style) 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() let encoder = JSONEncoder()
var render = "error();" var render = "error();"

View File

@ -1,5 +1,7 @@
<html> <html>
<head> <head>
<title></title>
<base href="">
<style> <style>
</style> </style>
<script src="main.js"></script> <script src="main.js"></script>

View File

@ -13,7 +13,7 @@ import Account
struct ArticleRenderer { struct ArticleRenderer {
typealias Rendering = (style: String, html: String) typealias Rendering = (style: String, html: String, title: String, baseURL: String)
typealias Page = (url: URL, baseURL: URL) typealias Page = (url: URL, baseURL: URL)
static var imageIconScheme = "nnwImageIcon" static var imageIconScheme = "nnwImageIcon"
@ -49,27 +49,27 @@ struct ArticleRenderer {
static func articleHTML(article: Article, extractedArticle: ExtractedArticle? = nil, style: ArticleStyle) -> Rendering { static func articleHTML(article: Article, extractedArticle: ExtractedArticle? = nil, style: ArticleStyle) -> Rendering {
let renderer = ArticleRenderer(article: article, extractedArticle: extractedArticle, style: style) 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 { static func multipleSelectionHTML(style: ArticleStyle) -> Rendering {
let renderer = ArticleRenderer(article: nil, extractedArticle: nil, style: style) 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 { static func loadingHTML(style: ArticleStyle) -> Rendering {
let renderer = ArticleRenderer(article: nil, extractedArticle: nil, style: style) 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 { static func noSelectionHTML(style: ArticleStyle) -> Rendering {
let renderer = ArticleRenderer(article: nil, extractedArticle: nil, style: style) 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 { static func noContentHTML(style: ArticleStyle) -> Rendering {
let renderer = ArticleRenderer(article: nil, extractedArticle: nil, style: style) 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 { private var articleHTML: String {
let body = try! MacroProcessor.renderedText(withTemplate: template(), substitutions: articleSubstitutions()) let body = try! MacroProcessor.renderedText(withTemplate: template(), substitutions: articleSubstitutions())
return renderHTML(withBody: body) return body
} }
private var multipleSelectionHTML: String { private var multipleSelectionHTML: String {
let body = "<h3 class='systemMessage'>Multiple selection</h3>" let body = "<h3 class='systemMessage'>Multiple selection</h3>"
return renderHTML(withBody: body) return body
} }
private var loadingHTML: String { private var loadingHTML: String {
let body = "<h3 class='systemMessage'>Loading...</h3>" let body = "<h3 class='systemMessage'>Loading...</h3>"
return renderHTML(withBody: body) return body
} }
private var noSelectionHTML: String { private var noSelectionHTML: String {
let body = "<h3 class='systemMessage'>No selection</h3>" let body = "<h3 class='systemMessage'>No selection</h3>"
return renderHTML(withBody: body) return body
} }
private var noContentHTML: String { private var noContentHTML: String {
return renderHTML(withBody: "") return ""
} }
static var defaultStyleSheet: String = { static var defaultStyleSheet: String = {
@ -225,17 +225,6 @@ private extension ArticleRenderer {
return dateFormatter.string(from: date) return dateFormatter.string(from: date)
} }
func renderHTML(withBody body: String) -> String {
var s = ""
if let baseURL = baseURL {
s += ("<base href=\"" + baseURL + "\"\n>")
}
s += title.htmlBySurroundingWithTag("title")
s += body
return s
}
} }
// MARK: - Article extension // MARK: - Article extension

View File

@ -90,6 +90,13 @@ function error() {
function render(data, scrollY) { function render(data, scrollY) {
document.getElementsByTagName("style")[0].innerHTML = data.style; 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; document.body.innerHTML = data.body;
window.scrollTo(0, scrollY); window.scrollTo(0, scrollY);

View File

@ -437,6 +437,8 @@ extension WebViewController: UIScrollViewDelegate {
private struct TemplateData: Codable { private struct TemplateData: Codable {
let style: String let style: String
let body: String let body: String
let title: String
let baseURL: String
} }
private struct ImageClickMessage: Codable { private struct ImageClickMessage: Codable {
@ -478,7 +480,7 @@ private extension WebViewController {
rendering = ArticleRenderer.noSelectionHTML(style: style) 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() let encoder = JSONEncoder()
var render = "error();" var render = "error();"

View File

@ -1,5 +1,7 @@
<html> <html>
<head> <head>
<title></title>
<base href="">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<style> <style>
:root { :root {