script/dom/html/
htmltablecolelement.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 dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, local_name, ns};
7use js::rust::HandleObject;
8use style::attr::{AttrValue, LengthOrPercentageOrAuto};
9
10use crate::dom::attr::Attr;
11use crate::dom::bindings::codegen::Bindings::HTMLTableColElementBinding::HTMLTableColElementMethods;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::root::{DomRoot, LayoutDom};
14use crate::dom::bindings::str::DOMString;
15use crate::dom::document::Document;
16use crate::dom::element::{AttributeMutation, Element, LayoutElementHelpers};
17use crate::dom::html::htmlelement::HTMLElement;
18use crate::dom::node::{Node, NodeDamage};
19use crate::dom::virtualmethods::VirtualMethods;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct HTMLTableColElement {
24    htmlelement: HTMLElement,
25}
26
27impl HTMLTableColElement {
28    fn new_inherited(
29        local_name: LocalName,
30        prefix: Option<Prefix>,
31        document: &Document,
32    ) -> HTMLTableColElement {
33        HTMLTableColElement {
34            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
35        }
36    }
37
38    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
39    pub(crate) fn new(
40        local_name: LocalName,
41        prefix: Option<Prefix>,
42        document: &Document,
43        proto: Option<HandleObject>,
44        can_gc: CanGc,
45    ) -> DomRoot<HTMLTableColElement> {
46        let n = Node::reflect_node_with_proto(
47            Box::new(HTMLTableColElement::new_inherited(
48                local_name, prefix, document,
49            )),
50            document,
51            proto,
52            can_gc,
53        );
54
55        n.upcast::<Node>().set_weird_parser_insertion_mode();
56        n
57    }
58}
59
60impl HTMLTableColElementMethods<crate::DomTypeHolder> for HTMLTableColElement {
61    // <https://html.spec.whatwg.org/multipage/#attr-col-span>
62    make_uint_getter!(Span, "span", 1);
63    // <https://html.spec.whatwg.org/multipage/#attr-col-span>
64    // > The span IDL attribute must reflect the content attribute of the same name. It is clamped
65    // > to the range [1, 1000], and its default value is 1.
66    make_clamped_uint_setter!(SetSpan, "span", 1, 1000, 1);
67}
68
69pub(crate) trait HTMLTableColElementLayoutHelpers<'dom> {
70    fn get_span(self) -> Option<u32>;
71    fn get_width(self) -> LengthOrPercentageOrAuto;
72}
73
74impl<'dom> HTMLTableColElementLayoutHelpers<'dom> for LayoutDom<'dom, HTMLTableColElement> {
75    fn get_span(self) -> Option<u32> {
76        self.upcast::<Element>()
77            .get_attr_for_layout(&ns!(), &local_name!("span"))
78            .map(AttrValue::as_uint)
79    }
80
81    fn get_width(self) -> LengthOrPercentageOrAuto {
82        self.upcast::<Element>()
83            .get_attr_for_layout(&ns!(), &local_name!("width"))
84            .map(AttrValue::as_dimension)
85            .cloned()
86            .unwrap_or(LengthOrPercentageOrAuto::Auto)
87    }
88}
89
90impl VirtualMethods for HTMLTableColElement {
91    fn super_type(&self) -> Option<&dyn VirtualMethods> {
92        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
93    }
94
95    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
96        if let Some(super_type) = self.super_type() {
97            super_type.attribute_mutated(attr, mutation, can_gc);
98        }
99
100        if matches!(*attr.local_name(), local_name!("span")) {
101            self.upcast::<Node>().dirty(NodeDamage::Other);
102        }
103    }
104
105    fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
106        match *local_name {
107            local_name!("span") => {
108                let mut attr = AttrValue::from_u32(value.into(), 1);
109                if let AttrValue::UInt(_, ref mut val) = attr {
110                    // From <https://html.spec.whatwg.org/multipage/#attr-col-span>:
111                    // > The span IDL attribute must reflect the content attribute of the same name.
112                    // > It is clamped to the range [1, 1000], and its default value is 1.
113                    *val = (*val).clamp(1, 1000);
114                }
115                attr
116            },
117            local_name!("width") => AttrValue::from_dimension(value.into()),
118            _ => self
119                .super_type()
120                .unwrap()
121                .parse_plain_attribute(local_name, value),
122        }
123    }
124}