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: StringImplementations§
Source§impl HTMLElement
impl HTMLElement
pub fn attrs_lower_decoded(&mut self) -> HashMap<String, String>
pub fn set_attributes(&mut self, attributes: &[(String, String)])
pub fn remove_attribute(&mut self, key: &str)
pub fn get_attr(&self, key: &str) -> Option<&str>
pub fn has_attr(&self, key: &str) -> bool
pub fn set_attr(&mut self, key: &str, val: &str)
pub fn remove_attr(&mut self, key: &str)
Sourcepub fn remove_id(&mut self)
pub fn remove_id(&mut self)
Convenience: remove the id attribute (safe wrapper for tests parity with JS removeAttribute(‘id’))
Sourcepub fn set_id(&mut self, id: &str)
pub fn set_id(&mut self, id: &str)
Convenience: set id attribute (safe wrapper to avoid direct raw mutation in tests)
pub fn attributes(&mut self) -> HashMap<String, String>
pub fn raw_attributes(&mut self) -> HashMap<String, String>
Sourcepub fn raw_attrs_str(&self) -> &str
pub fn raw_attrs_str(&self) -> &str
Read-only snapshot of the original raw attribute string (public accessor for tests like issue 136)
pub fn get_attribute(&mut self, key: &str) -> Option<String>
pub fn set_attribute(&mut self, key: &str, value: &str)
pub fn has_attribute(&mut self, key: &str) -> bool
Source§impl HTMLElement
impl HTMLElement
pub fn class_list(&mut self) -> Vec<String>
pub fn class_list_view(&self) -> Vec<&str>
pub fn class_list_contains(&mut self, token: &str) -> bool
pub fn class_list_add(&mut self, token: &str)
pub fn class_list_remove(&mut self, token: &str)
pub fn class_list_toggle(&mut self, token: &str)
pub fn class_list_replace(&mut self, old: &str, new: &str)
Source§impl HTMLElement
impl HTMLElement
Sourcepub fn insert_adjacent_html(
&mut self,
position: &str,
html: &str,
) -> Result<(), String>
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 中(若无父则忽略)
Sourcepub fn replace_with(&mut self, html_fragment: &str)
pub fn replace_with(&mut self, html_fragment: &str)
replaceWith(html_fragment) 用片段替换自身
pub fn replace_with_items(&mut self, items: &[NodeOrStr<'_>])
pub fn text(&self) -> String
Sourcepub fn inner_text(&self) -> String
pub fn inner_text(&self) -> String
对应 JS innerText: 返回未解码聚合文本 (即 rawText)
Sourcepub fn text_content(&self) -> String
pub fn text_content(&self) -> String
对应 JS textContent getter: 返回解码后的文本
Sourcepub fn set_text_content(&mut self, val: &str)
pub fn set_text_content(&mut self, val: &str)
对应 JS textContent setter: 先对传入进行实体编码,再替换子节点为单一文本节点
pub fn set_content_str(&mut self, content: &str, comment_override: Option<bool>)
Sourcepub fn set_content(&mut self, content: &str)
pub fn set_content(&mut self, content: &str)
JS 兼容别名:set_content(字符串) -> set_content_str,无注释覆盖。
pub fn set_content_nodes(&mut self, nodes: Vec<Node>)
Source§impl HTMLElement
impl HTMLElement
pub fn new( tag: Option<String>, raw_attrs: String, attrs: Vec<(String, String)>, is_void: bool, void_add_slash: bool, ) -> Self
pub fn is_root(&self) -> bool
pub fn name(&self) -> &str
Sourcepub fn set_tag_name(&mut self, new_name: &str)
pub fn set_tag_name(&mut self, new_name: &str)
JS HTMLElement.tagName setter 行为:赋值后序列化使用小写(JS 内部存 rawTagName 小写,tagName getter 返回大写)。 为贴近 JS,我们内部沿用小写存储,外部序列化 already 调用 self.name()(即原样)。
pub fn raw_text(&self) -> String
pub fn class_names(&self) -> String
pub fn inner_html(&self) -> String
Sourcepub fn set_inner_html(&mut self, html: &str)
pub fn set_inner_html(&mut self, html: &str)
设置 innerHTML:清空旧子节点并以解析后的片段替换
Sourcepub fn matches_selector<'a>(
&'a self,
root: &'a HTMLElement,
selector: &str,
) -> bool
pub fn matches_selector<'a>( &'a self, root: &'a HTMLElement, selector: &str, ) -> bool
判断当前元素是否匹配 selector(使用全局选择再比对引用,性能次优)。
Sourcepub fn root(&self) -> &HTMLElement
pub fn root(&self) -> &HTMLElement
获取当前树根元素(最外层容器)
Sourcepub fn closest(&self, selector: &str) -> Option<&HTMLElement>
pub fn closest(&self, selector: &str) -> Option<&HTMLElement>
JS closest(selector)
Sourcepub fn clone(&self) -> HTMLElement
pub fn clone(&self) -> HTMLElement
JS clone()
pub fn iter_elements<'a>(&'a self) -> impl Iterator<Item = &'a HTMLElement> + 'a
pub fn query_selector_all<'a>(&'a self, selector: &str) -> Vec<&'a HTMLElement>
pub fn query_selector<'a>(&'a self, selector: &str) -> Option<&'a HTMLElement>
pub fn remove_whitespace(&mut self)
Sourcepub fn trim_right(&mut self, pattern: &Regex)
pub fn trim_right(&mut self, pattern: &Regex)
模拟 JS HTMLElement.trimRight(pattern): 从右侧开始找到第一个匹配 TextNode 截断后续节点。
pub fn get_elements_by_tag_name<'a>(&'a self, tag: &str) -> Vec<&'a HTMLElement>
pub fn get_element_by_id<'a>(&'a self, id: &str) -> Option<&'a HTMLElement>
pub fn get_element_by_id_mut<'a>( &'a mut self, id: &str, ) -> Option<&'a mut HTMLElement>
pub fn clone_node(&self) -> HTMLElement
Sourcepub fn clone_shallow(&self) -> HTMLElement
pub fn clone_shallow(&self) -> HTMLElement
浅拷贝(不包含子节点)
pub fn set_range_start(&mut self, start: usize)
pub fn set_range_end(&mut self, end: usize)
pub fn range(&self) -> Option<(usize, usize)>
Source§impl HTMLElement
impl HTMLElement
Source§impl HTMLElement
impl HTMLElement
pub fn structured_text(&self) -> String
Source§impl HTMLElement
impl HTMLElement
Sourcepub fn before_nodes(&mut self, nodes: Vec<Node>)
pub fn before_nodes(&mut self, nodes: Vec<Node>)
Insert nodes (elements/text/comments) before this element in the parent’s children list.
Sourcepub fn next_element_sibling(&self) -> Option<&HTMLElement>
pub fn next_element_sibling(&self) -> Option<&HTMLElement>
下一个元素兄弟。
Sourcepub fn previous_element_sibling(&self) -> Option<&HTMLElement>
pub fn previous_element_sibling(&self) -> Option<&HTMLElement>
上一个元素兄弟。
Sourcepub fn next_sibling(&self) -> Option<&Node>
pub fn next_sibling(&self) -> Option<&Node>
下一个兄弟节点(包含文本/注释)。
Sourcepub fn previous_sibling(&self) -> Option<&Node>
pub fn previous_sibling(&self) -> Option<&Node>
上一个兄弟节点(包含文本/注释)。
Sourcepub fn after_nodes(&mut self, nodes: Vec<Node>)
pub fn after_nodes(&mut self, nodes: Vec<Node>)
Insert nodes after this element.
Sourcepub fn prepend_nodes(&mut self, nodes: Vec<Node>)
pub fn prepend_nodes(&mut self, nodes: Vec<Node>)
Prepend nodes to this element’s children.
Sourcepub fn append_nodes(&mut self, nodes: Vec<Node>)
pub fn append_nodes(&mut self, nodes: Vec<Node>)
Append nodes to this element’s children.
pub fn before_items(&mut self, items: &[NodeOrStr<'_>])
pub fn after_items(&mut self, items: &[NodeOrStr<'_>])
Sourcepub fn closest_in<'a>(
&'a self,
root: &'a HTMLElement,
selector: &str,
) -> Option<&'a HTMLElement>
pub fn closest_in<'a>( &'a self, root: &'a HTMLElement, selector: &str, ) -> Option<&'a HTMLElement>
由于当前结构不存 parent 指针,需要传入 root,用 DFS 寻找祖先链。
pub fn prepend_child(&mut self, node: Node)
Sourcepub fn append_child_element(&mut self, child: HTMLElement) -> &mut HTMLElement
pub fn append_child_element(&mut self, child: HTMLElement) -> &mut HTMLElement
JS 风格 appendChild(Element) -> 返回子元素可变引用以便链式操作
Sourcepub fn append_child_text(&mut self, text: &str)
pub fn append_child_text(&mut self, text: &str)
JS appendChild(TextNode) 等价:直接加入文本节点
pub fn remove_children_where<F: FnMut(&Node) -> bool>(&mut self, f: F)
Sourcepub fn set_content_node(&mut self, node: Node)
pub fn set_content_node(&mut self, node: Node)
JS 兼容别名:attributes() => 已解码的小写属性映射。
pub fn prepend(&mut self, html_fragment: &str)
pub fn append_items(&mut self, items: &[NodeOrStr<'_>])
pub fn prepend_items(&mut self, items: &[NodeOrStr<'_>])
Sourcepub fn next_element_sibling_in<'a>(
&'a self,
root: &'a HTMLElement,
) -> Option<&'a HTMLElement>
pub fn next_element_sibling_in<'a>( &'a self, root: &'a HTMLElement, ) -> Option<&'a HTMLElement>
获取下一个元素兄弟(模拟 JS nextElementSibling),需要 root (当前无 parent 指针)。
Sourcepub fn previous_element_sibling_in<'a>(
&'a self,
root: &'a HTMLElement,
) -> Option<&'a HTMLElement>
pub fn previous_element_sibling_in<'a>( &'a self, root: &'a HTMLElement, ) -> Option<&'a HTMLElement>
获取上一个元素兄弟(模拟 JS previousElementSibling)。
pub fn append_child(&mut self, node: Node)
pub fn first_child(&self) -> Option<&Node>
pub fn last_child(&self) -> Option<&Node>
pub fn first_element_child(&self) -> Option<&HTMLElement>
pub fn last_element_child(&self) -> Option<&HTMLElement>
pub fn first_element_child_mut(&mut self) -> Option<&mut HTMLElement>
Sourcepub fn parent(&self) -> Option<&HTMLElement>
pub fn parent(&self) -> Option<&HTMLElement>
返回父元素引用(只读)。
pub fn children_elements(&self) -> Vec<&HTMLElement>
pub fn child_element_count(&self) -> usize
Trait Implementations§
Source§impl Clone for HTMLElement
impl Clone for HTMLElement
Source§fn clone(&self) -> HTMLElement
fn clone(&self) -> HTMLElement
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for HTMLElement
impl Debug for HTMLElement
Auto Trait Implementations§
impl Freeze for HTMLElement
impl RefUnwindSafe for HTMLElement
impl !Send for HTMLElement
impl !Sync for HTMLElement
impl Unpin for HTMLElement
impl UnwindSafe for HTMLElement
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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