iris-cssom 0.1.1

Iris CSS Object Model (CSSOM) implementation: CSS parsing, style computation, CSS Modules, and Web API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! CSS 解析器
//!
//! 基于 cssparser 实现 CSS 样式表解析。

use cssparser::{Parser, ParserInput};

/// CSS 选择器类型
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq)]
pub enum SelectorType {
    /// 标签选择器: div, p, span
    Tag(String),
    /// ID 选择器: #id
    Id(String),
    /// Class 选择器: .class
    Class(String),
    /// 属性选择器: [attr=value]
    Attribute { name: String, value: Option<String> },
    /// 通配符: *
    Universal,
    /// 复合选择器: div.class#id
    Compound(Vec<SelectorType>),
    /// 后代选择器: div p
    Descendant(Box<SelectorType>, Box<SelectorType>),
    /// 子元素选择器: div > p
    Child(Box<SelectorType>, Box<SelectorType>),
}

/// CSS 选择器
#[derive(Debug, Clone, PartialEq)]
pub struct Selector {
    /// 选择器文本 (如 ".class", "#id", "div")
    pub text: String,
    /// 选择器类型(增强版)
    pub selector_type: SelectorType,
}

impl Selector {
    /// 创建新的选择器(自动解析类型)
    pub fn new(text: &str) -> Self {
        let selector_type = parse_selector_type(text);
        Self {
            text: text.to_string(),
            selector_type,
        }
    }

    /// 创建指定类型的选择器
    pub fn with_type(text: &str, selector_type: SelectorType) -> Self {
        Self {
            text: text.to_string(),
            selector_type,
        }
    }

    /// 判断是否为 ID 选择器
    pub fn is_id(&self) -> bool {
        matches!(self.selector_type, SelectorType::Id(_))
    }

    /// 判断是否为 Class 选择器
    pub fn is_class(&self) -> bool {
        matches!(self.selector_type, SelectorType::Class(_))
    }

    /// 判断是否为标签选择器
    pub fn is_tag(&self) -> bool {
        matches!(self.selector_type, SelectorType::Tag(_))
    }

    /// 判断是否为复合选择器
    pub fn is_compound(&self) -> bool {
        matches!(self.selector_type, SelectorType::Compound(_))
    }
}

/// 解析选择器类型(增强版)
fn parse_selector_type(text: &str) -> SelectorType {
    let text = text.trim();
    
    // 通配符
    if text == "*" {
        return SelectorType::Universal;
    }
    
    // 属性选择器 [attr] 或 [attr=value]
    if text.starts_with('[') && text.ends_with(']') {
        let content = &text[1..text.len() - 1];
        if let Some(eq_pos) = content.find('=') {
            let name = content[..eq_pos].trim().to_string();
            let value = Some(content[eq_pos + 1..].trim().trim_matches(|c| c == '"' || c == '\'').to_string());
            return SelectorType::Attribute { name, value };
        } else {
            return SelectorType::Attribute {
                name: content.trim().to_string(),
                value: None,
            };
        }
    }
    
    // 复合选择器(包含 . 或 # 的组合)
    if (text.contains('.') || text.contains('#')) && !text.starts_with('.') && !text.starts_with('#') {
        // 例如: div.class, div#id, div.class1.class2
        let mut parts = Vec::new();
        let mut current = String::new();
        let mut chars = text.chars().peekable();
        
        while let Some(ch) = chars.next() {
            if ch == '.' || ch == '#' {
                if !current.is_empty() {
                    // 保存之前的部分
                    if parts.is_empty() && !current.starts_with('.') && !current.starts_with('#') {
                        parts.push(SelectorType::Tag(current.clone()));
                    } else if current.starts_with('.') {
                        parts.push(SelectorType::Class(current[1..].to_string()));
                    } else if current.starts_with('#') {
                        parts.push(SelectorType::Id(current[1..].to_string()));
                    }
                    current.clear();
                }
                current.push(ch);
            } else {
                current.push(ch);
            }
        }
        
        // 处理最后一部分
        if !current.is_empty() {
            if current.starts_with('.') {
                parts.push(SelectorType::Class(current[1..].to_string()));
            } else if current.starts_with('#') {
                parts.push(SelectorType::Id(current[1..].to_string()));
            } else {
                parts.push(SelectorType::Class(current)); // 默认当作 class
            }
        }
        
        if parts.len() > 1 {
            return SelectorType::Compound(parts);
        }
    }
    
    // 简单选择器
    if text.starts_with('#') {
        SelectorType::Id(text[1..].to_string())
    } else if text.starts_with('.') {
        SelectorType::Class(text[1..].to_string())
    } else {
        SelectorType::Tag(text.to_string())
    }
}

