1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 10:17:35 +00:00

LibWeb: Make Element attribute getters take a StringView

These functions are deferring to NamedNodeMap::get_attribute which
already takes a StringView. This changes also leads to finding some
places which were passing though a const char* instead of an entry from
Attribute names. Fix that where applicable, and switch to has_attribute
in some of those places instead of deprecated_attribute where
equivalent.

Ideally this should be taking a 'FlyString const&', but to continue
porting away from DeprecatedString, just leave a FIXME for now.
This commit is contained in:
Shannon Booth 2023-09-21 21:22:56 +12:00 committed by Andreas Kling
parent dbf8ff64fb
commit 47514e07b4
6 changed files with 64 additions and 63 deletions

View file

@ -131,7 +131,7 @@ void Element::visit_edges(Cell::Visitor& visitor)
}
// https://dom.spec.whatwg.org/#dom-element-getattribute
DeprecatedString Element::get_attribute(DeprecatedFlyString const& name) const
DeprecatedString Element::get_attribute(StringView name) const
{
// 1. Let attr be the result of getting an attribute given qualifiedName and this.
auto const* attribute = m_attributes->get_attribute(name);
@ -145,7 +145,7 @@ DeprecatedString Element::get_attribute(DeprecatedFlyString const& name) const
}
// https://dom.spec.whatwg.org/#concept-element-attributes-get-value
DeprecatedString Element::get_attribute_value(DeprecatedFlyString const& local_name, DeprecatedFlyString const& namespace_) const
DeprecatedString Element::get_attribute_value(StringView local_name, DeprecatedFlyString const& namespace_) const
{
// 1. Let attr be the result of getting an attribute given namespace, localName, and element.
auto const* attribute = m_attributes->get_attribute_ns(namespace_, local_name);

View file

@ -96,8 +96,9 @@ public:
bool has_attribute_ns(DeprecatedFlyString namespace_, DeprecatedFlyString const& name) const;
bool has_attributes() const;
DeprecatedString deprecated_attribute(DeprecatedFlyString const& name) const { return get_attribute(name); }
Optional<String> attribute(DeprecatedFlyString const& name) const
// FIXME: This should be taking a 'FlyString const&'
DeprecatedString deprecated_attribute(StringView name) const { return get_attribute(name); }
Optional<String> attribute(StringView name) const
{
auto ret = deprecated_attribute(name);
if (ret.is_null())
@ -106,8 +107,8 @@ public:
}
// FIXME: This should be taking a 'FlyString const&' / 'Optional<FlyString> const&'
DeprecatedString get_attribute(DeprecatedFlyString const& name) const;
DeprecatedString get_attribute_value(DeprecatedFlyString const& local_name, DeprecatedFlyString const& namespace_ = {}) const;
DeprecatedString get_attribute(StringView name) const;
DeprecatedString get_attribute_value(StringView local_name, DeprecatedFlyString const& namespace_ = {}) const;
WebIDL::ExceptionOr<void> set_attribute(DeprecatedFlyString const& name, DeprecatedString const& value);
WebIDL::ExceptionOr<void> set_attribute(DeprecatedFlyString const& name, Optional<String> const& value);
@ -264,55 +265,55 @@ public:
}
// https://www.w3.org/TR/wai-aria-1.2/#accessibilityroleandproperties-correspondence
ARIA_IMPL(role, "role");
ARIA_IMPL(aria_active_descendant, "aria-activedescendant");
ARIA_IMPL(aria_atomic, "aria-atomic");
ARIA_IMPL(aria_auto_complete, "aria-autocomplete");
ARIA_IMPL(aria_busy, "aria-busy");
ARIA_IMPL(aria_checked, "aria-checked");
ARIA_IMPL(aria_col_count, "aria-colcount");
ARIA_IMPL(aria_col_index, "aria-colindex");
ARIA_IMPL(aria_col_span, "aria-colspan");
ARIA_IMPL(aria_controls, "aria-controls");
ARIA_IMPL(aria_current, "aria-current");
ARIA_IMPL(aria_described_by, "aria-describedby");
ARIA_IMPL(aria_details, "aria-details");
ARIA_IMPL(aria_drop_effect, "aria-dropeffect");
ARIA_IMPL(aria_error_message, "aria-errormessage");
ARIA_IMPL(aria_disabled, "aria-disabled");
ARIA_IMPL(aria_expanded, "aria-expanded");
ARIA_IMPL(aria_flow_to, "aria-flowto");
ARIA_IMPL(aria_grabbed, "aria-grabbed");
ARIA_IMPL(aria_has_popup, "aria-haspopup");
ARIA_IMPL(aria_hidden, "aria-hidden");
ARIA_IMPL(aria_invalid, "aria-invalid");
ARIA_IMPL(aria_key_shortcuts, "aria-keyshortcuts");
ARIA_IMPL(aria_label, "aria-label");
ARIA_IMPL(aria_labelled_by, "aria-labelledby");
ARIA_IMPL(aria_level, "aria-level");
ARIA_IMPL(aria_live, "aria-live");
ARIA_IMPL(aria_modal, "aria-modal");
ARIA_IMPL(aria_multi_line, "aria-multiline");
ARIA_IMPL(aria_multi_selectable, "aria-multiselectable");
ARIA_IMPL(aria_orientation, "aria-orientation");
ARIA_IMPL(aria_owns, "aria-owns");
ARIA_IMPL(aria_placeholder, "aria-placeholder");
ARIA_IMPL(aria_pos_in_set, "aria-posinset");
ARIA_IMPL(aria_pressed, "aria-pressed");
ARIA_IMPL(aria_read_only, "aria-readonly");
ARIA_IMPL(aria_relevant, "aria-relevant");
ARIA_IMPL(aria_required, "aria-required");
ARIA_IMPL(aria_role_description, "aria-roledescription");
ARIA_IMPL(aria_row_count, "aria-rowcount");
ARIA_IMPL(aria_row_index, "aria-rowindex");
ARIA_IMPL(aria_row_span, "aria-rowspan");
ARIA_IMPL(aria_selected, "aria-selected");
ARIA_IMPL(aria_set_size, "aria-setsize");
ARIA_IMPL(aria_sort, "aria-sort");
ARIA_IMPL(aria_value_max, "aria-valuemax");
ARIA_IMPL(aria_value_min, "aria-valuemin");
ARIA_IMPL(aria_value_now, "aria-valuenow");
ARIA_IMPL(aria_value_text, "aria-valuetext");
ARIA_IMPL(role, "role"sv);
ARIA_IMPL(aria_active_descendant, "aria-activedescendant"sv);
ARIA_IMPL(aria_atomic, "aria-atomic"sv);
ARIA_IMPL(aria_auto_complete, "aria-autocomplete"sv);
ARIA_IMPL(aria_busy, "aria-busy"sv);
ARIA_IMPL(aria_checked, "aria-checked"sv);
ARIA_IMPL(aria_col_count, "aria-colcount"sv);
ARIA_IMPL(aria_col_index, "aria-colindex"sv);
ARIA_IMPL(aria_col_span, "aria-colspan"sv);
ARIA_IMPL(aria_controls, "aria-controls"sv);
ARIA_IMPL(aria_current, "aria-current"sv);
ARIA_IMPL(aria_described_by, "aria-describedby"sv);
ARIA_IMPL(aria_details, "aria-details"sv);
ARIA_IMPL(aria_drop_effect, "aria-dropeffect"sv);
ARIA_IMPL(aria_error_message, "aria-errormessage"sv);
ARIA_IMPL(aria_disabled, "aria-disabled"sv);
ARIA_IMPL(aria_expanded, "aria-expanded"sv);
ARIA_IMPL(aria_flow_to, "aria-flowto"sv);
ARIA_IMPL(aria_grabbed, "aria-grabbed"sv);
ARIA_IMPL(aria_has_popup, "aria-haspopup"sv);
ARIA_IMPL(aria_hidden, "aria-hidden"sv);
ARIA_IMPL(aria_invalid, "aria-invalid"sv);
ARIA_IMPL(aria_key_shortcuts, "aria-keyshortcuts"sv);
ARIA_IMPL(aria_label, "aria-label"sv);
ARIA_IMPL(aria_labelled_by, "aria-labelledby"sv);
ARIA_IMPL(aria_level, "aria-level"sv);
ARIA_IMPL(aria_live, "aria-live"sv);
ARIA_IMPL(aria_modal, "aria-modal"sv);
ARIA_IMPL(aria_multi_line, "aria-multiline"sv);
ARIA_IMPL(aria_multi_selectable, "aria-multiselectable"sv);
ARIA_IMPL(aria_orientation, "aria-orientation"sv);
ARIA_IMPL(aria_owns, "aria-owns"sv);
ARIA_IMPL(aria_placeholder, "aria-placeholder"sv);
ARIA_IMPL(aria_pos_in_set, "aria-posinset"sv);
ARIA_IMPL(aria_pressed, "aria-pressed"sv);
ARIA_IMPL(aria_read_only, "aria-readonly"sv);
ARIA_IMPL(aria_relevant, "aria-relevant"sv);
ARIA_IMPL(aria_required, "aria-required"sv);
ARIA_IMPL(aria_role_description, "aria-roledescription"sv);
ARIA_IMPL(aria_row_count, "aria-rowcount"sv);
ARIA_IMPL(aria_row_index, "aria-rowindex"sv);
ARIA_IMPL(aria_row_span, "aria-rowspan"sv);
ARIA_IMPL(aria_selected, "aria-selected"sv);
ARIA_IMPL(aria_set_size, "aria-setsize"sv);
ARIA_IMPL(aria_sort, "aria-sort"sv);
ARIA_IMPL(aria_value_max, "aria-valuemax"sv);
ARIA_IMPL(aria_value_min, "aria-valuemin"sv);
ARIA_IMPL(aria_value_now, "aria-valuenow"sv);
ARIA_IMPL(aria_value_text, "aria-valuetext"sv);
#undef ARIA_IMPL

