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::bindings::codegen::Bindings::HTMLHRElementBinding::HTMLHRElementMethods;
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::root::{DomRoot, LayoutDom};
20use crate::dom::bindings::str::DOMString;
21use crate::dom::document::Document;
22use crate::dom::element::{Element, LayoutElementHelpers};
23use crate::dom::html::htmlelement::HTMLElement;
24use crate::dom::node::Node;
25use crate::dom::virtualmethods::VirtualMethods;
26use crate::script_runtime::CanGc;
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    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
45    pub(crate) fn new(
46        local_name: LocalName,
47        prefix: Option<Prefix>,
48        document: &Document,
49        proto: Option<HandleObject>,
50        can_gc: CanGc,
51    ) -> DomRoot<HTMLHRElement> {
52        Node::reflect_node_with_proto(
53            Box::new(HTMLHRElement::new_inherited(local_name, prefix, document)),
54            document,
55            proto,
56            can_gc,
57        )
58    }
59}
60
61impl HTMLHRElementMethods<crate::DomTypeHolder> for HTMLHRElement {
62    // https://html.spec.whatwg.org/multipage/#dom-hr-align
63    make_getter!(Align, "align");
64
65    // https://html.spec.whatwg.org/multipage/#dom-hr-align
66    make_atomic_setter!(SetAlign, "align");
67
68    // https://html.spec.whatwg.org/multipage/#dom-hr-color
69    make_getter!(Color, "color");
70
71    // https://html.spec.whatwg.org/multipage/#dom-hr-color
72    make_legacy_color_setter!(SetColor, "color");
73
74    // https://html.spec.whatwg.org/multipage/#dom-hr-noshade
75    make_bool_getter!(NoShade, "noshade");
76
77    // https://html.spec.whatwg.org/multipage/#dom-hr-noshade
78    make_bool_setter!(SetNoShade, "noshade");
79
80    // https://html.spec.whatwg.org/multipage/#dom-hr-size
81    make_getter!(Size, "size");
82
83    // https://html.spec.whatwg.org/multipage/#dom-hr-size
84    make_dimension_setter!(SetSize, "size");
85
86    // https://html.spec.whatwg.org/multipage/#dom-hr-width
87    make_getter!(Width, "width");
88
89    // https://html.spec.whatwg.org/multipage/#dom-hr-width
90    make_dimension_setter!(SetWidth, "width");
91}
92
93/// The result of applying the the presentational hint for the `size` attribute.
94///
95/// (This attribute can mean different things depending on its value and other attributes)
96#[allow(clippy::enum_variant_names)]
97pub(crate) enum SizePresentationalHint {
98    SetHeightTo(Size),
99    SetAllBorderWidthValuesTo(BorderSideWidth),
100    SetBottomBorderWidthToZero,
101}
102
103pub(crate) trait HTMLHRLayoutHelpers {
104    fn get_color(self) -> Option<AbsoluteColor>;
105    fn get_width(self) -> LengthOrPercentageOrAuto;
106    fn get_size_info(self) -> Option<SizePresentationalHint>;
107}
108
109impl HTMLHRLayoutHelpers for LayoutDom<'_, HTMLHRElement> {
110    fn get_color(self) -> Option<AbsoluteColor> {
111        self.upcast::<Element>()
112            .get_attr_for_layout(&ns!(), &local_name!("color"))
113            .and_then(AttrValue::as_color)
114            .cloned()
115    }
116
117    fn get_width(self) -> LengthOrPercentageOrAuto {
118        self.upcast::<Element>()
119            .get_attr_for_layout(&ns!(), &local_name!("width"))
120            .map(AttrValue::as_dimension)
121            .cloned()
122            .unwrap_or(LengthOrPercentageOrAuto::Auto)
123    }
124
125    fn get_size_info(self) -> Option<SizePresentationalHint> {
126        // https://html.spec.whatwg.org/multipage/#the-hr-element-2
127        let element = self.upcast::<Element>();
128        let size_value = element
129            .get_attr_val_for_layout(&ns!(), &local_name!("size"))
130            .and_then(|value| usize::from_str(value).ok())
131            .filter(|value| *value != 0)?;
132
133        let hint = if element
134            .get_attr_for_layout(&ns!(), &local_name!("color"))
135            .is_some() ||
136            element
137                .get_attr_for_layout(&ns!(), &local_name!("noshade"))
138                .is_some()
139        {
140            SizePresentationalHint::SetAllBorderWidthValuesTo(BorderSideWidth::from_px(
141                size_value as f32 / 2.0,
142            ))
143        } else if size_value == 1 {
144            SizePresentationalHint::SetBottomBorderWidthToZero
145        } else {
146            SizePresentationalHint::SetHeightTo(Size::LengthPercentage(NonNegative(
147                LengthPercentage::Length(NoCalcLength::from_px((size_value - 2) as f32)),
148            )))
149        };
150
151        Some(hint)
152    }
153}
154
155impl VirtualMethods for HTMLHRElement {
156    fn super_type(&self) -> Option<&dyn VirtualMethods> {
157        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
158    }
159
160    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
161        match *name {
162            local_name!("align") => AttrValue::from_dimension(value.into()),
163            local_name!("color") => AttrValue::from_legacy_color(value.into()),
164            local_name!("width") => AttrValue::from_dimension(value.into()),
165            _ => self
166                .super_type()
167                .unwrap()
168                .parse_plain_attribute(name, value),
169        }
170    }
171}