| 1 | import encoding.html |
| 2 | |
| 3 | fn test_escape_html() { |
| 4 | assert html.escape('<>&') == '<>&' |
| 5 | assert html.escape('No change') == 'No change' |
| 6 | assert html.escape('<b>Bold text</b>') == '<b>Bold text</b>' |
| 7 | assert html.escape('<img />') == '<img />' |
| 8 | assert html.escape("' onmouseover='alert(1)'") == '' onmouseover='alert(1)'' |
| 9 | assert html.escape("<a href='http://www.example.com'>link</a>") == '<a href='http://www.example.com'>link</a>' |
| 10 | assert html.escape("<script>alert('hello');</script>") == '<script>alert('hello');</script>' |
| 11 | // Cases obtained from: |
| 12 | // https://github.com/apache/commons-lang/blob/master/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java |
| 13 | assert html.escape('plain text') == 'plain text' |
| 14 | assert html.escape('') == '' |
| 15 | assert html.escape('bread & butter') == 'bread & butter' |
| 16 | assert html.escape('"bread" & butter') == '"bread" & butter' |
| 17 | assert html.escape('greater than >') == 'greater than >' |
| 18 | assert html.escape('< less than') == '< less than' |
| 19 | // Leave accents as-is |
| 20 | assert html.escape('café') == 'café' |
| 21 | assert html.escape('<p>façade</p>') == '<p>façade</p>' |
| 22 | } |
| 23 | |
| 24 | fn test_unescape_html() { |
| 25 | // Test different formats |
| 26 | assert html.unescape(''''') == "'''" |
| 27 | // Converse escape tests |
| 28 | assert html.unescape('<>&') == '<>&' |
| 29 | assert html.unescape('No change') == 'No change' |
| 30 | assert html.unescape('<b>Bold text</b>') == '<b>Bold text</b>' |
| 31 | assert html.unescape('<img />') == '<img />' |
| 32 | assert html.unescape('' onmouseover='alert(1)'') == "' onmouseover='alert(1)'" |
| 33 | assert html.unescape('<a href='http://www.example.com'>link</a>') == "<a href='http://www.example.com'>link</a>" |
| 34 | assert html.unescape('<script>alert('hello');</script>') == "<script>alert('hello');</script>" |
| 35 | // Cases obtained from: |
| 36 | // https://github.com/apache/commons-lang/blob/master/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java |
| 37 | assert html.unescape('plain text') == 'plain text' |
| 38 | assert html.unescape('') == '' |
| 39 | assert html.unescape('bread & butter') == 'bread & butter' |
| 40 | assert html.unescape('"bread" & butter') == '"bread" & butter' |
| 41 | assert html.unescape('greater than >') == 'greater than >' |
| 42 | assert html.unescape('< less than') == '< less than' |
| 43 | // Leave accents as-is |
| 44 | assert html.unescape('café') == 'café' |
| 45 | assert html.unescape('<p>façade</p>') == '<p>façade</p>' |
| 46 | } |
| 47 | |
| 48 | fn test_unescape_all_html() { |
| 49 | // Test different formats |
| 50 | assert html.unescape(''''', all: true) == "'''" |
| 51 | assert html.unescape('⩔ = ⩔ = ⩔ = ⩔', all: true) == '⩔ = ⩔ = ⩔ = ⩔' |
| 52 | // Converse escape tests |
| 53 | assert html.unescape('<>&', all: true) == '<>&' |
| 54 | assert html.unescape('No change', all: true) == 'No change' |
| 55 | assert html.unescape('<b>Bold text</b>', all: true) == '<b>Bold text</b>' |
| 56 | assert html.unescape('<img />', all: true) == '<img />' |
| 57 | assert html.unescape('' onmouseover='alert(1)'', all: true) == "' onmouseover='alert(1)'" |
| 58 | assert html.unescape('<a href='http://www.example.com'>link</a>', all: true) == "<a href='http://www.example.com'>link</a>" |
| 59 | assert html.unescape('<script>alert('hello');</script>', all: true) == "<script>alert('hello');</script>" |
| 60 | // Cases obtained from: |
| 61 | // https://github.com/apache/commons-lang/blob/master/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java |
| 62 | assert html.unescape('plain text', all: true) == 'plain text' |
| 63 | assert html.unescape('', all: true) == '' |
| 64 | assert html.unescape('bread & butter', all: true) == 'bread & butter' |
| 65 | assert html.unescape('"bread" & butter', all: true) == '"bread" & butter' |
| 66 | assert html.unescape('greater than >', all: true) == 'greater than >' |
| 67 | assert html.unescape('< less than', all: true) == '< less than' |
| 68 | // Leave accents as-is |
| 69 | assert html.unescape('café', all: true) == 'café' |
| 70 | assert html.unescape('<p>façade</p>', all: true) == '<p>façade</p>' |
| 71 | } |
| 72 | |