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