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        local_name: LocalName,
45        prefix: Option<Prefix>,
46        document: &Document,
47        proto: Option<HandleObject>,
48        can_gc: CanGc,
49    ) -> DomRoot<HTMLBodyElement> {
50        Node::reflect_node_with_proto(
51            Box::new(HTMLBodyElement::new_inherited(local_name, prefix, document)),
52            document,
53            proto,
54            can_gc,
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(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
155        let do_super_mutate = match (attr.local_name(), mutation) {
156            (name, AttributeMutation::Set(..)) if name.starts_with("on") => {
157                let window = self.owner_window();
158                // https://html.spec.whatwg.org/multipage/
159                // #event-handlers-on-elements,-document-objects,-and-window-objects:event-handlers-6
160                match name {
161                    &local_name!("onafterprint") |
162                    &local_name!("onbeforeprint") |
163                    &local_name!("onbeforeunload") |
164                    &local_name!("onerror") |
165                    &local_name!("onfocus") |
166                    &local_name!("onhashchange") |
167                    &local_name!("onload") |
168                    &local_name!("onlanguagechange") |
169                    &local_name!("onmessage") |
170                    &local_name!("onmessageerror") |
171                    &local_name!("onoffline") |
172                    &local_name!("ononline") |
173                    &local_name!("onpagehide") |
174                    &local_name!("onpagereveal") |
175                    &local_name!("onpageshow") |
176                    &local_name!("onpageswap") |
177                    &local_name!("onpopstate") |
178                    &local_name!("onrejectionhandled") |
179                    &local_name!("onresize") |
180                    &local_name!("onscroll") |
181                    &local_name!("onstorage") |
182                    &local_name!("onunhandledrejection") |
183                    &local_name!("onunload") => {
184                        let source = &**attr.value();
185                        let evtarget = window.upcast::<EventTarget>(); // forwarded event
186                        let source_line = 1; // TODO(#9604) obtain current JS execution line
187                        evtarget.set_event_handler_uncompiled(
188                            window.get_url(),
189                            source_line,
190                            &name[2..],
191                            source,
192                        );
193                        false
194                    },
195                    _ => true, // HTMLElement::attribute_mutated will take care of this.
196                }
197            },
198            _ => true,
199        };
200
201        if do_super_mutate {
202            self.super_type()
203                .unwrap()
204                .attribute_mutated(attr, mutation, can_gc);
205        }
206    }
207}