mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
encoding.html: add escape() function (#16666)
This commit is contained in:
parent
69f7c45bec
commit
b07e447764
20
vlib/encoding/html/escape.v
Normal file
20
vlib/encoding/html/escape.v
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
module html
|
||||||
|
|
||||||
|
[params]
|
||||||
|
pub struct EscapeConfig {
|
||||||
|
quote bool = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// escape converts special characters in the input, specifically "<", ">", and "&"
|
||||||
|
// to HTML-safe sequences. If `quote` is set to true (which is default), quotes in
|
||||||
|
// HTML will also be translated. Both double and single quotes will be affected.
|
||||||
|
// **Note:** escape() supports funky accents by doing nothing about them. V's UTF-8
|
||||||
|
// support through `string` is robust enough to deal with these cases.
|
||||||
|
pub fn escape(input string, config EscapeConfig) string {
|
||||||
|
tag_free_input := input.replace_each(['&', '&', '<', '<', '>', '>'])
|
||||||
|
return if config.quote {
|
||||||
|
tag_free_input.replace_each(['"', '"', "'", '''])
|
||||||
|
} else {
|
||||||
|
tag_free_input
|
||||||
|
}
|
||||||
|
}
|
22
vlib/encoding/html/escape_test.v
Normal file
22
vlib/encoding/html/escape_test.v
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import encoding.html
|
||||||
|
|
||||||
|
fn test_escape_html() {
|
||||||
|
assert html.escape('<>&') == '<>&'
|
||||||
|
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>'
|
||||||
|
// 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('greater than >') == 'greater than >'
|
||||||
|
assert html.escape('< less than') == '< less than'
|
||||||
|
// Leave accents as-is
|
||||||
|
assert html.escape('café') == 'café'
|
||||||
|
assert html.escape('<p>façade</p>') == '<p>façade</p>'
|
||||||
|
}
|
@ -10,6 +10,7 @@ import net.http
|
|||||||
import net.urllib
|
import net.urllib
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
|
import encoding.html
|
||||||
|
|
||||||
// A type which don't get filtered inside templates
|
// A type which don't get filtered inside templates
|
||||||
pub type RawHtml = string
|
pub type RawHtml = string
|
||||||
@ -725,12 +726,5 @@ fn send_string(mut conn net.TcpConn, s string) ! {
|
|||||||
// It used by `vlib/v/gen/c/str_intp.v:130` for string interpolation inside vweb templates
|
// It used by `vlib/v/gen/c/str_intp.v:130` for string interpolation inside vweb templates
|
||||||
// TODO: move it to template render
|
// TODO: move it to template render
|
||||||
fn filter(s string) string {
|
fn filter(s string) string {
|
||||||
return s.replace_each([
|
return html.escape(s)
|
||||||
'<',
|
|
||||||
'<',
|
|
||||||
'"',
|
|
||||||
'"',
|
|
||||||
'&',
|
|
||||||
'&',
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user