/// CSS 声明 (属性: 值)
#[derive(Debug, Clone)]
pub struct Declaration {
    /// 属性名 (如 "color", "font-size")
    pub property: String,
    /// 属性值 (如 "red", "16px")
    pub value: String,
}

/// CSS 规则 (选择器 { 声明块 })
#[derive(Debug, Clone)]
pub struct CSSRule {
    /// 选择器
    pub selector: Selector,
    /// 声明列表
    pub declarations: Vec<Declaration>,
}

impl CSSRule {
    /// 创建新的 CSS 规则
    pub fn new(selector: Selector, declarations: Vec<Declaration>) -> Self {
        Self {
            selector,
            declarations,
        }
    }
}

/// CSS 样式表
#[derive(Debug, Clone)]
pub struct Stylesheet {
    /// CSS 规则列表
    pub rules: Vec<CSSRule>,
}

impl Stylesheet {
    /// 创建空的样式表
    pub fn new() -> Self {
        Self {
            rules: Vec::new(),
        }
    }

    /// 添加规则
    pub fn add_rule(&mut self, rule: CSSRule) {
        self.rules.push(rule);
    }
}

/// 解析 CSS 字符串,生成样式表
///
/// # 示例
///
/// ```rust
/// use iris_cssom::css::parse_stylesheet;
///
/// let css = r#"
///     .container {
///         padding: 20px;
///         background-color: white;
///     }
///     
///     #title {
///         font-size: 24px;
///         color: blue;
///     }
/// "#;
///
/// let stylesheet = parse_stylesheet(css);
/// assert!(!stylesheet.rules.is_empty());
/// ```
pub fn parse_stylesheet(css: &str) -> Stylesheet {
    let mut input = ParserInput::new(css);
    let _parser = Parser::new(&mut input);
    
    let mut stylesheet = Stylesheet::new();
    
    // 简化实现:手动解析 CSS
    // 实际应该使用 cssparser 的完整解析能力
    parse_css_manual(css, &mut stylesheet);
    
    stylesheet
}

/// 手动解析 CSS (简化实现)
fn parse_css_manual(css: &str, stylesheet: &mut Stylesheet) {
    // 移除注释
    let css = remove_comments(css);
    
    // 分割规则
    let rules = split_rules(&css);
    
    for rule_text in rules {
        if let Some(rule) = parse_single_rule(rule_text.trim()) {
            stylesheet.add_rule(rule);
        }
    }
}

/// 移除 CSS 注释
fn remove_comments(css: &str) -> String {
    let mut result = String::new();
    let mut chars = css.chars().peekable();
    
    while let Some(ch) = chars.next() {
        if ch == '/' {
            if let Some(&'*') = chars.peek() {
                chars.next(); // consume '*'
                // Skip until */
                while let Some(ch) = chars.next() {
                    if ch == '*' {
                        if let Some(&'/') = chars.peek() {
                            chars.next(); // consume '/'
                            break;
                        }
                    }
                }
            } else {
                result.push(ch);
            }
        } else {
            result.push(ch);
        }
    }
    
    result
}

/// 分割 CSS 规则
fn split_rules(css: &str) -> Vec<&str> {
    let mut rules = Vec::new();
    let mut start = 0;
    let mut brace_count = 0;
    
    for (i, ch) in css.char_indices() {
        match ch {
            '{' => brace_count += 1,
            '}' => {
                brace_count -= 1;
                if brace_count == 0 {
                    rules.push(&css[start..=i]);
                    start = i + 1;
                }
            }
            _ => {}
        }
    }
    
    rules
}

