script/dom/html/
htmltablerowelement.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::bindings::codegen::Bindings::HTMLTableElementBinding::HTMLTableElementMethods;
12use crate::dom::bindings::codegen::Bindings::HTMLTableRowElementBinding::HTMLTableRowElementMethods;
13use crate::dom::bindings::codegen::Bindings::HTMLTableSectionElementBinding::HTMLTableSectionElementMethods;
14use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
15use crate::dom::bindings::error::{ErrorResult, Fallible};
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::document::Document;
20use crate::dom::element::{Element, LayoutElementHelpers};
21use crate::dom::html::htmlcollection::HTMLCollection;
22use crate::dom::html::htmlelement::HTMLElement;
23use crate::dom::html::htmltablecellelement::HTMLTableCellElement;
24use crate::dom::html::htmltableelement::HTMLTableElement;
25use crate::dom::html::htmltablesectionelement::HTMLTableSectionElement;
26use crate::dom::node::{Node, NodeTraits};
27use crate::dom::virtualmethods::VirtualMethods;
28use crate::script_runtime::CanGc;
29
30#[dom_struct]
31pub(crate) struct HTMLTableRowElement {
32    htmlelement: HTMLElement,
33    cells: MutNullableDom<HTMLCollection>,
34}
35
36impl HTMLTableRowElement {
37    fn new_inherited(
38        local_name: LocalName,
39        prefix: Option<Prefix>,
40        document: &Document,
41    ) -> HTMLTableRowElement {
42        HTMLTableRowElement {
43            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
44            cells: Default::default(),
45        }
46    }
47
48    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
49    pub(crate) fn new(
50        local_name: LocalName,
51        prefix: Option<Prefix>,
52        document: &Document,
53        proto: Option<HandleObject>,
54        can_gc: CanGc,
55    ) -> DomRoot<HTMLTableRowElement> {
56        let n = Node::reflect_node_with_proto(
57            Box::new(HTMLTableRowElement::new_inherited(
58                local_name, prefix, document,
59            )),
60            document,
61            proto,
62            can_gc,
63        );
64
65        n.upcast::<Node>().set_weird_parser_insertion_mode();
66        n
67    }
68
69    /// Determine the index for this `HTMLTableRowElement` within the given
70    /// `HTMLCollection`. Returns `-1` if not found within collection.
71    fn row_index(&self, collection: DomRoot<HTMLCollection>) -> i32 {
72        collection
73            .elements_iter()
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    // https://html.spec.whatwg.org/multipage/#dom-tr-bgcolor
81    make_getter!(BgColor, "bgcolor");
82
83    // https://html.spec.whatwg.org/multipage/#dom-tr-bgcolor
84    make_legacy_color_setter!(SetBgColor, "bgcolor");
85
86    // https://html.spec.whatwg.org/multipage/#dom-tr-cells
87    fn Cells(&self) -> DomRoot<HTMLCollection> {
88        self.cells.or_init(|| {
89            HTMLCollection::new_with_filter_fn(
90                &self.owner_window(),
91                self.upcast(),
92                |element, root| {
93                    (element.is::<HTMLTableCellElement>()) &&
94                        element.upcast::<Node>().GetParentNode().as_deref() == Some(root)
95                },
96                CanGc::note(),
97            )
98        })
99    }
100
101    // https://html.spec.whatwg.org/multipage/#dom-tr-insertcell
102    fn InsertCell(&self, index: i32, can_gc: CanGc) -> Fallible<DomRoot<HTMLElement>> {
103        let node = self.upcast::<Node>();
104        node.insert_cell_or_row(
105            index,
106            || self.Cells(),
107            || HTMLTableCellElement::new(local_name!("td"), None, &node.owner_doc(), None, can_gc),
108            can_gc,
109        )
110    }
111
112    // https://html.spec.whatwg.org/multipage/#dom-tr-deletecell
113    fn DeleteCell(&self, index: i32) -> ErrorResult {
114        let node = self.upcast::<Node>();
115        node.delete_cell_or_row(
116            index,
117            || self.Cells(),
118            |n| n.is::<HTMLTableCellElement>(),
119            CanGc::note(),
120        )
121    }
122
123    // https://html.spec.whatwg.org/multipage/#dom-tr-rowindex
124    fn RowIndex(&self) -> i32 {
125        let parent = match self.upcast::<Node>().GetParentNode() {
126            Some(parent) => parent,
127            None => return -1,
128        };
129        if let Some(table) = parent.downcast::<HTMLTableElement>() {
130            return self.row_index(table.Rows());
131        }
132        if !parent.is::<HTMLTableSectionElement>() {
133            return -1;
134        }
135        let grandparent = match parent.upcast::<Node>().GetParentNode() {
136            Some(parent) => parent,
137            None => return -1,
138        };
139        grandparent
140            .downcast::<HTMLTableElement>()
141            .map_or(-1, |table| self.row_index(table.Rows()))
142    }
143
144    // https://html.spec.whatwg.org/multipage/#dom-tr-sectionrowindex
145    fn SectionRowIndex(&self) -> i32 {
146        let parent = match self.upcast::<Node>().GetParentNode() {
147            Some(parent) => parent,
148            None => return -1,
149        };
150        let collection = if let Some(table) = parent.downcast::<HTMLTableElement>() {
151            table.Rows()
152        } else if let Some(table_section) = parent.downcast::<HTMLTableSectionElement>() {
153            table_section.Rows()
154        } else {
155            return -1;
156        };
157        self.row_index(collection)
158    }
159}
160
161pub(crate) trait HTMLTableRowElementLayoutHelpers {
162    fn get_background_color(self) -> Option<AbsoluteColor>;
163    fn get_height(self) -> LengthOrPercentageOrAuto;
164}
165
166impl HTMLTableRowElementLayoutHelpers for LayoutDom<'_, HTMLTableRowElement> {
167    fn get_background_color(self) -> Option<AbsoluteColor> {
168        self.upcast::<Element>()
169            .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
170            .and_then(AttrValue::as_color)
171            .cloned()
172    }
173
174    fn get_height(self) -> LengthOrPercentageOrAuto {
175        self.upcast::<Element>()
176            .get_attr_for_layout(&ns!(), &local_name!("height"))
177            .map(AttrValue::as_dimension)
178            .cloned()
179            .unwrap_or(LengthOrPercentageOrAuto::Auto)
180    }
181}
182
183impl VirtualMethods for HTMLTableRowElement {
184    fn super_type(&self) -> Option<&dyn VirtualMethods> {
185        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
186    }
187
188    fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
189        match *local_name {
190            local_name!("bgcolor") => AttrValue::from_legacy_color(value.into()),
191            local_name!("height") => AttrValue::from_dimension(value.into()),
192            _ => self
193                .super_type()
194                .unwrap()
195                .parse_plain_attribute(local_name, value),
196        }
197    }
198}