Bug 1896923 - Use normalize-newlines algorithm for setCustomValidity r=emilio

This is for https://github.com/whatwg/html/pull/10350

Differential Revision: https://phabricator.services.mozilla.com/D210688
This commit is contained in:
Sean Feng 2024-05-17 13:11:20 +00:00
parent 896ae1534e
commit d9ab1cd36a
2 changed files with 19 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include "mozilla/ErrorResult.h"
#include "nsAString.h"
#include "nsIContent.h"
#include "nsContentUtils.h"
namespace mozilla::dom {
@ -59,7 +60,9 @@ bool ConstraintValidation::CheckValidity() {
ConstraintValidation::ConstraintValidation() = default;
void ConstraintValidation::SetCustomValidity(const nsAString& aError) {
mCustomValidity.Assign(aError);
nsAutoString error(aError);
nsContentUtils::PlatformToDOMLineBreaks(error);
mCustomValidity.Assign(error);
SetValidityState(VALIDITY_STATE_CUSTOM_ERROR, !mCustomValidity.IsEmpty());
}

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<form>
<input id="email" required>
</form>
<script>
test(function() {
const input = document.getElementById("email");
input.setCustomValidity("First line\rSecond line\r\nThird line\nFourth line");
assert_equals(input.validationMessage, "First line\nSecond line\nThird line\nFourth line");
}, "setCustomValidity should normalize newlines from the given error message");
</script>
</html>