tor-browser

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

test_bug560112.html (6302B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=560112
      5 -->
      6 <head>
      7  <title>Test for Bug 560112</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     10 </head>
     11 <body>
     12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=560112">Mozilla Bug 560112</a>
     13 <p id="display"></p>
     14 <div id="content" style="display: none">
     15 </div>
     16 <pre id="test">
     17 <script type="application/javascript">
     18 
     19 /** Test for Bug 560112 */
     20 
     21 /**
     22 * Sets dataset property. Checks data attribute "attr".
     23 * Gets dataset property. Checks data attribute "attr".
     24 * Overwrites dataset property Checks data attribute "attr".
     25 * Deletes dataset property. Checks data attribute "attr".
     26 */
     27 function SetGetOverwriteDel(attr, prop)
     28 {
     29  var el = document.createElement('div');
     30 
     31  // Set property.
     32  is(prop in el.dataset, false, 'Property should not be in dataset before setting.');
     33  el.dataset[prop] = "zzzzzz";
     34  is(prop in el.dataset, true, 'Property should be in dataset after setting.');
     35  ok(el.hasAttribute(attr), 'Element should have data attribute for dataset property "' + prop + '".');
     36 
     37  // Get property.
     38  is(el.dataset[prop], "zzzzzz", 'Dataset property "' + prop + '" should have value "zzzzzz".');
     39  is(el.getAttribute(attr), "zzzzzz", 'Attribute "' + attr + '" should have value "zzzzzz".');
     40 
     41  // Overwrite property.
     42  el.dataset[prop] = "yyyyyy";
     43  is(el.dataset[prop], "yyyyyy", 'Dataset property "' + prop + '" should have value "yyyyyy".');
     44  is(el.getAttribute(attr), "yyyyyy", 'Attribute "' + attr + '" should have value "yyyyyy".');
     45 
     46  // Delete property.
     47  delete el.dataset[prop];
     48  ok(!el.hasAttribute(attr), 'Element should not have data attribute for dataset property "' + prop + '".');
     49  is(prop in el.dataset, false, 'Deleted property should not be in dataset.');
     50 }
     51 
     52 /**
     53 * Sets dataset property and expects exception.
     54 */
     55 function SetExpectException(prop)
     56 {
     57  var el = document.createElement('div');
     58 
     59  try {
     60    el.dataset[prop] = "xxxxxx";
     61    ok(false, 'Exception should have been thrown.');
     62  } catch (ex) {
     63    ok(true, 'Exception should have been thrown.');
     64  }
     65 }
     66 
     67 /**
     68 * Adds attributes in "attrList" to element.
     69 * Deletes attributes in "delList" from element.
     70 * Checks for "numProp" properties in dataset.
     71 */
     72 function DelAttrEnumerate(attrList, delList, numProp)
     73 {
     74  var el = document.createElement('div');
     75  
     76  // Adds attributes in "attrList".
     77  for (var i = 0; i < attrList.length; ++i) {
     78    el.setAttribute(attrList[i], "aaaaaa");
     79  }
     80 
     81  // Remove attributes in "delList".
     82  for (var i = 0; i < delList.length; ++i) {
     83    el.removeAttribute(delList[i]);
     84  }
     85 
     86  var numPropCounted = 0;
     87 
     88  for (var prop in el.dataset) {
     89    if (el.dataset[prop] == "aaaaaa") {
     90      ++numPropCounted;
     91    }
     92  }
     93 
     94  is(numPropCounted, numProp, 'Number of enumerable dataset properties is incorrent after attribute removal.');
     95 }
     96 
     97 /**
     98 * Adds attributes in "attrList" to element.
     99 * Checks for "numProp" properties in dataset.
    100 */
    101 function Enumerate(attrList, numProp)
    102 {
    103  var el = document.createElement('div');
    104 
    105  // Adds attributes in "attrList" to element.
    106  for (var i = 0; i < attrList.length; ++i) {
    107    el.setAttribute(attrList[i], "aaaaaa");
    108  }
    109 
    110  var numPropCounted = 0;
    111 
    112  for (var prop in el.dataset) {
    113    if (el.dataset[prop] == "aaaaaa") {
    114      ++numPropCounted;
    115    }
    116  }
    117 
    118  is(numPropCounted, numProp, 'Number of enumerable dataset properties is incorrect.');
    119 }
    120 
    121 /**
    122 * Adds dataset property then removes attribute from element and check for presence of
    123 * properties using the "in" operator.
    124 */
    125 function AddPropDelAttr(attr, prop)
    126 {
    127  var el = document.createElement('div');
    128 
    129  el.dataset[prop] = 'dddddd';
    130  is(prop in el.dataset, true, 'Operator "in" should return true after setting property.');
    131  el.removeAttribute(attr);
    132  is(prop in el.dataset, false, 'Operator "in" should return false for removed attribute.');
    133 }
    134 
    135 /**
    136 * Adds then removes attribute from element and check for presence of properties using the
    137 * "in" operator.
    138 */
    139 function AddDelAttr(attr, prop)
    140 {
    141  var el = document.createElement('div');
    142 
    143  el.setAttribute(attr, 'dddddd');
    144  is(prop in el.dataset, true, 'Operator "in" should return true after setting attribute.');
    145  el.removeAttribute(attr);
    146  is(prop in el.dataset, false, 'Operator "in" should return false for removed attribute.');
    147 }
    148 
    149 // Typical use case.
    150 SetGetOverwriteDel('data-property', 'property');
    151 SetGetOverwriteDel('data-a-longer-property', 'aLongerProperty');
    152 
    153 AddDelAttr('data-property', 'property');
    154 AddDelAttr('data-a-longer-property', 'aLongerProperty');
    155 
    156 AddPropDelAttr('data-property', 'property');
    157 AddPropDelAttr('data-a-longer-property', 'aLongerProperty');
    158 
    159 // Empty property name.
    160 SetGetOverwriteDel('data-', '');
    161 
    162 // Leading dash characters.
    163 SetGetOverwriteDel('data--', '-');
    164 SetGetOverwriteDel('data--d', 'D');
    165 SetGetOverwriteDel('data---d', '-D');
    166 
    167 // Trailing dash characters.
    168 SetGetOverwriteDel('data-d-', 'd-');
    169 SetGetOverwriteDel('data-d--', 'd--');
    170 SetGetOverwriteDel('data-d-d-', 'dD-');
    171 
    172 // "data-" in attribute name.
    173 SetGetOverwriteDel('data-data-', 'data-');
    174 SetGetOverwriteDel('data-data-data-', 'dataData-');
    175 
    176 // Longer attribute.
    177 SetGetOverwriteDel('data-long-long-long-long-long-long-long-long-long-long-long-long-long', 'longLongLongLongLongLongLongLongLongLongLongLongLong');
    178 
    179 var longAttr = 'data-long';
    180 var longProp = 'long';
    181 for (var i = 0; i < 30000; ++i) {
    182  // Create really long attribute and property names.
    183  longAttr += '-long';
    184  longProp += 'Long';
    185 }
    186 
    187 SetGetOverwriteDel(longAttr, longProp);
    188 
    189 // Syntax error in setting dataset property (dash followed by lower case).
    190 SetExpectException('-a');
    191 SetExpectException('a-a');
    192 SetExpectException('a-a-a');
    193 
    194 // Invalid character.
    195 SetExpectException('a a');
    196 
    197 // Enumeration over dataset properties.
    198 Enumerate(['data-a-big-fish'], 1);
    199 Enumerate(['dat-a-big-fish'], 0);
    200 Enumerate(['data-'], 1);
    201 Enumerate(['data-', 'data-more-data'], 2);
    202 Enumerate(['daaata-', 'data-more-data'], 1);
    203 
    204 // Delete data attributes and enumerate properties.
    205 DelAttrEnumerate(['data-one', 'data-two'], ['data-one'], 1);
    206 DelAttrEnumerate(['data-one', 'data-two'], ['data-three'], 2);
    207 DelAttrEnumerate(['data-one', 'data-two'], ['one'], 2);
    208 </script>
    209 </pre>
    210 </body>
    211 </html>