mirror of
https://github.com/vlang/v.git
synced 2025-09-08 06:41:58 -04:00
72 lines
4.1 KiB
V
72 lines
4.1 KiB
V
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>'
|
|
}
|