lewp_css/domain/selectors/
language_ranges.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 {super::LanguageRange, cssparser::ToCss, std::fmt};
5
6#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
7pub struct LanguageRanges(pub Vec<LanguageRange>);
8
9impl ToCss for LanguageRanges {
10    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
11        if self.0.is_empty() {
12            return Ok(());
13        }
14
15        let mut iterator = self.0.iter();
16        iterator.next().unwrap().to_css(dest)?;
17        for languageRange in iterator {
18            dest.write_char(',')?;
19            languageRange.to_css(dest)?;
20        }
21        Ok(())
22    }
23}
24
25impl LanguageRanges {
26    /// Returns whether the language is matched, as defined by [RFC 4647](<https://tools.ietf.org/html/rfc4647#section-3.3.2>).
27    pub fn matches_language(&self, tag: &str) -> bool {
28        self.0
29            .iter()
30            .any(|languageRange| languageRange.matches_language(tag))
31    }
32}