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