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