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