Skip to main content

atmos/os_lib/css/
flow_pseudo.rs

1//! 流れの中の空の `::before`/`::after` の箱の値(純粋モジュール。`layout::pseudo_box` は位置指定の方)。
2//! スタイル木へ子として加える処理は `cascade::attach_flow_pseudo_boxes`。
3
4extern crate alloc;
5use alloc::collections::BTreeMap;
6use alloc::string::String;
7
8/// 疑似要素へ引き継ぐ継承プロパティ(`layout::pseudo_box::INHERITED` と同じ)。
9const INHERITED: &[&str] = &["color", "font-family", "font-size", "font-weight", "font-style", "line-height", "visibility"];
10
11/// 流れの中に置く、空の `::before`/`::after` の箱の値の表(`display:block`/`flex`/`grid`/`table`/
12/// `flow-root`/`list-item`、または `float`。`content` が `''`/`' '` のもの)。
13///
14/// 【2026-11-30】`::before{content:'';display:block;height:20px}` が箱にならず、高さ 0 だった
15/// (疑似要素の宣言は要素自身の `x-before-*` に畳まれ、位置指定の疑似要素だけが描画側で箱になっていた)。
16/// ここで作る表を持つ `StyledNode` を、スタイル木の子(先頭/末尾)として加える(`css::cascade`)ので、
17/// レイアウトは通常の子と同じに扱う。中身のある `content`(文字)・`inline`/`inline-block` は従来どおり別経路。
18pub fn flow_pseudo_values(parent: &BTreeMap<String, String>, which: &str) -> Option<BTreeMap<String, String>> {
19    let mut key = String::with_capacity(16);
20    key.push_str("x-");
21    key.push_str(which);
22    key.push('-');
23    let prefix_len = key.len();
24    key.push_str("content");
25    let content = parent.get(&key)?;
26    if !content.trim().is_empty() {
27        return None;
28    }
29    let mut out = BTreeMap::new();
30    for k in INHERITED {
31        if let Some(v) = parent.get(*k) {
32            out.insert(String::from(*k), v.clone());
33        }
34    }
35    key.truncate(prefix_len);
36    for (k, v) in parent.range(key.clone()..) {
37        let Some(name) = k.strip_prefix(key.as_str()) else { break };
38        out.insert(String::from(name), v.clone());
39    }
40    out.remove("content");
41    if matches!(out.get("position").map(|p| p.trim()), Some("absolute") | Some("fixed")) {
42        return None;
43    }
44    let floated = matches!(out.get("float").map(|f| f.trim()), Some("left") | Some("right") | Some("inline-start") | Some("inline-end"));
45    let display_ok = match out.get("display").map(|d| d.trim()) {
46        Some("block" | "flow-root" | "list-item" | "flex" | "grid" | "table" | "block flow" | "block flow-root" | "block flex" | "block grid" | "block table") => true,
47        Some("none") => return None,
48        Some(_) => floated,
49        None => floated,
50    };
51    if !display_ok {
52        return None;
53    }
54    if !out.contains_key("display") || out.get("display").map(|d| d.trim()) == Some("inline") {
55        out.insert(String::from("display"), String::from("block"));
56    }
57    Some(out)
58}
59
60/// 流れの中の疑似要素の箱を子に持てる親か(置換要素・フォーム部品・表の内部・inline は除く)。
61pub fn flow_pseudo_parent_ok(tag: &str, display: &str) -> bool {
62    !matches!(
63        tag,
64        "img" | "iframe" | "input" | "textarea" | "select" | "video" | "canvas" | "progress" | "meter" | "svg" | "br"
65            | "hr" | "button" | "object" | "embed" | "table" | "thead" | "tbody" | "tfoot" | "tr" | "html"
66    ) && matches!(
67        display.trim(),
68        "block" | "flow-root" | "list-item" | "flex" | "grid" | "inline-block" | "table-cell" | "table-caption"
69    )
70}
71