All notable changes to the dom_query
crate will be documented in this file.
- Implemented the
markdown
feature, which allows serializing aDocument
orNodeRef
into Markdown text using themd()
method. - Implemented the
mini_selector
feature, providing a lightweight and faster alternative for element matching with limited CSS selector support. This includesNodeRef
additional methods:find_descendants
,try_find_descendants
,mini_is
, andmini_match
.
Selection::select
now returns nodes in ascending order if there were multiple underlying root nodes. If there was only one root node, it still returns nodes in ascending order, just as before.
- Implemented the
Node::id_attr
andNode::class
methods, which return theid
andclass
attributes of the node, respectively.The Selection::id
andSelection::class
methods do the same for the first node in the selection.
- Use
bit_set::BitSet
instead offoldhash::HashSet
for theMatches::next
method. Since it is necessary to ensure there are no duplicates in theMatches
result, and this check needs to be as cheap as possible,bit-set
was chosen. - Revised the
NodeRef::formatted_text
implementation: moved related code to a separate module, extended the formatting logic, and added more test cases.
- Issue where
DescendantNodes
could traverse beyond the initial node when iterating over descendants. This affectedNodeRef::descendants
andNodeRef::descendants_it
.
- Improved
NodeRef::formatted_text
behavior.
- Updated
html5ever
dependency to version 0.29.1.
- Fixed
NodeRef::find
to correctly collect nodes when there is only one item in the path slice.
- Corrected
NodeRef::normalized_char_count
behavior for various cases.
- Implemented
NodeRef::normalized_char_count
which estimates the number of characters in the text of descendant nodes as if the total string were normalized. - Implemented
Document::formatted_text
,Selection::formatted_text
, andNodeRef::formatted_text
, which return formatted text of the document, selection, or node respectively.
- Implemented
NodeRef::is_match
andNodeRef::is
methods, which allow checking if a node matches a given matcher (&Matcher
) or selector (&str
) without creating aSelection
object. - Implemented
Tree::base_uri
, a quick method that returns the base URI of the document based on thehref
attribute of the<base>
element.Document::base_uri
andNodeRef::base_uri
provide the same functionality. Inspired by Node: baseURI property. NodeRef::find
an experimental method to find all descendant elements of a node that match a given path. It is much faster thanSelection::select
method.
Selection
's internal code changes aimed at reducing calls toRefCell::borrow
andRefCell::borrow_mut
.- Internal code changes in
Matches
aimed at increasing selection performance.
- Implemented the
atomic
feature which switchesNodeData
from usingStrTendril
toTendril<tendril::fmt::UTF8, tendril::Atomic>
. This allowsNodeData
and all ascending structures, includingDocument
, to implement theSend
trait. - Implemented
Selection::set_text
method, which sets the content of each node in the selection to specified content. - Implemented
NodeRef::insert_siblings_after
method, which allows inserting a node and its siblings after the selected node. - Implemented
NodeRef::before_html
method, which allows inserting contents of an HTML fragment before the selected node.Selection::before_html
does the same thing for the every node inside selection. - Implemented
NodeRef::after_html
method, which allows inserting contents of an HTML fragment after the selected node.Selection::after_html
does the same thing for the every node inside selection. - Implemented
Selection::prepend_selection
method, which prepends nodes from another selection to the nodes in the current selection.
- Internal code changes aimed at reducing calls to
RefCell::borrow
andRefCell::borrow_mut
.
NodeRef::append_prev_sibling
is deprecated, please useNodeRef::insert_before
instead.NodeRef::append_prev_siblings
is deprecated, please useNodeRef::insert_siblings_before
instead.Tree::append_prev_sibling_of
is deprecated, please useTree::insert_before_of
instead.Document::from
andDocument::fragment
now callhtml5ever::parse_document
with thetree_builder
optionscripting_enabled
set tofalse
. This allows querying into thenoscript
element.
- Implemented
Ord
trait forNodeId
- Implemented
NodeRef::insert_after
method, which allows to insert a node after the selected node. - Implemented
NodeRef::descendants_it
method, which allows iterating over all descendants of a node. - Implemented
NodeRef::descendants
method, which returns a vector of all descendants of a node. - Implemented
NodeRef::normalize
method, which merges adjacent text nodes and removes empty text nodes.Document::normalize
does the same thing, but across all the document.
Document::text
method now returns the text content, whereas previously it returned an empty string.
- Hide visibility of
Tree::nodes
, which was accidentally exposed in the previous version.
- Using
Tree::merge_with_fn
instead ofTree::merge
to reduce code duplication. Tree::child_ids_of_it
now requirerev
argument. Settrue
to iterate children in reverse order.NodeRef::children_it
now requirerev
argument. Settrue
to iterate children in reverse order.- Improved internal logic of
Selection::append_selection
andSelection::replace_with_selection
.
- Introduced new
NodeRef::prepend_child
method, that inserts a child at the beginning of node content. - Introduced new
NodeRef::prepend_children
method, that inserts a child and its siblings at the beginning of the node content. - Introduced new
NodeRef::prepend_html
method, that parses html string and inserts its parsed nodes at the beginning of the node content. - Introduced new
Selection::prepend_html
method, which parses an HTML string and inserts its parsed nodes at the beginning of the content of all matched nodes. - Introduced new selection methods:
Selection::add_selection
,Selection:add_matcher
,Selection::add
andSelection::try_add
to extend current selection with other selections.
- Fixed
Selection::append_selection
to work with selections with multiple nodes and selections from another tree. - Fixed
Selection::replace_with_selection
to work with selections with multiple nodes and selections from another tree. - Fixed
Node::append_child
,Node::append_children
,Node::prepend_child
, andNode::prepend_children
: these methods now internally remove the child/children from their previous parent node before attachment. - Critical fix for
NodeRef::first_element_child
: Previously, due to a logic error, the method checked the node itself instead of its child nodes. This could have caused:- Incorrect element selection in DOM traversal
- Unexpected behavior in code relying on first element child detection
- Note: Code that depends on this method's behavior should be reviewed after upgrading
- Simplified
Node::has_text
. - Replaced generic types with the concrete type
NodeData
, simplifying code and improving readability without affecting the public API. - Replaced implementations for
Node
with implementations forNodeRef
.Node
is just an alias forNodeRef
. - Simplified internal logic of
Selection::replace_with_html
,Selection::set_html
,Selection::append_html
,Node::set_html
,Node::append_html
, andNode::replace_with_html
by usingTree::merge
.
- Added
Selection::filter
,Selection::filter_matcher
andSelection::try_filter
methods that filter a current selection. - Added
Selection::filter_selection
method that filters a current selection with another selection. - Added
NodeRef::replace_with
method that allows to replace a node with another one. - Added
NodeRef::replace_with_html
method that allows to replace a node with a new node created from the given HTML. - Added
NodeIdProver
trait and implementations forNodeRef
andNode
. Which allows to call some node functions with a&NodeRef
and&NodeId
. Previously these functions requiredNodeId
as a parameter. - Added a new pseudo-class
:only-text
that allows selecting a node with no child elements except a single text child node. - Added the
NodeRef::set_text
method, which sets the text content of a node, replacing any existing content. - Added
NodeRef::append_prev_siblings
method, which allows to prepend other nodes and their siblings before the selected node.
- Fixed
<NodeRef<'a> as selectors::Element>::is_empty
to correctly handle line breaks, whitespace, and ensure only elements pass the check.
- Removed
Tree::append_children_from_another_tree
method. - Removed
Tree::append_prev_siblings_from_another_tree
method. - Removed
Node::append_children_from_another_tree
method. - Removed
Node::append_prev_siblings_from_another_tree
method.
- Exposed
Matcher::match_element
since it can be useful outside the crate. - Changed
impl<'a> selectors::Element for Node<'a>::opaque
to work with:has
pseudo-element fromselectors
crate. - Switched to
:has
implementation fromselectors
crate. - Internal changes due to switch to
selectors
v0.26.0 switch. Selection
methods that required&mut
now doesn't require&mut
, finally.- Improve performance for
Document::from
,Selection::select
and others. - Switched from using
rustc-hash
tofoldhash
.
- Added
Node::ancestors
method, that allows to get all or limited number of ancestors of a node. - Added
From<Vec<NodeRef<'a, NodeData>>> for Selection<'a>
- Enable support for
:is()
and:where()
pseudo-classes. - Added
Node::element_children
method that returns children nodes, that arenode_data::Element
s. - Added
Node::children_it
method that returns an iterator over children nodes. - Added
Node::ancestors_it
method that returns an iterator over ancestors nodes. - Added
Tree:child_ids_of
andTree:child_ids_of_it
methods that returns ids of the child nodes as vec and as iterator respectively. - Added
Tree:ancestor_ids_of
andTree:ancestor_ids_of_it
methods that returns ids of the ancestor nodes as vec and as iterator respectively. - Added
Node::immediate_text
method returns text of the node without it's descendants.Selection::immediate_text
does the same thing for the every node inside selection.
- Exposed
Document::tree
. Selection
methods that required&mut
now doesn't require&mut
.- Changed the project structure, now modules are divided based on the
struct
implementations.
- Added
Node::append_html
andNode::set_html
methods for creating children nodes of a single selected node. - Added
Tree<NodeData>::new_element
, an easy way to create an empty element with a given name. - Added
NodeRef::last_child
. - Added
Node::has_attr
method, which returnstrue
if an attribute exists on the node element.Selection::has_attr
does the same thing for the first node inside selection. - Added
Node::remove_all_attrs
method for removing all attributes of a node.Selection::remove_all_attrs
does the same thing for the every node inside selection. - Added
Node::remove_attrs
method, a convenient way to remove multiple attributes from the node element.Selection::remove_attrs
does the same thing for the every node inside selection. - Added
Node::rename
method, which allows to change node's name.Selection::rename
does the same thing for the every node inside selection.
- Added
select_single_matcher
andselect_single
methods forDocument
andSelection
. - Added
Document::fragment
which allows to create a document fragment.
- Update documentation
- A small breaking change:
From
implementation forDocument
, now it is based onInto<StrTendril>
and because of that, previousFrom<&String>
implementation will not work anymore (they are in config). If your code consumes&String
, you should useString::as_str()
instead. - Refactored the code (
NodeData::Element
).
- Removed a
readability
examples and related dev-dependencies.
- Added
Selection::inner_html
,Selection::try_html
, andSelection::try_inner
methods. - Added more examples and doc-tests.
- Improved the documentation.
- Fixed
Iterator::next
forMatches
- Added doc-tests
- Update dependencies.
- Refactor code due to major changes in html5ever 0.29.0
- Update dependencies.
- Update dependencies.
- Add support for
:has-text
and:contains
pseudo-classes, which allow to search elements by their text contents. Some example are here.
- Fix
:has
selector name comparison. Previously:has
behavior worked, even if pseudo-class just started from:has
.
- add
InnerNode<NodeData>.is_comment
.
- expose
dom_tree::NodeData
. - minor adjustments.
- revise
matcher::Matcher
usage, nowselection::Selection
's underlyingselect
methods usesmatcher::Matcher
reference if it possible. css::CssString
now wrapsString
instead ofStrTendril
. BecauseStrTendril
can't be shared between multiple threads without mutex.
- Minor refactoring.
- Fix spelling.
- Refactor dom_tree.rs -- remove internal macro, revise some functions for better performance.
- Improve performance for some functions inside, traversal.rs (switching from hashmaps to vecs).
- Add optional feature
hashbrown
-- which replace standardHashSet
andHashMap
with it's own implementation.
CssString
now wraps aStrTendril
instead of aString
.- Use
rustc_hash::FxHashSet
instead ofstd::collections::HashSet
. - Use
rustc_hash::FxHashMap
instead ofstd::collections::HashMap
.
- Replace unsafe code with safe code.