Skip to content

Commit 133a907

Browse files
authored
Fix an issue with anchors not working (#25459)
1 parent 7bfe979 commit 133a907

4 files changed

Lines changed: 11 additions & 33 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
-----
33
* [*] All self-hosted sites now sign in using application passwords [#25424]
44
* [*] Reader: Fix button style [#25447]
5+
* [*] Reader: Fix a regression with anchors not working in posts [#25459]
56

67

78
26.8

WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailCoordinator.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,12 @@ class ReaderDetailCoordinator {
502502
///
503503
/// - Parameter url: the URL to be handled
504504
func handle(_ url: URL) {
505-
// If the URL has an anchor (#)
506-
// and the URL is equal to the current post URL
507-
if let hash = URLComponents(url: url, resolvingAgainstBaseURL: true)?.fragment,
508-
let postURL = permaLinkURL,
509-
postURL.isHostAndPathEqual(to: url) {
505+
// If the URL has an anchor (#) and the URL matches either the current
506+
// post URL or the webview's baseURL, scroll within the document.
507+
// In-document anchors (e.g. footnotes) resolve against the baseURL,
508+
// so we need to check both.
509+
let isInDocumentAnchor = permaLinkURL?.isHostAndPathEqual(to: url) == true || ReaderWebView.baseURL.isHostAndPathEqual(to: url)
510+
if let hash = URLComponents(url: url, resolvingAgainstBaseURL: true)?.fragment, isInDocumentAnchor {
510511
view?.scroll(to: hash)
511512
} else if let (gallery, index) = findGallery(containing: url) {
512513
presentGallery(gallery, startingAt: index)

WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ class ReaderDetailViewController: UIViewController, ReaderDetailView {
459459
return
460460
}
461461

462-
self.scrollView.setContentOffset(CGPoint(x: 0, y: height + self.webView.frame.origin.y), animated: true)
462+
let y = height + self.webView.frame.origin.y - self.scrollView.adjustedContentInset.top
463+
self.scrollView.setContentOffset(CGPoint(x: 0, y: y), animated: true)
463464
})
464465
}
465466

WordPress/Classes/ViewRelated/Reader/Detail/WebView/ReaderWebView.swift

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ReaderWebView: WKWebView {
2121
///
2222
/// Documentation: https://developers.google.com/youtube/terms/required-minimum-functionality#set-the-referer
2323
/// See also: https://stackoverflow.com/q/79802987/496295
24-
private let baseURL = URL(string: "https://wordpress.com/reader")!
24+
static let baseURL = URL(string: "https://wordpress.com/reader")!
2525

2626
let jsToRemoveSrcSet = "document.querySelectorAll('img, img-placeholder').forEach((el) => {el.removeAttribute('srcset')})"
2727

@@ -53,7 +53,7 @@ class ReaderWebView: WKWebView {
5353

5454
let content = formattedContent(addPlaceholder(string), additionalJavaScript: additionalJavaScript)
5555

56-
super.loadHTMLString(content, baseURL: baseURL)
56+
super.loadHTMLString(content, baseURL: Self.baseURL)
5757
}
5858

5959
/// Given a HTML content, returns it formatted.
@@ -79,31 +79,6 @@ class ReaderWebView: WKWebView {
7979
\(additionalJavaScript)
8080
// Remove autoplay to avoid media autoplaying
8181
document.querySelectorAll('video-placeholder, audio-placeholder').forEach((el) => {el.removeAttribute('autoplay')})
82-
83-
// Replaces the bundle URL with the post URL for each "blank" anchor tag (<a href="#anchor"></a>).
84-
// this fixes an issue where tapping on one would return a file url with the anchor attached to it
85-
let baseURL = "\(Bundle.wordPressSharedBundle.bundleURL)"
86-
let postURL = "\(postURL?.absoluteString ?? "")"
87-
88-
if(postURL.length > 0){
89-
let anchors = document.querySelectorAll('a')
90-
91-
anchors.forEach(function(elem){
92-
// Ignore any regular links that don't have hashes
93-
if(!elem.hash || elem.hash.length < 0) {
94-
return
95-
}
96-
97-
let href = elem.href;
98-
99-
// Skip any links that aren't the base URL
100-
if(href.substr(0, baseURL.length) != baseURL){
101-
return
102-
}
103-
104-
elem.href = postURL + elem.hash;
105-
});
106-
}
10782
})
10883
function debounce(fn, timeout) {
10984
let timer;

0 commit comments

Comments
 (0)