Skip to main content

CSSStyleDeclaration

Struct CSSStyleDeclaration 

Source
pub struct CSSStyleDeclaration { /* private fields */ }
Expand description

CSSStyleDeclaration 对象

表示一个 CSS 声明块,提供对样式属性的读写操作。 对标 Web API: CSSStyleDeclaration

Implementations§

Source§

impl CSSStyleDeclaration

Source

pub fn new() -> Self

创建空的样式声明

Source

pub fn from_declarations(declarations: &[Declaration]) -> Self

从声明列表创建

Source

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"), ""); // 不存在的属性
Source

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");
Source

pub fn set_property(&mut self, property: &str, value: &str, priority: &str)

设置属性值

§参数
  • property - 属性名(如 “color”, “font-size”)
  • value - 属性值(如 “red”, “16px”)
  • priority - 优先级(“” 或 “important”)
§示例
use iris_cssom::cssom::CSSStyleDeclaration;

let mut style = CSSStyleDeclaration::new();
style.set_property("color", "red", "");
style.set_property("font-weight", "bold", "important");
Source

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"), "");
Source

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);
Source

pub fn item(&self, index: usize) -> Option<String>

根据索引获取属性名

§注意

由于 HashMap 是无序的,这个方法返回的属性名顺序不保证稳定

§示例
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");
}
Source

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"));
Source

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");
Source

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()));
Source

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"));
Source

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);
Source

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"); // 新增
Source

pub fn to_declarations(&self) -> Vec<Declaration>

转换为内部声明列表(用于与 iris-layout 集成)

Trait Implementations§

Source§

impl Clone for CSSStyleDeclaration

Source§

fn clone(&self) -> CSSStyleDeclaration

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CSSStyleDeclaration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CSSStyleDeclaration

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.