Skip to content

Commit d70ad90

Browse files
committed
one more test and some comments
1 parent 51e5ed8 commit d70ad90

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

src/main/java/org/htmlunit/html/HtmlInput.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,14 @@ public boolean isTooShortValidityState() {
10441044
return getValue().length() < getMinLength();
10451045
}
10461046

1047+
// no need to override isTooLongValidityState()
1048+
// The HTML spec (§4.10.18.5) has a deliberate rule: tooLong only fires
1049+
// if the user has interacted with the field ("the element has a dirty value flag").
1050+
// A value set via JS (elem.value = '...') that was never touched by the user does
1051+
// not set the dirty flag, so tooLong stays false regardless of the value length.
1052+
// see HtmlTextInputTest
1053+
// maxLengthValidationInvalid()/maxLengthValidationInvalidInitial()/maxLengthValidationValid()
1054+
10471055
@Override
10481056
public boolean isValidValidityState() {
10491057
return !isCustomErrorValidityState()

src/test/java/org/htmlunit/html/HtmlTextInputTest.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,21 @@ public void minLengthValidationValid() throws Exception {
909909
validation("<input type='text' minlength='5' id='e1' name='k'>\n", "", "abcdefghi");
910910
}
911911

912+
/**
913+
* @throws Exception if an error occurs
914+
*/
915+
@Test
916+
@Alerts({"null",
917+
"",
918+
"true",
919+
"false-false-false-false-false-false-false-false-false-true-false",
920+
"true",
921+
"§§URL§§?k=x", "2"})
922+
public void validationMinLengthJs() throws Exception {
923+
validation("<input type='text' id='e1' name='k' minlength='2'>\n",
924+
"elem.value = 'x';", null);
925+
}
926+
912927
/**
913928
* @throws Exception if an error occurs
914929
*/
@@ -997,6 +1012,64 @@ public void validationEmpty() throws Exception {
9971012
validation("<input type='text' id='e1' name='k'>\n", "", null);
9981013
}
9991014

1015+
/**
1016+
* @throws Exception if an error occurs
1017+
*/
1018+
@Test
1019+
@Alerts({"null",
1020+
"x",
1021+
"false",
1022+
"false-false-false-false-false-false-false-true-false-false-false",
1023+
"true",
1024+
"§§URL§§", "1"})
1025+
public void validationMinLength() throws Exception {
1026+
validation("<input type='text' id='e1' name='k' minlength='2'>\n", "", "x");
1027+
}
1028+
1029+
/**
1030+
* @throws Exception if an error occurs
1031+
*/
1032+
@Test
1033+
@Alerts({"null",
1034+
"xy",
1035+
"true",
1036+
"false-false-false-false-false-false-false-false-false-true-false",
1037+
"true",
1038+
"§§URL§§?k=xy", "2"})
1039+
public void validationMaxLength() throws Exception {
1040+
validation("<input type='text' id='e1' name='k' maxlength='2'>\n", "", "xyz");
1041+
}
1042+
1043+
/**
1044+
* @throws Exception if an error occurs
1045+
*/
1046+
@Test
1047+
@Alerts({"null",
1048+
"",
1049+
"true",
1050+
"false-false-false-false-false-false-false-false-false-true-false",
1051+
"true",
1052+
"§§URL§§?k=toooLong", "2"})
1053+
public void validationMaxLengthJs() throws Exception {
1054+
validation("<input type='text' id='e1' name='k' maxlength='2'>\n",
1055+
"elem.value = 'toooLong';", null);
1056+
}
1057+
1058+
/**
1059+
* @throws Exception if an error occurs
1060+
*/
1061+
@Test
1062+
@Alerts({"null",
1063+
"",
1064+
"true",
1065+
"false-false-false-false-false-false-false-false-false-true-false",
1066+
"true",
1067+
"§§URL§§?k=toooLong", "2"})
1068+
public void validationMaxLengthJsEvent() throws Exception {
1069+
validation("<input type='text' id='e1' name='k' maxlength='2'>\n",
1070+
"elem.value = 'toooLong';elem.dispatchEvent(new Event('input'));", null);
1071+
}
1072+
10001073
/**
10011074
* @throws Exception if an error occurs
10021075
*/

0 commit comments

Comments
 (0)