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::context::JSContext;
8use js::rust::HandleObject;
9use style::attr::{AttrValue, LengthOrPercentageOrAuto};
10use style::color::AbsoluteColor;
11
12use crate::dom::attr::Attr;
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};
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::htmltablerowelement::HTMLTableRowElement;
26use crate::dom::node::{Node, NodeTraits};
27use crate::dom::virtualmethods::VirtualMethods;
28use crate::script_runtime::CanGc;
29
30#[dom_struct]
31pub(crate) struct HTMLTableSectionElement {
32    htmlelement: HTMLElement,
33}
34
35impl HTMLTableSectionElement {
36    fn new_inherited(
37        local_name: LocalName,
38        prefix: Option<Prefix>,
39        document: &Document,
40    ) -> HTMLTableSectionElement {
41        HTMLTableSectionElement {
42            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
43        }
44    }
45
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, cx: &mut JSContext, index: i32) -> Fallible<DomRoot<HTMLElement>> {
83        let node = self.upcast::<Node>();
84        node.insert_cell_or_row(
85            cx,
86            index,
87            || self.Rows(),
88            |cx| {
89                let row = Element::create(
90                    QualName::new(None, ns!(html), local_name!("tr")),
91                    None,
92                    &node.owner_doc(),
93                    ElementCreator::ScriptCreated,
94                    CustomElementCreationMode::Asynchronous,
95                    None,
96                    CanGc::from_cx(cx),
97                );
98                DomRoot::downcast::<HTMLTableRowElement>(row).unwrap()
99            },
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}