use core::ops::Range;
use crate::unescaped::Unescaped;
#[derive(Clone)]
pub struct XmlText<'xml> {
pub(crate) xml_text: &'xml str,
pub(crate) range: Range<usize>,
pub(crate) content_end: usize,
}
impl<'xml> XmlText<'xml> {
pub(crate) fn new(xml_text: &'xml str, range: Range<usize>, content_end: usize) -> Self {
XmlText {
xml_text,
range,
content_end,
}
}
#[must_use]
pub fn text(&self) -> &'xml str {
let s = &self.xml_text[self.range.clone()];
s.strip_prefix("<![CDATA[")
.and_then(|r| r.strip_suffix("]]>"))
.unwrap_or(s)
}
#[must_use]
pub fn is_cdata(&self) -> bool {
self.xml_text[self.range.clone()].starts_with("<![CDATA[")
}
#[must_use]
pub fn unescaped(&self) -> Unescaped<'xml> {
if self.is_cdata() {
Unescaped::from_cdata(self.text())
} else {
Unescaped::from_text(self.text())
}
}
#[must_use]
pub fn range(&self) -> Range<usize> {
self.range.clone()
}
#[must_use]
pub fn next_sibling(&self) -> Option<crate::body::BodyNode<'xml>> {
crate::helpers::next_body_sibling(self.xml_text, self.range.end, self.content_end)
}
}