From f82769ae3b31842623941e24f8b3033a89e40cb4 Mon Sep 17 00:00:00 2001 From: Kyle Hickinson Date: Wed, 12 Jul 2023 09:30:47 -0400 Subject: [PATCH] Fix #7532: Support reader mode text direction when Readability detects it (#7722) This adds support for RTL reader mode by grabbing it from the parsed readability result. This unfortunately only works when the page itself annotates using the `dir` attribute in their HTML and not when pages use CSS to specifically mark pieces of their content as RTL --- Sources/Brave/Frontend/Reader/Reader.html | 2 +- Sources/Brave/Frontend/Reader/ReaderModeUtils.swift | 1 + .../Sandboxed/ReaderModeScriptHandler.swift | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Sources/Brave/Frontend/Reader/Reader.html b/Sources/Brave/Frontend/Reader/Reader.html index 96be7609811..d4e1dbcef04 100644 --- a/Sources/Brave/Frontend/Reader/Reader.html +++ b/Sources/Brave/Frontend/Reader/Reader.html @@ -12,7 +12,7 @@ - +

diff --git a/Sources/Brave/Frontend/Reader/ReaderModeUtils.swift b/Sources/Brave/Frontend/Reader/ReaderModeUtils.swift index eaeb0238a2a..476fb1e98df 100644 --- a/Sources/Brave/Frontend/Reader/ReaderModeUtils.swift +++ b/Sources/Brave/Frontend/Reader/ReaderModeUtils.swift @@ -30,6 +30,7 @@ struct ReaderModeUtils { .replacingOccurrences(of: "%READER-TITLE%", with: readabilityResult.title.javaScriptEscapedString?.unquotedIfNecessary ?? readabilityResult.title.htmlEntityEncodedString) .replacingOccurrences(of: "%READER-CREDITS%", with: readabilityResult.credits.javaScriptEscapedString?.unquotedIfNecessary ?? readabilityResult.credits.htmlEntityEncodedString) .replacingOccurrences(of: "%READER-CONTENT%", with: readabilityResult.content) + .replacingOccurrences(of: "%READER-DIRECTION%", with: readabilityResult.direction.javaScriptEscapedString?.unquotedIfNecessary ?? readabilityResult.direction.htmlEntityEncodedString) .replacingOccurrences(of: "%READER-MESSAGE%", with: "") } } diff --git a/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ReaderModeScriptHandler.swift b/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ReaderModeScriptHandler.swift index ad0b16ae663..52ad743ada6 100644 --- a/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ReaderModeScriptHandler.swift +++ b/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ReaderModeScriptHandler.swift @@ -164,6 +164,7 @@ struct ReadabilityResult { var content = "" var title = "" var credits = "" + var direction = "auto" init?(object: AnyObject?) { if let dict = object as? NSDictionary { @@ -188,6 +189,9 @@ struct ReadabilityResult { if let credits = dict["byline"] as? String { self.credits = credits } + if let direction = dict["dir"] as? String { + self.direction = direction + } } else { return nil } @@ -201,6 +205,7 @@ struct ReadabilityResult { let content = object["content"].string let title = object["title"].string let credits = object["credits"].string + let direction = object["dir"].string if domain == nil || url == nil || content == nil || title == nil || credits == nil { return nil @@ -211,11 +216,12 @@ struct ReadabilityResult { self.content = content! self.title = title! self.credits = credits! + self.direction = direction ?? "auto" } /// Encode to a dictionary, which can then for example be json encoded func encode() -> [String: Any] { - return ["domain": domain, "url": url, "content": content, "title": title, "credits": credits] + return ["domain": domain, "url": url, "content": content, "title": title, "credits": credits, "dir": direction] } /// Encode to a JSON encoded string