test_bug560112.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 type="application/javascript" 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. /** Test for Bug 560112 **/
  19. /**
  20. * Sets dataset property. Checks data attribute "attr".
  21. * Gets dataset property. Checks data attribute "attr".
  22. * Overwrites dataset property Checks data attribute "attr".
  23. * Deletes dataset property. Checks data attribute "attr".
  24. */
  25. function SetGetOverwriteDel(attr, prop)
  26. {
  27. var el = document.createElement('div');
  28. // Set property.
  29. is(prop in el.dataset, false, 'Property should not be in dataset before setting.');
  30. el.dataset[prop] = "zzzzzz";
  31. is(prop in el.dataset, true, 'Property should be in dataset after setting.');
  32. ok(el.hasAttribute(attr), 'Element should have data attribute for dataset property "' + prop + '".');
  33. // Get property.
  34. is(el.dataset[prop], "zzzzzz", 'Dataset property "' + prop + '" should have value "zzzzzz".');
  35. is(el.getAttribute(attr), "zzzzzz", 'Attribute "' + attr + '" should have value "zzzzzz".');
  36. // Overwrite property.
  37. el.dataset[prop] = "yyyyyy";
  38. is(el.dataset[prop], "yyyyyy", 'Dataset property "' + prop + '" should have value "yyyyyy".');
  39. is(el.getAttribute(attr), "yyyyyy", 'Attribute "' + attr + '" should have value "yyyyyy".');
  40. // Delete property.
  41. delete el.dataset[prop];
  42. ok(!el.hasAttribute(attr), 'Element should not have data attribute for dataset property "' + prop + '".');
  43. is(prop in el.dataset, false, 'Deleted property should not be in dataset.');
  44. }
  45. /**
  46. * Sets dataset property and expects exception.
  47. */
  48. function SetExpectException(prop)
  49. {
  50. var el = document.createElement('div');
  51. try {
  52. el.dataset[prop] = "xxxxxx";
  53. ok(false, 'Exception should have been thrown.');
  54. } catch (ex) {
  55. ok(true, 'Exception should have been thrown.');
  56. }
  57. }
  58. /**
  59. * Adds attributes in "attrList" to element.
  60. * Deletes attributes in "delList" from element.
  61. * Checks for "numProp" properties in dataset.
  62. */
  63. function DelAttrEnumerate(attrList, delList, numProp)
  64. {
  65. var el = document.createElement('div');
  66. // Adds attributes in "attrList".
  67. for (var i = 0; i < attrList.length; ++i) {
  68. el.setAttribute(attrList[i], "aaaaaa");
  69. }
  70. // Remove attributes in "delList".
  71. for (var i = 0; i < delList.length; ++i) {
  72. el.removeAttribute(delList[i]);
  73. }
  74. var numPropCounted = 0;
  75. for (var prop in el.dataset) {
  76. if (el.dataset[prop] == "aaaaaa") {
  77. ++numPropCounted;
  78. }
  79. }
  80. is(numPropCounted, numProp, 'Number of enumerable dataset properties is incorrent after attribute removal.');
  81. }
  82. /**
  83. * Adds attributes in "attrList" to element.
  84. * Checks for "numProp" properties in dataset.
  85. */
  86. function Enumerate(attrList, numProp)
  87. {
  88. var el = document.createElement('div');
  89. // Adds attributes in "attrList" to element.
  90. for (var i = 0; i < attrList.length; ++i) {
  91. el.setAttribute(attrList[i], "aaaaaa");
  92. }
  93. var numPropCounted = 0;
  94. for (var prop in el.dataset) {
  95. if (el.dataset[prop] == "aaaaaa") {
  96. ++numPropCounted;
  97. }
  98. }
  99. is(numPropCounted, numProp, 'Number of enumerable dataset properties is incorrect.');
  100. }
  101. /**
  102. * Adds dataset property then removes attribute from element and check for presence of
  103. * properties using the "in" operator.
  104. */
  105. function AddPropDelAttr(attr, prop)
  106. {
  107. var el = document.createElement('div');
  108. el.dataset[prop] = 'dddddd';
  109. is(prop in el.dataset, true, 'Operator "in" should return true after setting property.');
  110. el.removeAttribute(attr);
  111. is(prop in el.dataset, false, 'Operator "in" should return false for removed attribute.');
  112. }
  113. /**
  114. * Adds then removes attribute from element and check for presence of properties using the
  115. * "in" operator.
  116. */
  117. function AddDelAttr(attr, prop)
  118. {
  119. var el = document.createElement('div');
  120. el.setAttribute(attr, 'dddddd');
  121. is(prop in el.dataset, true, 'Operator "in" should return true after setting attribute.');
  122. el.removeAttribute(attr);
  123. is(prop in el.dataset, false, 'Operator "in" should return false for removed attribute.');
  124. }
  125. // Typical use case.
  126. SetGetOverwriteDel('data-property', 'property');
  127. SetGetOverwriteDel('data-a-longer-property', 'aLongerProperty');
  128. AddDelAttr('data-property', 'property');
  129. AddDelAttr('data-a-longer-property', 'aLongerProperty');
  130. AddPropDelAttr('data-property', 'property');
  131. AddPropDelAttr('data-a-longer-property', 'aLongerProperty');
  132. // Empty property name.
  133. SetGetOverwriteDel('data-', '');
  134. // Leading dash characters.
  135. SetGetOverwriteDel('data--', '-');
  136. SetGetOverwriteDel('data--d', 'D');
  137. SetGetOverwriteDel('data---d', '-D');
  138. // Trailing dash characters.
  139. SetGetOverwriteDel('data-d-', 'd-');
  140. SetGetOverwriteDel('data-d--', 'd--');
  141. SetGetOverwriteDel('data-d-d-', 'dD-');
  142. // "data-" in attribute name.
  143. SetGetOverwriteDel('data-data-', 'data-');
  144. SetGetOverwriteDel('data-data-data-', 'dataData-');
  145. // Longer attribute.
  146. SetGetOverwriteDel('data-long-long-long-long-long-long-long-long-long-long-long-long-long', 'longLongLongLongLongLongLongLongLongLongLongLongLong');
  147. var longAttr = 'data-long';
  148. var longProp = 'long';
  149. for (var i = 0; i < 30000; ++i) {
  150. // Create really long attribute and property names.
  151. longAttr += '-long';
  152. longProp += 'Long';
  153. }
  154. SetGetOverwriteDel(longAttr, longProp);
  155. // Syntax error in setting dataset property (dash followed by lower case).
  156. SetExpectException('-a');
  157. SetExpectException('a-a');
  158. SetExpectException('a-a-a');
  159. // Invalid character.
  160. SetExpectException('a a');
  161. // Enumeration over dataset properties.
  162. Enumerate(['data-a-big-fish'], 1);
  163. Enumerate(['dat-a-big-fish'], 0);
  164. Enumerate(['data-'], 1);
  165. Enumerate(['data-', 'data-more-data'], 2);
  166. Enumerate(['daaata-', 'data-more-data'], 1);
  167. // Delete data attributes and enumerate properties.
  168. DelAttrEnumerate(['data-one', 'data-two'], ['data-one'], 1);
  169. DelAttrEnumerate(['data-one', 'data-two'], ['data-three'], 2);
  170. DelAttrEnumerate(['data-one', 'data-two'], ['one'], 2);
  171. </script>
  172. </pre>
  173. </body>
  174. </html>