-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement html_escape and html_unescape.
This PR removes import of html-escape crate.
- Loading branch information
Showing
5 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "<foo>") | ||
/// ``` | ||
pub fn html_escape(text: &str) -> String { | ||
let mut output = String::new(); | ||
for c in text.chars() { | ||
match c { | ||
'&' => output.push_str("&"), | ||
'<' => output.push_str("<"), | ||
'>' => output.push_str(">"), | ||
'"' => output.push_str("""), | ||
'\'' => output.push_str("'"), | ||
_ => output.push(c), | ||
} | ||
} | ||
output | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::html_escape; | ||
|
||
#[test] | ||
pub fn eval_html_escape() { | ||
let tests = vec![ | ||
("foo", "foo"), | ||
("<tag>", "<tag>"), | ||
("foo & bar", "foo & bar"), | ||
( | ||
"string with double quote: \"baz\"", | ||
"string with double quote: "baz"", | ||
), | ||
]; | ||
for (input, output) in tests.iter() { | ||
assert_eq!(html_escape(input), output.to_string()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters