pub struct CSSStyleDeclaration { /* private fields */ }Expand description
CSSStyleDeclaration 对象
表示一个 CSS 声明块,提供对样式属性的读写操作。
对标 Web API: CSSStyleDeclaration
Implementations§
Source§impl CSSStyleDeclaration
impl CSSStyleDeclaration
Sourcepub fn from_declarations(declarations: &[Declaration]) -> Self
pub fn from_declarations(declarations: &[Declaration]) -> Self
从声明列表创建
Sourcepub fn get_property_value(&self, property: &str) -> String
pub fn get_property_value(&self, property: &str) -> String
获取属性值
§示例
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"), ""); // 不存在的属性Sourcepub fn get_property_priority(&self, property: &str) -> String
pub fn get_property_priority(&self, property: &str) -> String
获取属性优先级
返回 “important” 或空字符串
§示例
use iris_cssom::cssom::CSSStyleDeclaration;
let mut style = CSSStyleDeclaration::new();
style.set_property("color", "red", "important");
assert_eq!(style.get_property_priority("color"), "important");Sourcepub fn set_property(&mut self, property: &str, value: &str, priority: &str)
pub fn set_property(&mut self, property: &str, value: &str, priority: &str)
Sourcepub fn remove_property(&mut self, property: &str) -> String
pub fn remove_property(&mut self, property: &str) -> String
移除属性
返回被移除的属性值,如果属性不存在则返回空字符串
§示例
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"), "");Sourcepub fn length(&self) -> usize
pub fn length(&self) -> usize
获取属性数量
§示例
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);Sourcepub fn get_css_text(&self) -> String
pub fn get_css_text(&self) -> String
获取 CSS 文本表示
返回格式:property1: value1; property2: value2 !important;
§示例
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"));Sourcepub fn set_css_text(&mut self, text: &str)
pub fn set_css_text(&mut self, text: &str)
设置 CSS 文本
解析 CSS 文本并替换所有属性
§示例
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");Sourcepub fn get_property_names(&self) -> Vec<String>
pub fn get_property_names(&self) -> Vec<String>
获取所有属性名列表
§示例
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()));Sourcepub fn has_property(&self, property: &str) -> bool
pub fn has_property(&self, property: &str) -> bool
检查是否包含某个属性
§示例
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"));Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
清空所有属性
§示例
use iris_cssom::cssom::CSSStyleDeclaration;
let mut style = CSSStyleDeclaration::new();
style.set_property("color", "red", "");
style.clear();
assert_eq!(style.length(), 0);Sourcepub fn merge(&mut self, other: &CSSStyleDeclaration)
pub fn merge(&mut self, other: &CSSStyleDeclaration)
合并另一个样式声明
只在当前没有该属性时才覆盖(低优先级)
§示例
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"); // 新增Sourcepub fn to_declarations(&self) -> Vec<Declaration>
pub fn to_declarations(&self) -> Vec<Declaration>
转换为内部声明列表(用于与 iris-layout 集成)
Trait Implementations§
Source§impl Clone for CSSStyleDeclaration
impl Clone for CSSStyleDeclaration
Source§fn clone(&self) -> CSSStyleDeclaration
fn clone(&self) -> CSSStyleDeclaration
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CSSStyleDeclaration
impl Debug for CSSStyleDeclaration
Auto Trait Implementations§
impl Freeze for CSSStyleDeclaration
impl RefUnwindSafe for CSSStyleDeclaration
impl Send for CSSStyleDeclaration
impl Sync for CSSStyleDeclaration
impl Unpin for CSSStyleDeclaration
impl UnsafeUnpin for CSSStyleDeclaration
impl UnwindSafe for CSSStyleDeclaration
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more