Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,101 @@
|
||||
module html
|
||||
|
||||
import encoding.hex
|
||||
import strconv
|
||||
|
||||
@[params]
|
||||
pub struct EscapeConfig {
|
||||
pub:
|
||||
quote bool = true
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct UnescapeConfig {
|
||||
EscapeConfig
|
||||
pub:
|
||||
all bool
|
||||
}
|
||||
|
||||
const escape_seq = ['&', '&', '<', '<', '>', '>']
|
||||
const escape_quote_seq = ['"', '"', "'", ''']
|
||||
const unescape_seq = ['&', '&', '<', '<', '>', '>']
|
||||
const unescape_quote_seq = ['"', '"', ''', "'"]
|
||||
|
||||
// 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 {
|
||||
return if config.quote {
|
||||
input.replace_each(escape_seq).replace_each(escape_quote_seq)
|
||||
} else {
|
||||
input.replace_each(escape_seq)
|
||||
}
|
||||
}
|
||||
|
||||
// unescape converts entities like "<" to "<". By default it is the converse of `escape`.
|
||||
// If `all` is set to true, it handles named, numeric, and hex values - for example,
|
||||
// `'''`, `'''`, and `'''` then unescape to "'".
|
||||
pub fn unescape(input string, config UnescapeConfig) string {
|
||||
return if config.all {
|
||||
unescape_all(input)
|
||||
} else if config.quote {
|
||||
input.replace_each(unescape_seq).replace_each(unescape_quote_seq)
|
||||
} else {
|
||||
input.replace_each(unescape_seq)
|
||||
}
|
||||
}
|
||||
|
||||
fn unescape_all(input string) string {
|
||||
mut result := []rune{}
|
||||
runes := input.runes()
|
||||
mut i := 0
|
||||
for i < runes.len {
|
||||
if runes[i] == `&` {
|
||||
mut j := i + 1
|
||||
for j < runes.len && runes[j] != `;` {
|
||||
j++
|
||||
}
|
||||
if j < runes.len && runes[i + 1] == `#` {
|
||||
// Numeric escape sequences (e.g., ' or ')
|
||||
if runes[i + 2] == `x` || runes[i + 2] == `X` {
|
||||
// Hexadecimal escape sequence
|
||||
if v := hex.decode(runes[i + 3..j].string()) {
|
||||
mut n := u16(0)
|
||||
for x in v {
|
||||
n = n * 256 + x
|
||||
}
|
||||
result << n
|
||||
} else {
|
||||
// Leave invalid sequences unchanged
|
||||
result << runes[i..j + 1]
|
||||
i = j + 1
|
||||
}
|
||||
} else {
|
||||
// Decimal escape sequence
|
||||
if v := strconv.atoi(runes[i + 2..j].string()) {
|
||||
result << v
|
||||
} else {
|
||||
// Leave invalid sequences unchanged
|
||||
result << runes[i..j + 1]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Named entity (e.g., <)
|
||||
entity := runes[i + 1..j].string()
|
||||
if v := named_references[entity] {
|
||||
result << v
|
||||
} else {
|
||||
// Leave unknown entities unchanged
|
||||
result << runes[i..j + 1]
|
||||
}
|
||||
}
|
||||
i = j + 1
|
||||
} else {
|
||||
result << runes[i]
|
||||
i++
|
||||
}
|
||||
}
|
||||
return result.string()
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
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>'
|
||||
}
|
||||
|
||||
fn test_unescape_html() {
|
||||
// Test different formats
|
||||
assert html.unescape(''''') == "'''"
|
||||
// Converse escape tests
|
||||
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.unescape('café') == 'café'
|
||||
assert html.unescape('<p>façade</p>') == '<p>façade</p>'
|
||||
}
|
||||
|
||||
fn test_unescape_all_html() {
|
||||
// Test different formats
|
||||
assert html.unescape(''''', all: true) == "'''"
|
||||
assert html.unescape('⩔ = ⩔ = ⩔ = ⩔', all: true) == '⩔ = ⩔ = ⩔ = ⩔'
|
||||
// Converse escape tests
|
||||
assert html.unescape('<>&', all: true) == '<>&'
|
||||
assert html.unescape('No change', all: true) == 'No change'
|
||||
assert html.unescape('<b>Bold text</b>', all: true) == '<b>Bold text</b>'
|
||||
assert html.unescape('<img />', all: true) == '<img />'
|
||||
assert html.unescape('' onmouseover='alert(1)'', all: true) == "' onmouseover='alert(1)'"
|
||||
assert html.unescape('<a href='http://www.example.com'>link</a>', all: true) == "<a href='http://www.example.com'>link</a>"
|
||||
assert html.unescape('<script>alert('hello');</script>', all: true) == "<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', all: true) == 'plain text'
|
||||
assert html.unescape('', all: true) == ''
|
||||
assert html.unescape('bread & butter', all: true) == 'bread & butter'
|
||||
assert html.unescape('"bread" & butter', all: true) == '"bread" & butter'
|
||||
assert html.unescape('greater than >', all: true) == 'greater than >'
|
||||
assert html.unescape('< less than', all: true) == '< less than'
|
||||
// Leave accents as-is
|
||||
assert html.unescape('café', all: true) == 'café'
|
||||
assert html.unescape('<p>façade</p>', all: true) == '<p>façade</p>'
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user