Manually resolve escaped symbols in xml.

This shouldn't be necesary.
Fixes #154 and fixes #147
This commit is contained in:
elkowar 2021-04-14 12:47:20 +02:00
parent b9c9e67c2e
commit 72457aaa11
3 changed files with 17 additions and 6 deletions

View file

@ -74,7 +74,12 @@ impl WidgetUse {
attrs: elem
.attributes()
.iter()
.map(|attr| (AttrName(attr.name().to_owned()), AttrValue::parse_string(attr.value())))
.map(|attr| {
(
AttrName(attr.name().to_owned()),
AttrValue::parse_string(&xml_ext::resolve_escaped_symbols(&attr.value())),
)
})
.collect::<HashMap<_, _>>(),
..WidgetUse::default()
},

View file

@ -54,7 +54,7 @@ impl ScriptVar {
let name = VarName(xml.attr("name")?.to_owned());
let command = xml.only_child()?.as_text()?.text();
if let Ok(interval) = xml.attr("interval") {
let interval = util::parse_duration(interval)?;
let interval = util::parse_duration(&interval)?;
Ok(ScriptVar::Poll(PollScriptVar { name, command, interval }))
} else {
Ok(ScriptVar::Tail(TailScriptVar { name, command }))

View file

@ -11,6 +11,11 @@ macro_rules! with_text_pos_context {
}};
}
/// resolve symbols such as &quot; to replace them with the actual " symbol
pub fn resolve_escaped_symbols(s: &str) -> String {
s.replace("&quot;", "\"").replace("&lt;", "<").replace("&gt;", ">")
}
#[derive(Debug, Clone)]
pub enum XmlNode<'a, 'b> {
Element(XmlElement<'a, 'b>),
@ -58,7 +63,7 @@ fn get_text_from_text_range(s: &str, (start_pos, end_pos): (TextPos, TextPos)) -
if let Some(last_line) = code_text.last_mut() {
*last_line = last_line.split_at(end_pos.col as usize - 1).0;
}
code_text.join("\n")
resolve_escaped_symbols(&code_text.join("\n"))
}
impl<'a, 'b> XmlNode<'a, 'b> {
@ -71,7 +76,7 @@ impl<'a, 'b> XmlNode<'a, 'b> {
}
pub fn as_text_or_sourcecode(&self) -> String {
self.as_text().map(|c| c.text()).unwrap_or_else(|_| self.get_sourcecode())
self.as_text().map(|c| resolve_escaped_symbols(&c.text())).unwrap_or_else(|_| self.get_sourcecode())
}
pub fn as_text(&self) -> Result<&XmlText<'a, 'b>> {
@ -118,7 +123,7 @@ impl<'a, 'b> fmt::Display for XmlText<'a, 'b> {
impl<'a, 'b> XmlText<'a, 'b> {
pub fn text(&self) -> String {
self.0.text().unwrap_or_default().trim_lines().trim_matches('\n').to_owned()
self.0.text().map(resolve_escaped_symbols).unwrap_or_default().trim_lines().trim_matches('\n').to_owned()
}
pub fn text_pos(&self) -> TextPos {
@ -181,10 +186,11 @@ impl<'a, 'b> XmlElement<'a, 'b> {
self.0.attributes()
}
pub fn attr(&self, key: &str) -> Result<&str> {
pub fn attr(&self, key: &str) -> Result<String> {
with_text_pos_context! { self =>
self.0
.attribute(key)
.map(resolve_escaped_symbols)
.with_context(|| anyhow!("'{}' missing attribute '{}'", self.as_tag_string(), key))?
}
}