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
438
439
440
441
442
443
444
//! CSSStyleDeclaration - CSSOM 样式声明对象
//!
//! 实现 Web 标准的 CSSStyleDeclaration API,用于操作 CSS 属性。
//!
//! # 示例
//!
//! ```rust
//! use iris_cssom::cssom::CSSStyleDeclaration;
//!
//! let mut style = CSSStyleDeclaration::new();
//! style.set_property("color", "red", "");
//! style.set_property("font-size", "16px", "important");
//!
//! assert_eq!(style.get_property_value("color"), "red");
//! assert_eq!(style.get_property_priority("font-size"), "important");
//! ```

use std::collections::HashMap;

/// CSS 属性值(包含优先级信息)
#[derive(Debug, Clone)]
struct CSSPropertyValue {
    value: String,
    important: bool,
}

/// CSSStyleDeclaration 对象
///
/// 表示一个 CSS 声明块,提供对样式属性的读写操作。
/// 对标 Web API: `CSSStyleDeclaration`
#[derive(Debug, Clone)]
pub struct CSSStyleDeclaration {
    /// 属性映射
    properties: HashMap<String, CSSPropertyValue>,
    /// 父样式表引用(可选)
    #[allow(dead_code)]
    parent_stylesheet: Option<String>,
}

impl CSSStyleDeclaration {
    /// 创建空的样式声明
    pub fn new() -> Self {
        Self {
            properties: HashMap::new(),
            parent_stylesheet: None,
        }
    }

    /// 从声明列表创建
    pub fn from_declarations(declarations: &[crate::css::Declaration]) -> Self {
        let mut properties = HashMap::new();
        for decl in declarations {
            properties.insert(
                decl.property.clone(),
                CSSPropertyValue {
                    value: decl.value.clone(),
                    important: false, // 默认不重要
                },
            );
        }
        Self {
            properties,
            parent_stylesheet: None,
        }
    }

    /// 获取属性值
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// assert_eq!(style.get_property_value("color"), "red");
    /// assert_eq!(style.get_property_value("background"), ""); // 不存在的属性
    /// ```
    pub fn get_property_value(&self, property: &str) -> String {
        self.properties
            .get(property)
            .map(|p| p.value.clone())
            .unwrap_or_default()
    }

    /// 获取属性优先级
    ///
    /// 返回 "important" 或空字符串
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "important");
    /// assert_eq!(style.get_property_priority("color"), "important");
    /// ```
    pub fn get_property_priority(&self, property: &str) -> String {
        self.properties
            .get(property)
            .map(|p| if p.important { "important" } else { "" })
            .unwrap_or_default()
            .to_string()
    }

    /// 设置属性值
    ///
    /// # 参数
    ///
    /// * `property` - 属性名(如 "color", "font-size")
    /// * `value` - 属性值(如 "red", "16px")
    /// * `priority` - 优先级("" 或 "important")
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// style.set_property("font-weight", "bold", "important");
    /// ```
    pub fn set_property(&mut self, property: &str, value: &str, priority: &str) {
        let important = priority.to_lowercase() == "important";
        self.properties.insert(
            property.to_lowercase(),
            CSSPropertyValue {
                value: value.to_string(),
                important,
            },
        );
    }

    /// 移除属性
    ///
    /// 返回被移除的属性值,如果属性不存在则返回空字符串
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// let removed = style.remove_property("color");
    /// assert_eq!(removed, "red");
    /// assert_eq!(style.get_property_value("color"), "");
    /// ```
    pub fn remove_property(&mut self, property: &str) -> String {
        self.properties
            .remove(&property.to_lowercase())
            .map(|p| p.value)
            .unwrap_or_default()
    }

    /// 获取属性数量
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// style.set_property("font-size", "16px", "");
    /// assert_eq!(style.length(), 2);
    /// ```
    pub fn length(&self) -> usize {
        self.properties.len()
    }

    /// 根据索引获取属性名
    ///
    /// # 注意
    ///
    /// 由于 HashMap 是无序的,这个方法返回的属性名顺序不保证稳定
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// if let Some(prop) = style.item(0) {
    ///     assert_eq!(prop, "color");
    /// }
    /// ```
    pub fn item(&self, index: usize) -> Option<String> {
        self.properties.keys().nth(index).cloned()
    }

    /// 获取 CSS 文本表示
    ///
    /// 返回格式:`property1: value1; property2: value2 !important;`
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// style.set_property("font-weight", "bold", "important");
    ///
    /// let css_text = style.get_css_text();
    /// assert!(css_text.contains("color: red"));
    /// assert!(css_text.contains("font-weight: bold !important"));
    /// ```
    pub fn get_css_text(&self) -> String {
        self.properties
            .iter()
            .map(|(k, v)| {
                let important = if v.important { " !important" } else { "" };
                format!("{}: {}{}", k, v.value, important)
            })
            .collect::<Vec<_>>()
            .join("; ")
    }

    /// 设置 CSS 文本
    ///
    /// 解析 CSS 文本并替换所有属性
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_css_text("color: red; font-size: 16px");
    /// assert_eq!(style.get_property_value("color"), "red");
    /// assert_eq!(style.get_property_value("font-size"), "16px");
    /// ```
    pub fn set_css_text(&mut self, text: &str) {
        self.properties.clear();
        
        // 简单的 CSS 解析器
        for declaration in text.split(';') {
            let declaration = declaration.trim();
            if declaration.is_empty() {
                continue;
            }
            
            if let Some(colon_pos) = declaration.find(':') {
                let property = declaration[..colon_pos].trim().to_lowercase();
                let mut value_part = declaration[colon_pos + 1..].trim();
                
                // 检查 !important
                let important = value_part.ends_with("!important");
                if important {
                    value_part = value_part[..value_part.len() - 10].trim();
                }
                
                if !property.is_empty() && !value_part.is_empty() {
                    self.properties.insert(
                        property,
                        CSSPropertyValue {
                            value: value_part.to_string(),
                            important,
                        },
                    );
                }
            }
        }
    }

    /// 获取所有属性名列表
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// style.set_property("font-size", "16px", "");
    ///
    /// let props = style.get_property_names();
    /// assert_eq!(props.len(), 2);
    /// assert!(props.contains(&"color".to_string()));
    /// ```
    pub fn get_property_names(&self) -> Vec<String> {
        self.properties.keys().cloned().collect()
    }

    /// 检查是否包含某个属性
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// assert!(style.has_property("color"));
    /// assert!(!style.has_property("background"));
    /// ```
    pub fn has_property(&self, property: &str) -> bool {
        self.properties.contains_key(&property.to_lowercase())
    }

    /// 清空所有属性
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style = CSSStyleDeclaration::new();
    /// style.set_property("color", "red", "");
    /// style.clear();
    /// assert_eq!(style.length(), 0);
    /// ```
    pub fn clear(&mut self) {
        self.properties.clear();
    }

    /// 合并另一个样式声明
    ///
    /// 只在当前没有该属性时才覆盖(低优先级)
    ///
    /// # 示例
    ///
    /// ```rust
    /// use iris_cssom::cssom::CSSStyleDeclaration;
    ///
    /// let mut style1 = CSSStyleDeclaration::new();
    /// style1.set_property("color", "red", "");
    ///
    /// let mut style2 = CSSStyleDeclaration::new();
    /// style2.set_property("font-size", "16px", "");
    /// style2.set_property("color", "blue", ""); // 不会覆盖 style1
    ///
    /// style1.merge(&style2);
    /// assert_eq!(style1.get_property_value("color"), "red"); // 保留原值
    /// assert_eq!(style1.get_property_value("font-size"), "16px"); // 新增
    /// ```
    pub fn merge(&mut self, other: &CSSStyleDeclaration) {
        for (key, value) in &other.properties {
            if !self.properties.contains_key(key) {
                self.properties.insert(key.clone(), value.clone());
            }
        }
    }

    /// 转换为内部声明列表(用于与 iris-layout 集成)
    pub fn to_declarations(&self) -> Vec<crate::css::Declaration> {
        self.properties
            .iter()
            .map(|(k, v)| crate::css::Declaration {
                property: k.clone(),
                value: v.value.clone(),
            })
            .collect()
    }
}

