use super::*;
pub(super) fn style_for_layout_element(
element: &Element,
signature: ElementSignature,
stylesheets: &Stylesheets<'_>,
parent: Option<&ComputedStyle>,
ancestors: &[ElementSignature],
) -> ComputedStyle {
style_for_layout_element_with_signature_transform(
element,
signature,
stylesheets,
parent,
ancestors,
None,
)
}
pub(super) fn style_for_layout_element_with_parent_ch_advance(
element: &Element,
signature: ElementSignature,
stylesheets: &Stylesheets<'_>,
parent: Option<&ComputedStyle>,
ancestors: &[ElementSignature],
parent_ch_advance: LayoutLength,
) -> ComputedStyle {
style_for_layout_element_with_signature_transform(
element,
signature,
stylesheets,
parent,
ancestors,
Some(parent_ch_advance),
)
}
fn style_for_layout_element_with_signature_transform(
element: &Element,
signature: ElementSignature,
stylesheets: &Stylesheets<'_>,
parent: Option<&ComputedStyle>,
ancestors: &[ElementSignature],
parent_ch_advance: Option<LayoutLength>,
) -> ComputedStyle {
let signature = layout_element_signature(element, signature, parent);
style_for_layout_signature_with_parent_ch_advance(
signature,
element.attrs.get("style").map(String::as_str),
stylesheets,
parent,
ancestors,
parent_ch_advance,
)
}
pub(super) fn style_for_layout_signature_with_parent_ch_advance(
signature: ElementSignature,
inline_style: Option<&str>,
stylesheets: &Stylesheets<'_>,
parent: Option<&ComputedStyle>,
ancestors: &[ElementSignature],
parent_ch_advance: Option<LayoutLength>,
) -> ComputedStyle {
if let Some(parent_ch_advance) = parent_ch_advance {
css::style_for_element_with_signature_and_parent_ch_advance(
signature,
inline_style,
stylesheets,
parent,
ancestors,
parent_ch_advance,
)
} else {
css::style_for_element_with_signature(
signature,
inline_style,
stylesheets,
parent,
ancestors,
)
}
}
pub(super) fn layout_element_signature(
element: &Element,
mut signature: ElementSignature,
parent: Option<&ComputedStyle>,
) -> ElementSignature {
if let Some(direction) = element_document_direction(element) {
signature = signature.with_document_direction(direction);
}
signature.selected_image_dimensions = element.selected_image_dimensions().cloned();
let html_direction = html_directionality(element);
if let Some(direction) = html_direction {
signature = signature.with_html_direction(direction);
}
let resolved_direction = html_direction
.or_else(|| element_dir_attribute_direction(element))
.or_else(|| parent.map(|style| style.direction))
.unwrap_or(Direction::Ltr);
signature = signature.with_resolved_direction(resolved_direction);
signature
}
pub(in crate::layout) fn element_document_direction(element: &Element) -> Option<Direction> {
element_dir_attribute_direction(element).or_else(|| html_directionality(element))
}
#[cfg(test)]
fn apply_html_directionality(element: &Element, style: &mut ComputedStyle) {
if let Some(direction) = html_directionality(element) {
style.direction = direction;
}
}
fn html_directionality(element: &Element) -> Option<Direction> {
(element_dir_attribute_state(element) == HtmlDirState::Auto
|| (element.tag == "bdi"
&& element_dir_attribute_state(element) == HtmlDirState::Undefined))
.then(|| html_auto_directionality(element).unwrap_or(Direction::Ltr))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HtmlDirState {
Ltr,
Rtl,
Auto,
Undefined,
}
fn element_dir_attribute_state(element: &Element) -> HtmlDirState {
match element.attrs.get("dir").map(|value| value.trim()) {
Some(value) if value.eq_ignore_ascii_case("ltr") => HtmlDirState::Ltr,
Some(value) if value.eq_ignore_ascii_case("rtl") => HtmlDirState::Rtl,
Some(value) if value.eq_ignore_ascii_case("auto") => HtmlDirState::Auto,
_ => HtmlDirState::Undefined,
}
}
fn element_dir_attribute_direction(element: &Element) -> Option<Direction> {
match element_dir_attribute_state(element) {
HtmlDirState::Ltr => Some(Direction::Ltr),
HtmlDirState::Rtl => Some(Direction::Rtl),
HtmlDirState::Auto | HtmlDirState::Undefined => None,
}
}
fn html_auto_directionality(element: &Element) -> Option<Direction> {
if let Some(value) = css::auto_directionality_input_value(&element.tag, &element.attrs) {
return plaintext_direction_for_text(value)
.or_else(|| (!value.is_empty()).then_some(Direction::Ltr));
}
contained_text_auto_directionality(element, false)
}
fn contained_text_auto_directionality(
element: &Element,
can_exclude_root: bool,
) -> Option<Direction> {
if can_exclude_root && excludes_auto_directionality_subtree(element) {
return None;
}
for child in &element.children {
match &child.kind {
NodeKind::Text(text) => {
if let Some(direction) = plaintext_direction_for_text(text) {
return Some(direction);
}
}
NodeKind::Element(child_element) => {
if excludes_auto_directionality_subtree(child_element) {
continue;
}
if let Some(direction) = contained_text_auto_directionality(child_element, false) {
return Some(direction);
}
}
}
}
None
}
fn excludes_auto_directionality_subtree(element: &Element) -> bool {
matches!(
element.tag.as_str(),
"bdi" | "script" | "style" | "textarea"
) || element_dir_attribute_state(element) != HtmlDirState::Undefined
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dom::parse;
fn first_element_by_tag(source: &str, tag: &str) -> Element {
let root = parse(source);
find_element_by_tag(root, tag).expect("expected element")
}
fn find_element_by_tag(node: Node, tag: &str) -> Option<Element> {
match node.kind {
NodeKind::Text(_) => None,
NodeKind::Element(element) => {
if element.tag == tag {
return Some(element);
}
element
.children
.into_iter()
.find_map(|child| find_element_by_tag(child, tag))
}
}
}
#[test]
fn dir_auto_uses_first_strong_descendant_text() {
let element = first_element_by_tag("<p dir=\"auto\">123 אבג abc</p>", "p");
let mut style = ComputedStyle::initial();
apply_html_directionality(&element, &mut style);
assert_eq!(style.direction, Direction::Rtl);
}
#[test]
fn dir_auto_skips_descendant_with_defined_dir_attribute() {
let element =
first_element_by_tag("<p dir=\"auto\"><span dir=\"rtl\">אבג</span> abc</p>", "p");
let mut style = ComputedStyle::initial();
apply_html_directionality(&element, &mut style);
assert_eq!(style.direction, Direction::Ltr);
}
#[test]
fn bdi_without_dir_uses_auto_directionality() {
let element = first_element_by_tag("<bdi>אבג</bdi>", "bdi");
let mut style = ComputedStyle::initial();
apply_html_directionality(&element, &mut style);
assert_eq!(style.direction, Direction::Rtl);
}
#[test]
fn dir_auto_input_uses_its_static_value_for_auto_directionality() {
let rtl = first_element_by_tag(r#"<input dir="auto" value=".-=123 אבגABC.">"#, "input");
let ltr = first_element_by_tag(r#"<input dir="auto" value="123ABCאבג.">"#, "input");
let empty = first_element_by_tag(r#"<input dir="auto" value="">"#, "input");
let excluded =
first_element_by_tag(r#"<input type="number" dir="auto" value="אבג">"#, "input");
assert_eq!(html_auto_directionality(&rtl), Some(Direction::Rtl));
assert_eq!(html_auto_directionality(<r), Some(Direction::Ltr));
assert_eq!(html_auto_directionality(&empty), None);
assert_eq!(html_auto_directionality(&excluded), None);
}
#[test]
fn dir_auto_unknown_input_type_uses_the_text_state() {
let element = first_element_by_tag(
r#"<input type="not-a-real-state" dir="auto" value="אבג">"#,
"input",
);
assert_eq!(html_auto_directionality(&element), Some(Direction::Rtl));
}
}