| OLD | NEW |
| (Empty) |
| 1 <!DOCTYPE html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <title>parseHtmlSubset test</title> | |
| 5 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.j
s"></script> | |
| 6 <script src="parse_html_subset.js"></script> | |
| 7 <script> | |
| 8 | |
| 9 goog.require('goog.testing.AsyncTestCase'); | |
| 10 goog.require('goog.testing.jsunit'); | |
| 11 | |
| 12 </script> | |
| 13 | |
| 14 </head> | |
| 15 <body> | |
| 16 <script> | |
| 17 | |
| 18 var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(); | |
| 19 | |
| 20 function parseAndAssertThrows() { | |
| 21 var args = arguments; | |
| 22 assertThrows(function() { | |
| 23 parseHtmlSubset.apply(null, args); | |
| 24 }); | |
| 25 } | |
| 26 | |
| 27 function parseAndAssertNotThrows() { | |
| 28 var args = arguments; | |
| 29 assertNotThrows(function() { | |
| 30 parseHtmlSubset.apply(null, args); | |
| 31 }); | |
| 32 } | |
| 33 | |
| 34 function testText() { | |
| 35 parseAndAssertNotThrows(''); | |
| 36 parseAndAssertNotThrows('abc'); | |
| 37 parseAndAssertNotThrows(' '); | |
| 38 } | |
| 39 | |
| 40 function testSupportedTags() { | |
| 41 parseAndAssertNotThrows('<b>bold</b>'); | |
| 42 parseAndAssertNotThrows('Some <b>bold</b> text'); | |
| 43 parseAndAssertNotThrows('Some <strong>strong</strong> text'); | |
| 44 parseAndAssertNotThrows('<B>bold</B>'); | |
| 45 parseAndAssertNotThrows('Some <B>bold</B> text'); | |
| 46 parseAndAssertNotThrows('Some <STRONG>strong</STRONG> text'); | |
| 47 } | |
| 48 | |
| 49 function testInvalidTags() { | |
| 50 parseAndAssertThrows('<unknown_tag>x</unknown_tag>'); | |
| 51 parseAndAssertThrows('<img>'); | |
| 52 parseAndAssertThrows('<script>alert(1)<' + '/script>'); | |
| 53 } | |
| 54 | |
| 55 function testInvalidAttributes() { | |
| 56 parseAndAssertThrows('<b onclick="alert(1)">x</b>'); | |
| 57 parseAndAssertThrows('<b style="color:red">x</b>'); | |
| 58 parseAndAssertThrows('<b foo>x</b>'); | |
| 59 parseAndAssertThrows('<b foo=bar></b>'); | |
| 60 } | |
| 61 | |
| 62 function testValidAnchors() { | |
| 63 parseAndAssertNotThrows('<a href="https://google.com">Google</a>'); | |
| 64 parseAndAssertNotThrows('<a href="chrome://settings">Google</a>'); | |
| 65 } | |
| 66 | |
| 67 function testInvalidAnchorHrefs() { | |
| 68 parseAndAssertThrows('<a href="http://google.com">Google</a>'); | |
| 69 parseAndAssertThrows('<a href="ftp://google.com">Google</a>'); | |
| 70 parseAndAssertThrows('<a href="http/google.com">Google</a>'); | |
| 71 parseAndAssertThrows('<a href="javascript:alert(1)">Google</a>'); | |
| 72 parseAndAssertThrows('<a href="chrome-extension://whurblegarble">Google</a>'); | |
| 73 } | |
| 74 | |
| 75 function testInvalidAnchorAttributes() { | |
| 76 parseAndAssertThrows('<a name=foo>Google</a>'); | |
| 77 parseAndAssertThrows( | |
| 78 '<a onclick="alert(1)" href="https://google.com">Google</a>'); | |
| 79 parseAndAssertThrows('<a foo="bar(1)" href="https://google.com">Google</a>'); | |
| 80 } | |
| 81 | |
| 82 function testAnchorTarget() { | |
| 83 parseAndAssertNotThrows( | |
| 84 '<a href="https://google.com" target="blank_">Google</a>'); | |
| 85 parseAndAssertNotThrows( | |
| 86 '<a href="https://google.com" target="foo">Google</a>'); | |
| 87 } | |
| 88 | |
| 89 function testCustomTags() { | |
| 90 parseAndAssertNotThrows('yo <I>ho</i><bR>yo <EM>ho</em>', ['i', 'EM', 'Br']); | |
| 91 } | |
| 92 | |
| 93 function testInvalidCustomTags() { | |
| 94 parseAndAssertThrows("a pirate's<script>lifeForMe();<" + '/script>', ['br']); | |
| 95 } | |
| 96 | |
| 97 function testCustomAttributes() { | |
| 98 function returnsTruthy(node, value) { | |
| 99 assertEquals('A', node.tagName); | |
| 100 assertEquals('fancy', value); | |
| 101 return true; | |
| 102 } | |
| 103 parseAndAssertNotThrows('<a class="fancy">I\'m fancy!</a>', null, | |
| 104 {class: returnsTruthy}); | |
| 105 } | |
| 106 | |
| 107 function testInvalidCustomAttributes() { | |
| 108 function returnsFalsey() { | |
| 109 return false; | |
| 110 } | |
| 111 parseAndAssertThrows('<a class="fancy">I\'m fancy!</a>', null, | |
| 112 {class: returnsFalsey}); | |
| 113 parseAndAssertThrows('<a class="fancy">I\'m fancy!</a>'); | |
| 114 } | |
| 115 | |
| 116 function testOnError() { | |
| 117 window.called = false; | |
| 118 | |
| 119 asyncTestCase.waitForAsync('Waiting for image error callbacks'); | |
| 120 | |
| 121 parseAndAssertThrows('<img onerror="window.called = true" src="_.png">'); | |
| 122 parseAndAssertThrows('<img src="_.png" onerror="window.called = true">'); | |
| 123 | |
| 124 window.setTimeout(function() { | |
| 125 asyncTestCase.continueTesting(); | |
| 126 assertFalse(window.called); | |
| 127 }); | |
| 128 } | |
| 129 | |
| 130 </script> | |
| 131 | |
| 132 </body> | |
| 133 </html> | |
| OLD | NEW |