script/dom/html/
htmlpreelement.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 dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, local_name};
7use js::rust::HandleObject;
8use style::attr::AttrValue;
9
10use crate::dom::bindings::codegen::Bindings::HTMLPreElementBinding::HTMLPreElementMethods;
11use crate::dom::bindings::inheritance::Castable;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::bindings::str::DOMString;
14use crate::dom::document::Document;
15use crate::dom::html::htmlelement::HTMLElement;
16use crate::dom::node::Node;
17use crate::dom::virtualmethods::VirtualMethods;
18use crate::script_runtime::CanGc;
19
20#[dom_struct]
21pub(crate) struct HTMLPreElement {
22    htmlelement: HTMLElement,
23}
24
25impl HTMLPreElement {
26    fn new_inherited(
27        local_name: LocalName,
28        prefix: Option<Prefix>,
29        document: &Document,
30    ) -> HTMLPreElement {
31        HTMLPreElement {
32            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
33        }
34    }
35
36    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
37    pub(crate) fn new(
38        local_name: LocalName,
39        prefix: Option<Prefix>,
40        document: &Document,
41        proto: Option<HandleObject>,
42        can_gc: CanGc,
43    ) -> DomRoot<HTMLPreElement> {
44        Node::reflect_node_with_proto(
45            Box::new(HTMLPreElement::new_inherited(local_name, prefix, document)),
46            document,
47            proto,
48            can_gc,
49        )
50    }
51}
52
53impl VirtualMethods for HTMLPreElement {
54    fn super_type(&self) -> Option<&dyn VirtualMethods> {
55        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
56    }
57
58    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
59        match *name {
60            local_name!("width") => AttrValue::from_limited_i32(value.into(), 0),
61            _ => self
62                .super_type()
63                .unwrap()
64                .parse_plain_attribute(name, value),
65        }
66    }
67}
68
69impl HTMLPreElementMethods<crate::DomTypeHolder> for HTMLPreElement {
70    // https://html.spec.whatwg.org/multipage/#dom-pre-width
71    make_int_getter!(Width, "width", 0);
72
73    // https://html.spec.whatwg.org/multipage/#dom-pre-width
74    make_int_setter!(SetWidth, "width", 0);
75}