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;
18
19#[dom_struct]
20pub(crate) struct HTMLPreElement {
21    htmlelement: HTMLElement,
22}
23
24impl HTMLPreElement {
25    fn new_inherited(
26        local_name: LocalName,
27        prefix: Option<Prefix>,
28        document: &Document,
29    ) -> HTMLPreElement {
30        HTMLPreElement {
31            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
32        }
33    }
34
35    pub(crate) fn new(
36        cx: &mut js::context::JSContext,
37        local_name: LocalName,
38        prefix: Option<Prefix>,
39        document: &Document,
40        proto: Option<HandleObject>,
41    ) -> DomRoot<HTMLPreElement> {
42        Node::reflect_node_with_proto(
43            cx,
44            Box::new(HTMLPreElement::new_inherited(local_name, prefix, document)),
45            document,
46            proto,
47        )
48    }
49}
50
51impl VirtualMethods for HTMLPreElement {
52    fn super_type(&self) -> Option<&dyn VirtualMethods> {
53        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
54    }
55
56    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
57        match *name {
58            local_name!("width") => AttrValue::from_limited_i32(value.into(), 0),
59            _ => self
60                .super_type()
61                .unwrap()
62                .parse_plain_attribute(name, value),
63        }
64    }
65}
66
67impl HTMLPreElementMethods<crate::DomTypeHolder> for HTMLPreElement {
68    // https://html.spec.whatwg.org/multipage/#dom-pre-width
69    make_int_getter!(Width, "width");
70
71    // https://html.spec.whatwg.org/multipage/#dom-pre-width
72    make_int_setter!(SetWidth, "width");
73}