struct Html5Sink {
tree: RefCell<XmlTree>,
qual_names: RefCell<FxHashMap<NodeId, QualName>>,
template_contents: RefCell<FxHashMap<NodeId, NodeId>>,
offset_counter: RefCell<usize>,
}Expand description
[TreeSink] implementation that bridges html5ever’s push-based API into
an XmlTree.
Node offsets are assigned from a monotonically increasing counter rather
than from source byte positions, because html5ever’s TreeSink callbacks
do not receive source positions. The counter advances by 1 per element and
by text.len() per text chunk, preserving the non-overlap invariant needed
by the layout engine’s page-finding binary search.
Fields§
§tree: RefCell<XmlTree>The tree being built. RefCell is required because multiple TreeSink
methods need mutable access and Rust’s borrow checker cannot see that
html5ever calls them non-concurrently.
qual_names: RefCell<FxHashMap<NodeId, QualName>>Maps each element NodeId to its fully-qualified name so that
elem_name can return a borrowed reference as required by the trait.
template_contents: RefCell<FxHashMap<NodeId, NodeId>>Maps <template> element NodeIds to their associated content root
NodeId, as required by the HTML5 template element spec.
offset_counter: RefCell<usize>Synthetic position counter. Incremented for every node created so that all offsets are unique and ordered by document position.
Implementations§
Source§impl Html5Sink
impl Html5Sink
Sourcefn next_offset(&self) -> usize
fn next_offset(&self) -> usize
Returns the current value of the offset counter without advancing it.
Sourcefn advance_offset(&self, by: usize)
fn advance_offset(&self, by: usize)
Advances the offset counter by by, clamped to a minimum of 1 to
guarantee that every node receives a strictly larger offset than the
previous one even for zero-length text runs.
Sourcefn is_whitespace_only(text: &str) -> bool
fn is_whitespace_only(text: &str) -> bool
Returns true when text contains only ASCII whitespace characters.
Sourcefn attr_name(attr: &Attribute) -> String
fn attr_name(attr: &Attribute) -> String
Converts an html5ever [Attribute] name to its string representation,
prefixing with the namespace if one is present (e.g. xml:lang).
Sourcefn build_attributes(attrs: Vec<Attribute>) -> Attributes
fn build_attributes(attrs: Vec<Attribute>) -> Attributes
Converts a Vec<Attribute> from html5ever into the Attributes map
used by the DOM.
Trait Implementations§
Source§impl TreeSink for Html5Sink
impl TreeSink for Html5Sink
Source§fn parse_error(&self, _msg: Cow<'static, str>)
fn parse_error(&self, _msg: Cow<'static, str>)
Silently ignores all parse errors. The dictionary content from reader-dict is often malformed HTML, and we rely on html5ever’s error-recovery rather than failing on bad input.
Source§fn create_element(
&self,
name: QualName,
attrs: Vec<Attribute>,
flags: ElementFlags,
) -> Self::Handle
fn create_element( &self, name: QualName, attrs: Vec<Attribute>, flags: ElementFlags, ) -> Self::Handle
Creates a new element node, assigns it the next synthetic offset, and
registers its qualified name for later elem_name lookups.
For <template> elements an additional content-root node is created
and stored in template_contents, as required by the spec.
Source§fn create_comment(&self, _text: Tendril<UTF8>) -> Self::Handle
fn create_comment(&self, _text: Tendril<UTF8>) -> Self::Handle
Maps an HTML comment to an empty whitespace node so it occupies a slot in the offset space without contributing visible content.
Source§fn create_pi(
&self,
_target: Tendril<UTF8>,
_data: Tendril<UTF8>,
) -> Self::Handle
fn create_pi( &self, _target: Tendril<UTF8>, _data: Tendril<UTF8>, ) -> Self::Handle
Maps a processing instruction to an empty whitespace node so it occupies a slot in the offset space without contributing visible content.
Source§fn append(&self, parent: &Self::Handle, child: NodeOrText<Self::Handle>)
fn append(&self, parent: &Self::Handle, child: NodeOrText<Self::Handle>)
Appends a child node or text run to parent.
Text runs are coalesced into the preceding sibling text node when one exists, to match the behaviour of the hand-rolled parser and avoid producing redundant nodes for adjacent text chunks.
Source§fn append_based_on_parent_node(
&self,
element: &Self::Handle,
prev_element: &Self::Handle,
child: NodeOrText<Self::Handle>,
)
fn append_based_on_parent_node( &self, element: &Self::Handle, prev_element: &Self::Handle, child: NodeOrText<Self::Handle>, )
Delegates to Self::append using element as the target parent.
Called by html5ever during foster-parenting and similar error-recovery situations where the intended parent is determined by the element rather than its previous sibling.
Source§fn append_before_sibling(
&self,
sibling: &Self::Handle,
new_node: NodeOrText<Self::Handle>,
)
fn append_before_sibling( &self, sibling: &Self::Handle, new_node: NodeOrText<Self::Handle>, )
Inserts a node or text run immediately before sibling.
Source§fn append_doctype_to_document(
&self,
_name: Tendril<UTF8>,
_public_id: Tendril<UTF8>,
_system_id: Tendril<UTF8>,
)
fn append_doctype_to_document( &self, _name: Tendril<UTF8>, _public_id: Tendril<UTF8>, _system_id: Tendril<UTF8>, )
DOCTYPE declarations are not represented in the tree.
Source§fn set_quirks_mode(&self, _mode: QuirksMode)
fn set_quirks_mode(&self, _mode: QuirksMode)
Quirks mode is accepted but has no effect on the tree representation.
Source§type Handle = NodeId
type Handle = NodeId
Handle is a reference to a DOM node. The tree builder requires
that a Handle implements Clone to get another reference to
the same node.type ElemName<'a> = Ref<'a, QualName>
Source§fn finish(self) -> Self::Output
fn finish(self) -> Self::Output
Source§fn get_document(&self) -> Self::Handle
fn get_document(&self) -> Self::Handle
Document node.Source§fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> Self::ElemName<'a>
fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> Self::ElemName<'a>
Source§fn get_template_contents(&self, target: &Self::Handle) -> Self::Handle
fn get_template_contents(&self, target: &Self::Handle) -> Self::Handle
Source§fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool
fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool
Source§fn add_attrs_if_missing(&self, target: &Self::Handle, attrs: Vec<Attribute>)
fn add_attrs_if_missing(&self, target: &Self::Handle, attrs: Vec<Attribute>)
Source§fn remove_from_parent(&self, target: &Self::Handle)
fn remove_from_parent(&self, target: &Self::Handle)
Source§fn reparent_children(&self, node: &Self::Handle, new_parent: &Self::Handle)
fn reparent_children(&self, node: &Self::Handle, new_parent: &Self::Handle)
§fn mark_script_already_started(&self, _node: &Self::Handle)
fn mark_script_already_started(&self, _node: &Self::Handle)
<script> as “already started”.§fn associate_with_form(
&self,
_target: &Self::Handle,
_form: &Self::Handle,
_nodes: (&Self::Handle, Option<&Self::Handle>),
)
fn associate_with_form( &self, _target: &Self::Handle, _form: &Self::Handle, _nodes: (&Self::Handle, Option<&Self::Handle>), )
§fn is_mathml_annotation_xml_integration_point(
&self,
_handle: &Self::Handle,
) -> bool
fn is_mathml_annotation_xml_integration_point( &self, _handle: &Self::Handle, ) -> bool
§fn set_current_line(&self, _line_number: u64)
fn set_current_line(&self, _line_number: u64)
fn allow_declarative_shadow_roots( &self, _intended_parent: &Self::Handle, ) -> bool
§fn attach_declarative_shadow(
&self,
_location: &Self::Handle,
_template: &Self::Handle,
_attrs: &[Attribute],
) -> bool
fn attach_declarative_shadow( &self, _location: &Self::Handle, _template: &Self::Handle, _attrs: &[Attribute], ) -> bool
§fn maybe_clone_an_option_into_selectedcontent(&self, option: &Self::Handle)
fn maybe_clone_an_option_into_selectedcontent(&self, option: &Self::Handle)
maybe clone an option into selectedcontent. Read moreAuto Trait Implementations§
impl !Freeze for Html5Sink
impl !RefUnwindSafe for Html5Sink
impl Send for Html5Sink
impl !Sync for Html5Sink
impl Unpin for Html5Sink
impl UnsafeUnpin for Html5Sink
impl UnwindSafe for Html5Sink
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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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