Skip to main content

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