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        local_name: LocalName,
56        prefix: Option<Prefix>,
57        document: &Document,
58        proto: Option<HandleObject>,
59        can_gc: CanGc,
60    ) -> DomRoot<HTMLObjectElement> {
61        Node::reflect_node_with_proto(
62            Box::new(HTMLObjectElement::new_inherited(
63                local_name, prefix, document,
64            )),
65            document,
66            proto,
67            can_gc,
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-fae-form>
100    fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
101        self.form_owner()
102    }
103
104    /// <https://html.spec.whatwg.org/multipage/#dom-cva-willvalidate>
105    fn WillValidate(&self) -> bool {
106        self.is_instance_validatable()
107    }
108
109    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validity>
110    fn Validity(&self, can_gc: CanGc) -> DomRoot<ValidityState> {
111        self.validity_state(can_gc)
112    }
113
114    /// <https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity>
115    fn CheckValidity(&self, cx: &mut JSContext) -> bool {
116        self.check_validity(cx)
117    }
118
119    /// <https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity>
120    fn ReportValidity(&self, cx: &mut JSContext) -> bool {
121        self.report_validity(cx)
122    }
123
124    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage>
125    fn ValidationMessage(&self) -> DOMString {
126        self.validation_message()
127    }
128
129    /// <https://html.spec.whatwg.org/multipage/#dom-cva-setcustomvalidity>
130    fn SetCustomValidity(&self, error: DOMString, can_gc: CanGc) {
131        self.validity_state(can_gc).set_custom_error_message(error);
132    }
133}
134
135impl Validatable for HTMLObjectElement {
136    fn as_element(&self) -> &Element {
137        self.upcast()
138    }
139
140    fn validity_state(&self, can_gc: CanGc) -> DomRoot<ValidityState> {
141        self.validity_state
142            .or_init(|| ValidityState::new(&self.owner_window(), self.upcast(), can_gc))
143    }
144
145    fn is_instance_validatable(&self) -> bool {
146        // https://html.spec.whatwg.org/multipage/#the-object-element%3Abarred-from-constraint-validation
147        false
148    }
149}
150
151impl VirtualMethods for HTMLObjectElement {
152    fn super_type(&self) -> Option<&dyn VirtualMethods> {
153        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
154    }
155
156    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
157        self.super_type()
158            .unwrap()
159            .attribute_mutated(attr, mutation, can_gc);
160        match *attr.local_name() {
161            local_name!("data") => {
162                if let AttributeMutation::Set(..) = mutation {
163                    self.process_data_url();
164                }
165            },
166            local_name!("form") => {
167                self.form_attribute_mutated(mutation, can_gc);
168            },
169            _ => {},
170        }
171    }
172}
173
174impl FormControl for HTMLObjectElement {
175    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
176        self.form_owner.get()
177    }
178
179    fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
180        self.form_owner.set(form);
181    }
182
183    fn to_element(&self) -> &Element {
184        self.upcast::<Element>()
185    }
186}