From 7b637b1eeee08852d2069ce0d67bac4d11c57d58 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Wed, 15 Jan 2025 22:12:57 +1300 Subject: [PATCH] LibWeb/HTML: Only use maxlength attribute when applicable --- Libraries/LibWeb/HTML/HTMLInputElement.cpp | 21 +++++++++++++++++-- .../the-input-element/maxlength-number.txt | 6 ++++++ .../the-input-element/maxlength-number.html | 19 +++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-input-element/maxlength-number.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-input-element/maxlength-number.html diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp index d84ba2f1330..6d32f8dfea9 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -2,7 +2,7 @@ * Copyright (c) 2018-2023, Andreas Kling * Copyright (c) 2022, Adam Hodgen * Copyright (c) 2022, Andrew Kaster - * Copyright (c) 2023-2024, Shannon Booth + * Copyright (c) 2023-2025, Shannon Booth * Copyright (c) 2023, Bastiaan van der Plaat * Copyright (c) 2024, Jelle Raaijmakers * Copyright (c) 2024, Fernando Kiotheka @@ -721,10 +721,27 @@ static bool is_allowed_to_be_readonly(HTML::HTMLInputElement::TypeAttributeState } } +// https://html.spec.whatwg.org/multipage/input.html#the-input-element:attr-input-maxlength-3 +static bool is_applicable_for_maxlength_attribute(HTML::HTMLInputElement::TypeAttributeState state) +{ + switch (state) { + case HTML::HTMLInputElement::TypeAttributeState::Text: + case HTML::HTMLInputElement::TypeAttributeState::Search: + case HTML::HTMLInputElement::TypeAttributeState::Telephone: + case HTML::HTMLInputElement::TypeAttributeState::URL: + case HTML::HTMLInputElement::TypeAttributeState::Email: + case HTML::HTMLInputElement::TypeAttributeState::Password: + return true; + default: + return false; + } +} + // https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength void HTMLInputElement::handle_maxlength_attribute() { - if (m_text_node) { + // The maxlength attribute, when it applies, is a form control maxlength attribute. + if (m_text_node && is_applicable_for_maxlength_attribute(type_state())) { auto max_length = this->max_length(); if (max_length >= 0) { m_text_node->set_max_length(max_length); diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-input-element/maxlength-number.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-input-element/maxlength-number.txt new file mode 100644 index 00000000000..af8fcdc4864 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-input-element/maxlength-number.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass maxlength doesn't apply to input type=number \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-input-element/maxlength-number.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-input-element/maxlength-number.html new file mode 100644 index 00000000000..345c6e2b224 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-input-element/maxlength-number.html @@ -0,0 +1,19 @@ + +input type=number maxlength + + + + + + + +