Skip to content

Commit

Permalink
Implement html_escape and html_unescape.
Browse files Browse the repository at this point in the history
This PR removes import of html-escape crate.
  • Loading branch information
jcamiel committed Dec 12, 2022
1 parent 93ad62d commit 4514a7d
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 4 deletions.
66 changes: 66 additions & 0 deletions packages/hurl/src/html/escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2022 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

/// Replaces special characters "&", "<" and ">" to HTML-safe sequences.
///
/// Both double quote (") and single quote (') characters are also
/// translated.
///
/// # Examples
///
/// ```
/// use hurl::html;
///
/// let output = html::html_escape("<foo>");
/// assert_eq!(output, "&lt;foo&gt;")
/// ```
pub fn html_escape(text: &str) -> String {
let mut output = String::new();
for c in text.chars() {
match c {
'&' => output.push_str("&amp;"),
'<' => output.push_str("&lt;"),
'>' => output.push_str("&gt;"),
'"' => output.push_str("&quot;"),
'\'' => output.push_str("&#x27;"),
_ => output.push(c),
}
}
output
}

#[cfg(test)]
mod tests {
use super::html_escape;

#[test]
pub fn eval_html_escape() {
let tests = vec![
("foo", "foo"),
("<tag>", "&lt;tag&gt;"),
("foo & bar", "foo &amp; bar"),
(
"string with double quote: \"baz\"",
"string with double quote: &quot;baz&quot;",
),
];
for (input, output) in tests.iter() {
assert_eq!(html_escape(input), output.to_string())
}
}
}
22 changes: 22 additions & 0 deletions packages/hurl/src/html/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2022 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
mod escape;
mod unescape;

pub use self::escape::html_escape;
pub use self::unescape::html_unescape;
22 changes: 22 additions & 0 deletions packages/hurl/src/html/unescape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2022 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use html_escape;

pub fn html_unescape(text: &str) -> String {
html_escape::decode_html_entities(text).to_string()
}
1 change: 1 addition & 0 deletions packages/hurl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#![cfg_attr(feature = "strict", deny(warnings))]

pub mod cli;
pub mod html;
pub mod http;
pub mod json;
pub mod jsonpath;
Expand Down
33 changes: 29 additions & 4 deletions packages/hurl/src/runner/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*
*/
use crate::html;
use crate::runner::template::eval_template;
use crate::runner::{Error, RunnerError, Value};
use hurl_core::ast::{Filter, FilterValue, RegexValue, SourceInfo};
Expand Down Expand Up @@ -158,9 +159,8 @@ 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()))
let encoded = html::html_escape(value);
Ok(Value::String(encoded))
}
v => Err(Error {
source_info: source_info.clone(),
Expand All @@ -173,7 +173,7 @@ fn eval_html_encode(value: &Value, source_info: &SourceInfo) -> Result<Value, Er
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();
let decoded = html::html_unescape(value);
Ok(Value::String(decoded))
}
v => Err(Error {
Expand Down Expand Up @@ -418,4 +418,29 @@ pub mod tests {
RunnerError::FilterInvalidInput("bool <true>".to_string())
);
}

#[test]
pub fn eval_filter_html_escape() {
let variables = HashMap::new();
let filter = Filter {
source_info: SourceInfo::new(1, 1, 1, 1),
value: FilterValue::HtmlEscape {},
};

let tests = vec![
("foo", "foo"),
("<tag>", "&lt;tag&gt;"),
("foo & bar", "foo &amp; bar"),
(
"string with double quote: \"baz\"",
"string with double quote: &quot;baz&quot;",
),
];
for (input, output) in tests.iter() {
assert_eq!(
eval_filter(&filter, &Value::String(input.to_string()), &variables).unwrap(),
Value::String(output.to_string())
);
}
}
}

0 comments on commit 4514a7d

Please sign in to comment.