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