impl Default for CSSStyleDeclaration {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_set_and_get_property() {
        let mut style = CSSStyleDeclaration::new();
        style.set_property("color", "red", "");
        assert_eq!(style.get_property_value("color"), "red");
    }

    #[test]
    fn test_important_priority() {
        let mut style = CSSStyleDeclaration::new();
        style.set_property("color", "red", "important");
        assert_eq!(style.get_property_priority("color"), "important");
    }

    #[test]
    fn test_remove_property() {
        let mut style = CSSStyleDeclaration::new();
        style.set_property("color", "red", "");
        let removed = style.remove_property("color");
        assert_eq!(removed, "red");
        assert!(!style.has_property("color"));
    }

    #[test]
    fn test_css_text() {
        let mut style = CSSStyleDeclaration::new();
        style.set_property("color", "red", "");
        style.set_property("font-weight", "bold", "important");
        
        let css_text = style.get_css_text();
        assert!(css_text.contains("color: red"));
        assert!(css_text.contains("font-weight: bold !important"));
    }

    #[test]
    fn test_set_css_text() {
        let mut style = CSSStyleDeclaration::new();
        style.set_css_text("color: red; font-size: 16px !important");
        
        assert_eq!(style.get_property_value("color"), "red");
        assert_eq!(style.get_property_value("font-size"), "16px");
        assert_eq!(style.get_property_priority("font-size"), "important");
    }

    #[test]
    fn test_length_and_item() {
        let mut style = CSSStyleDeclaration::new();
        style.set_property("color", "red", "");
        style.set_property("font-size", "16px", "");
        
        assert_eq!(style.length(), 2);
        assert!(style.item(0).is_some());
    }

    #[test]
    fn test_merge() {
        let mut style1 = CSSStyleDeclaration::new();
        style1.set_property("color", "red", "");
        
        let mut style2 = CSSStyleDeclaration::new();
        style2.set_property("font-size", "16px", "");
        
        style1.merge(&style2);
        assert_eq!(style1.get_property_value("color"), "red");
        assert_eq!(style1.get_property_value("font-size"), "16px");
    }

    #[test]
    fn test_clear() {
        let mut style = CSSStyleDeclaration::new();
        style.set_property("color", "red", "");
        style.clear();
        assert_eq!(style.length(), 0);
    }
}