style/stylesheets/
namespace_rule.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
5//! The `@namespace` at-rule.
6
7use crate::shared_lock::{SharedRwLockReadGuard, ToCssWithGuard};
8use crate::{Namespace, Prefix};
9use cssparser::SourceLocation;
10use std::fmt::{self, Write};
11use style_traits::{CssStringWriter, CssWriter, ToCss};
12
13/// A `@namespace` rule.
14#[derive(Clone, Debug, PartialEq, ToShmem)]
15#[allow(missing_docs)]
16pub struct NamespaceRule {
17    /// The namespace prefix, and `None` if it's the default Namespace
18    pub prefix: Option<Prefix>,
19    /// The actual namespace url.
20    pub url: Namespace,
21    /// The source location this rule was found at.
22    pub source_location: SourceLocation,
23}
24
25impl ToCssWithGuard for NamespaceRule {
26    // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSNamespaceRule
27    fn to_css(
28        &self,
29        _guard: &SharedRwLockReadGuard,
30        dest_str: &mut CssStringWriter,
31    ) -> fmt::Result {
32        let mut dest = CssWriter::new(dest_str);
33        dest.write_str("@namespace ")?;
34        if let Some(ref prefix) = self.prefix {
35            prefix.to_css(&mut dest)?;
36            dest.write_char(' ')?;
37        }
38        dest.write_str("url(")?;
39        self.url.to_string().to_css(&mut dest)?;
40        dest.write_str(");")
41    }
42}