1use alloc::format;
6use alloc::string::String;
7use alloc::vec::Vec;
8
9pub fn resolve_url(base_url: &str, rel: &str) -> String {
15 let rel = rel.trim();
16 if rel.is_empty() {
17 return String::from(rel);
18 }
19 if rel.starts_with("http://") || rel.starts_with("https://") || rel.starts_with("data:") {
20 return String::from(rel);
21 }
22 let (scheme, rest) = if let Some(r) = base_url.strip_prefix("https://") {
23 ("https://", r)
24 } else if let Some(r) = base_url.strip_prefix("http://") {
25 ("http://", r)
26 } else {
27 return String::from(rel);
28 };
29 let host = match rest.find('/') {
30 Some(idx) => rest.get(..idx).unwrap_or(rest),
31 None => rest,
32 };
33 if let Some(no_scheme) = rel.strip_prefix("//") {
34 return format!("{}{}", scheme, no_scheme);
35 }
36 if let Some(abs_path) = rel.strip_prefix('/') {
37 return format!("{}{}/{}", scheme, host, abs_path);
38 }
39 let base_path = match rest.find('/') {
41 Some(idx) => rest.get(idx..).unwrap_or("/"),
42 None => "/",
43 };
44 let mut dir_segments: Vec<&str> = base_path.split('/').collect();
45 dir_segments.pop(); for seg in rel.split('/') {
47 match seg {
48 "." | "" => {}
49 ".." => {
50 if dir_segments.len() > 1 {
51 dir_segments.pop();
52 }
53 }
54 s => dir_segments.push(s),
55 }
56 }
57 let joined_path = dir_segments.join("/");
58 format!("{}{}{}", scheme, host, joined_path)
59}
60
61pub fn rewrite_css_urls(css: &str, css_base_url: &str) -> String {
66 if !css.contains("url(") {
67 return String::from(css);
68 }
69 let mut out = String::with_capacity(css.len());
70 let mut rest = css;
71 while let Some(idx) = rest.find("url(") {
72 if let Some(prefix) = rest.get(..idx) {
73 out.push_str(prefix);
74 }
75 out.push_str("url(");
76 let after_url = match rest.get(idx + 4..) {
77 Some(s) => s,
78 None => break,
79 };
80 let bytes = after_url.as_bytes();
81 let mut depth = 1i32;
82 let mut i = 0usize;
83 while i < bytes.len() && depth > 0 {
84 match bytes[i] {
85 b'(' => depth += 1,
86 b')' => depth -= 1,
87 _ => {}
88 }
89 if depth > 0 {
90 i += 1;
91 }
92 }
93 let inner = after_url.get(..i).unwrap_or("");
94 let trimmed = inner.trim();
95 let (quote, unquoted) = if (trimmed.starts_with('"') && trimmed.ends_with('"'))
96 || (trimmed.starts_with('\'') && trimmed.ends_with('\''))
97 {
98 let q = trimmed.get(..1).unwrap_or("");
99 (q, trimmed.get(1..trimmed.len() - 1).unwrap_or(""))
100 } else {
101 ("", trimmed)
102 };
103 let resolved = resolve_url(css_base_url, unquoted);
104 out.push_str(quote);
105 out.push_str(&resolved);
106 out.push_str(quote);
107 if i < bytes.len() {
108 out.push(')');
109 i += 1;
110 }
111 rest = after_url.get(i..).unwrap_or("");
112 }
113 out.push_str(rest);
114 out
115}
116
117pub fn truncate_to_balanced_css(css: &str) -> &str {
126 let bytes = css.as_bytes();
127 let mut depth: i32 = 0;
128 let mut last_balanced_end = 0usize;
129 let mut i = 0usize;
130 while i < bytes.len() {
131 match bytes[i] {
132 b'/' if bytes.get(i + 1) == Some(&b'*') => {
133 i += 2;
135 while i < bytes.len() && !(bytes[i] == b'*' && bytes.get(i + 1) == Some(&b'/')) {
136 i += 1;
137 }
138 i = (i + 2).min(bytes.len());
139 continue;
140 }
141 b'"' | b'\'' => {
142 let quote = bytes[i];
144 i += 1;
145 while i < bytes.len() && bytes[i] != quote {
146 if bytes[i] == b'\\' {
147 i += 1;
148 }
149 i += 1;
150 }
151 i = (i + 1).min(bytes.len());
152 continue;
153 }
154 b'{' => {
155 depth += 1;
156 i += 1;
157 }
158 b'}' => {
159 depth -= 1;
160 i += 1;
161 if depth <= 0 {
162 depth = 0;
163 last_balanced_end = i;
164 }
165 }
166 _ => {
167 i += 1;
168 }
169 }
170 }
171 if depth == 0 {
172 css
173 } else {
174 css.get(..last_balanced_end).unwrap_or("")
175 }
176}
177
178pub fn split_target(
193 url: &str,
194 current_is_https: bool,
195 current_host: &str,
196) -> (bool, String, String) {
197 fn split_host_path(rest: &str) -> (String, String) {
198 match rest.find('/') {
199 Some(idx) => (
200 String::from(rest.get(..idx).unwrap_or(rest)),
201 String::from(rest.get(idx..).unwrap_or("/")),
202 ),
203 None => (String::from(rest), String::from("/")),
204 }
205 }
206 let url = match url.split_once('#') {
208 Some((before, _)) => before,
209 None => url,
210 };
211 if let Some(rest) = url.strip_prefix("https://") {
212 let (h, p) = split_host_path(rest);
213 (true, h, p)
214 } else if let Some(rest) = url.strip_prefix("http://") {
215 let (h, p) = split_host_path(rest);
216 (false, h, p)
217 } else if let Some(rest) = url.strip_prefix("//") {
218 let (h, p) = split_host_path(rest);
221 (current_is_https, h, p)
222 } else {
223 (current_is_https, String::from(current_host), String::from(url))
224 }
225}
226
227pub const ABOUT_HOST_PREFIX: &str = "about:";
234
235pub fn about_name(input: &str) -> Option<String> {
240 let t = input.trim();
241 let head = t.get(..ABOUT_HOST_PREFIX.len())?;
242 if !head.eq_ignore_ascii_case(ABOUT_HOST_PREFIX) {
243 return None;
244 }
245 let name = t.get(ABOUT_HOST_PREFIX.len()..)?.trim();
246 let mut s = String::from(name);
247 s.make_ascii_lowercase();
248 Some(s)
249}
250
251pub fn normalize_about(input: &str) -> Option<String> {
253 about_name(input).map(|n| format!("{}{}", ABOUT_HOST_PREFIX, n))
254}
255
256pub fn is_about_host(host: &str) -> bool {
258 host.starts_with(ABOUT_HOST_PREFIX)
259}
260
261pub fn split_host_path(rest: &str) -> (String, String) {
263 match rest.split_once('/') {
264 Some((h, p)) => (String::from(h), format!("/{}", p)),
265 None => (String::from(rest), String::from("/")),
266 }
267}
268
269pub fn split_explicit_url(input: &str) -> Option<(bool, String, String)> {
272 let t = input.trim();
273 let (is_https, rest) = if let Some(r) = t.strip_prefix("https://") {
274 (true, r)
275 } else if let Some(r) = t.strip_prefix("http://") {
276 (false, r)
277 } else {
278 return None;
279 };
280 let (h, p) = split_host_path(rest);
281 Some((is_https, h, p))
282}
283
284#[derive(Debug, Clone, PartialEq, Eq)]
291pub enum NavTarget {
292 Ignore,
294 About(String),
296 Fragment(String),
298 Page(bool, String, String),
300}
301
302fn normalize_path(path: &str) -> String {
305 let mut segs: Vec<&str> = Vec::new();
306 let mut trailing = false;
307 for seg in path.split('/') {
308 match seg {
309 "" | "." => trailing = true,
310 ".." => {
311 segs.pop();
312 trailing = true;
313 }
314 s => {
315 segs.push(s);
316 trailing = false;
317 }
318 }
319 }
320 let mut out = String::from("/");
321 out.push_str(&segs.join("/"));
322 if trailing && !segs.is_empty() {
323 out.push('/');
324 }
325 out
326}
327
328fn has_other_scheme(href: &str) -> bool {
332 let Some((scheme, _)) = href.split_once(':') else {
333 return false;
334 };
335 let mut cs = scheme.chars();
336 cs.next().is_some_and(|c| c.is_ascii_alphabetic())
337 && cs.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
338}
339
340pub fn resolve_nav_target(
346 base_is_https: bool,
347 base_host: &str,
348 base_path: &str,
349 href: &str,
350) -> NavTarget {
351 let t = href.trim();
352 if t.is_empty() {
353 return NavTarget::Ignore;
354 }
355 if let Some(about) = normalize_about(t) {
356 return NavTarget::About(about);
357 }
358 if t.starts_with('#') {
359 return NavTarget::Fragment(String::from(t));
360 }
361 if let Some((is_https, h, p)) = split_explicit_url(t) {
362 return NavTarget::Page(is_https, h, p);
363 }
364 if has_other_scheme(t) {
365 return NavTarget::Ignore;
366 }
367 if let Some(rest) = t.strip_prefix("//") {
368 let (h, p) = split_host_path(rest);
369 return NavTarget::Page(base_is_https, h, p);
370 }
371 let (no_frag, frag) = match t.split_once('#') {
373 Some((a, f)) => (a, Some(f)),
374 None => (t, None),
375 };
376 let (rel_path, query) = match no_frag.split_once('?') {
377 Some((a, q)) => (a, Some(q)),
378 None => (no_frag, None),
379 };
380 let base_clean = base_path.split(['?', '#']).next().unwrap_or("");
382 let base_clean = if base_clean.is_empty() { "/" } else { base_clean };
383 let mut path = if rel_path.is_empty() {
384 String::from(base_clean)
386 } else if rel_path.starts_with('/') {
387 normalize_path(rel_path)
388 } else {
389 let dir_end = base_clean.rfind('/').map(|i| i + 1).unwrap_or(1);
390 let dir = base_clean.get(..dir_end).unwrap_or("/");
391 normalize_path(&format!("{}{}", dir, rel_path))
392 };
393 if let Some(q) = query {
394 path.push('?');
395 path.push_str(q);
396 }
397 if let Some(f) = frag {
398 path.push('#');
399 path.push_str(f);
400 }
401 NavTarget::Page(base_is_https, String::from(base_host), path)
402}
403
404pub fn tab_label(title: &str, url: &str) -> String {
409 let t = title.trim();
410 if !t.is_empty() && t != "Untitled Page" {
411 return String::from(t);
412 }
413 let rest = url
414 .strip_prefix("https://")
415 .or_else(|| url.strip_prefix("http://"))
416 .unwrap_or(url);
417 let rest = rest.trim_end_matches('/');
418 if rest.is_empty() {
419 return String::from("Untitled");
420 }
421 String::from(rest)
422}