| | | require 'mediacloth/mediawikilexer' | 1 |
|---|
| | | | 2 |
|---|
| | | require 'test/unit' | 3 |
|---|
| | | require 'testhelper' | 4 |
|---|
| | | | 5 |
|---|
| | | class SanitizationTest < Test::Unit::TestCase | 6 |
|---|
| | | | 7 |
|---|
| | | def setup | 8 |
|---|
| | | @@lexer ||= MediaWikiLexer.new | 9 |
|---|
| | | end | 10 |
|---|
| | | | 11 |
|---|
| | | def test_sanitizes_script_tags | 12 |
|---|
| | | assert_sanitizes_to "<script>alert('Unescaped code!')<\/script>", | 13 |
|---|
| | | "<script>alert('Unescaped code!')</script>" | 14 |
|---|
| | | end | 15 |
|---|
| | | | 16 |
|---|
| | | def test_keeps_deleted_and_inserted_tags | 17 |
|---|
| | | assert_no_sanitization "This is <del>bold</del>, and that is <ins>inserted</ins>" | 18 |
|---|
| | | end | 19 |
|---|
| | | | 20 |
|---|
| | | def test_keeps_bold_and_italics_tags | 21 |
|---|
| | | assert_no_sanitization "This is <b>bold</b>, this is in <i>italics</i> and that is <em>emphasized</em>" | 22 |
|---|
| | | end | 23 |
|---|
| | | | 24 |
|---|
| | | def test_keeps_underline_and_strikethrough_tags | 25 |
|---|
| | | assert_no_sanitization "This is very <u>important</u>, but that | 26 |
|---|
| | | can <s>safely</s> be <strike>ignored</strike>" | 27 |
|---|
| | | end | 28 |
|---|
| | | | 29 |
|---|
| | | def test_keeps_font_tags | 30 |
|---|
| | | assert_no_sanitization %{Fonts can be <font face="serif">changed</font> using HTML tags} | 31 |
|---|
| | | end | 32 |
|---|
| | | | 33 |
|---|
| | | def test_keeps_big_and_small_tags | 34 |
|---|
| | | assert_no_sanitization "Text can be made <big>big</big>, <small>small</small>" | 35 |
|---|
| | | end | 36 |
|---|
| | | | 37 |
|---|
| | | def test_keeps_sub_and_superscripts | 38 |
|---|
| | | assert_no_sanitization "We can also use <sub>sub</sub> and <sup>superscripts</sup>" | 39 |
|---|
| | | end | 40 |
|---|
| | | | 41 |
|---|
| | | def test_keeps_citation_tags | 42 |
|---|
| | | assert_no_sanitization %{<cite>"Perfection is achieved, not when there is | 43 |
|---|
| | | nothing left to add, but when there is | 44 |
|---|
| | | nothing left to remove."</cite> | 45 |
|---|
| | | -- Antoine de Saint-Exupery} | 46 |
|---|
| | | end | 47 |
|---|
| | | | 48 |
|---|
| | | def test_keeps_code_and_teletype | 49 |
|---|
| | | assert_no_sanitization "Text inside <code>code</code> and <tt>teletype</tt> | 50 |
|---|
| | | usually get rendered with a fixed width font" | 51 |
|---|
| | | end | 52 |
|---|
| | | | 53 |
|---|
| | | def test_keeps_variable_tags | 54 |
|---|
| | | assert_no_sanitization "Here is a <var>variable</var>" | 55 |
|---|
| | | end | 56 |
|---|
| | | | 57 |
|---|
| | | def test_keeps_strong_tags | 58 |
|---|
| | | assert_no_sanitization "That was a very <strong>strong</strong> claim" | 59 |
|---|
| | | end | 60 |
|---|
| | | | 61 |
|---|
| | | def test_keeps_spans | 62 |
|---|
| | | assert_no_sanitization %{Most environments will render | 63 |
|---|
| | | <span style="color: red">this text</span> with | 64 |
|---|
| | | different colours} | 65 |
|---|
| | | end | 66 |
|---|
| | | | 67 |
|---|
| | | def test_keeps_headings | 68 |
|---|
| | | assert_no_sanitization "<h1>Heading 1</h1> | 69 |
|---|
| | | <h2>Heading 2</h2> | 70 |
|---|
| | | <h3>Heading 3</h3> | 71 |
|---|
| | | <h4>Heading 4</h4> | 72 |
|---|
| | | <h5>Heading 5</h5> | 73 |
|---|
| | | <h6>Heading 6</h6>" | 74 |
|---|
| | | end | 75 |
|---|
| | | | 76 |
|---|
| | | def test_keeps_divs | 77 |
|---|
| | | assert_no_sanitization "<div>Division</div>" | 78 |
|---|
| | | end | 79 |
|---|
| | | | 80 |
|---|
| | | def test_keeps_center_tags | 81 |
|---|
| | | assert_no_sanitization "<center>Center</center>" | 82 |
|---|
| | | end | 83 |
|---|
| | | | 84 |
|---|
| | | def test_keeps_blockquote_tags | 85 |
|---|
| | | assert_no_sanitization "<blockquote>Blockquote</blockquote>" | 86 |
|---|
| | | end | 87 |
|---|
| | | | 88 |
|---|
| | | def test_keeps_ordered_and_unordered_lists | 89 |
|---|
| | | assert_no_sanitization "<ol> | 90 |
|---|
| | | <li>Ordered</li> | 91 |
|---|
| | | <li>List</li> | 92 |
|---|
| | | <li>(And list items)</li> | 93 |
|---|
| | | </ol> | 94 |
|---|
| | | | 95 |
|---|
| | | <ul> | 96 |
|---|
| | | <li>Unordered</li> | 97 |
|---|
| | | <li>List</li> | 98 |
|---|
| | | <li>(And list items)</li> | 99 |
|---|
| | | </ul>" | 100 |
|---|
| | | end | 101 |
|---|
| | | | 102 |
|---|
| | | def test_keeps_table_and_main_components | 103 |
|---|
| | | assert_no_sanitization "<table> | 104 |
|---|
| | | <tr><th>Table</th> <th>tag</th> <th /></tr> | 105 |
|---|
| | | <tr><td>and</td> <td>its</td> <td>components</td></tr> | 106 |
|---|
| | | <tr><td>including</td><td>header</td><td>tags</td></tr> | 107 |
|---|
| | | </table>" | 108 |
|---|
| | | end | 109 |
|---|
| | | | 110 |
|---|
| | | def test_keeps_ruby_tag_and_components | 111 |
|---|
| | | assert_no_sanitization "<ruby> | 112 |
|---|
| | | <rb>Ruby base</rb> | 113 |
|---|
| | | <rp>(</rp> | 114 |
|---|
| | | <rt>Ruby text</rt> | 115 |
|---|
| | | <rp>)</rp> | 116 |
|---|
| | | </ruby>" | 117 |
|---|
| | | end | 118 |
|---|
| | | | 119 |
|---|
| | | def test_keeps_paragraph_tags | 120 |
|---|
| | | assert_no_sanitization "We can also break <p>paragraphs</p> with HTML." | 121 |
|---|
| | | end | 122 |
|---|
| | | | 123 |
|---|
| | | def test_keeps_linebreaks | 124 |
|---|
| | | assert_no_sanitization "Break lines with an empty element<br /><br/> | 125 |
|---|
| | | Or using the opening tag only <br>" | 126 |
|---|
| | | end | 127 |
|---|
| | | | 128 |
|---|
| | | def test_keeps_horizontal_rules | 129 |
|---|
| | | assert_no_sanitization "<hr />Display an horizontal rule" | 130 |
|---|
| | | end | 131 |
|---|
| | | | 132 |
|---|
| | | def test_keeps_definition_lists | 133 |
|---|
| | | assert_no_sanitization "<dl> | 134 |
|---|
| | | <dt>Definition terms</dt> | 135 |
|---|
| | | <dd>And descriptions</dt> | 136 |
|---|
| | | </dl>" | 137 |
|---|
| | | end | 138 |
|---|
| | | | 139 |
|---|
| | | def test_keeps_preformatted_text | 140 |
|---|
| | | assert_no_sanitization "<pre>Preformatted\ntext</pre>" | 141 |
|---|
| | | end | 142 |
|---|
| | | | 143 |
|---|
| | | def test_keeps_nowiki_tags | 144 |
|---|
| | | assert_no_sanitization "<nowiki>No wiki tag</nowiki>" | 145 |
|---|
| | | end | 146 |
|---|
| | | | 147 |
|---|
| | | def test_keeps_math_tags | 148 |
|---|
| | | assert_no_sanitization "<math>1 == 1</math>" | 149 |
|---|
| | | end | 150 |
|---|
| | | | 151 |
|---|
| | | def test_sanitizes_thead_and_tbody_tags | 152 |
|---|
| | | assert_sanitizes_to "<thead>Table header</thead><tbody>Table body</tbody>", | 153 |
|---|
| | | "<thead>Table header</thead><tbody>Table body</tbody>" | 154 |
|---|
| | | end | 155 |
|---|
| | | | 156 |
|---|
| | | def test_sanitizes_form_label_and_input_tags | 157 |
|---|
| | | assert_sanitizes_to %{<form action="/send" method="post"> | 158 |
|---|
| | | <label for="username">Username</label> | 159 |
|---|
| | | <input name="login" id="username" /> | 160 |
|---|
| | | </form>}, | 161 |
|---|
| | | %{<form action="/send" method="post"> | 162 |
|---|
| | | <label for="username">Username</label> | 163 |
|---|
| | | <input name="login" id="username" /> | 164 |
|---|
| | | </form>} | 165 |
|---|
| | | end | 166 |
|---|
| | | | 167 |
|---|
| | | # TODO sanitize tags with closing spaces | 168 |
|---|
| | | # TODO removes "on" attributes even in legal tags | 169 |
|---|
| | | | 170 |
|---|
| | | private | 171 |
|---|
| | | | 172 |
|---|
| | | def assert_sanitizes_to(expected, actual) | 173 |
|---|
| | | assert_equal expected, @@lexer.sanitize(actual) | 174 |
|---|
| | | end | 175 |
|---|
| | | | 176 |
|---|
| | | def assert_no_sanitization(expected) | 177 |
|---|
| | | assert_sanitizes_to(expected, expected) | 178 |
|---|
| | | end | 179 |
|---|
| | | | 180 |
|---|
| | | end | 181 |
|---|