Add htmlEscape and htmlUnescape filters

These filters use the html_escape crate (inspired by the use of the percent_encoding crate) to escape and unescape html.
This commit is contained in:
Luke Mondy 2022-12-06 09:30:28 +11:00
parent 7289930c7b
commit db486f9695
No known key found for this signature in database
6 changed files with 68 additions and 0 deletions

16
Cargo.lock generated
View File

@ -495,6 +495,15 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0"
[[package]]
name = "html-escape"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15315cfa9503e9aa85a477138eff76a1b203a430703548052c330b69d8d8c205"
dependencies = [
"utf8-width",
]
[[package]]
name = "hurl"
version = "2.0.0-SNAPSHOT"
@ -512,6 +521,7 @@ dependencies = [
"glob",
"hex",
"hex-literal",
"html-escape",
"hurl_core",
"indexmap",
"libflate",
@ -1182,6 +1192,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "utf8-width"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1"
[[package]]
name = "vcpkg"
version = "0.2.15"

View File

@ -29,6 +29,7 @@ float-cmp = "0.9.0"
glob = "0.3.0"
hex = "0.4.3"
hex-literal = "0.3.4"
html-escape = "0.2.12"
hurl_core = { version = "2.0.0-SNAPSHOT", path = "../hurl_core" }
indexmap = "1.9.2"
libflate = "1.2.0"

View File

@ -47,6 +47,8 @@ fn eval_filter(
FilterValue::Count {} => eval_count(value, &filter.source_info),
FilterValue::UrlEncode { .. } => eval_url_encode(value, &filter.source_info),
FilterValue::UrlDecode { .. } => eval_url_decode(value, &filter.source_info),
FilterValue::HtmlEscape { .. } => eval_html_encode(value, &filter.source_info),
FilterValue::HtmlUnescape { .. } => eval_html_decode(value, &filter.source_info),
FilterValue::ToInt { .. } => eval_to_int(value, &filter.source_info),
}
}
@ -153,6 +155,35 @@ fn eval_url_decode(value: &Value, source_info: &SourceInfo) -> Result<Value, Err
}
}
fn eval_html_encode(value: &Value, source_info: &SourceInfo) -> Result<Value, Error> {
match value {
Value::String(value) => {
let mut enco = String::from(value);
let encoded = html_escape::encode_text_to_string(value, &mut enco);
Ok(Value::String(encoded.to_string()))
}
v => Err(Error {
source_info: source_info.clone(),
inner: RunnerError::FilterInvalidInput(v._type()),
assert: false,
}),
}
}
fn eval_html_decode(value: &Value, source_info: &SourceInfo) -> Result<Value, Error> {
match value {
Value::String(value) => {
let decoded = html_escape::decode_html_entities(value).to_string();
Ok(Value::String(decoded))
}
v => Err(Error {
source_info: source_info.clone(),
inner: RunnerError::FilterInvalidInput(v._type()),
assert: false,
}),
}
}
fn eval_to_int(value: &Value, source_info: &SourceInfo) -> Result<Value, Error> {
match value {
Value::Integer(v) => Ok(Value::Integer(*v)),

View File

@ -877,5 +877,7 @@ pub enum FilterValue {
},
UrlEncode {},
UrlDecode {},
HtmlEscape {},
HtmlUnescape {},
ToInt {},
}

View File

@ -1083,6 +1083,12 @@ impl Htmlable for FilterValue {
}
FilterValue::UrlEncode {} => "<span class=\"filter-type\">urlEncode</span>".to_string(),
FilterValue::UrlDecode {} => "<span class=\"filter-type\">urlDecode</span>".to_string(),
FilterValue::HtmlEscape {} => {
"<span class=\"filter-type\">htmlEscape</span>".to_string()
}
FilterValue::HtmlUnescape {} => {
"<span class=\"filter-type\">htmlUnescape</span>".to_string()
}
FilterValue::ToInt {} => "<span class=\"filter-type\">toInt</span>".to_string(),
}
}

View File

@ -54,6 +54,8 @@ pub fn filter(reader: &mut Reader) -> ParseResult<'static, Filter> {
regex_filter,
url_encode_filter,
url_decode_filter,
html_encode_filter,
html_decode_filter,
to_int_filter,
],
reader,
@ -98,6 +100,16 @@ fn url_decode_filter(reader: &mut Reader) -> ParseResult<'static, FilterValue> {
Ok(FilterValue::UrlDecode {})
}
fn html_encode_filter(reader: &mut Reader) -> ParseResult<'static, FilterValue> {
try_literal("htmlEscape", reader)?;
Ok(FilterValue::HtmlEscape {})
}
fn html_decode_filter(reader: &mut Reader) -> ParseResult<'static, FilterValue> {
try_literal("htmlUnescape", reader)?;
Ok(FilterValue::HtmlUnescape {})
}
fn to_int_filter(reader: &mut Reader) -> ParseResult<'static, FilterValue> {
try_literal("toInt", reader)?;
Ok(FilterValue::ToInt {})