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