test_maxlength_attribute.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=345624
  5. -->
  6. <head>
  7. <title>Test for Bug 345624</title>
  8. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. <style>
  12. input, textarea { background-color: rgb(0,0,0) !important; }
  13. :-moz-any(input,textarea):valid { background-color: rgb(0,255,0) !important; }
  14. :-moz-any(input,textarea):invalid { background-color: rgb(255,0,0) !important; }
  15. </style>
  16. </head>
  17. <body>
  18. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=345624">Mozilla Bug 345624</a>
  19. <p id="display"></p>
  20. <div id="content">
  21. <input id='i'>
  22. <textarea id='t'></textarea>
  23. </div>
  24. <pre id="test">
  25. <script type="application/javascript">
  26. /** Test for Bug 345624 **/
  27. /**
  28. * This test is checking only tooLong related features
  29. * related to constraint validation.
  30. */
  31. function checkTooLongValidity(element)
  32. {
  33. element.value = "foo";
  34. ok(!element.validity.tooLong,
  35. "Element should not be too long when maxlength is not set");
  36. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  37. "rgb(0, 255, 0)", ":valid pseudo-class should apply");
  38. ok(element.validity.valid, "Element should be valid");
  39. ok(element.checkValidity(), "The element should be valid");
  40. element.maxLength = 1;
  41. ok(!element.validity.tooLong,
  42. "Element should not be too long unless the user edits it");
  43. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  44. "rgb(0, 255, 0)", ":valid pseudo-class should apply");
  45. ok(element.validity.valid, "Element should be valid");
  46. ok(element.checkValidity(), "The element should be valid");
  47. element.focus();
  48. synthesizeKey("VK_BACK_SPACE", {});
  49. is(element.value, "fo", "value should have changed");
  50. ok(element.validity.tooLong,
  51. "Element should be too long after a user edit that does not make it short enough");
  52. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  53. "rgb(255, 0, 0)", ":invalid pseudo-class should apply");
  54. ok(!element.validity.valid, "Element should be invalid");
  55. ok(!element.checkValidity(), "The element should not be valid");
  56. is(element.validationMessage,
  57. "Please shorten this text to 1 characters or less (you are currently using 2 characters).",
  58. "The validation message text is not correct");
  59. synthesizeKey("VK_BACK_SPACE", {});
  60. is(element.value, "f", "value should have changed");
  61. ok(!element.validity.tooLong,
  62. "Element should not be too long after a user edit makes it short enough");
  63. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  64. "rgb(0, 255, 0)", ":valid pseudo-class should apply");
  65. ok(element.validity.valid, "Element should be valid");
  66. element.maxLength = 2;
  67. ok(!element.validity.tooLong,
  68. "Element should remain valid if maxlength changes but maxlength > length");
  69. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  70. "rgb(0, 255, 0)", ":valid pseudo-class should apply");
  71. ok(element.validity.valid, "Element should be valid");
  72. element.maxLength = 1;
  73. ok(!element.validity.tooLong,
  74. "Element should remain valid if maxlength changes but maxlength = length");
  75. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  76. "rgb(0, 255, 0)", ":valid pseudo-class should apply");
  77. ok(element.validity.valid, "Element should be valid");
  78. ok(element.checkValidity(), "The element should be valid");
  79. element.maxLength = 0;
  80. ok(element.validity.tooLong,
  81. "Element should become invalid if maxlength changes and maxlength < length");
  82. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  83. "rgb(255, 0, 0)", ":invalid pseudo-class should apply");
  84. ok(!element.validity.valid, "Element should be invalid");
  85. ok(!element.checkValidity(), "The element should not be valid");
  86. is(element.validationMessage,
  87. "Please shorten this text to 0 characters or less (you are currently using 1 characters).",
  88. "The validation message text is not correct");
  89. element.maxLength = 1;
  90. ok(!element.validity.tooLong,
  91. "Element should become valid if maxlength changes and maxlength = length");
  92. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  93. "rgb(0, 255, 0)", ":valid pseudo-class should apply");
  94. ok(element.validity.valid, "Element should be valid");
  95. ok(element.checkValidity(), "The element should be valid");
  96. element.value = "test";
  97. ok(!element.validity.tooLong,
  98. "Element should stay valid after programmatic edit (even if value is too long)");
  99. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  100. "rgb(0, 255, 0)", ":valid pseudo-class should apply");
  101. ok(element.validity.valid, "Element should be valid");
  102. ok(element.checkValidity(), "The element should be valid");
  103. element.setCustomValidity("custom message");
  104. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  105. "rgb(255, 0, 0)", ":invalid pseudo-class should apply");
  106. is(element.validationMessage, "custom message",
  107. "Custom message should be shown instead of too long one");
  108. }
  109. checkTooLongValidity(document.getElementById('i'));
  110. checkTooLongValidity(document.getElementById('t'));
  111. </script>
  112. </pre>
  113. </body>
  114. </html>