Skip to main content

CSSStyleSheet

Struct CSSStyleSheet 

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

CSSStyleSheet 对象

表示一个 CSS 样式表,提供对样式表规则的完整操作能力。 对标 Web API: CSSStyleSheet

§示例

use iris_cssom::stylesheet::CSSStyleSheet;

let mut sheet = CSSStyleSheet::new();
sheet.insert_rule(".container { color: red; }", 0).unwrap();

assert_eq!(sheet.css_rules().lock().unwrap().length(), 1);

Implementations§

Source§

impl CSSStyleSheet

Source

pub fn new() -> Self

创建空的样式表

§示例
use iris_cssom::stylesheet::CSSStyleSheet;

let sheet = CSSStyleSheet::new();
assert!(!sheet.disabled());
assert_eq!(sheet.css_rules().lock().unwrap().length(), 0);
Source

pub fn from_css(css_text: &str) -> Self

从 CSS 文本创建样式表

§参数
  • css_text - CSS 文本内容
§示例
use iris_cssom::stylesheet::CSSStyleSheet;

let css = r#"
    .container { padding: 20px; }
    #header { background: blue; }
"#;
let sheet = CSSStyleSheet::from_css(css);
assert!(sheet.css_rules().lock().unwrap().length() > 0);
Source

pub fn disabled(&self) -> bool

获取样式表是否禁用

Source

pub fn set_disabled(&mut self, disabled: bool)

设置样式表禁用状态

Source

pub fn href(&self) -> Option<&str>

获取样式表 URL

Source

pub fn set_href(&mut self, href: &str)

设置样式表 URL

Source

pub fn owner_node(&self) -> Option<&str>

获取拥有此样式表的 DOM 元素标识

Source

pub fn set_owner_node(&mut self, node_id: &str)

设置拥有此样式表的 DOM 元素标识

Source

pub fn css_rules(&self) -> Arc<Mutex<CSSRuleList>>

获取规则列表(实时更新的 live list)

§示例
use iris_cssom::stylesheet::CSSStyleSheet;

let mut sheet = CSSStyleSheet::new();
sheet.insert_rule(".class { color: red; }", 0).unwrap();

let rules = sheet.css_rules();
assert_eq!(rules.lock().unwrap().length(), 1);
Source

pub fn insert_rule(&mut self, rule: &str, index: usize) -> Result<u32, String>

插入一条新规则

§参数
  • rule - CSS 规则文本(如 “.class { color: red; }”)
  • index - 插入位置(从 0 开始)
§返回值

成功返回插入位置的索引,失败返回错误

§错误
  • 如果索引超出范围,返回 Err
  • 如果 CSS 规则语法错误,返回 Err
§示例
use iris_cssom::stylesheet::CSSStyleSheet;

let mut sheet = CSSStyleSheet::new();
let index = sheet.insert_rule(".container { color: red; }", 0);
assert!(index.is_ok());
assert_eq!(index.unwrap(), 0);
Source

pub fn delete_rule(&mut self, index: usize) -> Result<(), String>

删除指定索引的规则

§参数
  • index - 要删除的规则索引
§错误

如果索引超出范围,返回 Err

§示例
use iris_cssom::stylesheet::CSSStyleSheet;

let mut sheet = CSSStyleSheet::new();
sheet.insert_rule(".class { color: red; }", 0).unwrap();
sheet.delete_rule(0).unwrap();

assert_eq!(sheet.css_rules().lock().unwrap().length(), 0);
Source

pub fn replace_rule( &mut self, old_rule: &str, new_rule: &str, ) -> Result<u32, String>

替换指定索引的规则

§参数
  • old_rule - 旧的 CSS 规则文本(用于匹配)
  • new_rule - 新的 CSS 规则文本
§返回值

成功返回 Ok(()),失败返回错误

§注意

这是较新的 API(replaceRule),部分浏览器支持

Source

pub fn add_rule(&mut self, rule: &str) -> Result<u32, String>

添加一条规则到末尾(较新的 API)

§参数
  • rule - CSS 规则文本
§返回值

成功返回新规则的索引

Source

pub fn internal_stylesheet(&self) -> Stylesheet

获取内部样式表(用于与 iris-layout 集成)

Source

pub fn get_css_text(&self) -> String

获取 CSS 文本(整个样式表的文本表示)

§示例
use iris_cssom::stylesheet::CSSStyleSheet;

let mut sheet = CSSStyleSheet::new();
sheet.insert_rule(".class { color: red; }", 0).unwrap();

let css_text = sheet.get_css_text();
assert!(css_text.contains(".class"));
Source

pub fn clear(&mut self)

清空所有规则

Source

pub fn rule_count(&self) -> u32

获取规则数量

Trait Implementations§

Source§

impl Clone for CSSStyleSheet

Source§

fn clone(&self) -> CSSStyleSheet

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 CSSStyleSheet

Source§

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

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

impl Default for CSSStyleSheet

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.