use std::sync::Arc;
use icu_locale_core::Locale;
use super::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ResolvedLanguage {
Unresolved,
Resolved(ContentLanguage),
}
impl ResolvedLanguage {
pub(crate) fn from_html_attribute(value: &str) -> Self {
Self::Resolved(ContentLanguage::from_html_attribute(value))
}
pub(crate) fn from_computed(value: &ContentLanguage) -> Self {
Self::Resolved(value.clone())
}
pub(crate) fn as_computed_language(&self) -> ContentLanguage {
match self {
Self::Resolved(language) => language.clone(),
Self::Unresolved => ContentLanguage::Unknown,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ElementSignature {
pub(crate) selector: ElementSiblingSignature,
pub sibling_index: Option<usize>,
pub sibling_signatures: ElementSiblingSignatureList,
pub html_direction: Option<Direction>,
pub resolved_direction: Option<Direction>,
pub resolved_language: ResolvedLanguage,
pub(crate) selected_image_dimensions: Option<crate::dom::ImageDimensionAttributes>,
}
impl std::ops::Deref for ElementSignature {
type Target = ElementSelectorSnapshot;
fn deref(&self) -> &Self::Target {
&self.selector
}
}
impl std::ops::DerefMut for ElementSignature {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.selector
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) enum ContentLanguage {
#[default]
Unknown,
Tagged(LanguageTag),
}
impl ContentLanguage {
pub(crate) fn from_html_attribute(value: &str) -> Self {
let value = value.trim();
if value.is_empty() {
Self::Unknown
} else {
Self::Tagged(LanguageTag::new(value))
}
}
pub(crate) fn as_deref(&self) -> Option<&str> {
match self {
Self::Unknown => None,
Self::Tagged(tag) => tag.locale().map(|_| tag.as_str()),
}
}
#[cfg(test)]
pub(crate) fn shares_tag_storage_with(&self, other: &Self) -> bool {
match (self, other) {
(Self::Unknown, Self::Unknown) => true,
(Self::Tagged(left), Self::Tagged(right)) => Arc::ptr_eq(&left.source, &right.source),
_ => false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LanguageTag {
source: Arc<str>,
locale: Option<Locale>,
}
impl LanguageTag {
pub(crate) fn new(value: impl AsRef<str>) -> Self {
let source: Arc<str> = Arc::from(value.as_ref());
let locale = (!source.contains('_'))
.then(|| source.parse::<Locale>().ok())
.flatten();
Self { source, locale }
}
pub(crate) fn as_str(&self) -> &str {
&self.source
}
pub(crate) fn locale(&self) -> Option<&Locale> {
self.locale.as_ref()
}
}