Home | History | Annotate | Download | only in js
      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4 <title></title>
      5 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
      6 <script src="parse_html_subset.js"></script>
      7 <script>
      8 
      9 goog.require('goog.testing.jsunit');
     10 
     11 </script>
     12 
     13 </head>
     14 <body>
     15 <script>
     16 
     17 function parseAndAssertThrows(s) {
     18   assertThrows(function() {
     19     parseHtmlSubset(s);
     20   });
     21 }
     22 
     23 function parseAndAssertNotThrows(s) {
     24   assertNotThrows(function() {
     25     parseHtmlSubset(s);
     26   });
     27 }
     28 
     29 function testText() {
     30   parseAndAssertNotThrows('');
     31   parseAndAssertNotThrows('abc');
     32   parseAndAssertNotThrows('&nbsp;');
     33 }
     34 
     35 function testSupportedTags() {
     36   parseAndAssertNotThrows('<b>bold</b>');
     37   parseAndAssertNotThrows('Some <b>bold</b> text');
     38   parseAndAssertNotThrows('Some <strong>strong</strong> text');
     39   parseAndAssertNotThrows('<B>bold</B>');
     40   parseAndAssertNotThrows('Some <B>bold</B> text');
     41   parseAndAssertNotThrows('Some <STRONG>strong</STRONG> text');
     42 }
     43 
     44 function testInvaliTags() {
     45   parseAndAssertThrows('<unknown_tag>x</unknown_tag>');
     46   parseAndAssertThrows('<img>');
     47   parseAndAssertThrows('<script>alert(1)<' + '/script>');
     48 }
     49 
     50 function testInvalidAttributes() {
     51   parseAndAssertThrows('<b onclick="alert(1)">x</b>');
     52   parseAndAssertThrows('<b style="color:red">x</b>');
     53   parseAndAssertThrows('<b foo>x</b>');
     54   parseAndAssertThrows('<b foo=bar></b>');
     55 }
     56 
     57 function testValidAnchors() {
     58   parseAndAssertNotThrows('<a href="http://google.com">Google</a>');
     59   parseAndAssertNotThrows('<a href="https://google.com">Google</a>');
     60 }
     61 
     62 function testInvalidAnchorHrefs() {
     63   parseAndAssertThrows('<a href="ftp://google.com">Google</a>');
     64   parseAndAssertThrows('<a href="http/google.com">Google</a>');
     65   parseAndAssertThrows('<a href="javascript:alert(1)">Google</a>');
     66 }
     67 
     68 function testInvalidAnchorAttributes() {
     69   parseAndAssertThrows('<a name=foo>Google</a>');
     70   parseAndAssertThrows(
     71       '<a onclick="alert(1)" href="http://google.com">Google</a>');
     72   parseAndAssertThrows('<a foo="bar(1)" href="http://google.com">Google</a>');
     73 }
     74 
     75 function testAnchorTarget() {
     76   parseAndAssertNotThrows(
     77       '<a href="http://google.com" target="blank_">Google</a>');
     78   parseAndAssertNotThrows(
     79       '<a href="http://google.com" target="foo">Google</a>');
     80 }
     81 
     82 </script>
     83 
     84 </body>
     85 </html>
     86