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