Skip to main content

script/dom/html/document_structure/
htmlbodyelement.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 embedder_traits::{EmbedderMsg, LoadStatus};
7use html5ever::{LocalName, Prefix, local_name, ns};
8use js::context::JSContext;
9use js::rust::HandleObject;
10use style::attr::AttrValue;
11use style::color::AbsoluteColor;
12
13use crate::dom::bindings::codegen::Bindings::HTMLBodyElementBinding::HTMLBodyElementMethods;
14use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
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::attributes::storage::AttrRef;
20use crate::dom::element::{AttributeMutation, Element};
21use crate::dom::eventtarget::EventTarget;
22use crate::dom::html::htmlelement::HTMLElement;
23use crate::dom::node::virtualmethods::VirtualMethods;
24use crate::dom::node::{BindContext, Node, NodeTraits};
25
26#[dom_struct]
27pub(crate) struct HTMLBodyElement {
28    htmlelement: HTMLElement,
29}
30
31impl HTMLBodyElement {
32    fn new_inherited(
33        local_name: LocalName,
34        prefix: Option<Prefix>,
35        document: &Document,
36    ) -> HTMLBodyElement {
37        HTMLBodyElement {
38            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
39        }
40    }
41
42    pub(crate) fn new(
43        cx: &mut JSContext,
44        local_name: LocalName,
45        prefix: Option<Prefix>,
46        document: &Document,
47        proto: Option<HandleObject>,
48    ) -> DomRoot<HTMLBodyElement> {
49        Node::reflect_node_with_proto(
50            cx,
51            Box::new(HTMLBodyElement::new_inherited(local_name, prefix, document)),
52            document,
53            proto,
54        )
55    }
56}
57
58impl HTMLBodyElementMethods<crate::DomTypeHolder> for HTMLBodyElement {
59    // https://html.spec.whatwg.org/multipage/#dom-body-bgcolor
60    make_getter!(BgColor, "bgcolor");
61
62    // https://html.spec.whatwg.org/multipage/#dom-body-bgcolor
63    make_legacy_color_setter!(SetBgColor, "bgcolor");
64
65    // https://html.spec.whatwg.org/multipage/#dom-body-text
66    make_getter!(Text, "text");
67
68    // https://html.spec.whatwg.org/multipage/#dom-body-text
69    make_legacy_color_setter!(SetText, "text");
70
71    // https://html.spec.whatwg.org/multipage/#dom-body-background
72    make_getter!(Background, "background");
73
74    /// <https://html.spec.whatwg.org/multipage/#dom-body-background>
75    fn SetBackground(&self, cx: &mut JSContext, input: DOMString) {
76        let value =
77            AttrValue::from_resolved_url(&self.owner_document().base_url().get_arc(), input.into());
78        self.upcast::<Element>()
79            .set_attribute(cx, &local_name!("background"), value);
80    }
81
82    // https://html.spec.whatwg.org/multipage/#windoweventhandlers
83    window_event_handlers!(ForwardToWindow);
84}
85
86impl LayoutDom<'_, HTMLBodyElement> {
87    pub(crate) fn get_background_color(self) -> Option<AbsoluteColor> {
88        self.upcast::<Element>()
89            .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
90            .and_then(AttrValue::as_color)
91            .cloned()
92    }
93
94    pub(crate) fn get_color(self) -> Option<AbsoluteColor> {
95        self.upcast::<Element>()
96            .get_attr_for_layout(&ns!(), &local_name!("text"))
97            .and_then(AttrValue::as_color)
98            .cloned()
99    }
100}
101
102impl VirtualMethods for HTMLBodyElement {
103    fn super_type(&self) -> Option<&dyn VirtualMethods> {
104        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
105    }
106
107    fn attribute_affects_presentational_hints(&self, attr: AttrRef<'_>) -> bool {
108        if matches!(
109            attr.local_name(),
110            &local_name!("bgcolor") | &local_name!("text")
111        ) {
112            return true;
113        }
114
115        self.super_type()
116            .unwrap()
117            .attribute_affects_presentational_hints(attr)
118    }
119
120    fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
121        if let Some(s) = self.super_type() {
122            s.bind_to_tree(cx, context);
123        }
124
125        if !context.tree_is_in_a_document_tree {
126            return;
127        }
128
129        let window = self.owner_window();
130        window.prevent_layout_until_load_event();
131        if window.is_top_level() {
132            window.send_to_embedder(EmbedderMsg::NotifyLoadStatusChanged(
133                window.webview_id(),
134                LoadStatus::HeadParsed,
135            ));
136        }
137    }
138
139    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
140        match *name {
141            local_name!("bgcolor") | local_name!("text") => {
142                AttrValue::from_legacy_color(value.into())
143            },
144            _ => self
145                .super_type()
146                .unwrap()
147                .parse_plain_attribute(name, value),
148        }
149    }
150
151    fn attribute_mutated(
152        &self,
153        cx: &mut JSContext,
154        attr: AttrRef<'_>,
155        mutation: AttributeMutation,
156    ) {
157        let do_super_mutate = match (attr.local_name(), mutation) {
158            (name, AttributeMutation::Set(_)) if name.starts_with("on") => {
159                let document = self.owner_document();
160                // https://html.spec.whatwg.org/multipage/
161                // #event-handlers-on-elements,-document-objects,-and-window-objects:event-handlers-6
162                match name {
163                    &local_name!("onafterprint") |
164                    &local_name!("onbeforeprint") |
165                    &local_name!("onbeforeunload") |
166                    &local_name!("onerror") |
167                    &local_name!("onfocus") |
168                    &local_name!("onhashchange") |
169                    &local_name!("onload") |
170                    &local_name!("onlanguagechange") |
171                    &local_name!("onmessage") |
172                    &local_name!("onmessageerror") |
173                    &local_name!("onoffline") |
174                    &local_name!("ononline") |
175                    &local_name!("onpagehide") |
176                    &local_name!("onpagereveal") |
177                    &local_name!("onpageshow") |
178                    &local_name!("onpageswap") |
179                    &local_name!("onpopstate") |
180                    &local_name!("onrejectionhandled") |
181                    &local_name!("onresize") |
182                    &local_name!("onscroll") |
183                    &local_name!("onstorage") |
184                    &local_name!("onunhandledrejection") |
185                    &local_name!("onunload") => {
186                        if document.has_browsing_context() {
187                            // https://html.spec.whatwg.org/multipage/webappapis.html
188                            // #event-handler-attributes%3Aevent-handler-content-attributes-3
189                            // This matches the `has_browsing_context()` check done by the
190                            // `window_event_handlers!(ForwardToWindow)` macro for WebIDL attributes.
191                            let window = document.window();
192                            let source = &**attr.value();
193                            let evtarget = window.upcast::<EventTarget>(); // forwarded event
194                            let source_line = 1; // TODO(#9604) obtain current JS execution line
195                            evtarget.set_event_handler_uncompiled(
196                                cx,
197                                window.get_url(),
198                                source_line,
199                                &name[2..],
200                                source,
201                            );
202                        }
203                        false
204                    },
205                    _ => true, // HTMLElement::attribute_mutated will take care of this.
206                }
207            },
208            _ => true,
209        };
210
211        if do_super_mutate {
212            self.super_type()
213                .unwrap()
214                .attribute_mutated(cx, attr, mutation);
215        }
216    }
217}