use std::borrow::{Borrow, Cow};
use html_escape::encode_double_quoted_attribute as escape;
use super::tag::TagOpen;
use crate::arguments::Arguments;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TagBuilder<'a> {
tag: TagOpen<'a, Cow<'a, str>>,
}
impl<'a> TagBuilder<'a> {
pub fn new(name: &'a str) -> Self {
Self {
tag: TagOpen {
name,
arguments: Arguments::default(),
empty: false,
},
}
}
pub fn build(self) -> TagOpen<'a, Cow<'a, str>> {
self.tag
}
pub fn push(&mut self, value: &'a str) -> &mut Self {
self.tag.arguments.push(value.into());
self
}
pub fn escape_and_push(&mut self, value: &'a str) -> &mut Self {
self.tag.arguments.push(escape(value));
self
}
pub fn push_all<I>(&mut self, iter: I) -> &mut Self
where
I: IntoIterator,
I::Item: Borrow<&'a str>,
{
self.tag
.arguments
.extend(iter.into_iter().map(|s| Cow::Borrowed(*s.borrow())));
self
}
pub fn escape_and_push_all<I>(&mut self, iter: I) -> &mut Self
where
I: IntoIterator,
I::Item: Borrow<&'a str>,
{
self.tag
.arguments
.extend(iter.into_iter().map(|s| escape(*s.borrow())));
self
}
pub fn insert(&mut self, name: &'a str, value: &'a str) -> &mut Self {
self.tag.arguments.insert(name, value.into());
self
}
pub fn escape_and_insert(&mut self, name: &'a str, value: &'a str) -> &mut Self {
self.tag.arguments.insert(name, escape(value));
self
}
pub fn insert_all<I, K, V>(&mut self, iter: I) -> &mut Self
where
I: IntoIterator,
I::Item: Borrow<(K, V)>,
K: Borrow<&'a str>,
V: Borrow<&'a str>,
{
self.tag.arguments.extend(iter.into_iter().map(|entry| {
let (k, v) = entry.borrow();
(*k.borrow(), Cow::Borrowed(*v.borrow()))
}));
self
}
pub fn escape_and_insert_all<I, K, V>(&mut self, iter: I) -> &mut Self
where
I: IntoIterator,
I::Item: Borrow<(K, V)>,
K: Borrow<&'a str>,
V: Borrow<&'a str>,
{
self.tag.arguments.extend(iter.into_iter().map(|entry| {
let (k, v) = entry.borrow();
(*k.borrow(), escape(*v.borrow()))
}));
self
}
pub fn set_empty(&mut self, empty: bool) -> &mut Self {
self.tag.empty = empty;
self
}
}