/// 解析单个 CSS 规则
fn parse_single_rule(rule_text: &str) -> Option<CSSRule> {
    // 查找 { 的位置
    let brace_pos = rule_text.find('{')?;
    
    let selector_text = rule_text[..brace_pos].trim();
    let declarations_text = &rule_text[brace_pos + 1..rule_text.len() - 1]; // remove { }
    
    if selector_text.is_empty() {
        return None;
    }
    
    let selector = Selector::new(selector_text);
    let declarations = parse_declarations(declarations_text);
    
    Some(CSSRule::new(selector, declarations))
}

/// 解析声明块
fn parse_declarations(text: &str) -> Vec<Declaration> {
    let mut declarations = Vec::new();
    
    for decl_text in text.split(';') {
        let decl_text = decl_text.trim();
        if decl_text.is_empty() {
            continue;
        }
        
        if let Some(colon_pos) = decl_text.find(':') {
            let property = decl_text[..colon_pos].trim().to_string();
            let value = decl_text[colon_pos + 1..].trim().to_string();
            
            declarations.push(Declaration { property, value });
        }
    }
    
    declarations
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_css() {
        let css = r#"
            .container {
                padding: 20px;
                background-color: white;
            }
        "#;
        
        let stylesheet = parse_stylesheet(css);
        assert_eq!(stylesheet.rules.len(), 1);
        assert_eq!(stylesheet.rules[0].selector.text, ".container");
        assert_eq!(stylesheet.rules[0].declarations.len(), 2);
    }

    #[test]
    fn test_parse_multiple_rules() {
        let css = r#"
            .class1 { color: red; }
            #id1 { font-size: 16px; }
            div { margin: 0; }
        "#;
        
        let stylesheet = parse_stylesheet(css);
        assert_eq!(stylesheet.rules.len(), 3);
    }

    #[test]
    fn test_selector_types() {
        let class_sel = Selector::new(".container");
        let id_sel = Selector::new("#main");
        let tag_sel = Selector::new("div");
        
        assert!(class_sel.is_class());
        assert!(id_sel.is_id());
        assert!(tag_sel.is_tag());
    }

    #[test]
    fn test_parse_with_comments() {
        let css = r#"
            /* This is a comment */
            .container {
                padding: 20px; /* inline comment */
            }
        "#;
        
        let stylesheet = parse_stylesheet(css);
        assert_eq!(stylesheet.rules.len(), 1);
    }

    #[test]
    fn test_attribute_selector() {
        let sel1 = Selector::new("[data-type]");
        let sel2 = Selector::new("[data-type=button]");
        let sel3 = Selector::new("[href=\"https://example.com\"]");
        
        assert!(sel1.is_compound() || matches!(sel1.selector_type, crate::css::SelectorType::Attribute { .. }));
        assert!(sel2.is_compound() || matches!(sel2.selector_type, crate::css::SelectorType::Attribute { .. }));
        assert!(sel3.is_compound() || matches!(sel3.selector_type, crate::css::SelectorType::Attribute { .. }));
    }

    #[test]
    fn test_compound_selector() {
        let sel = Selector::new("div.container");
        assert!(sel.is_compound());
        
        let sel2 = Selector::new("div#main.container");
        assert!(sel2.is_compound());
    }

    #[test]
    fn test_universal_selector() {
        let sel = Selector::new("*");
        assert!(matches!(sel.selector_type, crate::css::SelectorType::Universal));
    }

    #[test]
    fn test_selector_type_parsing() {
        use crate::css::SelectorType;
        
        let id_sel = Selector::new("#main");
        assert!(matches!(id_sel.selector_type, SelectorType::Id(s) if s == "main"));
        
        let class_sel = Selector::new(".container");
        assert!(matches!(class_sel.selector_type, SelectorType::Class(s) if s == "container"));
        
        let tag_sel = Selector::new("div");
        assert!(matches!(tag_sel.selector_type, SelectorType::Tag(s) if s == "div"));
    }
}