test_bug535043.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=535043
  5. -->
  6. <head>
  7. <title>Test for Bug 535043</title>
  8. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. </head>
  12. <body>
  13. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=535043">Mozilla Bug 535043</a>
  14. <p id="display"></p>
  15. <div id="content">
  16. <textarea></textarea>
  17. <textarea maxlength="-1"></textarea>
  18. <textarea maxlength="0"></textarea>
  19. <textarea maxlength="2"></textarea>
  20. </div>
  21. <pre id="test">
  22. <script type="text/javascript">
  23. /** Test for Bug 535043 **/
  24. function checkTextArea(textArea) {
  25. textArea.value = '';
  26. textArea.focus();
  27. for (var j = 0; j < 3; j++) {
  28. synthesizeKey('x', {});
  29. }
  30. var htmlMaxLength = textArea.getAttribute('maxlength');
  31. var domMaxLength = textArea.maxLength;
  32. if (htmlMaxLength == null) {
  33. is(domMaxLength, -1,
  34. 'maxlength is unset but maxLength DOM attribute is not -1');
  35. } else if (htmlMaxLength < 0) {
  36. // Per the HTML5 spec, out-of-range values are supposed to translate to -1,
  37. // not 0, but they don't?
  38. is(domMaxLength, -1,
  39. 'maxlength is out of range but maxLength DOM attribute is not -1');
  40. } else {
  41. is(domMaxLength, parseInt(htmlMaxLength),
  42. 'maxlength in DOM does not match provided value');
  43. }
  44. if (textArea.maxLength == -1) {
  45. is(textArea.value.length, 3,
  46. 'textarea with maxLength -1 should have no length limit');
  47. } else {
  48. is(textArea.value.length, textArea.maxLength, 'textarea has maxLength ' +
  49. textArea.maxLength + ' but length ' + textArea.value.length );
  50. }
  51. }
  52. SimpleTest.waitForFocus(function() {
  53. var textAreas = document.getElementsByTagName('textarea');
  54. for (var i = 0; i < textAreas.length; i++) {
  55. checkTextArea(textAreas[i]);
  56. }
  57. textArea = textAreas[0];
  58. testNums = [-42, -1, 0, 2];
  59. for (var i = 0; i < testNums.length; i++) {
  60. textArea.removeAttribute('maxlength');
  61. var caught = false;
  62. try {
  63. textArea.maxLength = testNums[i];
  64. } catch (e) {
  65. caught = true;
  66. }
  67. if (testNums[i] < 0) {
  68. ok(caught, 'Setting negative maxLength should throw exception');
  69. } else {
  70. ok(!caught, 'Setting nonnegative maxLength should not throw exception');
  71. }
  72. checkTextArea(textArea);
  73. textArea.setAttribute('maxlength', testNums[i]);
  74. checkTextArea(textArea);
  75. }
  76. SimpleTest.finish();
  77. });
  78. SimpleTest.waitForExplicitFinish();
  79. </script>
  80. </pre>
  81. </body>
  82. </html>