1use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, QualName, local_name, ns};
7use js::context::{JSContext, NoGC};
8use js::rust::HandleObject;
9use style::attr::{AttrValue, LengthOrPercentageOrAuto};
10use style::color::AbsoluteColor;
11
12use crate::dom::bindings::codegen::Bindings::HTMLTableElementBinding::HTMLTableElementMethods;
13use crate::dom::bindings::codegen::Bindings::HTMLTableRowElementBinding::HTMLTableRowElementMethods;
14use crate::dom::bindings::codegen::Bindings::HTMLTableSectionElementBinding::HTMLTableSectionElementMethods;
15use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
16use crate::dom::bindings::error::{ErrorResult, Fallible};
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
19use crate::dom::bindings::str::DOMString;
20use crate::dom::document::Document;
21use crate::dom::element::attributes::storage::AttrRef;
22use crate::dom::element::{CustomElementCreationMode, Element, ElementCreator};
23use crate::dom::html::htmlcollection::HTMLCollection;
24use crate::dom::html::htmlelement::HTMLElement;
25use crate::dom::html::htmltablecellelement::HTMLTableCellElement;
26use crate::dom::html::htmltableelement::HTMLTableElement;
27use crate::dom::html::htmltablesectionelement::HTMLTableSectionElement;
28use crate::dom::node::virtualmethods::VirtualMethods;
29use crate::dom::node::{Node, NodeTraits};
30
31#[dom_struct]
32pub(crate) struct HTMLTableRowElement {
33 htmlelement: HTMLElement,
34 cells: MutNullableDom<HTMLCollection>,
35}
36
37impl HTMLTableRowElement {
38 fn new_inherited(
39 local_name: LocalName,
40 prefix: Option<Prefix>,
41 document: &Document,
42 ) -> HTMLTableRowElement {
43 HTMLTableRowElement {
44 htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
45 cells: Default::default(),
46 }
47 }
48
49 pub(crate) fn new(
50 cx: &mut js::context::JSContext,
51 local_name: LocalName,
52 prefix: Option<Prefix>,
53 document: &Document,
54 proto: Option<HandleObject>,
55 ) -> DomRoot<HTMLTableRowElement> {
56 let n = Node::reflect_node_with_proto(
57 cx,
58 Box::new(HTMLTableRowElement::new_inherited(
59 local_name, prefix, document,
60 )),
61 document,
62 proto,
63 );
64
65 n.upcast::<Node>().set_weird_parser_insertion_mode();
66 n
67 }
68
69 fn row_index(&self, no_gc: &NoGC, collection: DomRoot<HTMLCollection>) -> i32 {
72 collection
73 .elements_iter(no_gc)
74 .position(|elem| (&elem as &Element) == self.upcast())
75 .map_or(-1, |i| i as i32)
76 }
77}
78
79impl HTMLTableRowElementMethods<crate::DomTypeHolder> for HTMLTableRowElement {
80 make_getter!(BgColor, "bgcolor");
82
83 make_legacy_color_setter!(SetBgColor, "bgcolor");
85
86 fn Cells(&self, cx: &mut JSContext) -> DomRoot<HTMLCollection> {
88 self.cells.or_init(|| {
89 HTMLCollection::new_with_filter_fn(
90 cx,
91 &self.owner_window(),
92 self.upcast(),
93 |element, root| {
94 (element.is::<HTMLTableCellElement>()) &&
95 element.upcast::<Node>().GetParentNode().as_deref() == Some(root)
96 },
97 )
98 })
99 }
100
101 fn InsertCell(&self, cx: &mut JSContext, index: i32) -> Fallible<DomRoot<HTMLElement>> {
103 let node = self.upcast::<Node>();
104 node.insert_cell_or_row(
105 cx,
106 index,
107 |cx| self.Cells(cx),
108 |cx| {
109 let cell = Element::create(
110 cx,
111 QualName::new(None, ns!(html), local_name!("td")),
112 None,
113 &node.owner_doc(),
114 ElementCreator::ScriptCreated,
115 CustomElementCreationMode::Asynchronous,
116 None,
117 );
118 DomRoot::downcast::<HTMLTableCellElement>(cell).unwrap()
119 },
120 )
121 }
122
123 fn DeleteCell(&self, cx: &mut JSContext, index: i32) -> ErrorResult {
125 let node = self.upcast::<Node>();
126 node.delete_cell_or_row(
127 cx,
128 index,
129 |cx| self.Cells(cx),
130 |n| n.is::<HTMLTableCellElement>(),
131 )
132 }
133
134 fn RowIndex(&self, cx: &mut JSContext) -> i32 {
136 let parent = match self.upcast::<Node>().GetParentNode() {
137 Some(parent) => parent,
138 None => return -1,
139 };
140 if let Some(table) = parent.downcast::<HTMLTableElement>() {
141 let rows = table.Rows(cx);
142 return self.row_index(cx.no_gc(), rows);
143 }
144 if !parent.is::<HTMLTableSectionElement>() {
145 return -1;
146 }
147 let grandparent = match parent.upcast::<Node>().GetParentNode() {
148 Some(parent) => parent,
149 None => return -1,
150 };
151 grandparent
152 .downcast::<HTMLTableElement>()
153 .map_or(-1, |table| {
154 let rows = table.Rows(cx);
155 self.row_index(cx.no_gc(), rows)
156 })
157 }
158
159 fn SectionRowIndex(&self, cx: &mut JSContext) -> i32 {
161 let parent = match self.upcast::<Node>().GetParentNode() {
162 Some(parent) => parent,
163 None => return -1,
164 };
165 let collection = if let Some(table) = parent.downcast::<HTMLTableElement>() {
166 table.Rows(cx)
167 } else if let Some(table_section) = parent.downcast::<HTMLTableSectionElement>() {
168 table_section.Rows(cx)
169 } else {
170 return -1;
171 };
172 self.row_index(cx.no_gc(), collection)
173 }
174}
175
176impl LayoutDom<'_, HTMLTableRowElement> {
177 pub(crate) fn get_background_color(self) -> Option<AbsoluteColor> {
178 self.upcast::<Element>()
179 .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
180 .and_then(AttrValue::as_color)
181 .cloned()
182 }
183
184 pub(crate) fn get_height(self) -> LengthOrPercentageOrAuto {
185 self.upcast::<Element>()
186 .get_attr_for_layout(&ns!(), &local_name!("height"))
187 .map(AttrValue::as_dimension)
188 .cloned()
189 .unwrap_or(LengthOrPercentageOrAuto::Auto)
190 }
191}
192
193impl VirtualMethods for HTMLTableRowElement {
194 fn super_type(&self) -> Option<&dyn VirtualMethods> {
195 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
196 }
197
198 fn attribute_affects_presentational_hints(&self, attr: AttrRef<'_>) -> bool {
199 match attr.local_name() {
200 &local_name!("height") => true,
201 _ => self
202 .super_type()
203 .unwrap()
204 .attribute_affects_presentational_hints(attr),
205 }
206 }
207
208 fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
209 match *local_name {
210 local_name!("bgcolor") => AttrValue::from_legacy_color(value.into()),
211 local_name!("height") => AttrValue::from_dimension(value.into()),
212 _ => self
213 .super_type()
214 .unwrap()
215 .parse_plain_attribute(local_name, value),
216 }
217 }
218}