use crate::node::NodeRef;
use crate::set_dom_style::{StyleObject, get_style_object_from_css};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComputedStyle {
pub direction: String,
pub writing_mode: String,
}
impl Default for ComputedStyle {
fn default() -> Self {
Self {
direction: "ltr".to_owned(),
writing_mode: "horizontal-tb".to_owned(),
}
}
}
pub fn get_computed_style_for_element(element: &NodeRef) -> Option<ComputedStyle> {
let editor = crate::updates::get_active_editor()?;
editor
.get_root_element()?
.0
.computed_style(element.get_key())
}
pub fn get_computed_style_for_parent(node: &NodeRef) -> Option<ComputedStyle> {
let parent = if node.is_root() {
Some(node.clone())
} else {
node.get_parent()
};
parent.and_then(|parent| get_computed_style_for_element(&parent))
}
pub fn is_parent_rtl(node: &NodeRef) -> bool {
get_computed_style_for_parent(node).is_some_and(|styles| styles.direction == "rtl")
}
pub fn get_style_object_from_raw_css(css: &str) -> StyleObject {
get_style_object_from_css(css)
}
pub fn get_css_from_style_object(styles: &StyleObject) -> String {
let mut css = String::new();
for (style, value) in styles.iter() {
if !style.is_empty() {
css.push_str(style);
css.push_str(": ");
css.push_str(value);
css.push(';');
}
}
css
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SelectionRect {
pub left: f64,
pub top: f64,
pub right: f64,
pub bottom: f64,
pub width: f64,
pub height: f64,
}
impl SelectionRect {
pub fn new(left: f64, top: f64, width: f64, height: f64) -> Self {
Self {
left,
top,
right: left + width,
bottom: top + height,
width,
height,
}
}
}
pub fn filter_selection_rects(
rects: &[SelectionRect],
root_width: f64,
root_padding: f64,
) -> Vec<SelectionRect> {
let mut selection_rects: Vec<SelectionRect> = Vec::with_capacity(rects.len());
for rect in rects {
let mut i = selection_rects.len();
while i > 0 && sorts_before(rect, &selection_rects[i - 1]) {
i -= 1;
}
selection_rects.insert(i, *rect);
}
let mut prev_rect: Option<SelectionRect> = None;
let mut i = 0;
while i < selection_rects.len() {
let selection_rect = selection_rects[i];
let is_contained_rect = prev_rect.is_some_and(|prev| {
prev.top <= selection_rect.top
&& prev.bottom >= selection_rect.bottom
&& prev.left <= selection_rect.left
&& prev.right >= selection_rect.right
});
let selection_spans_element = selection_rect.width + root_padding == root_width;
if is_contained_rect || selection_spans_element {
selection_rects.remove(i);
continue;
}
prev_rect = Some(selection_rect);
i += 1;
}
selection_rects
}
fn sorts_before(a: &SelectionRect, b: &SelectionRect) -> bool {
let top = a.top - b.top;
if top.abs() <= 3.0 {
return a.left - b.left < 0.0;
}
top < 0.0
}
#[cfg(test)]
mod tests {
use super::*;
fn sides(rects: &[SelectionRect]) -> Vec<[f64; 4]> {
rects
.iter()
.map(|r| [r.left, r.top, r.width, r.height])
.collect()
}
#[test]
fn filter_selection_rects_matches_create_rects_from_dom_range() {
let r = SelectionRect::new;
let rects = [
r(50.0, 22.0, 30.0, 18.0),
r(10.0, 0.0, 40.0, 18.0),
r(5.0, 2.0, 20.0, 18.0),
r(0.0, 20.0, 480.0, 18.0),
r(12.0, 4.0, 10.0, 10.0),
r(60.0, 40.0, 10.0, 18.0),
r(12.0, 1.0, 10.0, 10.0),
];
assert_eq!(
sides(&filter_selection_rects(&rects, 500.0, 20.0)),
[
[5.0, 2.0, 20.0, 18.0],
[10.0, 0.0, 40.0, 18.0],
[50.0, 22.0, 30.0, 18.0],
[60.0, 40.0, 10.0, 18.0],
]
);
let rects = [
r(10.0, 0.0, 100.0, 20.0),
r(20.0, 5.0, 50.0, 10.0),
r(200.0, 1.0, 10.0, 10.0),
r(100.0, 0.0, 30.0, 20.0),
];
assert_eq!(
sides(&filter_selection_rects(&rects, 500.0, 20.0)),
[
[10.0, 0.0, 100.0, 20.0],
[100.0, 0.0, 30.0, 20.0],
[200.0, 1.0, 10.0, 10.0],
[20.0, 5.0, 50.0, 10.0],
]
);
}
}