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