Skip to main content

script/dom/css/
cssstyleproperties.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 js::context::{JSContext, NoGC};
7use script_bindings::reflector::reflect_dom_object;
8use style::properties::{LonghandId, PropertyId, ShorthandId};
9use style::selector_parser::PseudoElement;
10
11use crate::dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods;
12use crate::dom::bindings::codegen::Bindings::CSSStylePropertiesBinding::CSSStylePropertiesMethods;
13use crate::dom::bindings::error::ErrorResult;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::css::cssstyledeclaration::{
17    CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner,
18};
19use crate::dom::window::Window;
20
21/// <https://drafts.csswg.org/cssom/#cssstyleproperties>
22#[dom_struct]
23pub(crate) struct CSSStyleProperties {
24    style_declaration: CSSStyleDeclaration,
25}
26
27macro_rules! css_properties(
28    ( $([$getter:ident, $setter:ident, $id:expr],)* ) => (
29        $(
30            fn $getter(&self, no_gc: &NoGC) -> DOMString {
31                debug_assert!(
32                    $id.enabled_for_all_content(),
33                    "Someone forgot a #[Pref] annotation"
34                );
35                self.style_declaration.get_property_value(no_gc, $id)
36            }
37            fn $setter(&self, cx: &mut JSContext, value: DOMString) -> ErrorResult {
38                debug_assert!(
39                    $id.enabled_for_all_content(),
40                    "Someone forgot a #[Pref] annotation"
41                );
42                self.style_declaration.set_property(cx, $id, value, DOMString::new())
43            }
44        )*
45    );
46);
47
48impl CSSStyleProperties {
49    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
50    pub(crate) fn new(
51        cx: &mut JSContext,
52        global: &Window,
53        owner: CSSStyleOwner,
54        pseudo: Option<PseudoElement>,
55        modification_access: CSSModificationAccess,
56    ) -> DomRoot<CSSStyleProperties> {
57        reflect_dom_object(
58            cx,
59            Box::new(CSSStyleProperties {
60                style_declaration: CSSStyleDeclaration::new_inherited(
61                    owner,
62                    pseudo,
63                    modification_access,
64                ),
65            }),
66            global,
67        )
68    }
69}
70
71impl CSSStylePropertiesMethods<crate::DomTypeHolder> for CSSStyleProperties {
72    /// <https://drafts.csswg.org/cssom/#dom-cssstyleproperties-cssfloat>
73    fn CssFloat(&self, no_gc: &NoGC) -> DOMString {
74        self.style_declaration
75            .get_property_value(no_gc, PropertyId::NonCustom(LonghandId::Float.into()))
76    }
77
78    /// <https://drafts.csswg.org/cssom/#dom-cssstyleproperties-cssfloat>
79    fn SetCssFloat(&self, cx: &mut JSContext, value: DOMString) -> ErrorResult {
80        self.style_declaration.set_property(
81            cx,
82            PropertyId::NonCustom(LonghandId::Float.into()),
83            value,
84            DOMString::new(),
85        )
86    }
87
88    fn IndexedGetter(&self, no_gc: &NoGC, index: u32) -> Option<DOMString> {
89        self.style_declaration.IndexedGetter(no_gc, index)
90    }
91
92    fn Length(&self, cx: &JSContext) -> u32 {
93        self.style_declaration.Length(cx.no_gc())
94    }
95
96    // https://drafts.csswg.org/cssom/#dom-cssstyleproperties-camel-cased-attribute
97    style::css_properties_accessors!(css_properties);
98}