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};
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
73impl<'dom> LayoutDom<'dom, HTMLTableColElement> {
74    pub(crate) fn get_span(self) -> Option<u32> {
75        self.upcast::<Element>()
76            .get_attr_for_layout(&ns!(), &local_name!("span"))
77            .map(AttrValue::as_uint)
78    }
79
80    pub(crate) fn get_width(self) -> LengthOrPercentageOrAuto {
81        self.upcast::<Element>()
82            .get_attr_for_layout(&ns!(), &local_name!("width"))
83            .map(AttrValue::as_dimension)
84            .cloned()
85            .unwrap_or(LengthOrPercentageOrAuto::Auto)
86    }
87}
88
89impl VirtualMethods for HTMLTableColElement {
90    fn super_type(&self) -> Option<&dyn VirtualMethods> {
91        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
92    }
93
94    fn attribute_mutated(
95        &self,
96        cx: &mut js::context::JSContext,
97        attr: &Attr,
98        mutation: AttributeMutation,
99    ) {
100        if let Some(super_type) = self.super_type() {
101            super_type.attribute_mutated(cx, attr, mutation);
102        }
103
104        if matches!(*attr.local_name(), local_name!("span")) {
105            self.upcast::<Node>().dirty(NodeDamage::Other);
106        }
107    }
108
109    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
110        match attr.local_name() {
111            &local_name!("width") => true,
112            _ => self
113                .super_type()
114                .unwrap()
115                .attribute_affects_presentational_hints(attr),
116        }
117    }
118
119    fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
120        match *local_name {
121            local_name!("span") => {
122                let mut attr = AttrValue::from_u32(value.into(), 1);
123                if let AttrValue::UInt(_, ref mut val) = attr {
124                    // From <https://html.spec.whatwg.org/multipage/#attr-col-span>:
125                    // > The span IDL attribute must reflect the content attribute of the same name.
126                    // > It is clamped to the range [1, 1000], and its default value is 1.
127                    *val = (*val).clamp(1, 1000);
128                }
129                attr
130            },
131            local_name!("width") => AttrValue::from_dimension(value.into()),
132            _ => self
133                .super_type()
134                .unwrap()
135                .parse_plain_attribute(local_name, value),
136        }
137    }
138}