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        cx: &mut js::context::JSContext,
48        local_name: LocalName,
49        prefix: Option<Prefix>,
50        document: &Document,
51        proto: Option<HandleObject>,
52    ) -> DomRoot<HTMLTableSectionElement> {
53        let n = Node::reflect_node_with_proto(
54            cx,
55            Box::new(HTMLTableSectionElement::new_inherited(
56                local_name, prefix, document,
57            )),
58            document,
59            proto,
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                    cx,
91                    QualName::new(None, ns!(html), local_name!("tr")),
92                    None,
93                    &node.owner_doc(),
94                    ElementCreator::ScriptCreated,
95                    CustomElementCreationMode::Asynchronous,
96                    None,
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, cx: &mut JSContext, index: i32) -> ErrorResult {
105        let node = self.upcast::<Node>();
106        node.delete_cell_or_row(cx, index, || self.Rows(), |n| n.is::<HTMLTableRowElement>())
107    }
108}
109
110pub(crate) trait HTMLTableSectionElementLayoutHelpers {
111    fn get_background_color(self) -> Option<AbsoluteColor>;
112    fn get_height(self) -> LengthOrPercentageOrAuto;
113}
114
115impl HTMLTableSectionElementLayoutHelpers for LayoutDom<'_, HTMLTableSectionElement> {
116    fn get_background_color(self) -> Option<AbsoluteColor> {
117        self.upcast::<Element>()
118            .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
119            .and_then(AttrValue::as_color)
120            .cloned()
121    }
122
123    fn get_height(self) -> LengthOrPercentageOrAuto {
124        self.upcast::<Element>()
125            .get_attr_for_layout(&ns!(), &local_name!("height"))
126            .map(AttrValue::as_dimension)
127            .cloned()
128            .unwrap_or(LengthOrPercentageOrAuto::Auto)
129    }
130}
131
132impl VirtualMethods for HTMLTableSectionElement {
133    fn super_type(&self) -> Option<&dyn VirtualMethods> {
134        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
135    }
136
137    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
138        match attr.local_name() {
139            &local_name!("height") => true,
140            _ => self
141                .super_type()
142                .unwrap()
143                .attribute_affects_presentational_hints(attr),
144        }
145    }
146
147    fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
148        match *local_name {
149            local_name!("bgcolor") => AttrValue::from_legacy_color(value.into()),
150            local_name!("height") => AttrValue::from_dimension(value.into()),
151            _ => self
152                .super_type()
153                .unwrap()
154                .parse_plain_attribute(local_name, value),
155        }
156    }
157}