Skip to main content

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::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::{BindContext, Node, NodeTraits};
24use crate::dom::virtualmethods::VirtualMethods;
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 attr.local_name() == &local_name!("bgcolor") {
109            return true;
110        }
111
112        self.super_type()
113            .unwrap()
114            .attribute_affects_presentational_hints(attr)
115    }
116
117    fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
118        if let Some(s) = self.super_type() {
119            s.bind_to_tree(cx, context);
120        }
121
122        if !context.tree_is_in_a_document_tree {
123            return;
124        }
125
126        let window = self.owner_window();
127        window.prevent_layout_until_load_event();
128        if window.is_top_level() {
129            window.send_to_embedder(EmbedderMsg::NotifyLoadStatusChanged(
130                window.webview_id(),
131                LoadStatus::HeadParsed,
132            ));
133        }
134    }
135
136    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
137        match *name {
138            local_name!("bgcolor") | local_name!("text") => {
139                AttrValue::from_legacy_color(value.into())
140            },
141            _ => self
142                .super_type()
143                .unwrap()
144                .parse_plain_attribute(name, value),
145        }
146    }
147
148    fn attribute_mutated(
149        &self,
150        cx: &mut JSContext,
151        attr: AttrRef<'_>,
152        mutation: AttributeMutation,
153    ) {
154        let do_super_mutate = match (attr.local_name(), mutation) {
155            (name, AttributeMutation::Set(..)) if name.starts_with("on") => {
156                let document = self.owner_document();
157                // https://html.spec.whatwg.org/multipage/
158                // #event-handlers-on-elements,-document-objects,-and-window-objects:event-handlers-6
159                match name {
160                    &local_name!("onafterprint") |
161                    &local_name!("onbeforeprint") |
162                    &local_name!("onbeforeunload") |
163                    &local_name!("onerror") |
164                    &local_name!("onfocus") |
165                    &local_name!("onhashchange") |
166                    &local_name!("onload") |
167                    &local_name!("onlanguagechange") |
168                    &local_name!("onmessage") |
169                    &local_name!("onmessageerror") |
170                    &local_name!("onoffline") |
171                    &local_name!("ononline") |
172                    &local_name!("onpagehide") |
173                    &local_name!("onpagereveal") |
174                    &local_name!("onpageshow") |
175                    &local_name!("onpageswap") |
176                    &local_name!("onpopstate") |
177                    &local_name!("onrejectionhandled") |
178                    &local_name!("onresize") |
179                    &local_name!("onscroll") |
180                    &local_name!("onstorage") |
181                    &local_name!("onunhandledrejection") |
182                    &local_name!("onunload") => {
183                        if document.has_browsing_context() {
184                            // https://html.spec.whatwg.org/multipage/webappapis.html
185                            // #event-handler-attributes%3Aevent-handler-content-attributes-3
186                            // This matches the `has_browsing_context()` check done by the
187                            // `window_event_handlers!(ForwardToWindow)` macro for WebIDL attributes.
188                            let window = document.window();
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                        }
199                        false
200                    },
201                    _ => true, // HTMLElement::attribute_mutated will take care of this.
202                }
203            },
204            _ => true,
205        };
206
207        if do_super_mutate {
208            self.super_type()
209                .unwrap()
210                .attribute_mutated(cx, attr, mutation);
211        }
212    }
213}