LibWeb: Support displaying HTMLInputElement placeholder values

This adds support for parsing the ::placeholder pseudo-element and
injecting an anonymous layout node with that element when the input
element's data is empty.
This commit is contained in:
Timothy Flynn 2022-11-30 22:15:12 -05:00 committed by Tim Flynn
parent fddbc2e378
commit 4a30446999
Notes: sideshowbarker 2024-07-17 03:48:55 +09:00
5 changed files with 91 additions and 0 deletions

View file

@ -317,6 +317,47 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value(String value)
return {};
}
// https://html.spec.whatwg.org/multipage/input.html#the-input-element:attr-input-placeholder-3
static bool is_allowed_to_have_placeholder(HTML::HTMLInputElement::TypeAttributeState state)
{
switch (state) {
case HTML::HTMLInputElement::TypeAttributeState::Text:
case HTML::HTMLInputElement::TypeAttributeState::Search:
case HTML::HTMLInputElement::TypeAttributeState::URL:
case HTML::HTMLInputElement::TypeAttributeState::Telephone:
case HTML::HTMLInputElement::TypeAttributeState::Email:
case HTML::HTMLInputElement::TypeAttributeState::Password:
case HTML::HTMLInputElement::TypeAttributeState::Number:
return true;
default:
return false;
}
}
// https://html.spec.whatwg.org/multipage/input.html#attr-input-placeholder
Optional<String> HTMLInputElement::placeholder_value() const
{
if (!m_text_node || !m_text_node->data().is_empty())
return {};
if (!is_allowed_to_have_placeholder(type_state()))
return {};
if (!has_attribute(HTML::AttributeNames::placeholder))
return {};
auto placeholder = attribute(HTML::AttributeNames::placeholder);
if (placeholder.contains('\r') || placeholder.contains('\n')) {
StringBuilder builder;
for (auto ch : placeholder) {
if (ch != '\r' && ch != '\n')
builder.append(ch);
}
placeholder = builder.to_string();
}
return placeholder;
}
void HTMLInputElement::create_shadow_tree_if_needed()
{
if (shadow_root())