script/dom/html/
htmltablesectionelement.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::HTMLTableSectionElementBinding::HTMLTableSectionElementMethods;
12use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
13use crate::dom::bindings::error::{ErrorResult, Fallible};
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::{Element, LayoutElementHelpers};
19use crate::dom::html::htmlcollection::HTMLCollection;
20use crate::dom::html::htmlelement::HTMLElement;
21use crate::dom::html::htmltablerowelement::HTMLTableRowElement;
22use crate::dom::node::{Node, NodeTraits};
23use crate::dom::virtualmethods::VirtualMethods;
24use crate::script_runtime::CanGc;
25
26#[dom_struct]
27pub(crate) struct HTMLTableSectionElement {
28    htmlelement: HTMLElement,
29}
30
31impl HTMLTableSectionElement {
32    fn new_inherited(
33        local_name: LocalName,
34        prefix: Option<Prefix>,
35        document: &Document,
36    ) -> HTMLTableSectionElement {
37        HTMLTableSectionElement {
38            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
39        }
40    }
41
42    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
43    pub(crate) fn new(
44        local_name: LocalName,
45        prefix: Option<Prefix>,
46        document: &Document,
47        proto: Option<HandleObject>,
48        can_gc: CanGc,
49    ) -> DomRoot<HTMLTableSectionElement> {
50        let n = Node::reflect_node_with_proto(
51            Box::new(HTMLTableSectionElement::new_inherited(
52                local_name, prefix, document,
53            )),
54            document,
55            proto,
56            can_gc,
57        );
58
59        n.upcast::<Node>().set_weird_parser_insertion_mode();
60        n
61    }
62}
63
64impl HTMLTableSectionElementMethods<crate::DomTypeHolder> for HTMLTableSectionElement {
65    // https://html.spec.whatwg.org/multipage/#dom-tbody-rows
66    fn Rows(&self) -> DomRoot<HTMLCollection> {
67        HTMLCollection::new_with_filter_fn(
68            &self.owner_window(),
69            self.upcast(),
70            |element, root| {
71                element.is::<HTMLTableRowElement>() &&
72                    element.upcast::<Node>().GetParentNode().as_deref() == Some(root)
73            },
74            CanGc::note(),
75        )
76    }
77
78    // https://html.spec.whatwg.org/multipage/#dom-tbody-insertrow
79    fn InsertRow(&self, index: i32, can_gc: CanGc) -> Fallible<DomRoot<HTMLElement>> {
80        let node = self.upcast::<Node>();
81        node.insert_cell_or_row(
82            index,
83            || self.Rows(),
84            || HTMLTableRowElement::new(local_name!("tr"), None, &node.owner_doc(), None, can_gc),
85            can_gc,
86        )
87    }
88
89    // https://html.spec.whatwg.org/multipage/#dom-tbody-deleterow
90    fn DeleteRow(&self, index: i32) -> ErrorResult {
91        let node = self.upcast::<Node>();
92        node.delete_cell_or_row(
93            index,
94            || self.Rows(),
95            |n| n.is::<HTMLTableRowElement>(),
96            CanGc::note(),
97        )
98    }
99}
100
101pub(crate) trait HTMLTableSectionElementLayoutHelpers {
102    fn get_background_color(self) -> Option<AbsoluteColor>;
103    fn get_height(self) -> LengthOrPercentageOrAuto;
104}
105
106impl HTMLTableSectionElementLayoutHelpers for LayoutDom<'_, HTMLTableSectionElement> {
107    fn get_background_color(self) -> Option<AbsoluteColor> {
108        self.upcast::<Element>()
109            .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
110            .and_then(AttrValue::as_color)
111            .cloned()
112    }
113
114    fn get_height(self) -> LengthOrPercentageOrAuto {
115        self.upcast::<Element>()
116            .get_attr_for_layout(&ns!(), &local_name!("height"))
117            .map(AttrValue::as_dimension)
118            .cloned()
119            .unwrap_or(LengthOrPercentageOrAuto::Auto)
120    }
121}
122
123impl VirtualMethods for HTMLTableSectionElement {
124    fn super_type(&self) -> Option<&dyn VirtualMethods> {
125        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
126    }
127
128    fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
129        match *local_name {
130            local_name!("bgcolor") => AttrValue::from_legacy_color(value.into()),
131            local_name!("height") => AttrValue::from_dimension(value.into()),
132            _ => self
133                .super_type()
134                .unwrap()
135                .parse_plain_attribute(local_name, value),
136        }
137    }
138}