//! Clearwake's engine. //! //! Everything a Clearwake window needs that is not drawing: web addresses, //! the always-on ad and tracker filter, and a reader that turns a web page //! into clean blocks of text, links and pictures. No scripts ever run. //! //! The crate is `no_std` + `alloc` and uses integer math only, so the same //! code runs in the desktop, Android and web apps and inside the Glimmer //! kernel, which has no floating point unit. #![cfg_attr(not(any(feature = "std", test)), no_std)] extern crate alloc; pub mod bookmarks; pub mod filter; pub mod history; pub mod html; pub mod url; pub use filter::Filter; pub use html::{Block, Document, Inline}; pub use url::Url; /// What the address bar should open for what the person typed. /// /// A web address opens directly. Anything else becomes a search. pub fn resolve_input(input: &str) -> Option { let s = input.trim(); if s.is_empty() { return None; } if s == "home" || s == "about:home" { return Url::parse("about:home"); } if let Some(u) = Url::parse(s) { if u.scheme == "http" || u.scheme == "https" || u.scheme == "about" { return Some(u); } } let looks_like_host = !s.contains(' ') && (s.contains('.') || s.starts_with("localhost")) && !s.starts_with('.') && !s.ends_with('.'); if looks_like_host { let mut full = alloc::string::String::from("https://"); full.push_str(s); if let Some(u) = Url::parse(&full) { return Some(u); } } let mut q = alloc::string::String::from("https://html.duckduckgo.com/html/?q="); q.push_str(&url::encode_component(s)); Url::parse(&q) } #[cfg(test)] mod tests { use super::*; #[test] fn address_bar() { assert_eq!(resolve_input("example.com").unwrap().to_string(), "https://example.com/"); assert_eq!(resolve_input("http://a.b/c?d").unwrap().to_string(), "http://a.b/c?d"); assert!(resolve_input("rust language").unwrap().to_string().starts_with("https://html.duckduckgo.com/html/?q=rust+language")); assert_eq!(resolve_input("home").unwrap().to_string(), "about:home"); assert!(resolve_input(" ").is_none()); } }