trix
trix copied to clipboard
W3C Constraint Validation Violation: `setCustomValidity` Silently Erased on User Input
Location
- File: src/trix/elements/trix_editor_element.js
- Lines: 230–235, 244–256
Root Cause
In ElementInternalsDelegate:
setFormValue(value) {
this.value = value
this.#validate()
this.#internals.setFormValue(this.element.disabled ? undefined : this.value)
}
setCustomValidity(validationMessage) {
this.#validate(validationMessage)
}
#validate(customValidationMessage = "") {
const { required, value } = this.element
const valueMissing = required && !value
const customError = !!customValidationMessage
const input = makeElement("input", { required })
const validationMessage = customValidationMessage || input.validationMessage
this.#internals.setValidity({ valueMissing, customError }, validationMessage)
}
- Under the WHATWG HTML Constraint Validation Specification, calling
element.setCustomValidity("Error message")sets the control's custom validity error state. This error state must persist until explicitly cleared by callingelement.setCustomValidity(""). - In
ElementInternalsDelegate, there is no private#customValidationMessagefield stored on the instance. - When
setCustomValidity("Please enter at least 50 words")is called,#validate("...")setscustomError: true. - However, as soon as the user types a single character in the editor,
EditorController.updateInputElementcallssetFormValue(value). setFormValueinvokesthis.#validate()with no arguments, which evaluates the default parametercustomValidationMessage = "".customErrorbecomesfalse, immediately erasing the developer's custom validation message and marking the form valid on the user's first keystroke.
Reproduction
const el = document.querySelector("trix-editor")
el.setCustomValidity("Character limit not reached")
console.log(el.checkValidity()) // false
console.log(el.validationMessage) // "Character limit not reached"
// User types one character
el.editor.insertString("a")
// Custom error was wiped out!
console.log(el.checkValidity()) // true (Expected: false until cleared by setCustomValidity(""))
console.log(el.validationMessage) // "" (Expected: "Character limit not reached")
Proposed Patch
In src/trix/elements/trix_editor_element.js:
class ElementInternalsDelegate {
value = ""
#internals
#formDisabled
+ #customValidationMessage = ""
constructor(element) {
this.element = element
this.#internals = element.attachInternals()
this.#formDisabled = false
}
setCustomValidity(validationMessage) {
+ this.#customValidationMessage = validationMessage || ""
- this.#validate(validationMessage)
+ this.#validate()
}
- #validate(customValidationMessage = "") {
+ #validate() {
const { required, value } = this.element
const valueMissing = required && !value
- const customError = !!customValidationMessage
+ const customError = !!this.#customValidationMessage
const input = makeElement("input", { required })
- const validationMessage = customValidationMessage || input.validationMessage
+ const validationMessage = this.#customValidationMessage || input.validationMessage
this.#internals.setValidity({ valueMissing, customError }, validationMessage)
}