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