Skip to content

Commit

Permalink
Add htmlEscape and htmlUnescape filters
Browse files Browse the repository at this point in the history
These filters use the html_escape crate (inspired by the use of the percent_encoding crate) to escape and unescape html.
  • Loading branch information
ad8lmondy committed Dec 6, 2022
1 parent 7289930 commit db486f9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/hurl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions packages/hurl/src/runner/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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)),
Expand Down
2 changes: 2 additions & 0 deletions packages/hurl_core/src/ast/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,5 +877,7 @@ pub enum FilterValue {
},
UrlEncode {},
UrlDecode {},
HtmlEscape {},
HtmlUnescape {},
ToInt {},
}
6 changes: 6 additions & 0 deletions packages/hurl_core/src/format/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/hurl_core/src/parser/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {})
Expand Down

0 comments on commit db486f9

Please sign in to comment.