script/dom/html/
htmltablecellelement.rs1use 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, LayoutElementHelpers};
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::{LayoutNodeHelpers, 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 make_uint_getter!(ColSpan, "colspan", DEFAULT_COLSPAN);
69
70 make_clamped_uint_setter!(SetColSpan, "colspan", 1, 1000, 1);
74
75 make_uint_getter!(RowSpan, "rowspan", DEFAULT_ROWSPAN);
77
78 make_clamped_uint_setter!(SetRowSpan, "rowspan", 0, 65534, 1);
82
83 make_getter!(BgColor, "bgcolor");
85
86 make_legacy_color_setter!(SetBgColor, "bgcolor");
88
89 make_getter!(Height, "height");
91
92 make_nonzero_dimension_setter!(SetHeight, "height");
94
95 make_getter!(Width, "width");
97
98 make_nonzero_dimension_setter!(SetWidth, "width");
100
101 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
119pub(crate) trait HTMLTableCellElementLayoutHelpers<'dom> {
120 fn get_background_color(self) -> Option<AbsoluteColor>;
121 fn get_colspan(self) -> Option<u32>;
122 fn get_rowspan(self) -> Option<u32>;
123 fn get_table(self) -> Option<LayoutDom<'dom, HTMLTableElement>>;
124 fn get_width(self) -> LengthOrPercentageOrAuto;
125 fn get_height(self) -> LengthOrPercentageOrAuto;
126}
127
128impl<'dom> HTMLTableCellElementLayoutHelpers<'dom> for LayoutDom<'dom, HTMLTableCellElement> {
129 fn get_background_color(self) -> Option<AbsoluteColor> {
130 self.upcast::<Element>()
131 .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
132 .and_then(AttrValue::as_color)
133 .cloned()
134 }
135
136 fn get_colspan(self) -> Option<u32> {
137 self.upcast::<Element>()
138 .get_attr_for_layout(&ns!(), &local_name!("colspan"))
139 .map(AttrValue::as_uint)
140 }
141
142 fn get_rowspan(self) -> Option<u32> {
143 self.upcast::<Element>()
144 .get_attr_for_layout(&ns!(), &local_name!("rowspan"))
145 .map(AttrValue::as_uint)
146 }
147
148 fn get_table(self) -> Option<LayoutDom<'dom, HTMLTableElement>> {
149 let row = self.upcast::<Node>().composed_parent_node_ref()?;
150 row.downcast::<HTMLTableRowElement>()?;
151 let section = row.composed_parent_node_ref()?;
152 section.downcast::<HTMLTableElement>().or_else(|| {
153 section.downcast::<HTMLTableSectionElement>()?;
154 let table = section.composed_parent_node_ref()?;
155 table.downcast::<HTMLTableElement>()
156 })
157 }
158
159 fn get_width(self) -> LengthOrPercentageOrAuto {
160 self.upcast::<Element>()
161 .get_attr_for_layout(&ns!(), &local_name!("width"))
162 .map(AttrValue::as_dimension)
163 .cloned()
164 .unwrap_or(LengthOrPercentageOrAuto::Auto)
165 }
166
167 fn get_height(self) -> LengthOrPercentageOrAuto {
168 self.upcast::<Element>()
169 .get_attr_for_layout(&ns!(), &local_name!("height"))
170 .map(AttrValue::as_dimension)
171 .cloned()
172 .unwrap_or(LengthOrPercentageOrAuto::Auto)
173 }
174}
175
176impl VirtualMethods for HTMLTableCellElement {
177 fn super_type(&self) -> Option<&dyn VirtualMethods> {
178 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
179 }
180
181 fn attribute_mutated(
182 &self,
183 cx: &mut js::context::JSContext,
184 attr: &Attr,
185 mutation: AttributeMutation,
186 ) {
187 if let Some(super_type) = self.super_type() {
188 super_type.attribute_mutated(cx, attr, mutation);
189 }
190
191 if matches!(*attr.local_name(), local_name!("colspan")) {
192 self.upcast::<Node>().dirty(NodeDamage::Other);
193 }
194 if matches!(*attr.local_name(), local_name!("rowspan")) {
195 self.upcast::<Node>().dirty(NodeDamage::Other);
196 }
197 }
198
199 fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
200 match attr.local_name() {
201 &local_name!("width") | &local_name!("height") => true,
202 _ => self
203 .super_type()
204 .unwrap()
205 .attribute_affects_presentational_hints(attr),
206 }
207 }
208
209 fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
210 match *local_name {
211 local_name!("colspan") => {
212 let mut attr = AttrValue::from_u32(value.into(), DEFAULT_COLSPAN);
213 if let AttrValue::UInt(_, ref mut value) = attr {
214 *value = (*value).clamp(1, 1000);
218 }
219 attr
220 },
221 local_name!("rowspan") => {
222 let mut attr = AttrValue::from_u32(value.into(), DEFAULT_ROWSPAN);
223 if let AttrValue::UInt(_, ref mut value) = attr {
224 *value = (*value).clamp(0, 65534);
229 }
230 attr
231 },
232 local_name!("bgcolor") => AttrValue::from_legacy_color(value.into()),
233 local_name!("width") => AttrValue::from_nonzero_dimension(value.into()),
234 local_name!("height") => AttrValue::from_nonzero_dimension(value.into()),
235 _ => self
236 .super_type()
237 .unwrap()
238 .parse_plain_attribute(local_name, value),
239 }
240 }
241}