script/dom/html/
htmlobjectelement.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 std::default::Default;
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix, local_name};
9use js::context::JSContext;
10use js::rust::HandleObject;
11use pixels::RasterImage;
12use servo_arc::Arc;
13
14use crate::dom::attr::Attr;
15use crate::dom::bindings::cell::DomRefCell;
16use crate::dom::bindings::codegen::Bindings::HTMLObjectElementBinding::HTMLObjectElementMethods;
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::root::{DomRoot, MutNullableDom};
19use crate::dom::bindings::str::DOMString;
20use crate::dom::document::Document;
21use crate::dom::element::{AttributeMutation, Element};
22use crate::dom::html::htmlelement::HTMLElement;
23use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
24use crate::dom::node::{Node, NodeTraits};
25use crate::dom::validation::Validatable;
26use crate::dom::validitystate::ValidityState;
27use crate::dom::virtualmethods::VirtualMethods;
28use crate::script_runtime::CanGc;
29
30#[dom_struct]
31pub(crate) struct HTMLObjectElement {
32    htmlelement: HTMLElement,
33    #[ignore_malloc_size_of = "RasterImage"]
34    #[no_trace]
35    image: DomRefCell<Option<Arc<RasterImage>>>,
36    form_owner: MutNullableDom<HTMLFormElement>,
37    validity_state: MutNullableDom<ValidityState>,
38}
39
40impl HTMLObjectElement {
41    fn new_inherited(
42        local_name: LocalName,
43        prefix: Option<Prefix>,
44        document: &Document,
45    ) -> HTMLObjectElement {
46        HTMLObjectElement {
47            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
48            image: DomRefCell::new(None),
49            form_owner: Default::default(),
50            validity_state: Default::default(),
51        }
52    }
53
54    pub(crate) fn new(
55        cx: &mut js::context::JSContext,
56        local_name: LocalName,
57        prefix: Option<Prefix>,
58        document: &Document,
59        proto: Option<HandleObject>,
60    ) -> DomRoot<HTMLObjectElement> {
61        Node::reflect_node_with_proto(
62            cx,
63            Box::new(HTMLObjectElement::new_inherited(
64                local_name, prefix, document,
65            )),
66            document,
67            proto,
68        )
69    }
70}
71
72trait ProcessDataURL {
73    fn process_data_url(&self);
74}
75
76impl ProcessDataURL for &HTMLObjectElement {
77    // Makes the local `data` member match the status of the `data` attribute and starts
78    /// prefetching the image. This method must be called after `data` is changed.
79    fn process_data_url(&self) {
80        let element = self.upcast::<Element>();
81
82        // TODO: support other values
83        if let (None, Some(_uri)) = (
84            element.get_attribute(&local_name!("type")),
85            element.get_attribute(&local_name!("data")),
86        ) {
87            // TODO(gw): Prefetch the image here.
88        }
89    }
90}
91
92impl HTMLObjectElementMethods<crate::DomTypeHolder> for HTMLObjectElement {
93    // https://html.spec.whatwg.org/multipage/#dom-object-type
94    make_getter!(Type, "type");
95
96    // https://html.spec.whatwg.org/multipage/#dom-object-type
97    make_setter!(SetType, "type");
98
99    // https://html.spec.whatwg.org/multipage/#dom-object-usemap
100    make_getter!(UseMap, "usemap");
101
102    // https://html.spec.whatwg.org/multipage/#dom-object-usemap
103    make_setter!(SetUseMap, "usemap");
104
105    /// <https://html.spec.whatwg.org/multipage/#dom-fae-form>
106    fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
107        self.form_owner()
108    }
109
110    /// <https://html.spec.whatwg.org/multipage/#dom-cva-willvalidate>
111    fn WillValidate(&self) -> bool {
112        self.is_instance_validatable()
113    }
114
115    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validity>
116    fn Validity(&self, can_gc: CanGc) -> DomRoot<ValidityState> {
117        self.validity_state(can_gc)
118    }
119
120    /// <https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity>
121    fn CheckValidity(&self, cx: &mut JSContext) -> bool {
122        self.check_validity(cx)
123    }
124
125    /// <https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity>
126    fn ReportValidity(&self, cx: &mut JSContext) -> bool {
127        self.report_validity(cx)
128    }
129
130    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage>
131    fn ValidationMessage(&self) -> DOMString {
132        self.validation_message()
133    }
134
135    /// <https://html.spec.whatwg.org/multipage/#dom-cva-setcustomvalidity>
136    fn SetCustomValidity(&self, error: DOMString, can_gc: CanGc) {
137        self.validity_state(can_gc).set_custom_error_message(error);
138    }
139}
140
141impl Validatable for HTMLObjectElement {
142    fn as_element(&self) -> &Element {
143        self.upcast()
144    }
145
146    fn validity_state(&self, can_gc: CanGc) -> DomRoot<ValidityState> {
147        self.validity_state
148            .or_init(|| ValidityState::new(&self.owner_window(), self.upcast(), can_gc))
149    }
150
151    fn is_instance_validatable(&self) -> bool {
152        // https://html.spec.whatwg.org/multipage/#the-object-element%3Abarred-from-constraint-validation
153        false
154    }
155}
156
157impl VirtualMethods for HTMLObjectElement {
158    fn super_type(&self) -> Option<&dyn VirtualMethods> {
159        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
160    }
161
162    fn attribute_mutated(
163        &self,
164        cx: &mut js::context::JSContext,
165        attr: &Attr,
166        mutation: AttributeMutation,
167    ) {
168        self.super_type()
169            .unwrap()
170            .attribute_mutated(cx, attr, mutation);
171        match *attr.local_name() {
172            local_name!("data") => {
173                if let AttributeMutation::Set(..) = mutation {
174                    self.process_data_url();
175                }
176            },
177            local_name!("form") => {
178                self.form_attribute_mutated(mutation, CanGc::from_cx(cx));
179            },
180            _ => {},
181        }
182    }
183}
184
185impl FormControl for HTMLObjectElement {
186    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
187        self.form_owner.get()
188    }
189
190    fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
191        self.form_owner.set(form);
192    }
193
194    fn to_element(&self) -> &Element {
195        self.upcast::<Element>()
196    }
197}