View file

@ -25,7 +25,7 @@ public:
virtual DeprecatedString aria_level() const override
{
// TODO: aria-level = the number in the element's tag name
return get_attribute("aria-level");
return get_attribute("aria-level"sv);
}
private:

View file

@ -1088,7 +1088,7 @@ Optional<ARIA::Role> HTMLInputElement::default_role() const
if (type_state() == TypeAttributeState::Checkbox)
return ARIA::Role::checkbox;
// https://www.w3.org/TR/html-aria/#el-input-email
if (type_state() == TypeAttributeState::Email && deprecated_attribute("list").is_null())
if (type_state() == TypeAttributeState::Email && !has_attribute(AttributeNames::list))
return ARIA::Role::textbox;
// https://www.w3.org/TR/html-aria/#el-input-image
if (type_state() == TypeAttributeState::ImageButton)
@ -1111,10 +1111,10 @@ Optional<ARIA::Role> HTMLInputElement::default_role() const
|| type_state() == TypeAttributeState::Telephone
|| type_state() == TypeAttributeState::URL
|| type_state() == TypeAttributeState::Email)
&& !deprecated_attribute("list").is_null())
&& has_attribute(AttributeNames::list))
return ARIA::Role::combobox;
// https://www.w3.org/TR/html-aria/#el-input-search
if (type_state() == TypeAttributeState::Search && deprecated_attribute("list").is_null())
if (type_state() == TypeAttributeState::Search && !has_attribute(AttributeNames::list))
return ARIA::Role::textbox;
// https://www.w3.org/TR/html-aria/#el-input-submit
if (type_state() == TypeAttributeState::SubmitButton)
@ -1123,10 +1123,10 @@ Optional<ARIA::Role> HTMLInputElement::default_role() const
if (type_state() == TypeAttributeState::Telephone)
return ARIA::Role::textbox;
// https://www.w3.org/TR/html-aria/#el-input-text
if (type_state() == TypeAttributeState::Text && deprecated_attribute("list").is_null())
if (type_state() == TypeAttributeState::Text && !has_attribute(AttributeNames::list))
return ARIA::Role::textbox;
// https://www.w3.org/TR/html-aria/#el-input-url
if (type_state() == TypeAttributeState::URL && deprecated_attribute("list").is_null())
if (type_state() == TypeAttributeState::URL && !has_attribute(AttributeNames::list))
return ARIA::Role::textbox;
// https://www.w3.org/TR/html-aria/#el-input-color

View file

@ -167,7 +167,7 @@ Optional<ARIA::Role> HTMLSelectElement::default_role() const
if (has_attribute(AttributeNames::multiple))
return ARIA::Role::listbox;
if (has_attribute(AttributeNames::size)) {
auto size_attribute = deprecated_attribute("size").to_int();
auto size_attribute = deprecated_attribute(AttributeNames::size).to_int();
if (size_attribute.has_value() && size_attribute.value() > 1)
return ARIA::Role::listbox;
}

View file

@ -1345,7 +1345,7 @@ Messages::WebDriverClient::ElementClickResponse WebDriverConnection::element_cli
// 3. If elements container has the multiple attribute, toggle the elements selectedness state
// by setting it to the opposite value of its current selectedness.
if (parent_node.has_value() && parent_node->has_attribute("multiple")) {
if (parent_node.has_value() && parent_node->has_attribute(Web::HTML::AttributeNames::multiple)) {
option_element.set_selected(!option_element.selected());
}
// Otherwise, set the elements selectedness state to true.