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    pub(crate) fn new(
39        local_name: LocalName,
40        prefix: Option<Prefix>,
41        document: &Document,
42        proto: Option<HandleObject>,
43        can_gc: CanGc,
44    ) -> DomRoot<HTMLTableColElement> {
45        let n = Node::reflect_node_with_proto(
46            Box::new(HTMLTableColElement::new_inherited(
47                local_name, prefix, document,
48            )),
49            document,
50            proto,
51            can_gc,
52        );
53
54        n.upcast::<Node>().set_weird_parser_insertion_mode();
55        n
56    }
57}
58
59impl HTMLTableColElementMethods<crate::DomTypeHolder> for HTMLTableColElement {
60    // <https://html.spec.whatwg.org/multipage/#attr-col-span>
61    make_uint_getter!(Span, "span", 1);
62    // <https://html.spec.whatwg.org/multipage/#attr-col-span>
63    // > The span IDL attribute must reflect the content attribute of the same name. It is clamped
64    // > to the range [1, 1000], and its default value is 1.
65    make_clamped_uint_setter!(SetSpan, "span", 1, 1000, 1);
66
67    // <https://html.spec.whatwg.org/multipage/#dom-col-width>
68    make_getter!(Width, "width");
69
70    // <https://html.spec.whatwg.org/multipage/#dom-col-width>
71    make_dimension_setter!(SetWidth, "width");
72}
73
74pub(crate) trait HTMLTableColElementLayoutHelpers<'dom> {
75    fn get_span(self) -> Option<u32>;
76    fn get_width(self) -> LengthOrPercentageOrAuto;
77}
78
79impl<'dom> HTMLTableColElementLayoutHelpers<'dom> for LayoutDom<'dom, HTMLTableColElement> {
80    fn get_span(self) -> Option<u32> {
81        self.upcast::<Element>()
82            .get_attr_for_layout(&ns!(), &local_name!("span"))
83            .map(AttrValue::as_uint)
84    }
85
86    fn get_width(self) -> LengthOrPercentageOrAuto {
87        self.upcast::<Element>()
88            .get_attr_for_layout(&ns!(), &local_name!("width"))
89            .map(AttrValue::as_dimension)
90            .cloned()
91            .unwrap_or(LengthOrPercentageOrAuto::Auto)
92    }
93}
94
95impl VirtualMethods for HTMLTableColElement {
96    fn super_type(&self) -> Option<&dyn VirtualMethods> {
97        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
98    }
99
100    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
101        if let Some(super_type) = self.super_type() {
102            super_type.attribute_mutated(attr, mutation, can_gc);
103        }
104
105        if matches!(*attr.local_name(), local_name!("span")) {
106            self.upcast::<Node>().dirty(NodeDamage::Other);
107        }
108    }
109
110    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
111        match attr.local_name() {
112            &local_name!("width") => true,
113            _ => self
114                .super_type()
115                .unwrap()
116                .attribute_affects_presentational_hints(attr),
117        }
118    }
119
120    fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
121        match *local_name {
122            local_name!("span") => {
123                let mut attr = AttrValue::from_u32(value.into(), 1);
124                if let AttrValue::UInt(_, ref mut val) = attr {
125                    // From <https://html.spec.whatwg.org/multipage/#attr-col-span>:
126                    // > The span IDL attribute must reflect the content attribute of the same name.
127                    // > It is clamped to the range [1, 1000], and its default value is 1.
128                    *val = (*val).clamp(1, 1000);
129                }
130                attr
131            },
132            local_name!("width") => AttrValue::from_dimension(value.into()),
133            _ => self
134                .super_type()
135                .unwrap()
136                .parse_plain_attribute(local_name, value),
137        }
138    }
139}