Skip to content

Commit 76ebf5d

Browse files
Don't normalize HTTP URLs for the Viewer (#818)
* don't normalize URLs * Localize special logic in `is_http_url()` helper (#821) --------- Co-authored-by: Davis Vaughan <[email protected]>
1 parent 15ea580 commit 76ebf5d

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

crates/ark/src/modules/positron/utils.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# utils.R
33
#
4-
# Copyright (C) 2022-2024 Posit Software, PBC. All rights reserved.
4+
# Copyright (C) 2022-2025 Posit Software, PBC. All rights reserved.
55
#
66
#
77

@@ -141,3 +141,7 @@ node_poke_cdr <- function(node, cdr) {
141141
is_string <- function(x) {
142142
is.character(x) && length(x) == 1 && !is.na(x)
143143
}
144+
145+
is_http_url <- function(x) {
146+
is_string(x) && grepl("^https?://", x)
147+
}
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
#
22
# viewer.R
33
#
4-
# Copyright (C) 2023-2024 Posit Software, PBC. All rights reserved.
4+
# Copyright (C) 2023-2025 Posit Software, PBC. All rights reserved.
55
#
66
#
77

88
options("viewer" = function(url, height = NULL, ...) {
99
# Validate the URL argument.
10-
if (!is.character(url) || (length(url) != 1))
11-
stop("url must be a single element character vector.")
12-
13-
# Normalize paths for comparison. This is necessary because on e.g. macOS,
14-
# the `tempdir()` may contain `//` or other non-standard path separators.
15-
normalizedPath <- normalizePath(url, mustWork = FALSE)
16-
normalizedTempdir <- normalizePath(tempdir(), mustWork = FALSE)
10+
if (!is_string(url)) {
11+
stop("`url` must be a string.")
12+
}
1713

1814
# Validate the height argument.
1915
height <- .ps.validate.viewer.height(height)
2016

17+
# Open `http(s)://` urls in the browser immediately, avoid normalizing their
18+
# paths since they aren't files (posit-dev/positron#4843)
19+
if (is_http_url(url)) {
20+
return(utils::browseURL(url, ...))
21+
}
22+
23+
# Normalize file paths for comparison against the `tempdir()`. This is
24+
# necessary because on e.g. macOS, the `tempdir()` may contain `//` or other
25+
# non-standard path separators.
26+
normalizedPath <- normalizePath(url, mustWork = FALSE)
27+
normalizedTempdir <- normalizePath(tempdir(), mustWork = FALSE)
28+
2129
# Is the URL a temporary file?
2230
if (startsWith(normalizedPath, normalizedTempdir)) {
2331
# Derive a title for the viewer from the path.
@@ -26,7 +34,7 @@ options("viewer" = function(url, height = NULL, ...) {
2634
# If so, open it in the HTML viewer.
2735
.ps.Call("ps_html_viewer", normalizedPath, title, height, FALSE)
2836
} else {
29-
# If not, open it in the system browser.
37+
# If not, fall back to opening it in the system browser.
3038
utils::browseURL(normalizedPath, ...)
3139
}
3240
})

crates/ark/src/modules/rstudio/stubs.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@
5757
stop("url must be a single element character vector.")
5858
height <- .ps.validate.viewer.height(height)
5959

60-
url <- normalizePath(url, mustWork = FALSE)
60+
if (!is_http_url(url)) {
61+
# Only normalize file path urls (posit-dev/positron#4843)
62+
url <- normalizePath(url, mustWork = FALSE)
63+
}
6164

6265
# Derive a title for the viewer from the path.
6366
title <- .ps.viewer.title(url)

0 commit comments

Comments
 (0)