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, ns};
9use js::rust::HandleObject;
10use pixels::RasterImage;
11use servo_arc::Arc;
12
13use crate::dom::attr::Attr;
14use crate::dom::bindings::cell::DomRefCell;
15use crate::dom::bindings::codegen::Bindings::HTMLObjectElementBinding::HTMLObjectElementMethods;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::{DomRoot, MutNullableDom};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::document::Document;
20use crate::dom::element::{AttributeMutation, Element};
21use crate::dom::html::htmlelement::HTMLElement;
22use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
23use crate::dom::node::{Node, NodeTraits};
24use crate::dom::validation::Validatable;
25use crate::dom::validitystate::ValidityState;
26use crate::dom::virtualmethods::VirtualMethods;
27use crate::script_runtime::CanGc;
28
29#[dom_struct]
30pub(crate) struct HTMLObjectElement {
31    htmlelement: HTMLElement,
32    #[ignore_malloc_size_of = "Arc"]
33    #[no_trace]
34    image: DomRefCell<Option<Arc<RasterImage>>>,
35    form_owner: MutNullableDom<HTMLFormElement>,
36    validity_state: MutNullableDom<ValidityState>,
37}
38
39impl HTMLObjectElement {
40    fn new_inherited(
41        local_name: LocalName,
42        prefix: Option<Prefix>,
43        document: &Document,
44    ) -> HTMLObjectElement {
45        HTMLObjectElement {
46            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
47            image: DomRefCell::new(None),
48            form_owner: Default::default(),
49            validity_state: Default::default(),
50        }
51    }
52
53    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
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(&ns!(), &local_name!("type")),
85            element.get_attribute(&ns!(), &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) -> DomRoot<ValidityState> {
111        self.validity_state()
112    }
113
114    // https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity
115    fn CheckValidity(&self, can_gc: CanGc) -> bool {
116        self.check_validity(can_gc)
117    }
118
119    // https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity
120    fn ReportValidity(&self, can_gc: CanGc) -> bool {
121        self.report_validity(can_gc)
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) {
131        self.validity_state().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) -> DomRoot<ValidityState> {
141        self.validity_state
142            .or_init(|| ValidityState::new(&self.owner_window(), self.upcast(), CanGc::note()))
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}