Implement javascript string escaping.
This commit is contained in:
parent
f3fef17d4b
commit
8d2728a767
@ -11,5 +11,5 @@
|
|||||||
"object": {
|
"object": {
|
||||||
"foo": "bar"
|
"foo": "bar"
|
||||||
},
|
},
|
||||||
"special_characters": "<>&\"'"
|
"special_characters": "<>xx\b&\"'\t\f\n\r\\"
|
||||||
}
|
}
|
||||||
|
@ -20,3 +20,9 @@ Object html escaping disabled: {object|s}{~n}
|
|||||||
Object stringified: {object|js}{~n}
|
Object stringified: {object|js}{~n}
|
||||||
Object stringified and parsed: {object|js|jp}{~n}
|
Object stringified and parsed: {object|js|jp}{~n}
|
||||||
Object stringified, html escaping disabled, parsed, stringified, and html escaped: {object|js|s|jp|js|h}{~n}
|
Object stringified, html escaping disabled, parsed, stringified, and html escaped: {object|js|s|jp|js|h}{~n}
|
||||||
|
|
||||||
|
Special characters: {special_characters}{~n}
|
||||||
|
Special characters html escaping disabled and javascript escaped: {special_characters|s|j}{~n}
|
||||||
|
Special characters javascript escaped and html escaping disabled: {special_characters|j|s}{~n}
|
||||||
|
Special characters javascript escaped once: {special_characters|j}{~n}
|
||||||
|
Special characters javascript escaped twice: {special_characters|j|j}{~n}
|
||||||
|
25
src/bin.rs
25
src/bin.rs
@ -88,6 +88,23 @@ fn html_escape(inp: &str) -> String {
|
|||||||
output
|
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(
|
fn apply_filter(
|
||||||
json_value: &serde_json::Value,
|
json_value: &serde_json::Value,
|
||||||
filter: &Filter,
|
filter: &Filter,
|
||||||
@ -114,7 +131,13 @@ fn apply_filter(
|
|||||||
(_, Filter::JsonStringify) => {
|
(_, Filter::JsonStringify) => {
|
||||||
Ok(serde_json::Value::String(json_value.to_string()))
|
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: EncodeUri
|
||||||
// TODO: EncodeUriComponent
|
// TODO: EncodeUriComponent
|
||||||
_ => panic!("Unimplemented"),
|
_ => panic!("Unimplemented"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user