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