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    pub(crate) fn new(
37        local_name: LocalName,
38        prefix: Option<Prefix>,
39        document: &Document,
40        proto: Option<HandleObject>,
41        can_gc: CanGc,
42    ) -> DomRoot<HTMLPreElement> {
43        Node::reflect_node_with_proto(
44            Box::new(HTMLPreElement::new_inherited(local_name, prefix, document)),
45            document,
46            proto,
47            can_gc,
48        )
49    }
50}
51
52impl VirtualMethods for HTMLPreElement {
53    fn super_type(&self) -> Option<&dyn VirtualMethods> {
54        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
55    }
56
57    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
58        match *name {
59            local_name!("width") => AttrValue::from_limited_i32(value.into(), 0),
60            _ => self
61                .super_type()
62                .unwrap()
63                .parse_plain_attribute(name, value),
64        }
65    }
66}
67
68impl HTMLPreElementMethods<crate::DomTypeHolder> for HTMLPreElement {
69    // https://html.spec.whatwg.org/multipage/#dom-pre-width
70    make_int_getter!(Width, "width", 0);
71
72    // https://html.spec.whatwg.org/multipage/#dom-pre-width
73    make_int_setter!(SetWidth, "width", 0);
74}