script/dom/html/
htmlhrelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::str::FromStr;
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix, local_name, ns};
9use js::rust::HandleObject;
10use style::attr::{AttrValue, LengthOrPercentageOrAuto};
11use style::color::AbsoluteColor;
12use style::values::generics::NonNegative;
13use style::values::specified::border::BorderSideWidth;
14use style::values::specified::length::Size;
15use style::values::specified::{LengthPercentage, NoCalcLength};
16
17use crate::dom::attr::Attr;
18use crate::dom::bindings::codegen::Bindings::HTMLHRElementBinding::HTMLHRElementMethods;
19use crate::dom::bindings::inheritance::Castable;
20use crate::dom::bindings::root::{DomRoot, LayoutDom};
21use crate::dom::bindings::str::DOMString;
22use crate::dom::document::Document;
23use crate::dom::element::Element;
24use crate::dom::html::htmlelement::HTMLElement;
25use crate::dom::node::Node;
26use crate::dom::virtualmethods::VirtualMethods;
27
28#[dom_struct]
29pub(crate) struct HTMLHRElement {
30    htmlelement: HTMLElement,
31}
32
33impl HTMLHRElement {
34    fn new_inherited(
35        local_name: LocalName,
36        prefix: Option<Prefix>,
37        document: &Document,
38    ) -> HTMLHRElement {
39        HTMLHRElement {
40            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
41        }
42    }
43
44    pub(crate) fn new(
45        cx: &mut js::context::JSContext,
46        local_name: LocalName,
47        prefix: Option<Prefix>,
48        document: &Document,
49        proto: Option<HandleObject>,
50    ) -> DomRoot<HTMLHRElement> {
51        Node::reflect_node_with_proto(
52            cx,
53            Box::new(HTMLHRElement::new_inherited(local_name, prefix, document)),
54            document,
55            proto,
56        )
57    }
58}
59
60impl HTMLHRElementMethods<crate::DomTypeHolder> for HTMLHRElement {
61    // https://html.spec.whatwg.org/multipage/#dom-hr-align
62    make_getter!(Align, "align");
63
64    // https://html.spec.whatwg.org/multipage/#dom-hr-align
65    make_atomic_setter!(cx, SetAlign, "align");
66
67    // https://html.spec.whatwg.org/multipage/#dom-hr-color
68    make_getter!(Color, "color");
69
70    // https://html.spec.whatwg.org/multipage/#dom-hr-color
71    make_legacy_color_setter!(SetColor, "color");
72
73    // https://html.spec.whatwg.org/multipage/#dom-hr-noshade
74    make_bool_getter!(NoShade, "noshade");
75
76    // https://html.spec.whatwg.org/multipage/#dom-hr-noshade
77    make_bool_setter!(cx, SetNoShade, "noshade");
78
79    // https://html.spec.whatwg.org/multipage/#dom-hr-size
80    make_getter!(Size, "size");
81
82    // https://html.spec.whatwg.org/multipage/#dom-hr-size
83    make_dimension_setter!(SetSize, "size");
84
85    // <https://html.spec.whatwg.org/multipage/#dom-hr-width>
86    make_getter!(Width, "width");
87
88    // <https://html.spec.whatwg.org/multipage/#dom-hr-width>
89    make_dimension_setter!(SetWidth, "width");
90}
91
92/// The result of applying the presentational hint for the `size` attribute.
93///
94/// (This attribute can mean different things depending on its value and other attributes)
95#[expect(clippy::enum_variant_names)]
96pub(crate) enum SizePresentationalHint {
97    SetHeightTo(Size),
98    SetAllBorderWidthValuesTo(BorderSideWidth),
99    SetBottomBorderWidthToZero,
100}
101
102impl LayoutDom<'_, HTMLHRElement> {
103    pub(crate) fn get_color(self) -> Option<AbsoluteColor> {
104        self.upcast::<Element>()
105            .get_attr_for_layout(&ns!(), &local_name!("color"))
106            .and_then(AttrValue::as_color)
107            .cloned()
108    }
109
110    pub(crate) fn get_width(self) -> LengthOrPercentageOrAuto {
111        self.upcast::<Element>()
112            .get_attr_for_layout(&ns!(), &local_name!("width"))
113            .map(AttrValue::as_dimension)
114            .cloned()
115            .unwrap_or(LengthOrPercentageOrAuto::Auto)
116    }
117
118    pub(crate) fn get_size_info(self) -> Option<SizePresentationalHint> {
119        // https://html.spec.whatwg.org/multipage/#the-hr-element-2
120        let element = self.upcast::<Element>();
121        let size_value = element
122            .get_attr_val_for_layout(&ns!(), &local_name!("size"))
123            .and_then(|value| usize::from_str(value).ok())
124            .filter(|value| *value != 0)?;
125
126        let hint = if element
127            .get_attr_for_layout(&ns!(), &local_name!("color"))
128            .is_some() ||
129            element
130                .get_attr_for_layout(&ns!(), &local_name!("noshade"))
131                .is_some()
132        {
133            SizePresentationalHint::SetAllBorderWidthValuesTo(BorderSideWidth::from_px(
134                size_value as f32 / 2.0,
135            ))
136        } else if size_value == 1 {
137            SizePresentationalHint::SetBottomBorderWidthToZero
138        } else {
139            SizePresentationalHint::SetHeightTo(Size::LengthPercentage(NonNegative(
140                LengthPercentage::Length(NoCalcLength::from_px((size_value - 2) as f32)),
141            )))
142        };
143
144        Some(hint)
145    }
146}
147
148impl VirtualMethods for HTMLHRElement {
149    fn super_type(&self) -> Option<&dyn VirtualMethods> {
150        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
151    }
152
153    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
154        match attr.local_name() {
155            &local_name!("width") => true,
156            _ => self
157                .super_type()
158                .unwrap()
159                .attribute_affects_presentational_hints(attr),
160        }
161    }
162
163    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
164        match *name {
165            local_name!("align") => AttrValue::from_dimension(value.into()),
166            local_name!("color") => AttrValue::from_legacy_color(value.into()),
167            local_name!("width") => AttrValue::from_dimension(value.into()),
168            _ => self
169                .super_type()
170                .unwrap()
171                .parse_plain_attribute(name, value),
172        }
173    }
174}