Skip to main content

atmos/os_lib/web_engine/
url_resolve.rs

1// web_engine/url_resolve.rs - 外部CSS/リソースの相対URL解決。
2// 依存: alloc のみ(ホスト側ユニットテスト tests/ から直接 #[path] 取り込み可能にするため、
3// crate::os_lib/crate::kernel 側の型には一切依存しない)。
4
5use alloc::format;
6use alloc::string::String;
7use alloc::vec::Vec;
8
9/// `base_url`(絶対URL、例: `https://cdnjs.cloudflare.com/.../css/all.min.css`)を
10/// 基点として、CSS内の相対URL参照(`../webfonts/x.woff2` 等)を絶対URLへ解決する。
11/// 外部CSSファイル自身の場所を基準にする必要がある(HTMLドキュメント自身の
12/// ホストとは異なることが多い。例: cdnjs.cloudflare.com上のCSSが参照する
13/// webfontはcdnjs.cloudflare.com上の相対パス)。
14pub 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    // ベースURLのパス部分から、末尾のファイル名を除いたディレクトリを求める。
40    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(); // 末尾のファイル名部分を除去
46    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
61/// 外部CSSテキスト中の `url(...)` 参照を、そのCSSファイル自身の絶対URL
62/// (`css_base_url`)を基準に絶対URLへ書き換える。`@font-face { src: url(...) }`
63/// や `background-image: url(...)` 等、CSS内の相対パスは常にCSSファイル自身の
64/// 場所基準で解決されるべき(HTMLドキュメントのURLではない)という仕様に従う。
65pub 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
117/// ネットワーク切断(TLS読み取りエラー等)で外部CSSが波括弧の途中で
118/// 打ち切られた場合、そのまま後続のCSS(他の`<link>`分)と連結すると、
119/// 閉じられなかった`{`のせいでパーサーが「まだ前のルールの中にいる」と
120/// 誤認し、以降の本物のルールを大量に読み飛ばしてしまう(2026-07-25発見)。
121/// 文字列/コメントを簡易的に読み飛ばしつつ波括弧の深さを追跡し、深さが
122/// 0に戻った最後の位置(=最後まで完結した最後のトップレベルルールの
123/// 直後)までに安全側で切り詰める。既に完全にバランスしている場合は
124/// 元の文字列をそのまま返す。
125pub 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                // ブロックコメント内の { } を誤ってカウントしないよう読み飛ばす。
134                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                // 文字列リテラル内の { } を誤ってカウントしないよう読み飛ばす。
143                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
178/// 取得先の `(https か, ホスト, パス)` を求める。
179///
180/// `https://h/p`・`http://h/p`・`//h/p`(プロトコル相対)・`/p`(ルート相対)を
181/// 扱う。現在のページのスキームとホストを引き継ぐ場面があるので受け取る。
182///
183/// # なぜ共通化するか
184///
185/// この判定は**同じ誤りを 4 箇所で繰り返していた**。`//host/path` を
186/// `starts_with('/')` の枝に巻き込み、現在のホスト配下のパスとして
187/// 取りに行ってしまう誤りで、`js/builtins.rs`・`web_engine/render.rs` で
188/// 個別に直された後、`fetch_script_text_inner` に 4 例目が残っていた
189/// (実サイトの `//ajax.googleapis.com/.../jquery.min.js` が
190/// 自ホストの HTML を返し、それを JS として実行して構文エラーになっていた)。
191/// 判定を 1 箇所に集めて、5 例目が生まれないようにする。
192pub 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    // 素片(`#...`)は取得先に含めない。
207    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        // プロトコル相対。**現在のスキームを引き継ぎ、ホストは相手側**。
219        // ここを `/` の枝に落とすのが繰り返された誤り。
220        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
227// ---------------------------------------------------------------------------
228// アドレス欄・起動引数の入力の解釈(2026-11-17)
229// ---------------------------------------------------------------------------
230
231/// `about:` URL を内部で表すときのホスト名の接頭辞。
232/// `current_host` が `about:` で始まるページは、ネットワークへ出ない内蔵ページとして扱う。
233pub const ABOUT_HOST_PREFIX: &str = "about:";
234
235/// 入力が `about:` URL なら、正規化した名前(小文字・前後の空白なし。例 `blank`)を返す。
236/// 大文字小文字と前後の空白は無視する。`about:` で始まらなければ None。
237/// 【2026-11-17】従来は `about:blank` が接頭辞なしの入力として `http://about:blank/` に
238/// 化け、ホスト名 `about:blank` へ接続しようとして失敗していた。
239pub 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
251/// `about:` の正規化した URL 文字列(`about:blank` など)。`about:` でなければ None。
252pub fn normalize_about(input: &str) -> Option<String> {
253    about_name(input).map(|n| format!("{}{}", ABOUT_HOST_PREFIX, n))
254}
255
256/// `current_host` が内蔵の `about:` ページを指すか。
257pub fn is_about_host(host: &str) -> bool {
258    host.starts_with(ABOUT_HOST_PREFIX)
259}
260
261/// スキームなしの部分(`host/path`)をホストとパスに分ける。パスは必ず `/` で始まる。
262pub 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
269/// 明示的なスキームを持つ入力(`http://`・`https://`)を (https か, ホスト, パス) に分ける。
270/// それ以外(スキームなし・`about:` など)は None。
271pub 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// ---------------------------------------------------------------------------
285// ページ遷移先の解決(2026-11-17)
286// ---------------------------------------------------------------------------
287
288/// 遷移要求(`location.href = ...`・`assign`・`replace`・`<a href>`・フォームの GET 等)の
289/// 解決結果。
290#[derive(Debug, Clone, PartialEq, Eq)]
291pub enum NavTarget {
292    /// 遷移しない(空・`javascript:`・`data:`/`blob:`/`mailto:` など http(s)/about 以外のスキーム)。
293    Ignore,
294    /// 内蔵の `about:` ページ(正規化した `about:blank` など)。
295    About(String),
296    /// 同一ページ内の `#...` だけの移動。値は `#` を含む素片。
297    Fragment(String),
298    /// 取得先(https か, ホスト, パス)。パスは `/` で始まり、クエリ・素片を含む。
299    Page(bool, String, String),
300}
301
302/// パスのドットセグメント(`.`・`..`)と連続スラッシュを畳む。先頭は必ず `/`。
303/// 末尾が `/`・`.`・`..` なら、結果の末尾も `/` にする。
304fn 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
328/// `href` が http/https/about 以外のスキーム(`data:`・`blob:`・`javascript:`・`mailto:` 等)を
329/// 持つか。RFC 3986 のスキーム形式(先頭は英字、続けて英数字と `+-.`)で、`:` より前に
330/// `/`・`?`・`#` が無いものを指す。
331fn 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
340/// 遷移先 `href` を、現在のページ(`base_is_https`・`base_host`・`base_path`)を基準に解決する。
341/// 対応: 絶対 `http(s)://`、`about:`、`#素片`、`//host/path`、`/絶対パス`、`?query`、
342/// `foo.html`・`./foo.html`・`../x/foo.html`。
343/// 【2026-11-17】従来の `navigate_to_href` は「`.` を含み `/` を含まない相対 URL」を
344/// ホスト名として扱い、`location.href = "mini.html"` が `http://mini.html/` へ飛んでいた。
345pub 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    // 素片・クエリ・パスに分ける。
372    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    // 基準パスからクエリ・素片を除く。
381    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        // `?query` だけ(または空に近い入力)は基準のパスを保つ。
385        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
404/// タブ一覧に出す名前を決める(【2026-11-18】)。
405/// ページの題名があればそれ、無ければ `about:` ページは `about:blank` のような名前、
406/// それ以外は URL の「ホスト + パス」(スキームと末尾の `/` は省く)。
407/// 内蔵ページの既定の題名 "Untitled Page" も「題名なし」とみなす。
408pub 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}