Skip to content

Commit

Permalink
fix(templ): escape closing curly braces
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Nov 7, 2024
1 parent 56989aa commit ef70385
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
8 changes: 5 additions & 3 deletions crates/diff-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ fn pre_diff_element_massaging_handlers<'a>(
el.remove_attribute("data-flaw");
Ok(())
}),
element!("*.page-not-created", |el| {
el.remove_attribute("class");
Ok(())
}),
// remove ids from notecards, example-headers, code-examples
element!("div.notecard, div.example-header, div.code-example", |el| {
el.remove_attribute("id");
Expand Down Expand Up @@ -734,15 +738,13 @@ fn main() -> Result<(), anyhow::Error> {
.as_ref()
.map(|(l, r)| (l.as_str(), r.as_str()))
.unwrap_or((left, right));
let broken_link = r#" class="page-not-created" title="This is a link to an unwritten page""#;
let left = left.replace(broken_link, "");
if left == right {
println!("only broken links differ");
same.fetch_add(1, Relaxed);
return None;
}
if arg.inline {
println!("{}", diff_words(&left, right));
println!("{}", diff_words(left, right));
}
Some((k.clone(), format!(
r#"<li><span>{k}</span><div class="a">{}</div><div class="b">{}</div></li>"#,
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/helpers/css_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub fn css_inital(locale: Locale) -> Result<String, DocError> {
)
}

fn write_missing(out: &mut String, locale: Locale) -> Result<(), DocError> {
pub fn write_missing(out: &mut String, locale: Locale) -> Result<(), DocError> {
let missing = l10n_json_data("CSS", "missing", locale)?;
Ok(write!(out, "<span style=\"color:red;\">{missing}</span>")?)
}
Expand Down
18 changes: 16 additions & 2 deletions crates/rari-doc/src/templ/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ pub(crate) fn decode_ref(input: &str, templs: &[String]) -> Result<String, DocEr
for (i, sub_frag) in frag.splitn(2, DELIM_END).enumerate() {
if i == 0 && has_ks {
frags.push(sub_frag);
//decode_macro(sub_frag, &mut decoded)?;
} else {
//decoded.push_str(strip_escape_residues(sub_frag))
frags.push(sub_frag)
}
}
Expand Down Expand Up @@ -161,6 +159,15 @@ pub(crate) fn decode_ref(input: &str, templs: &[String]) -> Result<String, DocEr
fn push_text(out: &mut String, slice: &str) {
let mut last = 0;
for (i, _) in slice.match_indices("\\{") {
push_text_inner(out, &slice[last..i]);
last = i + 1;
}
push_text_inner(out, &slice[last..]);
}

fn push_text_inner(out: &mut String, slice: &str) {
let mut last = 0;
for (i, _) in slice.match_indices("\\}") {
out.push_str(&slice[last..i]);
last = i + 1;
}
Expand All @@ -171,6 +178,13 @@ fn push_text(out: &mut String, slice: &str) {
mod test {
use super::*;

#[test]
fn test_push_text() {
let mut out = String::new();
push_text(&mut out, "foo \\\\{ bar \\\\} \\} 2000");
assert_eq!(out, "foo \\{ bar \\} } 2000")
}

#[test]
fn test_basic() -> Result<(), DocError> {
let env = RariEnv {
Expand Down
14 changes: 5 additions & 9 deletions crates/rari-doc/src/templ/templs/cssinfo.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::fmt::Write;

use rari_templ_func::rari_f;
use rari_utils::concat_strs;
use serde_json::Value;

use crate::error::DocError;
use crate::helpers::css_info::{
css_info_properties, get_css_l10n_for_locale, mdn_data_files, write_computed_output,
css_info_properties, mdn_data_files, write_computed_output, write_missing,
};

#[rari_f]
Expand All @@ -32,15 +31,12 @@ pub fn cssinfo() -> Result<String, DocError> {
};
let props = css_info_properties(at_rule, env.locale, css_info_data)?;

let mut out = String::new();

if props.is_empty() {
return Ok(concat_strs!(
"<span style=\"color:red;\">",
get_css_l10n_for_locale("missing", env.locale),
"</span>"
));
write_missing(&mut out, env.locale)?;
return Ok(out);
}

let mut out = String::new();
out.push_str(r#"<table class="properties"><tbody>"#);
for (name, label) in props {
write!(&mut out, r#"<tr><th scope="row">{label}</th><td>"#)?;
Expand Down
6 changes: 3 additions & 3 deletions crates/rari-doc/src/templ/templs/embeds/livesample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ pub fn live_sample(
}
}
}
if let Some(allowed_features) = allowed_features {
write!(&mut out, r#"allow="{}" "#, allowed_features)?;
}
write!(
&mut out,
r#"src="{}{}{}runner.html?id={}" "#,
Expand All @@ -52,6 +49,9 @@ pub fn live_sample(
if env.url.ends_with('/') { "" } else { "/" },
id
)?;
if let Some(allowed_features) = allowed_features {
write!(&mut out, r#"allow="{}" "#, allowed_features)?;
}
out.push_str(r#"sandbox="allow-same-origin allow-scripts" "#);
out.push_str(r#"loading="lazy"></iframe></div>"#);
Ok(out)
Expand Down

0 comments on commit ef70385

Please sign in to comment.