Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #7532: Support reader mode text direction when Readability detect…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
kylehickinson authored Jul 12, 2023
1 parent 6507323 commit f82769a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/Brave/Frontend/Reader/Reader.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<title id="reader-page-title"></title>
</head>

<body>
<body dir="%READER-DIRECTION%">
<div id="reader-header" class="header">
<h1 id="reader-title"></h1>
<div id="reader-credits" class="credits"></div>
Expand Down
1 change: 1 addition & 0 deletions Sources/Brave/Frontend/Reader/ReaderModeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ struct ReadabilityResult {
var content = ""
var title = ""
var credits = ""
var direction = "auto"

init?(object: AnyObject?) {
if let dict = object as? NSDictionary {
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f82769a

Please sign in to comment.