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