lewp_css/domain/specified_url.rs
1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5 crate::{
6 parsers::{Parse, ParserContext},
7 CustomParseError,
8 },
9 cssparser::{ParseError, Parser, ToCss},
10 std::fmt,
11};
12
13/// A specified url() value; should be resolved relative to the stylesheet containing it
14#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
15pub struct SpecifiedUrl(pub String);
16
17impl ToCss for SpecifiedUrl {
18 fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
19 dest.write_str("url(")?;
20 dest.write_str(&self.0)?;
21 dest.write_str(")")
22 }
23}
24
25impl Parse for SpecifiedUrl {
26 fn parse<'i, 't>(
27 _context: &ParserContext,
28 input: &mut Parser<'i, 't>,
29 ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
30 let url = input.expect_url()?;
31 Ok(SpecifiedUrl(url.as_ref().to_owned()))
32 }
33}
34
35impl SpecifiedUrl {
36 /// See <https://drafts.csswg.org/css-values/#local-urls>
37 pub fn is_fragment(&self) -> bool {
38 self.0.chars().next().map_or(false, |c| c == '#')
39 }
40}