Links to other sites
A link to another site opens in the person's browser, and a handler opens a URL there with Os::open_external.
Links in a page
A window shows only the pages of the application's own server, http://127.0.0.1:<port>/. When a page asks the window to go to any other URL, the window stays on the page it shows and hands the URL to the system, which opens it in the program it opens such URLs with. The same holds for a link with target="_blank" and for window.open: a URL of the application opens in a new window of the application, and any other URL goes to the system.
| The URL | What happens |
|---|---|
| A page of the application | The window shows it. With target="_blank" or window.open, a new window shows it. |
http: or https: on another host or port | The person's default browser opens it. |
mailto: | The person's mail program opens it. |
| Any other scheme | Nothing opens, and hypersig_desktop logs a warning with tracing. |
A link built with El::href takes a SafeUrl for an address the router does not serve, and a SafeUrl has the scheme http:, https:, mailto:, or none, so every link to another site built with El::href falls in the second or third row. Values the types do not check covers SafeUrl.
Opening a URL from a handler
impl Os {
pub fn open_external(&self, url: &SafeUrl) -> Result<(), Error>;
}Os::open_external hands url to the system as a link does, for a URL the handler decides on, such as the page of a newer version or a form for reporting a problem. It is not async.
url | Returns |
|---|---|
http:, https:, or mailto:, in any letter case | Ok(()), once the system has been asked to open it |
No scheme, as in /notes, #top, or //example.com | Err whose kind() is ErrorKind::NotExternal |
| A URL the system fails to open | Err whose kind() is ErrorKind::Io |
ErrorKind::NotExternal exists because the system would open a URL without a scheme as a path to a file. The check runs before anything is opened, so the URL never reaches the system:
// Both are `SafeUrl`s, and neither names a scheme.
for url in [SafeUrl::new("/notes"), SafeUrl::new("//example.com")] {
let error = os.open_external(&url).unwrap_err();
assert_eq!(error.kind(), ErrorKind::NotExternal);
}What the type rules out
url is a &SafeUrl, so a &str does not compile:
// A `&str` is not a `&SafeUrl`: E0308.
os.open_external("https://example.com")?;A URL written in the program is a const made with SafeUrl::new, and a scheme that is not http, https, or mailto, such as file: or javascript:, is E0080 there:
// `file:` is not a scheme a `SafeUrl` takes, so this `const` fails to compile.
const HOSTS: SafeUrl = SafeUrl::new("file:///etc/hosts");
os.open_external(&HOSTS)?;A URL that arrives at run time, from a file or from what the person typed, goes through SafeUrl::parse, which returns Err for the same schemes. The host is not checked: a SafeUrl may name any site, and open_external opens whichever one it names.
A link and a button
The page below has a link to the repository, which the browser opens when it is clicked, and a button whose handler opens the form for a new issue. The Os comes from the router's state, as on File dialogs:
#[derive(Signals, Serialize, Deserialize)]
struct About {
status: String,
}
#[derive(Clone)]
struct AppState {
os: Os,
}
hypersig::routes! {
fn actions(Router<AppState>);
action REPORT: Action<About> = post "/report" => report;
}
const SOURCE: SafeUrl = SafeUrl::new("https://github.com/imaimai17468/hypersig");
const NEW_ISSUE: SafeUrl = SafeUrl::new("https://github.com/imaimai17468/hypersig/issues/new");
fn about() -> Page<About> {
Page::new("About", About { status: String::new() }).child(
div()
// Clicked, this opens in the person's browser and the window stays.
.child(a().href(SOURCE).text("Source"))
.child(button().on_click(REPORT.call()).text("Report a problem"))
.child(p().bind_text(About::status())),
)
}
/// Opens the form for a new issue in the person's browser.
async fn report(state: AppState, (): ()) -> Patches<About> {
match state.os.open_external(&NEW_ISSUE) {
Ok(()) => Patches::new(),
Err(error) => About::status().patch(format!("Not opened: {error}")).into(),
}
}In a test, FakeOs::opened_externally returns each SafeUrl the handlers passed to open_external, and opens nothing. A link the person clicks is handled by the window, not by a handler, so opened_externally does not list it.
Next
- Opening windows: links and
window.opento the application's own pages. - Testing without a window:
FakeOsand what it records.