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::rust::HandleObject;
9use style::attr::AttrValue;
10use style::color::AbsoluteColor;
11
12use crate::dom::attr::Attr;
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::{AttributeMutation, Element, LayoutElementHelpers};
20use crate::dom::eventtarget::EventTarget;
21use crate::dom::html::htmlelement::HTMLElement;
22use crate::dom::node::{BindContext, Node, NodeTraits};
23use crate::dom::virtualmethods::VirtualMethods;
24use crate::script_runtime::CanGc;
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        local_name: LocalName,
44        prefix: Option<Prefix>,
45        document: &Document,
46        proto: Option<HandleObject>,
47        can_gc: CanGc,
48    ) -> DomRoot<HTMLBodyElement> {
49        Node::reflect_node_with_proto(
50            Box::new(HTMLBodyElement::new_inherited(local_name, prefix, document)),
51            document,
52            proto,
53            can_gc,
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, input: DOMString, can_gc: CanGc) {
76        let value =
77            AttrValue::from_resolved_url(&self.owner_document().base_url().get_arc(), input.into());
78        self.upcast::<Element>()
79            .set_attribute(&local_name!("background"), value, can_gc);
80    }
81
82    // https://html.spec.whatwg.org/multipage/#windoweventhandlers
83    window_event_handlers!(ForwardToWindow);
84}
85
86pub(crate) trait HTMLBodyElementLayoutHelpers {
87    fn get_background_color(self) -> Option<AbsoluteColor>;
88    fn get_color(self) -> Option<AbsoluteColor>;
89}
90
91impl HTMLBodyElementLayoutHelpers for LayoutDom<'_, HTMLBodyElement> {
92    fn get_background_color(self) -> Option<AbsoluteColor> {
93        self.upcast::<Element>()
94            .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
95            .and_then(AttrValue::as_color)
96            .cloned()
97    }
98
99    fn get_color(self) -> Option<AbsoluteColor> {
100        self.upcast::<Element>()
101            .get_attr_for_layout(&ns!(), &local_name!("text"))
102            .and_then(AttrValue::as_color)
103            .cloned()
104    }
105}
106
107impl VirtualMethods for HTMLBodyElement {
108    fn super_type(&self) -> Option<&dyn VirtualMethods> {
109        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
110    }
111
112    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
113        if attr.local_name() == &local_name!("bgcolor") {
114            return true;
115        }
116
117        self.super_type()
118            .unwrap()
119            .attribute_affects_presentational_hints(attr)
120    }
121
122    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
123        if let Some(s) = self.super_type() {
124            s.bind_to_tree(context, can_gc);
125        }
126
127        if !context.tree_is_in_a_document_tree {
128            return;
129        }
130
131        let window = self.owner_window();
132        window.prevent_layout_until_load_event();
133        if window.is_top_level() {
134            window.send_to_embedder(EmbedderMsg::NotifyLoadStatusChanged(
135                window.webview_id(),
136                LoadStatus::HeadParsed,
137            ));
138        }
139    }
140
141    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
142        match *name {
143            local_name!("bgcolor") | local_name!("text") => {
144                AttrValue::from_legacy_color(value.into())
145            },
146            _ => self
147                .super_type()
148                .unwrap()
149                .parse_plain_attribute(name, value),
150        }
151    }
152
153    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
154        let do_super_mutate = match (attr.local_name(), mutation) {
155            (name, AttributeMutation::Set(..)) if name.starts_with("on") => {
156                let window = self.owner_window();
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                        let source = &**attr.value();
184                        let evtarget = window.upcast::<EventTarget>(); // forwarded event
185                        let source_line = 1; // TODO(#9604) obtain current JS execution line
186                        evtarget.set_event_handler_uncompiled(
187                            window.get_url(),
188                            source_line,
189                            &name[2..],
190                            source,
191                        );
192                        false
193                    },
194                    _ => true, // HTMLElement::attribute_mutated will take care of this.
195                }
196            },
197            _ => true,
198        };
199
200        if do_super_mutate {
201            self.super_type()
202                .unwrap()
203                .attribute_mutated(attr, mutation, can_gc);
204        }
205    }
206}