script/dom/html/
htmltablecellelement.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};
9use style::color::AbsoluteColor;
10
11use crate::dom::attr::Attr;
12use crate::dom::bindings::codegen::Bindings::HTMLTableCellElementBinding::HTMLTableCellElementMethods;
13use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::root::{DomRoot, LayoutDom};
16use crate::dom::bindings::str::DOMString;
17use crate::dom::document::Document;
18use crate::dom::element::{AttributeMutation, Element};
19use crate::dom::html::htmlelement::HTMLElement;
20use crate::dom::html::htmltableelement::HTMLTableElement;
21use crate::dom::html::htmltablerowelement::HTMLTableRowElement;
22use crate::dom::html::htmltablesectionelement::HTMLTableSectionElement;
23use crate::dom::node::{Node, NodeDamage};
24use crate::dom::virtualmethods::VirtualMethods;
25
26const DEFAULT_COLSPAN: u32 = 1;
27const DEFAULT_ROWSPAN: u32 = 1;
28
29#[dom_struct]
30pub(crate) struct HTMLTableCellElement {
31    htmlelement: HTMLElement,
32}
33
34impl HTMLTableCellElement {
35    fn new_inherited(
36        local_name: LocalName,
37        prefix: Option<Prefix>,
38        document: &Document,
39    ) -> HTMLTableCellElement {
40        HTMLTableCellElement {
41            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
42        }
43    }
44
45    pub(crate) fn new(
46        cx: &mut js::context::JSContext,
47        local_name: LocalName,
48        prefix: Option<Prefix>,
49        document: &Document,
50        proto: Option<HandleObject>,
51    ) -> DomRoot<HTMLTableCellElement> {
52        let n = Node::reflect_node_with_proto(
53            cx,
54            Box::new(HTMLTableCellElement::new_inherited(
55                local_name, prefix, document,
56            )),
57            document,
58            proto,
59        );
60
61        n.upcast::<Node>().set_weird_parser_insertion_mode();
62        n
63    }
64}
65
66impl HTMLTableCellElementMethods<crate::DomTypeHolder> for HTMLTableCellElement {
67    // https://html.spec.whatwg.org/multipage/#dom-tdth-colspan
68    make_uint_getter!(ColSpan, "colspan", DEFAULT_COLSPAN);
69
70    // https://html.spec.whatwg.org/multipage/#dom-tdth-colspan
71    // > The colSpan IDL attribute must reflect the colspan content attribute. It is clamped to
72    // > the range [1, 1000], and its default value is 1.
73    make_clamped_uint_setter!(SetColSpan, "colspan", 1, 1000, 1);
74
75    // https://html.spec.whatwg.org/multipage/#dom-tdth-rowspan
76    make_uint_getter!(RowSpan, "rowspan", DEFAULT_ROWSPAN);
77
78    // https://html.spec.whatwg.org/multipage/#dom-tdth-rowspan
79    // > The rowSpan IDL attribute must reflect the rowspan content attribute. It is clamped to
80    // > the range [0, 65534], and its default value is 1.
81    make_clamped_uint_setter!(SetRowSpan, "rowspan", 0, 65534, 1);
82
83    // https://html.spec.whatwg.org/multipage/#dom-tdth-bgcolor
84    make_getter!(BgColor, "bgcolor");
85
86    // https://html.spec.whatwg.org/multipage/#dom-tdth-bgcolor
87    make_legacy_color_setter!(SetBgColor, "bgcolor");
88
89    // <https://html.spec.whatwg.org/multipage/#dom-tdth-height>
90    make_getter!(Height, "height");
91
92    // <https://html.spec.whatwg.org/multipage/#dom-tdth-height>
93    make_nonzero_dimension_setter!(SetHeight, "height");
94
95    // <https://html.spec.whatwg.org/multipage/#dom-tdth-width>
96    make_getter!(Width, "width");
97
98    // <https://html.spec.whatwg.org/multipage/#dom-tdth-width>
99    make_nonzero_dimension_setter!(SetWidth, "width");
100
101    /// <https://html.spec.whatwg.org/multipage/#dom-tdth-cellindex>
102    fn CellIndex(&self) -> i32 {
103        let self_node = self.upcast::<Node>();
104
105        let parent_children = match self_node.GetParentNode() {
106            Some(ref parent_node) if parent_node.is::<HTMLTableRowElement>() => {
107                parent_node.children()
108            },
109            _ => return -1,
110        };
111
112        parent_children
113            .filter(|c| c.is::<HTMLTableCellElement>())
114            .position(|c| &*c == self_node)
115            .map_or(-1, |p| p as i32)
116    }
117}
118
119impl<'dom> LayoutDom<'dom, HTMLTableCellElement> {
120    pub(crate) fn get_background_color(self) -> Option<AbsoluteColor> {
121        self.upcast::<Element>()
122            .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
123            .and_then(AttrValue::as_color)
124            .cloned()
125    }
126
127    pub(crate) fn get_colspan(self) -> Option<u32> {
128        self.upcast::<Element>()
129            .get_attr_for_layout(&ns!(), &local_name!("colspan"))
130            .map(AttrValue::as_uint)
131    }
132
133    pub(crate) fn get_rowspan(self) -> Option<u32> {
134        self.upcast::<Element>()
135            .get_attr_for_layout(&ns!(), &local_name!("rowspan"))
136            .map(AttrValue::as_uint)
137    }
138
139    pub(crate) fn get_table(self) -> Option<LayoutDom<'dom, HTMLTableElement>> {
140        let row = self.upcast::<Node>().composed_parent_node_ref()?;
141        row.downcast::<HTMLTableRowElement>()?;
142        let section = row.composed_parent_node_ref()?;
143        section.downcast::<HTMLTableElement>().or_else(|| {
144            section.downcast::<HTMLTableSectionElement>()?;
145            let table = section.composed_parent_node_ref()?;
146            table.downcast::<HTMLTableElement>()
147        })
148    }
149
150    pub(crate) fn get_width(self) -> LengthOrPercentageOrAuto {
151        self.upcast::<Element>()
152            .get_attr_for_layout(&ns!(), &local_name!("width"))
153            .map(AttrValue::as_dimension)
154            .cloned()
155            .unwrap_or(LengthOrPercentageOrAuto::Auto)
156    }
157
158    pub(crate) fn get_height(self) -> LengthOrPercentageOrAuto {
159        self.upcast::<Element>()
160            .get_attr_for_layout(&ns!(), &local_name!("height"))
161            .map(AttrValue::as_dimension)
162            .cloned()
163            .unwrap_or(LengthOrPercentageOrAuto::Auto)
164    }
165}
166
167impl VirtualMethods for HTMLTableCellElement {
168    fn super_type(&self) -> Option<&dyn VirtualMethods> {
169        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
170    }
171
172    fn attribute_mutated(
173        &self,
174        cx: &mut js::context::JSContext,
175        attr: &Attr,
176        mutation: AttributeMutation,
177    ) {
178        if let Some(super_type) = self.super_type() {
179            super_type.attribute_mutated(cx, attr, mutation);
180        }
181
182        if matches!(*attr.local_name(), local_name!("colspan")) {
183            self.upcast::<Node>().dirty(NodeDamage::Other);
184        }
185        if matches!(*attr.local_name(), local_name!("rowspan")) {
186            self.upcast::<Node>().dirty(NodeDamage::Other);
187        }
188    }
189
190    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
191        match attr.local_name() {
192            &local_name!("width") | &local_name!("height") => true,
193            _ => self
194                .super_type()
195                .unwrap()
196                .attribute_affects_presentational_hints(attr),
197        }
198    }
199
200    fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
201        match *local_name {
202            local_name!("colspan") => {
203                let mut attr = AttrValue::from_u32(value.into(), DEFAULT_COLSPAN);
204                if let AttrValue::UInt(_, ref mut value) = attr {
205                    // From <https://html.spec.whatwg.org/multipage/#dom-tdth-colspan>:
206                    // > The colSpan IDL attribute must reflect the colspan content attribute. It is clamped to
207                    // > the range [1, 1000], and its default value is 1.
208                    *value = (*value).clamp(1, 1000);
209                }
210                attr
211            },
212            local_name!("rowspan") => {
213                let mut attr = AttrValue::from_u32(value.into(), DEFAULT_ROWSPAN);
214                if let AttrValue::UInt(_, ref mut value) = attr {
215                    // From <https://html.spec.whatwg.org/multipage/#dom-tdth-rowspan>:
216                    // > The rowSpan IDL attribute must reflect the rowspan content attribute. It is clamped to
217                    // > the range [0, 65534], and its default value is 1.
218                    // Note Firefox floors by 1 in quirks mode, but like Chrome and Safari we don't do that.
219                    *value = (*value).clamp(0, 65534);
220                }
221                attr
222            },
223            local_name!("bgcolor") => AttrValue::from_legacy_color(value.into()),
224            local_name!("width") => AttrValue::from_nonzero_dimension(value.into()),
225            local_name!("height") => AttrValue::from_nonzero_dimension(value.into()),
226            _ => self
227                .super_type()
228                .unwrap()
229                .parse_plain_attribute(local_name, value),
230        }
231    }
232}