|
|
|
@ -88,6 +88,23 @@ fn html_escape(inp: &str) -> String {
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn javascript_escape(inp: &str) -> String {
|
|
|
|
|
// Adding 10% space from the original to avoid re-allocations by
|
|
|
|
|
// leaving room for escaped sequences.
|
|
|
|
|
let mut output = String::with_capacity(((inp.len() as f64) * 1.1) as usize);
|
|
|
|
|
inp.chars().for_each(|c| match c {
|
|
|
|
|
'"' => output.push_str(r#"\""#),
|
|
|
|
|
'\'' => output.push_str(r#"\'"#),
|
|
|
|
|
'\t' => output.push_str(r#"\t"#),
|
|
|
|
|
'\x0C' => output.push_str(r#"\f"#),
|
|
|
|
|
'\n' => output.push_str(r#"\n"#),
|
|
|
|
|
'\r' => output.push_str(r#"\r"#),
|
|
|
|
|
'\\' => output.push_str(r#"\\"#),
|
|
|
|
|
_ => output.push(c),
|
|
|
|
|
});
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn apply_filter(
|
|
|
|
|
json_value: &serde_json::Value,
|
|
|
|
|
filter: &Filter,
|
|
|
|
@ -114,7 +131,13 @@ fn apply_filter(
|
|
|
|
|
(_, Filter::JsonStringify) => {
|
|
|
|
|
Ok(serde_json::Value::String(json_value.to_string()))
|
|
|
|
|
}
|
|
|
|
|
// TODO: javascript string escape
|
|
|
|
|
// Javascript escape filter
|
|
|
|
|
(serde_json::Value::String(string), Filter::JavascriptStringEncode) => {
|
|
|
|
|
Ok(serde_json::Value::String(javascript_escape(string)))
|
|
|
|
|
}
|
|
|
|
|
(_, Filter::JavascriptStringEncode) => Ok(serde_json::Value::String(javascript_escape(
|
|
|
|
|
&json_value.render(&Vec::new())?,
|
|
|
|
|
))),
|
|
|
|
|
// TODO: EncodeUri
|
|
|
|
|
// TODO: EncodeUriComponent
|
|
|
|
|
_ => panic!("Unimplemented"),
|
|
|
|
|