mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:28:11 +00:00
LibWeb: Add textarea value properties
This commit is contained in:
parent
e33ae9a58b
commit
091faf1aae
5 changed files with 105 additions and 9 deletions
4
Tests/LibWeb/Text/expected/textarea-value.txt
Normal file
4
Tests/LibWeb/Text/expected/textarea-value.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1. "PASS"
|
||||
2. "PASS"
|
||||
3. "PASS"
|
||||
4. 4
|
39
Tests/LibWeb/Text/input/textarea-value.html
Normal file
39
Tests/LibWeb/Text/input/textarea-value.html
Normal file
|
@ -0,0 +1,39 @@
|
|||
<script src="include.js"></script>
|
||||
<script>
|
||||
test(() => {
|
||||
let testCounter = 1;
|
||||
function testPart(part) {
|
||||
println(`${testCounter++}. ${JSON.stringify(part())}`);
|
||||
}
|
||||
|
||||
// 1. Textarea get value
|
||||
testPart(() => {
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.textContent = 'PASS';
|
||||
return textarea.value;
|
||||
});
|
||||
|
||||
// 2. Textarea set value
|
||||
testPart(() => {
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.textContent = 'PASS';
|
||||
textarea.value = 'FAIL';
|
||||
return textarea.textContent;
|
||||
});
|
||||
|
||||
// 3. Textarea set default value
|
||||
testPart(() => {
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.textContent = 'FAIL';
|
||||
textarea.defaultValue = 'PASS';
|
||||
return textarea.textContent;
|
||||
});
|
||||
|
||||
// 4. Textarea text length
|
||||
testPart(() => {
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.innerHTML = 'PASS';
|
||||
return textarea.textLength;
|
||||
});
|
||||
});
|
||||
</script>
|
|
@ -98,6 +98,51 @@ void HTMLTextAreaElement::form_associated_element_was_removed(DOM::Node*)
|
|||
set_shadow_root(nullptr);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-defaultvalue
|
||||
String HTMLTextAreaElement::default_value() const
|
||||
{
|
||||
// The defaultValue attribute's getter must return the element's child text content.
|
||||
return child_text_content();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-defaultvalue
|
||||
void HTMLTextAreaElement::set_default_value(String const& default_value)
|
||||
{
|
||||
// The defaultValue attribute's setter must string replace all with the given value within this element.
|
||||
string_replace_all(default_value);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
|
||||
String HTMLTextAreaElement::value() const
|
||||
{
|
||||
// The value IDL attribute must, on getting, return the element's API value.
|
||||
return m_raw_value;
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value
|
||||
void HTMLTextAreaElement::set_value(String const& value)
|
||||
{
|
||||
// FIXME: 1. Let oldAPIValue be this element's API value.
|
||||
|
||||
// 2. Set this element's raw value to the new value.
|
||||
m_raw_value = value;
|
||||
|
||||
// 3. Set this element's dirty value flag to true.
|
||||
m_dirty = true;
|
||||
|
||||
// FIXME: 4. If the new API value is different from oldAPIValue, then move the text entry cursor position to the end of the text control, unselecting any selected text and resetting the selection direction to "none".
|
||||
update_placeholder_visibility();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-textlength
|
||||
u32 HTMLTextAreaElement::text_length() const
|
||||
{
|
||||
// The textLength IDL attribute must return the length of the element's API value.
|
||||
// FIXME: This is inefficient!
|
||||
auto utf16_data = MUST(AK::utf8_to_utf16(m_raw_value));
|
||||
return Utf16View { utf16_data }.length_in_code_units();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-cols
|
||||
unsigned HTMLTextAreaElement::cols() const
|
||||
{
|
||||
|
@ -109,9 +154,9 @@ unsigned HTMLTextAreaElement::cols() const
|
|||
return 20;
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_cols(unsigned value)
|
||||
WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_cols(unsigned cols)
|
||||
{
|
||||
return set_attribute(HTML::AttributeNames::cols, MUST(String::number(value)));
|
||||
return set_attribute(HTML::AttributeNames::cols, MUST(String::number(cols)));
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-rows
|
||||
|
@ -125,9 +170,9 @@ unsigned HTMLTextAreaElement::rows() const
|
|||
return 2;
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_rows(unsigned value)
|
||||
WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_rows(unsigned rows)
|
||||
{
|
||||
return set_attribute(HTML::AttributeNames::rows, MUST(String::number(value)));
|
||||
return set_attribute(HTML::AttributeNames::rows, MUST(String::number(rows)));
|
||||
}
|
||||
|
||||
void HTMLTextAreaElement::create_shadow_tree_if_needed()
|
||||
|
|
|
@ -69,11 +69,19 @@ public:
|
|||
// https://www.w3.org/TR/html-aria/#el-textarea
|
||||
virtual Optional<ARIA::Role> default_role() const override { return ARIA::Role::textbox; }
|
||||
|
||||
String default_value() const;
|
||||
void set_default_value(String const&);
|
||||
|
||||
String value() const override;
|
||||
void set_value(String const&);
|
||||
|
||||
u32 text_length() const;
|
||||
|
||||
unsigned cols() const;
|
||||
WebIDL::ExceptionOr<void> set_cols(unsigned value);
|
||||
WebIDL::ExceptionOr<void> set_cols(unsigned);
|
||||
|
||||
unsigned rows() const;
|
||||
WebIDL::ExceptionOr<void> set_rows(unsigned value);
|
||||
WebIDL::ExceptionOr<void> set_rows(unsigned);
|
||||
|
||||
private:
|
||||
HTMLTextAreaElement(DOM::Document&, DOM::QualifiedName);
|
||||
|
|
|
@ -21,9 +21,9 @@ interface HTMLTextAreaElement : HTMLElement {
|
|||
[CEReactions, Reflect] attribute DOMString wrap;
|
||||
|
||||
readonly attribute DOMString type;
|
||||
// FIXME: [CEReactions] attribute DOMString defaultValue;
|
||||
// FIXME: attribute [LegacyNullToEmptyString] DOMString value;
|
||||
// FIXME: readonly attribute unsigned long textLength;
|
||||
[CEReactions] attribute DOMString defaultValue;
|
||||
[LegacyNullToEmptyString] attribute DOMString value;
|
||||
readonly attribute unsigned long textLength;
|
||||
|
||||
// FIXME: readonly attribute boolean willValidate;
|
||||
// FIXME: readonly attribute ValidityState validity;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue