HTMLElement

Struct HTMLElement 

Source
pub struct HTMLElement {
    pub attrs: Vec<(String, String)>,
    pub children: Vec<Node>,
    pub id: String,
    /* private fields */
}

Fields§

§attrs: Vec<(String, String)>§children: Vec<Node>§id: String

Implementations§

Source§

impl HTMLElement

Source

pub fn attrs_lower_decoded(&mut self) -> HashMap<String, String>

Source

pub fn set_attributes(&mut self, attributes: &[(String, String)])

Source

pub fn remove_attribute(&mut self, key: &str)

Source

pub fn get_attr(&self, key: &str) -> Option<&str>

Source

pub fn has_attr(&self, key: &str) -> bool

Source

pub fn set_attr(&mut self, key: &str, val: &str)

Source

pub fn remove_attr(&mut self, key: &str)

Source

pub fn remove_id(&mut self)

Convenience: remove the id attribute (safe wrapper for tests parity with JS removeAttribute(‘id’))

Source

pub fn set_id(&mut self, id: &str)

Convenience: set id attribute (safe wrapper to avoid direct raw mutation in tests)

Source

pub fn attributes(&mut self) -> HashMap<String, String>

Source

pub fn raw_attributes(&mut self) -> HashMap<String, String>

Source

pub fn raw_attrs_str(&self) -> &str

Read-only snapshot of the original raw attribute string (public accessor for tests like issue 136)

Source

pub fn get_attribute(&mut self, key: &str) -> Option<String>

Source

pub fn set_attribute(&mut self, key: &str, value: &str)

Source

pub fn has_attribute(&mut self, key: &str) -> bool

Source§

impl HTMLElement

Source

pub fn class_list(&mut self) -> Vec<String>

Source

pub fn class_list_view(&self) -> Vec<&str>

Source

pub fn class_list_contains(&mut self, token: &str) -> bool

Source

pub fn class_list_add(&mut self, token: &str)

Source

pub fn class_list_remove(&mut self, token: &str)

Source

pub fn class_list_toggle(&mut self, token: &str)

Source

pub fn class_list_replace(&mut self, old: &str, new: &str)

Source§

impl HTMLElement

Source

pub fn insert_adjacent_html( &mut self, position: &str, html: &str, ) -> Result<(), String>

解析片段为节点列表(不包裹额外 root),使用默认 Options insertAdjacentHTML(position, html) 支持: beforebegin | afterbegin | beforeend | afterend beforebegin/afterend 会插入到父节点 children 中(若无父则忽略)

Source

pub fn replace_with(&mut self, html_fragment: &str)

replaceWith(html_fragment) 用片段替换自身

Source

pub fn replace_with_items(&mut self, items: &[NodeOrStr<'_>])

Source

pub fn text(&self) -> String

Source

pub fn inner_text(&self) -> String

对应 JS innerText: 返回未解码聚合文本 (即 rawText)

Source

pub fn text_content(&self) -> String

对应 JS textContent getter: 返回解码后的文本

Source

pub fn set_text_content(&mut self, val: &str)

对应 JS textContent setter: 先对传入进行实体编码,再替换子节点为单一文本节点

Source

pub fn set_content_str(&mut self, content: &str, comment_override: Option<bool>)

Source

pub fn set_content(&mut self, content: &str)

JS 兼容别名:set_content(字符串) -> set_content_str,无注释覆盖。

Source

pub fn set_content_nodes(&mut self, nodes: Vec<Node>)

Source§

impl HTMLElement

Source

pub fn new( tag: Option<String>, raw_attrs: String, attrs: Vec<(String, String)>, is_void: bool, void_add_slash: bool, ) -> Self

Source

pub fn is_root(&self) -> bool

Source

pub fn name(&self) -> &str

Source

pub fn set_tag_name(&mut self, new_name: &str)

JS HTMLElement.tagName setter 行为:赋值后序列化使用小写(JS 内部存 rawTagName 小写,tagName getter 返回大写)。 为贴近 JS,我们内部沿用小写存储,外部序列化 already 调用 self.name()(即原样)。

Source

pub fn raw_text(&self) -> String

Source

pub fn class_names(&self) -> String

Source

pub fn inner_html(&self) -> String

Source

pub fn set_inner_html(&mut self, html: &str)

设置 innerHTML:清空旧子节点并以解析后的片段替换

Source

pub fn matches_selector<'a>( &'a self, root: &'a HTMLElement, selector: &str, ) -> bool

判断当前元素是否匹配 selector(使用全局选择再比对引用,性能次优)。

Source

pub fn matches(&self, selector: &str) -> bool

JS Element.matches(selector)

Source

pub fn root(&self) -> &HTMLElement

获取当前树根元素(最外层容器)

Source

pub fn closest(&self, selector: &str) -> Option<&HTMLElement>

JS closest(selector)

Source

pub fn clone(&self) -> HTMLElement

JS clone()

Source

pub fn iter_elements<'a>(&'a self) -> impl Iterator<Item = &'a HTMLElement> + 'a

Source

pub fn query_selector_all<'a>(&'a self, selector: &str) -> Vec<&'a HTMLElement>

Source

pub fn query_selector<'a>(&'a self, selector: &str) -> Option<&'a HTMLElement>

Source

pub fn remove_whitespace(&mut self)

