script/dom/html/
htmlbodyelement.rs1use 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};
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 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 make_getter!(BgColor, "bgcolor");
62
63 make_legacy_color_setter!(SetBgColor, "bgcolor");
65
66 make_getter!(Text, "text");
68
69 make_legacy_color_setter!(SetText, "text");
71
72 make_getter!(Background, "background");
74
75 fn SetBackground(&self, cx: &mut JSContext, input: DOMString) {
77 let value =
78 AttrValue::from_resolved_url(&self.owner_document().base_url().get_arc(), input.into());
79 self.upcast::<Element>().set_attribute(
80 &local_name!("background"),
81 value,
82 CanGc::from_cx(cx),
83 );
84 }
85
86 window_event_handlers!(ForwardToWindow);
88}
89
90impl LayoutDom<'_, HTMLBodyElement> {
91 pub(crate) fn get_background_color(self) -> Option<AbsoluteColor> {
92 self.upcast::<Element>()
93 .get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
94 .and_then(AttrValue::as_color)
95 .cloned()
96 }
97
98 pub(crate) fn get_color(self) -> Option<AbsoluteColor> {
99 self.upcast::<Element>()
100 .get_attr_for_layout(&ns!(), &local_name!("text"))
101 .and_then(AttrValue::as_color)
102 .cloned()
103 }
104}
105
106impl VirtualMethods for HTMLBodyElement {
107 fn super_type(&self) -> Option<&dyn VirtualMethods> {
108 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
109 }
110
111 fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
112 if attr.local_name() == &local_name!("bgcolor") {
113 return true;
114 }
115
116 self.super_type()
117 .unwrap()
118 .attribute_affects_presentational_hints(attr)
119 }
120
121 fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
122 if let Some(s) = self.super_type() {
123 s.bind_to_tree(cx, context);
124 }
125
126 if !context.tree_is_in_a_document_tree {
127 return;
128 }
129
130 let window = self.owner_window();
131 window.prevent_layout_until_load_event();
132 if window.is_top_level() {
133 window.send_to_embedder(EmbedderMsg::NotifyLoadStatusChanged(
134 window.webview_id(),
135 LoadStatus::HeadParsed,
136 ));
137 }
138 }
139
140 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
141 match *name {
142 local_name!("bgcolor") | local_name!("text") => {
143 AttrValue::from_legacy_color(value.into())
144 },
145 _ => self
146 .super_type()
147 .unwrap()
148 .parse_plain_attribute(name, value),
149 }
150 }
151
152 fn attribute_mutated(&self, cx: &mut JSContext, attr: &Attr, mutation: AttributeMutation) {
153 let do_super_mutate = match (attr.local_name(), mutation) {
154 (name, AttributeMutation::Set(..)) if name.starts_with("on") => {
155 let document = self.owner_document();
156 match name {
159 &local_name!("onafterprint") |
160 &local_name!("onbeforeprint") |
161 &local_name!("onbeforeunload") |
162 &local_name!("onerror") |
163 &local_name!("onfocus") |
164 &local_name!("onhashchange") |
165 &local_name!("onload") |
166 &local_name!("onlanguagechange") |
167 &local_name!("onmessage") |
168 &local_name!("onmessageerror") |
169 &local_name!("onoffline") |
170 &local_name!("ononline") |
171 &local_name!("onpagehide") |
172 &local_name!("onpagereveal") |
173 &local_name!("onpageshow") |
174 &local_name!("onpageswap") |
175 &local_name!("onpopstate") |
176 &local_name!("onrejectionhandled") |
177 &local_name!("onresize") |
178 &local_name!("onscroll") |
179 &local_name!("onstorage") |
180 &local_name!("onunhandledrejection") |
181 &local_name!("onunload") => {
182 if document.has_browsing_context() {
183 let window = document.window();
188 let source = &**attr.value();
189 let evtarget = window.upcast::<EventTarget>(); let source_line = 1; evtarget.set_event_handler_uncompiled(
192 window.get_url(),
193 source_line,
194 &name[2..],
195 source,
196 );
197 }
198 false
199 },
200 _ => true, }
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}