mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
Merge b7970f6386
into b7afe6b236
This commit is contained in:
commit
9c58aea7df
@ -13,7 +13,7 @@ pub struct EscapeConfig {
|
||||
pub fn escape(input string, config EscapeConfig) string {
|
||||
tag_free_input := input.replace_each(['&', '&', '<', '<', '>', '>'])
|
||||
return if config.quote {
|
||||
tag_free_input.replace_each(['"', '"', "'", '''])
|
||||
tag_free_input.replace_each(['"', '"', "'", '''])
|
||||
} else {
|
||||
tag_free_input
|
||||
}
|
||||
|
@ -5,15 +5,15 @@ fn test_escape_html() {
|
||||
assert html.escape('No change') == 'No change'
|
||||
assert html.escape('<b>Bold text</b>') == '<b>Bold text</b>'
|
||||
assert html.escape('<img />') == '<img />'
|
||||
assert html.escape("' onmouseover='alert(1)'") == '' onmouseover='alert(1)''
|
||||
assert html.escape("<a href='http://www.example.com'>link</a>") == '<a href='http://www.example.com'>link</a>'
|
||||
assert html.escape("<script>alert('hello');</script>") == '<script>alert('hello');</script>'
|
||||
assert html.escape("' onmouseover='alert(1)'") == '' onmouseover='alert(1)''
|
||||
assert html.escape("<a href='http://www.example.com'>link</a>") == '<a href='http://www.example.com'>link</a>'
|
||||
assert html.escape("<script>alert('hello');</script>") == '<script>alert('hello');</script>'
|
||||
// Cases obtained from:
|
||||
// https://github.com/apache/commons-lang/blob/master/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java
|
||||
assert html.escape('plain text') == 'plain text'
|
||||
assert html.escape('') == ''
|
||||
assert html.escape('bread & butter') == 'bread & butter'
|
||||
assert html.escape('"bread" & butter') == '"bread" & butter'
|
||||
assert html.escape('"bread" & butter') == '"bread" & butter'
|
||||
assert html.escape('greater than >') == 'greater than >'
|
||||
assert html.escape('< less than') == '< less than'
|
||||
// Leave accents as-is
|
||||
|
22
vlib/encoding/html/unescape.v
Normal file
22
vlib/encoding/html/unescape.v
Normal file
@ -0,0 +1,22 @@
|
||||
module html
|
||||
|
||||
/*
|
||||
unescape() automatically convert HTML Encoded text to its original text
|
||||
and it doesn't matter the HTML Encoded be in which type (symbolic, decimal, hex)
|
||||
all the types are supported
|
||||
Usage:
|
||||
```
|
||||
import html
|
||||
fn main() {
|
||||
text := "Hello, 'World'"
|
||||
decode := html.unescape(text)
|
||||
println(decode)
|
||||
}
|
||||
```
|
||||
*/
|
||||
pub fn unescape(input string) string {
|
||||
mut escape_to_text := ['&', '&', '<', '<', '>', '>', ''', "'", '"', '"',
|
||||
'&', '&', '<', '<', '>', '>', ''', "'", '"', '"', '&', '&', '<',
|
||||
'<', '>', '>', ''', "'", '"', '"']
|
||||
return input.replace_each(escape_to_text)
|
||||
}
|
22
vlib/encoding/html/unescape_test.v
Normal file
22
vlib/encoding/html/unescape_test.v
Normal file
@ -0,0 +1,22 @@
|
||||
import encoding.html
|
||||
|
||||
fn test_unescape_html() {
|
||||
assert html.unescape('<>&') == '<>&'
|
||||
assert html.unescape('No change') == 'No change'
|
||||
assert html.unescape('<b>Bold text</b>') == '<b>Bold text</b>'
|
||||
assert html.unescape('<img />') == '<img />'
|
||||
assert html.unescape('' onmouseover='alert(1)'') == "' onmouseover='alert(1)'"
|
||||
assert html.unescape('<a href='http://www.example.com'>link</a>') == "<a href='http://www.example.com'>link</a>"
|
||||
assert html.unescape('<script>alert('hello');</script>') == "<script>alert('hello');</script>"
|
||||
// Cases obtained from:
|
||||
// https://github.com/apache/commons-lang/blob/master/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java
|
||||
assert html.unescape('plain text') == 'plain text'
|
||||
assert html.unescape('') == ''
|
||||
assert html.unescape('bread & butter') == 'bread & butter'
|
||||
assert html.unescape('"bread" & butter') == '"bread" & butter'
|
||||
assert html.unescape('greater than >') == 'greater than >'
|
||||
assert html.unescape('< less than') == '< less than'
|
||||
// Leave accents as-is
|
||||
assert html.escape('café') == 'café'
|
||||
assert html.escape('<p>façade</p>') == '<p>façade</p>'
|
||||
}
|
Loading…
Reference in New Issue
Block a user