Source

pub fn trim_right(&mut self, pattern: &Regex)

模拟 JS HTMLElement.trimRight(pattern): 从右侧开始找到第一个匹配 TextNode 截断后续节点。

Source

pub fn structure(&self) -> String

输出结构字符串(对应 JS structure 属性)。

Source

pub fn get_elements_by_tag_name<'a>(&'a self, tag: &str) -> Vec<&'a HTMLElement>

Source

pub fn get_element_by_id<'a>(&'a self, id: &str) -> Option<&'a HTMLElement>

Source

pub fn get_element_by_id_mut<'a>( &'a mut self, id: &str, ) -> Option<&'a mut HTMLElement>

Source

pub fn clone_node(&self) -> HTMLElement

Source

pub fn clone_shallow(&self) -> HTMLElement

浅拷贝(不包含子节点)

Source

pub fn set_range_start(&mut self, start: usize)

Source

pub fn set_range_end(&mut self, end: usize)

Source

pub fn range(&self) -> Option<(usize, usize)>

Source§

impl HTMLElement

Source

pub fn outer_html(&self) -> String

Source

pub fn to_string(&self) -> String

JS API 等价:toString() => outerHTML (保留显式方法以便测试调用)

Source§

impl HTMLElement

Source

pub fn structured_text(&self) -> String

Source§

impl HTMLElement

Source

pub fn before_nodes(&mut self, nodes: Vec<Node>)

Insert nodes (elements/text/comments) before this element in the parent’s children list.

Source

pub fn next_element_sibling(&self) -> Option<&HTMLElement>

下一个元素兄弟。

Source

pub fn previous_element_sibling(&self) -> Option<&HTMLElement>

上一个元素兄弟。

Source

pub fn next_sibling(&self) -> Option<&Node>

下一个兄弟节点(包含文本/注释)。

Source

pub fn previous_sibling(&self) -> Option<&Node>

上一个兄弟节点(包含文本/注释)。

Source

pub fn after_nodes(&mut self, nodes: Vec<Node>)

Insert nodes after this element.

Source

pub fn prepend_nodes(&mut self, nodes: Vec<Node>)

Prepend nodes to this element’s children.

Source

pub fn append_nodes(&mut self, nodes: Vec<Node>)

Append nodes to this element’s children.

Source

pub fn remove(&mut self)

remove() 移除自身

Source

pub fn before(&mut self, html_fragment: &str)

before(html_fragment) 在自身前插入兄弟

Source

pub fn before_items(&mut self, items: &[NodeOrStr<'_>])

Source

pub fn after(&mut self, html_fragment: &str)

after(html_fragment) 在自身后插入兄弟

Source

pub fn after_items(&mut self, items: &[NodeOrStr<'_>])

Source

pub fn closest_in<'a>( &'a self, root: &'a HTMLElement, selector: &str, ) -> Option<&'a HTMLElement>

由于当前结构不存 parent 指针,需要传入 root,用 DFS 寻找祖先链。

Source

pub fn prepend_child(&mut self, node: Node)

Source

pub fn append_child_element(&mut self, child: HTMLElement) -> &mut HTMLElement

JS 风格 appendChild(Element) -> 返回子元素可变引用以便链式操作

Source

pub fn append_child_text(&mut self, text: &str)

JS appendChild(TextNode) 等价:直接加入文本节点

Source

pub fn remove_children_where<F: FnMut(&Node) -> bool>(&mut self, f: F)

Source

pub fn set_content_node(&mut self, node: Node)

JS 兼容别名:attributes() => 已解码的小写属性映射。

Source

pub fn append(&mut self, html_fragment: &str)

— Variadic mutation helpers (支持字符串与已存在 Node) —

Source

pub fn prepend(&mut self, html_fragment: &str)

Source

pub fn append_items(&mut self, items: &[NodeOrStr<'_>])

Source

pub fn prepend_items(&mut self, items: &[NodeOrStr<'_>])

Source

pub fn next_element_sibling_in<'a>( &'a self, root: &'a HTMLElement, ) -> Option<&'a HTMLElement>

获取下一个元素兄弟(模拟 JS nextElementSibling),需要 root (当前无 parent 指针)。

Source

pub fn previous_element_sibling_in<'a>( &'a self, root: &'a HTMLElement, ) -> Option<&'a HTMLElement>

获取上一个元素兄弟(模拟 JS previousElementSibling)。

Source

pub fn append_child(&mut self, node: Node)

Source

pub fn first_child(&self) -> Option<&Node>

Source

pub fn last_child(&self) -> Option<&Node>

Source

pub fn first_element_child(&self) -> Option<&HTMLElement>

Source

pub fn last_element_child(&self) -> Option<&HTMLElement>

Source

pub fn first_element_child_mut(&mut self) -> Option<&mut HTMLElement>

Source

pub fn parent(&self) -> Option<&HTMLElement>

返回父元素引用(只读)。

Source

pub fn children_elements(&self) -> Vec<&HTMLElement>

Source

pub fn child_element_count(&self) -> usize

Trait Implementations§

Source§

impl Clone for HTMLElement

Source§

fn clone(&self) -> HTMLElement

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 HTMLElement

Source§

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

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

impl Display for HTMLElement

Source§

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

Formats the value using the given formatter. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.