#[allow(unused_imports, deprecated)]
use std::ascii::AsciiExt;
use std::str::FromStr;
use language_tags::LanguageTag;
use crate::accept_language::{LanguageTags, UserLanguage};
use crate::error::*;
use crate::rsvg_log;
use crate::session::Session;
static IMPLEMENTED_EXTENSIONS: &[&str] = &[];
#[derive(Debug, PartialEq)]
pub struct RequiredExtensions(pub bool);
impl RequiredExtensions {
pub fn from_attribute(s: &str) -> RequiredExtensions {
RequiredExtensions(
s.split_whitespace()
.all(|f| IMPLEMENTED_EXTENSIONS.binary_search(&f).is_ok()),
)
}
pub fn eval(&self) -> bool {
self.0
}
}
static IMPLEMENTED_FEATURES: &[&str] = &[
"http://www.w3.org/TR/SVG11/feature#BasicFilter",
"http://www.w3.org/TR/SVG11/feature#BasicGraphicsAttribute",
"http://www.w3.org/TR/SVG11/feature#BasicPaintAttribute",
"http://www.w3.org/TR/SVG11/feature#BasicStructure",
"http://www.w3.org/TR/SVG11/feature#BasicText",
"http://www.w3.org/TR/SVG11/feature#ConditionalProcessing",
"http://www.w3.org/TR/SVG11/feature#ContainerAttribute",
"http://www.w3.org/TR/SVG11/feature#Filter",
"http://www.w3.org/TR/SVG11/feature#Gradient",
"http://www.w3.org/TR/SVG11/feature#Image",
"http://www.w3.org/TR/SVG11/feature#Marker",
"http://www.w3.org/TR/SVG11/feature#Mask",
"http://www.w3.org/TR/SVG11/feature#OpacityAttribute",
"http://www.w3.org/TR/SVG11/feature#Pattern",
"http://www.w3.org/TR/SVG11/feature#SVG",
"http://www.w3.org/TR/SVG11/feature#SVG-static",
"http://www.w3.org/TR/SVG11/feature#Shape",
"http://www.w3.org/TR/SVG11/feature#Structure",
"http://www.w3.org/TR/SVG11/feature#Style",
"http://www.w3.org/TR/SVG11/feature#View",
"org.w3c.svg.static", ];
#[derive(Debug, PartialEq)]
pub struct RequiredFeatures(pub bool);
impl RequiredFeatures {
pub fn from_attribute(s: &str) -> RequiredFeatures {
RequiredFeatures(
s.split_whitespace()
.all(|f| IMPLEMENTED_FEATURES.binary_search(&f).is_ok()),
)
}
pub fn eval(&self) -> bool {
self.0
}
}
#[derive(Debug, PartialEq)]
pub enum SystemLanguage {
Valid(LanguageTags),
Invalid,
}
impl SystemLanguage {
pub fn from_attribute(s: &str, session: &Session) -> SystemLanguage {
let attribute_tags = s
.split(',')
.map(str::trim)
.map(|s| {
LanguageTag::from_str(s).map_err(|e| {
ValueErrorKind::parse_error(&format!("invalid language tag \"{s}\": {e}"))
})
})
.collect::<Result<Vec<LanguageTag>, _>>();
match attribute_tags {
Ok(tags) => SystemLanguage::Valid(LanguageTags::from(tags)),
Err(e) => {
rsvg_log!(
session,
"ignoring systemLanguage attribute with invalid value: {}",
e
);
SystemLanguage::Invalid
}
}
}
pub fn eval(&self, user_language: &UserLanguage) -> bool {
match *self {
SystemLanguage::Valid(ref tags) => user_language.any_matches(tags),
SystemLanguage::Invalid => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use locale_config::Locale;
#[test]
fn required_extensions() {
assert_eq!(
RequiredExtensions::from_attribute("http://test.org/NotExisting/1.0"),
RequiredExtensions(false)
);
}
#[test]
fn required_features() {
assert_eq!(
RequiredFeatures::from_attribute("http://www.w3.org/TR/SVG11/feature#NotExisting"),
RequiredFeatures(false)
);
assert_eq!(
RequiredFeatures::from_attribute("http://www.w3.org/TR/SVG11/feature#BasicFilter"),
RequiredFeatures(true)
);
assert_eq!(
RequiredFeatures::from_attribute(
"http://www.w3.org/TR/SVG11/feature#BasicFilter \
http://www.w3.org/TR/SVG11/feature#NotExisting",
),
RequiredFeatures(false)
);
assert_eq!(
RequiredFeatures::from_attribute(
"http://www.w3.org/TR/SVG11/feature#BasicFilter \
http://www.w3.org/TR/SVG11/feature#BasicText",
),
RequiredFeatures(true)
);
}
#[test]
fn system_language() {
let session = Session::new_for_test_suite();
let locale = Locale::new("de,en-US").unwrap();
let user_language = UserLanguage::LanguageTags(LanguageTags::from_locale(&locale).unwrap());
assert!(matches!(
SystemLanguage::from_attribute("", &session),
SystemLanguage::Invalid
));
assert!(matches!(
SystemLanguage::from_attribute("12345", &session),
SystemLanguage::Invalid
));
assert!(!SystemLanguage::from_attribute("fr", &session).eval(&user_language));
assert!(!SystemLanguage::from_attribute("en", &session).eval(&user_language));
assert!(SystemLanguage::from_attribute("de", &session).eval(&user_language));
assert!(SystemLanguage::from_attribute("en-US", &session).eval(&user_language));
assert!(!SystemLanguage::from_attribute("en-GB", &session).eval(&user_language));
assert!(SystemLanguage::from_attribute("DE", &session).eval(&user_language));
assert!(SystemLanguage::from_attribute("de-LU", &session).eval(&user_language));
assert!(SystemLanguage::from_attribute("fr, de", &session).eval(&user_language));
}
}