mod safe;
use super::clone::string_to_owned;
use crate::id_prefix::isolate_ids;
use crate::parsing::parse_boolean;
use crate::settings::WikitextSettings;
use crate::url::normalize_href;
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::fmt::{self, Debug};
use unicase::UniCase;
pub use self::safe::{
BOOLEAN_ATTRIBUTES, SAFE_ATTRIBUTE_PREFIXES, SAFE_ATTRIBUTES, URL_ATTRIBUTES,
is_safe_attribute,
};
#[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
pub struct AttributeMap<'t> {
#[serde(flatten)]
inner: BTreeMap<Cow<'t, str>, Cow<'t, str>>,
}
impl<'t> AttributeMap<'t> {
#[inline]
pub fn new() -> Self {
AttributeMap::default()
}
pub fn from_arguments(arguments: &HashMap<UniCase<&'t str>, Cow<'t, str>>) -> Self {
let inner = arguments
.iter()
.filter(|&(key, _)| is_safe_attribute(*key))
.filter_map(|(key, value)| {
let mut value = Cow::clone(value);
if BOOLEAN_ATTRIBUTES.contains(key)
&& let Ok(boolean_value) = parse_boolean(&value)
{
if boolean_value {
value = cow!("");
} else {
return None;
}
}
if URL_ATTRIBUTES.contains(key) {
let url = normalize_href(&value, None).into_owned();
value = Cow::Owned(url);
}
let key = key.into_inner().to_ascii_lowercase();
Some((Cow::Owned(key), value))
})
.collect();
AttributeMap { inner }
}
pub fn insert(&mut self, attribute: &'t str, value: Cow<'t, str>) -> bool {
let will_insert = is_safe_attribute(UniCase::ascii(attribute));
if will_insert {
self.inner.insert(cow!(attribute), value);
}
will_insert
}
#[inline]
pub fn remove(&mut self, attribute: &str) -> Option<Cow<'t, str>> {
self.inner.remove(attribute)
}
#[inline]
pub fn get(&self) -> &BTreeMap<Cow<'t, str>, Cow<'t, str>> {
&self.inner
}
pub fn isolate_id(&mut self, settings: &WikitextSettings) {
if settings.isolate_user_ids
&& let Some(value) = self.inner.get_mut("id")
{
trace!("Found 'id' attribute, isolating value");
*value = Cow::Owned(isolate_ids(value));
}
}
pub fn to_owned(&self) -> AttributeMap<'static> {
let mut inner = BTreeMap::new();
for (key, value) in self.inner.iter() {
let key = string_to_owned(key);
let value = string_to_owned(value);
inner.insert(key, value);
}
AttributeMap { inner }
}
}
impl Debug for AttributeMap<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}
impl<'t> From<BTreeMap<Cow<'t, str>, Cow<'t, str>>> for AttributeMap<'t> {
#[inline]
fn from(map: BTreeMap<Cow<'t, str>, Cow<'t, str>>) -> AttributeMap<'t> {
AttributeMap { inner: map }
}
}