tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sanitizer-parseHTML.tentative.html (4286B)


      1 <!DOCTYPE html>
      2 <head>
      3 <title>Testcases for parseHTML and parseHTMLUnsafe</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="support/html5lib-testcase-support.js"></script>
      7 <!--
      8  This is a set of basic Sanitizer test cases using the parseHTML and
      9  parseHTMLUnsafe methods.
     10 -->
     11 <script id="all" type="html5lib-testcases">
     12 #data
     13 text
     14 #document
     15 | <html>
     16 |   <head>
     17 |   <body>
     18 |     "text"
     19 
     20 #data
     21 <div>text
     22 #config
     23 { "elements": ["html", "body", "div"] }
     24 #document
     25 | <html>
     26 |   <body>
     27 |     <div>
     28 |       "text"
     29 
     30 #data
     31 <div>text
     32 #config
     33 { "elements": ["body", "div"] }
     34 #document
     35 
     36 #data
     37 <div>text
     38 #config
     39 { "elements": ["html", "div"] }
     40 #document
     41 | <html>
     42 
     43 #data
     44 <html onload="3 + 3"><div>a
     45 #config
     46 { "replaceWithChildrenElements": ["html"], "elements": ["head", "body", "div"] }
     47 #document
     48 
     49 </script>
     50 <script id="safe" type="html5lib-testcases">
     51 #data
     52 <script>hello
     53 #document
     54 | <html>
     55 |   <head>
     56 |   <body>
     57 
     58 #data
     59 <html onload="2+2"><body onload="3+3"><div>hello
     60 #document
     61 | <html>
     62 |   <head>
     63 |   <body>
     64 |     <div>
     65 |       "hello"
     66 
     67 #data
     68 <div data-xyz="1" id="2" title="3">
     69 #config
     70 { "attributes": ["id"] }
     71 #document
     72 | <html>
     73 |   <head>
     74 |   <body>
     75 |     <div>
     76 |       id="2"
     77 
     78 #data
     79 <div>a<!-- xx -->b
     80 #config
     81 { }
     82 #document
     83 | <html>
     84 |   <head>
     85 |   <body>
     86 |     <div>
     87 |       "a"
     88 |       "b"
     89 
     90 #data
     91 <html onload="2 + 2"><div>a
     92 #config
     93 { "replaceWithChildrenElements": ["html"], "removeElements": [] }
     94 #document
     95 | <html>
     96 |   <head>
     97 |   <body>
     98 |     <div>
     99 |       "a"
    100 
    101 </script>
    102 <script id="unsafe" type="html5lib-testcases">
    103 #data
    104 <script>hello
    105 #document
    106 | <html>
    107 |   <head>
    108 |     <script>
    109 |       "hello"
    110 |   <body>
    111 
    112 #data
    113 <html onload="2+2"><body onload="3+3"><div>hello
    114 #document
    115 | <html>
    116 |   onload="2+2"
    117 |   <head>
    118 |   <body>
    119 |     onload="3+3"
    120 |     <div>
    121 |       "hello"
    122 
    123 #data
    124 <div data-xyz="1" id="2" title="3">
    125 #config
    126 { "attributes": ["id"] }
    127 #document
    128 | <html>
    129 |   <head>
    130 |   <body>
    131 |     <div>
    132 |       data-xyz="1"
    133 |       id="2"
    134 
    135 #data
    136 <div>a<!-- xx -->b
    137 #config
    138 { }
    139 #document
    140 | <html>
    141 |   <head>
    142 |   <body>
    143 |     <div>
    144 |       "a"
    145 |       <!-- xx -->
    146 |       "b"
    147 
    148 #data
    149 <html onload="2 + 2"><div>a
    150 #config
    151 { "replaceWithChildrenElements": ["html"], "removeElements": [] }
    152 #document
    153 | <html>
    154 |   onload="2 + 2"
    155 |   <head>
    156 |   <body>
    157 |     <div>
    158 |       "a"
    159 </script>
    160 <script id="document" type="html5lib-testcases">
    161 #data
    162 <!DOCTYPE html>
    163 text
    164 #document
    165 | <!DOCTYPE html "" "">
    166 | <html>
    167 |   <head>
    168 |   <body>
    169 |     "text"
    170 
    171 </script>
    172 <script>
    173 function test_safe(testcase, index) {
    174  let config = undefined;
    175  if (testcase.config) {
    176    config = { sanitizer: JSON.parse(testcase.config) };
    177  }
    178  test(_ => {
    179    assert_testcase(Document.parseHTML(testcase.data, config), testcase);
    180  }, `parseHTML testcase ${index}, "${testcase.data}"`);
    181 }
    182 function test_unsafe(testcase, index) {
    183  let config = undefined;
    184  if (testcase.config) {
    185    config = { sanitizer: JSON.parse(testcase.config) };
    186  }
    187  test(_ => {
    188    assert_testcase(Document.parseHTMLUnsafe(testcase.data, config), testcase);
    189  }, `parseHTMLUnsafe testcase ${index}, "${testcase.data}"`);
    190 }
    191 
    192 const all = parse_html5lib_testcases(
    193    document.getElementById("all").textContent);
    194 const safe = parse_html5lib_testcases(
    195    document.getElementById("safe").textContent);
    196 const unsafe = parse_html5lib_testcases(
    197    document.getElementById("unsafe").textContent);
    198 all.forEach(test_safe);
    199 all.forEach(test_unsafe);
    200 safe.forEach(test_safe);
    201 unsafe.forEach(test_unsafe);
    202 
    203 
    204 // DOM only supports Document Type Declarations as children of documents. This
    205 // trips up the assert_testcase implementation, so we'll handle that seperately.
    206 parse_html5lib_testcases(
    207    document.getElementById("document").textContent).
    208    forEach((testcase, index) => {
    209  test(_ => {
    210    const tree = build_node_tree(new Document(), testcase.document);
    211    assert_subtree_equals(Document.parseHTMLUnsafe(testcase.data, {}), tree);
    212  }, `parseHTMLUnsafe full document testcase ${index}, "${testcase.data}"`);
    213  test(_ => {
    214    const tree = build_node_tree(new Document(), testcase.document);
    215    assert_subtree_equals(Document.parseHTML(testcase.data, {}), tree);
    216  }, `parseHTML full document testcase ${index}, "${testcase.data}"`);
    217 });
    218 </script>
    219 </head>
    220 <body>
    221 </body>