use crate::dom::abstractrange::BoundaryPoint;
use crate::dom::bindings::root::Dom;
use crate::dom::node::Node;
use crate::dom::range::Range;
#[derive(Clone, JSTraceable, PartialEq, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub(crate) struct SelectionBoundary {
pub container: Dom<Node>,
pub offset: u32,
}
impl SelectionBoundary {
pub(crate) fn new(container: &Node, offset: u32) -> Self {
Self {
container: Dom::from_ref(container),
offset,
}
}
}
impl PartialEq<BoundaryPoint> for SelectionBoundary {
fn eq(&self, boundary_point: &BoundaryPoint) -> bool {
*self.container == *boundary_point.node().get() && self.offset == boundary_point.offset().0 as u32
}
}
#[derive(JSTraceable, PartialEq, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub(crate) struct SelectionRange {
pub start: SelectionBoundary,
pub end: SelectionBoundary,
}
impl SelectionRange {
#[cfg_attr(crown, expect(crown::unrooted_must_root))]
pub(crate) fn new(start: SelectionBoundary, end: SelectionBoundary) -> Self {
Self { start, end }
}
#[cfg_attr(crown, expect(crown::unrooted_must_root))]
pub(crate) fn collapsed_at(at: SelectionBoundary) -> Self {
Self {
start: at.clone(),
end: at,
}
}
pub(crate) fn collapsed(&self) -> bool {
self.start == self.end
}
}
impl From<&Range> for SelectionRange {
fn from(range: &Range) -> Self {
Self::new(
SelectionBoundary::new(&range.start_container(), range.start_offset()),
SelectionBoundary::new(&range.end_container(), range.end_offset()),
)
}
}