mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
LibWeb: Build out the ARIA role model
We now have implemented the ARIA role model. These classes will control which states and properties are exposed to end users.
This commit is contained in:
parent
125792e5ff
commit
e9840bfd4e
Notes:
sideshowbarker
2024-07-17 09:49:48 +09:00
Author: https://github.com/Epigenetic Commit: https://github.com/SerenityOS/serenity/commit/e9840bfd4e Pull-request: https://github.com/SerenityOS/serenity/pull/18960 Reviewed-by: https://github.com/AtkinsSJ ✅ Reviewed-by: https://github.com/kemzeb
6 changed files with 4956 additions and 0 deletions
|
@ -6,5 +6,6 @@ lagom_tool(GenerateCSSPropertyID SOURCES GenerateCSSPropertyID.cpp LIB
|
|||
lagom_tool(GenerateCSSTransformFunctions SOURCES GenerateCSSTransformFunctions.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateCSSValueID SOURCES GenerateCSSValueID.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateWindowOrWorkerInterfaces SOURCES GenerateWindowOrWorkerInterfaces.cpp LIBS LibMain LibIDL)
|
||||
lagom_tool(GenerateAriaRoles SOURCES GenerateAriaRoles.cpp LIBS LibMain)
|
||||
|
||||
add_subdirectory(BindingsGenerator)
|
||||
|
|
398
Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateAriaRoles.cpp
Normal file
398
Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateAriaRoles.cpp
Normal file
|
@ -0,0 +1,398 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "GeneratorUtil.h"
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
ErrorOr<void> generate_header_file(JsonObject& roles_data, Core::File& file);
|
||||
ErrorOr<void> generate_implementation_file(JsonObject& roles_data, Core::File& file);
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
StringView generated_header_path;
|
||||
StringView generated_implementation_path;
|
||||
StringView identifiers_json_path;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(generated_header_path, "Path to the TransformFunctions header file to generate", "generated-header-path", 'h', "generated-header-path");
|
||||
args_parser.add_option(generated_implementation_path, "Path to the TransformFunctions implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
|
||||
args_parser.add_option(identifiers_json_path, "Path to the JSON file to read from", "json-path", 'j', "json-path");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
auto json = TRY(read_entire_file_as_json(identifiers_json_path));
|
||||
VERIFY(json.is_object());
|
||||
auto roles_data = json.as_object();
|
||||
|
||||
auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write));
|
||||
auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write));
|
||||
|
||||
TRY(generate_header_file(roles_data, *generated_header_file));
|
||||
TRY(generate_implementation_file(roles_data, *generated_implementation_file));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ErrorOr<void> generate_header_file(JsonObject& roles_data, Core::File& file)
|
||||
{
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
||||
generator.append(R"~~~(
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/ARIA/RoleType.h>
|
||||
|
||||
namespace Web::ARIA {
|
||||
)~~~");
|
||||
|
||||
TRY(roles_data.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
|
||||
VERIFY(value.is_object());
|
||||
JsonObject const& value_object = value.as_object();
|
||||
|
||||
auto class_definition_generator = TRY(generator.fork());
|
||||
class_definition_generator.set("spec_link"sv, value_object.get_deprecated_string("specLink"sv).value());
|
||||
class_definition_generator.set("description"sv, value_object.get_deprecated_string("description"sv).value());
|
||||
class_definition_generator.set("name"sv, name);
|
||||
class_definition_generator.append(R"~~~(
|
||||
// @spec_link@
|
||||
// @description@
|
||||
class @name@ :
|
||||
)~~~");
|
||||
|
||||
JsonArray const& super_classes = value_object.get_array("superClassRoles"sv).value();
|
||||
bool first = true;
|
||||
TRY(super_classes.try_for_each([&](JsonValue const& value) -> ErrorOr<void> {
|
||||
VERIFY(value.is_string());
|
||||
|
||||
class_definition_generator.append(first ? " "sv : ", "sv);
|
||||
class_definition_generator.append(TRY(String::formatted("public {}", value.as_string())));
|
||||
first = false;
|
||||
return {};
|
||||
}));
|
||||
|
||||
class_definition_generator.append(R"~~~(
|
||||
{
|
||||
public:
|
||||
@name@(AriaData const&);
|
||||
|
||||
virtual HashTable<StateAndProperties> const& supported_states() const override;
|
||||
virtual HashTable<StateAndProperties> const& supported_properties() const override;
|
||||
|
||||
virtual HashTable<StateAndProperties> const& required_states() const override;
|
||||
virtual HashTable<StateAndProperties> const& required_properties() const override;
|
||||
|
||||
virtual HashTable<StateAndProperties> const& prohibited_properties() const override;
|
||||
virtual HashTable<StateAndProperties> const& prohibited_states() const override;
|
||||
|
||||
virtual HashTable<Role> const& required_context_roles() const override;
|
||||
virtual HashTable<Role> const& required_owned_elements() const override;
|
||||
virtual bool accessible_name_required() const override;
|
||||
virtual bool children_are_presentational() const override;
|
||||
virtual DefaultValueType default_value_for_property_or_state(StateAndProperties) const override;
|
||||
protected:
|
||||
@name@();
|
||||
)~~~");
|
||||
|
||||
auto name_from_source = value.as_object().get("nameFromSource"sv).value();
|
||||
if (!name_from_source.is_null())
|
||||
class_definition_generator.append(R"~~~(
|
||||
public:
|
||||
virtual NameFromSource name_from_source() const override;
|
||||
)~~~");
|
||||
class_definition_generator.appendln("};");
|
||||
return {};
|
||||
}));
|
||||
|
||||
generator.appendln("}");
|
||||
|
||||
TRY(file.write_until_depleted((generator.as_string_view().bytes())));
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<String> generate_hash_table_population(JsonArray const& values, StringView hash_table_name, StringView enum_class)
|
||||
{
|
||||
StringBuilder builder;
|
||||
TRY(values.try_for_each([&](auto& value) -> ErrorOr<void> {
|
||||
VERIFY(value.is_string());
|
||||
TRY(builder.try_appendff(" {}.set({}::{});\n", hash_table_name, enum_class, value.as_string()));
|
||||
return {};
|
||||
}));
|
||||
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
ErrorOr<void> generate_hash_table_member(SourceGenerator& generator, StringView member_name, StringView hash_table_name, StringView enum_class, JsonArray const& values)
|
||||
{
|
||||
auto member_generator = TRY(generator.fork());
|
||||
member_generator.set("member_name"sv, member_name);
|
||||
member_generator.set("hash_table_name"sv, hash_table_name);
|
||||
member_generator.set("enum_class"sv, enum_class);
|
||||
TRY(member_generator.set("hash_table_size"sv, TRY(String::number(values.size()))));
|
||||
|
||||
if (values.size() == 0) {
|
||||
member_generator.append(R"~~~(
|
||||
HashTable<@enum_class@> const& @name@::@member_name@() const
|
||||
{
|
||||
static HashTable<@enum_class@> @hash_table_name@;
|
||||
return @hash_table_name@;
|
||||
}
|
||||
)~~~");
|
||||
return {};
|
||||
}
|
||||
|
||||
member_generator.append(R"~~~(
|
||||
HashTable<@enum_class@> const& @name@::@member_name@() const
|
||||
{
|
||||
static HashTable<@enum_class@> @hash_table_name@;
|
||||
if (@hash_table_name@.is_empty()) {
|
||||
@hash_table_name@.ensure_capacity(@hash_table_size@);
|
||||
)~~~");
|
||||
member_generator.append(TRY(generate_hash_table_population(values, hash_table_name, enum_class)));
|
||||
member_generator.append(R"~~~(
|
||||
}
|
||||
return @hash_table_name@;
|
||||
}
|
||||
)~~~");
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
StringView aria_name_to_enum_name(StringView name)
|
||||
{
|
||||
if (name == "aria-activedescendant"sv) {
|
||||
return "AriaActiveDescendant"sv;
|
||||
} else if (name == "aria-atomic"sv) {
|
||||
return "AriaAtomic"sv;
|
||||
} else if (name == "aria-autocomplete"sv) {
|
||||
return "AriaAutoComplete"sv;
|
||||
} else if (name == "aria-busy"sv) {
|
||||
return "AriaBusy"sv;
|
||||
} else if (name == "aria-checked"sv) {
|
||||
return "AriaChecked"sv;
|
||||
} else if (name == "aria-colcount"sv) {
|
||||
return "AriaColCount"sv;
|
||||
} else if (name == "aria-colindex"sv) {
|
||||
return "AriaColIndex"sv;
|
||||
} else if (name == "aria-colspan"sv) {
|
||||
return "AriaColSpan"sv;
|
||||
} else if (name == "aria-controls"sv) {
|
||||
return "AriaControls"sv;
|
||||
} else if (name == "aria-current"sv) {
|
||||
return "AriaCurrent"sv;
|
||||
} else if (name == "aria-describedby"sv) {
|
||||
return "AriaDescribedBy"sv;
|
||||
} else if (name == "aria-details"sv) {
|
||||
return "AriaDetails"sv;
|
||||
} else if (name == "aria-disabled"sv) {
|
||||
return "AriaDisabled"sv;
|
||||
} else if (name == "aria-dropeffect"sv) {
|
||||
return "AriaDropEffect"sv;
|
||||
} else if (name == "aria-errormessage"sv) {
|
||||
return "AriaErrorMessage"sv;
|
||||
} else if (name == "aria-expanded"sv) {
|
||||
return "AriaExpanded"sv;
|
||||
} else if (name == "aria-flowto"sv) {
|
||||
return "AriaFlowTo"sv;
|
||||
} else if (name == "aria-grabbed"sv) {
|
||||
return "AriaGrabbed"sv;
|
||||
} else if (name == "aria-haspopup"sv) {
|
||||
return "AriaHasPopup"sv;
|
||||
} else if (name == "aria-hidden"sv) {
|
||||
return "AriaHidden"sv;
|
||||
} else if (name == "aria-invalid"sv) {
|
||||
return "AriaInvalid"sv;
|
||||
} else if (name == "aria-keyshortcuts"sv) {
|
||||
return "AriaKeyShortcuts"sv;
|
||||
} else if (name == "aria-label"sv) {
|
||||
return "AriaLabel"sv;
|
||||
} else if (name == "aria-labelledby"sv) {
|
||||
return "AriaLabelledBy"sv;
|
||||
} else if (name == "aria-level"sv) {
|
||||
return "AriaLevel"sv;
|
||||
} else if (name == "aria-live"sv) {
|
||||
return "AriaLive"sv;
|
||||
} else if (name == "aria-modal"sv) {
|
||||
return "AriaModal"sv;
|
||||
} else if (name == "aria-multiline"sv) {
|
||||
return "AriaMultiLine"sv;
|
||||
} else if (name == "aria-multiselectable"sv) {
|
||||
return "AriaMultiSelectable"sv;
|
||||
} else if (name == "aria-orientation"sv) {
|
||||
return "AriaOrientation"sv;
|
||||
} else if (name == "aria-owns"sv) {
|
||||
return "AriaOwns"sv;
|
||||
} else if (name == "aria-placeholder"sv) {
|
||||
return "AriaPlaceholder"sv;
|
||||
} else if (name == "aria-posinset"sv) {
|
||||
return "AriaPosInSet"sv;
|
||||
} else if (name == "aria-pressed"sv) {
|
||||
return "AriaPressed"sv;
|
||||
} else if (name == "aria-readonly"sv) {
|
||||
return "AriaReadOnly"sv;
|
||||
} else if (name == "aria-relevant"sv) {
|
||||
return "AriaRelevant"sv;
|
||||
} else if (name == "aria-required"sv) {
|
||||
return "AriaRequired"sv;
|
||||
} else if (name == "aria-roledescription"sv) {
|
||||
return "AriaRoleDescription"sv;
|
||||
} else if (name == "aria-rowcount"sv) {
|
||||
return "AriaRowCount"sv;
|
||||
} else if (name == "aria-rowindex"sv) {
|
||||
return "AriaRowIndex"sv;
|
||||
} else if (name == "aria-rowspan"sv) {
|
||||
return "AriaRowSpan"sv;
|
||||
} else if (name == "aria-selected"sv) {
|
||||
return "AriaSelected"sv;
|
||||
} else if (name == "aria-setsize"sv) {
|
||||
return "AriaSetSize"sv;
|
||||
} else if (name == "aria-sort"sv) {
|
||||
return "AriaSort"sv;
|
||||
} else if (name == "aria-valuemax"sv) {
|
||||
return "AriaValueMax"sv;
|
||||
} else if (name == "aria-valuemin"sv) {
|
||||
return "AriaValueMin"sv;
|
||||
} else if (name == "aria-valuenow"sv) {
|
||||
return "AriaValueNow"sv;
|
||||
} else if (name == "aria-valuetext"sv) {
|
||||
return "AriaValueText"sv;
|
||||
} else {
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
ErrorOr<JsonArray> translate_aria_names_to_enum(JsonArray const& names)
|
||||
{
|
||||
JsonArray translated_names;
|
||||
TRY(names.try_for_each([&](JsonValue const& value) -> ErrorOr<void> {
|
||||
VERIFY(value.is_string());
|
||||
auto name = value.as_string();
|
||||
TRY(translated_names.append(aria_name_to_enum_name(name)));
|
||||
return {};
|
||||
}));
|
||||
return translated_names;
|
||||
}
|
||||
|
||||
ErrorOr<void> generate_implementation_file(JsonObject& roles_data, Core::File& file)
|
||||
{
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
||||
generator.append(R"~~~(
|
||||
#include <LibWeb/ARIA/AriaRoles.h>
|
||||
|
||||
namespace Web::ARIA {
|
||||
)~~~");
|
||||
|
||||
TRY(roles_data.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
|
||||
VERIFY(value.is_object());
|
||||
|
||||
auto member_generator = TRY(generator.fork());
|
||||
member_generator.set("name"sv, name);
|
||||
|
||||
JsonObject const& value_object = value.as_object();
|
||||
|
||||
JsonArray const& supported_states = TRY(translate_aria_names_to_enum(value_object.get_array("supportedStates"sv).value()));
|
||||
TRY(generate_hash_table_member(member_generator, "supported_states"sv, "states"sv, "StateAndProperties"sv, supported_states));
|
||||
JsonArray const& supported_properties = TRY(translate_aria_names_to_enum(value_object.get_array("supportedProperties"sv).value()));
|
||||
TRY(generate_hash_table_member(member_generator, "supported_properties"sv, "properties"sv, "StateAndProperties"sv, supported_properties));
|
||||
|
||||
JsonArray const& required_states = TRY(translate_aria_names_to_enum(value_object.get_array("requiredStates"sv).value()));
|
||||
TRY(generate_hash_table_member(member_generator, "required_states"sv, "states"sv, "StateAndProperties"sv, required_states));
|
||||
JsonArray const& required_properties = TRY(translate_aria_names_to_enum(value_object.get_array("requiredProperties"sv).value()));
|
||||
TRY(generate_hash_table_member(member_generator, "required_properties"sv, "properties"sv, "StateAndProperties"sv, required_properties));
|
||||
|
||||
JsonArray const& prohibited_states = TRY(translate_aria_names_to_enum(value_object.get_array("prohibitedStates"sv).value()));
|
||||
TRY(generate_hash_table_member(member_generator, "prohibited_states"sv, "states"sv, "StateAndProperties"sv, prohibited_states));
|
||||
JsonArray const& prohibited_properties = TRY(translate_aria_names_to_enum(value_object.get_array("prohibitedProperties"sv).value()));
|
||||
TRY(generate_hash_table_member(member_generator, "prohibited_properties"sv, "properties"sv, "StateAndProperties"sv, prohibited_properties));
|
||||
|
||||
JsonArray const& required_context_roles = value_object.get_array("requiredContextRoles"sv).value();
|
||||
TRY(generate_hash_table_member(member_generator, "required_context_roles"sv, "roles"sv, "Role"sv, required_context_roles));
|
||||
JsonArray const& required_owned_elements = value_object.get_array("requiredOwnedElements"sv).value();
|
||||
TRY(generate_hash_table_member(member_generator, "required_owned_elements"sv, "roles"sv, "Role"sv, required_owned_elements));
|
||||
|
||||
bool accessible_name_required = value_object.get_bool("accessibleNameRequired"sv).value();
|
||||
member_generator.set("accessible_name_required"sv, accessible_name_required ? "true"sv : "false"sv);
|
||||
bool children_are_presentational = value_object.get_bool("childrenArePresentational"sv).value();
|
||||
member_generator.set("children_are_presentational", children_are_presentational ? "true"sv : "false"sv);
|
||||
|
||||
JsonArray const& super_classes = value.as_object().get_array("superClassRoles"sv).value();
|
||||
member_generator.set("parent", super_classes.at(0).as_string());
|
||||
|
||||
member_generator.append(R"~~~(
|
||||
@name@::@name@() { }
|
||||
|
||||
@name@::@name@(AriaData const& data)
|
||||
: @parent@(data)
|
||||
{
|
||||
}
|
||||
|
||||
bool @name@::accessible_name_required() const
|
||||
{
|
||||
return @accessible_name_required@;
|
||||
}
|
||||
|
||||
bool @name@::children_are_presentational() const
|
||||
{
|
||||
return @children_are_presentational@;
|
||||
}
|
||||
)~~~");
|
||||
|
||||
JsonObject const& implicit_value_for_role = value_object.get_object("implicitValueForRole"sv).value();
|
||||
if (implicit_value_for_role.size() == 0) {
|
||||
member_generator.append(R"~~~(
|
||||
DefaultValueType @name@::default_value_for_property_or_state(StateAndProperties) const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
)~~~");
|
||||
} else {
|
||||
member_generator.append(R"~~~(
|
||||
DefaultValueType @name@::default_value_for_property_or_state(StateAndProperties state_or_property) const
|
||||
{
|
||||
switch (state_or_property) {
|
||||
)~~~");
|
||||
TRY(implicit_value_for_role.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
|
||||
auto case_generator = TRY(member_generator.fork());
|
||||
VERIFY(value.is_string());
|
||||
case_generator.set("state_or_property"sv, aria_name_to_enum_name(name));
|
||||
case_generator.set("implicit_value"sv, value.as_string());
|
||||
case_generator.append(R"~~~(
|
||||
case StateAndProperties::@state_or_property@:
|
||||
return @implicit_value@;
|
||||
)~~~");
|
||||
return {};
|
||||
}));
|
||||
member_generator.append(R"~~~(
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
)~~~");
|
||||
}
|
||||
|
||||
JsonValue const& name_from_source = value.as_object().get("nameFromSource"sv).value();
|
||||
if (!name_from_source.is_null()) {
|
||||
member_generator.set("name_from_source"sv, name_from_source.as_string());
|
||||
member_generator.append(R"~~~(
|
||||
NameFromSource @name@::name_from_source() const
|
||||
{
|
||||
return NameFromSource::@name_from_source@;
|
||||
}
|
||||
)~~~");
|
||||
}
|
||||
|
||||
return {};
|
||||
}));
|
||||
|
||||
generator.append("}");
|
||||
|
||||
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
|
||||
return {};
|
||||
}
|
4165
Userland/Libraries/LibWeb/ARIA/AriaRoles.json
Normal file
4165
Userland/Libraries/LibWeb/ARIA/AriaRoles.json
Normal file
File diff suppressed because it is too large
Load diff
320
Userland/Libraries/LibWeb/ARIA/RoleType.cpp
Normal file
320
Userland/Libraries/LibWeb/ARIA/RoleType.cpp
Normal file
|
@ -0,0 +1,320 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/ARIA/ARIAMixin.h>
|
||||
#include <LibWeb/ARIA/AriaRoles.h>
|
||||
#include <LibWeb/ARIA/RoleType.h>
|
||||
|
||||
namespace Web::ARIA {
|
||||
|
||||
RoleType::RoleType(AriaData const& data)
|
||||
: m_data(data)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr StateAndProperties supported_state_array[] = {
|
||||
StateAndProperties::AriaBusy,
|
||||
StateAndProperties::AriaCurrent,
|
||||
StateAndProperties::AriaDisabled,
|
||||
StateAndProperties::AriaGrabbed,
|
||||
StateAndProperties::AriaHidden,
|
||||
StateAndProperties::AriaInvalid
|
||||
};
|
||||
constexpr StateAndProperties supported_properties_array[] = {
|
||||
StateAndProperties::AriaAtomic,
|
||||
StateAndProperties::AriaControls,
|
||||
StateAndProperties::AriaDescribedBy,
|
||||
StateAndProperties::AriaDetails,
|
||||
StateAndProperties::AriaDropEffect,
|
||||
StateAndProperties::AriaFlowTo,
|
||||
StateAndProperties::AriaHasPopup,
|
||||
StateAndProperties::AriaKeyShortcuts,
|
||||
StateAndProperties::AriaLabel,
|
||||
StateAndProperties::AriaLabelledBy,
|
||||
StateAndProperties::AriaLive,
|
||||
StateAndProperties::AriaOwns,
|
||||
StateAndProperties::AriaRelevant,
|
||||
StateAndProperties::AriaRoleDescription
|
||||
};
|
||||
|
||||
HashTable<StateAndProperties> const& RoleType::supported_states() const
|
||||
{
|
||||
static HashTable<StateAndProperties> states;
|
||||
if (states.is_empty())
|
||||
states.set_from(supported_state_array);
|
||||
return states;
|
||||
}
|
||||
|
||||
HashTable<StateAndProperties> const& RoleType::supported_properties() const
|
||||
{
|
||||
static HashTable<StateAndProperties> properties;
|
||||
if (properties.is_empty())
|
||||
properties.set_from(supported_properties_array);
|
||||
return properties;
|
||||
}
|
||||
|
||||
HashTable<StateAndProperties> const& RoleType::required_states() const
|
||||
{
|
||||
static HashTable<StateAndProperties> states;
|
||||
return states;
|
||||
}
|
||||
|
||||
HashTable<StateAndProperties> const& RoleType::required_properties() const
|
||||
{
|
||||
static HashTable<StateAndProperties> properties;
|
||||
return properties;
|
||||
}
|
||||
|
||||
HashTable<StateAndProperties> const& RoleType::prohibited_properties() const
|
||||
{
|
||||
static HashTable<StateAndProperties> properties;
|
||||
return properties;
|
||||
}
|
||||
|
||||
HashTable<StateAndProperties> const& RoleType::prohibited_states() const
|
||||
{
|
||||
static HashTable<StateAndProperties> states;
|
||||
return states;
|
||||
}
|
||||
|
||||
HashTable<Role> const& RoleType::required_context_roles() const
|
||||
{
|
||||
static HashTable<Role> roles;
|
||||
return roles;
|
||||
}
|
||||
|
||||
HashTable<Role> const& RoleType::required_owned_elements() const
|
||||
{
|
||||
static HashTable<Role> roles;
|
||||
return roles;
|
||||
}
|
||||
|
||||
ErrorOr<void> RoleType::serialize_as_json(JsonObjectSerializer<StringBuilder>& object) const
|
||||
{
|
||||
auto state_object = TRY(object.add_object("state"sv));
|
||||
for (auto const state : supported_states()) {
|
||||
auto value = TRY(ARIA::state_or_property_to_string_value(state, m_data, default_value_for_property_or_state(state)));
|
||||
TRY(state_object.add(ARIA::state_or_property_to_string(state), value));
|
||||
}
|
||||
TRY(state_object.finish());
|
||||
|
||||
auto properties_object = TRY(object.add_object("properties"sv));
|
||||
for (auto const property : supported_properties()) {
|
||||
auto value = TRY(ARIA::state_or_property_to_string_value(property, m_data, default_value_for_property_or_state(property)));
|
||||
TRY(properties_object.add(ARIA::state_or_property_to_string(property), value));
|
||||
}
|
||||
TRY(properties_object.finish());
|
||||
|
||||
auto required_states_object = TRY(object.add_object("required_state"sv));
|
||||
for (auto const state : required_states()) {
|
||||
auto value = TRY(ARIA::state_or_property_to_string_value(state, m_data, default_value_for_property_or_state(state)));
|
||||
TRY(required_states_object.add(ARIA::state_or_property_to_string(state), value));
|
||||
}
|
||||
TRY(required_states_object.finish());
|
||||
|
||||
auto required_properties_object = TRY(object.add_object("required_properties"sv));
|
||||
for (auto const property : required_properties()) {
|
||||
auto value = TRY(ARIA::state_or_property_to_string_value(property, m_data, default_value_for_property_or_state(property)));
|
||||
TRY(required_properties_object.add(ARIA::state_or_property_to_string(property), value));
|
||||
}
|
||||
TRY(required_properties_object.finish());
|
||||
|
||||
auto prohibited_states_object = TRY(object.add_object("prohibited_state"sv));
|
||||
for (auto const state : prohibited_states()) {
|
||||
auto value = TRY(ARIA::state_or_property_to_string_value(state, m_data, default_value_for_property_or_state(state)));
|
||||
TRY(prohibited_states_object.add(ARIA::state_or_property_to_string(state), value));
|
||||
}
|
||||
TRY(prohibited_states_object.finish());
|
||||
|
||||
auto prohibited_properties_object = TRY(object.add_object("prohibited_properties"sv));
|
||||
for (auto const property : required_properties()) {
|
||||
auto value = TRY(ARIA::state_or_property_to_string_value(property, m_data, default_value_for_property_or_state(property)));
|
||||
TRY(prohibited_properties_object.add(ARIA::state_or_property_to_string(property), value));
|
||||
}
|
||||
TRY(prohibited_properties_object.finish());
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<RoleType>> RoleType::build_role_object(Role role, bool focusable, AriaData const& data)
|
||||
{
|
||||
if (is_abstract_role(role))
|
||||
return Error::from_string_view("Cannot construct a role object for an abstract role."sv);
|
||||
|
||||
switch (role) {
|
||||
case Role::alert:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Alert(data));
|
||||
case Role::alertdialog:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Alert*>((new (nothrow) AlertDialog(data))));
|
||||
case Role::application:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Application(data));
|
||||
case Role::article:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Article(data));
|
||||
case Role::banner:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Banner(data));
|
||||
case Role::blockquote:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) BlockQuote(data));
|
||||
case Role::button:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Button(data));
|
||||
case Role::caption:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Caption(data));
|
||||
case Role::cell:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Cell(data));
|
||||
case Role::checkbox:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) CheckBox(data));
|
||||
case Role::code:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Code(data));
|
||||
case Role::columnheader:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Cell*>(new (nothrow) ColumnHeader(data)));
|
||||
case Role::combobox:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) ComboBox(data));
|
||||
case Role::complementary:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Complementary(data));
|
||||
case Role::composite:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) ContentInfo(data));
|
||||
case Role::definition:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Definition(data));
|
||||
case Role::deletion:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Deletion(data));
|
||||
case Role::dialog:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Dialog(data));
|
||||
case Role::directory:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Directory(data));
|
||||
case Role::document:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Document(data));
|
||||
case Role::emphasis:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Emphasis(data));
|
||||
case Role::feed:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Feed(data));
|
||||
case Role::figure:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Figure(data));
|
||||
case Role::form:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Form(data));
|
||||
case Role::generic:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Generic(data));
|
||||
case Role::grid:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Composite*>(new (nothrow) Grid(data)));
|
||||
case Role::gridcell:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Cell*>(new (nothrow) GridCell(data)));
|
||||
case Role::group:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Group(data));
|
||||
case Role::heading:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Heading(data));
|
||||
case Role::img:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Img(data));
|
||||
case Role::input:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Input(data));
|
||||
case Role::insertion:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Insertion(data));
|
||||
case Role::landmark:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Landmark(data));
|
||||
case Role::link:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Link(data));
|
||||
case Role::list:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) List(data));
|
||||
case Role::listbox:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Composite*>(new (nothrow) ListBox(data)));
|
||||
case Role::listitem:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) ListItem(data));
|
||||
case Role::log:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Log(data));
|
||||
case Role::main:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Main(data));
|
||||
case Role::marquee:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Marquee(data));
|
||||
case Role::math:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Math(data));
|
||||
case Role::meter:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Meter(data));
|
||||
case Role::menu:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Composite*>(new (nothrow) Menu(data)));
|
||||
case Role::menubar:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Composite*>(new (nothrow) MenuBar(data)));
|
||||
case Role::menuitem:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) MenuItem(data));
|
||||
case Role::menuitemcheckbox:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) MenuItemCheckBox(data));
|
||||
case Role::menuitemradio:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) MenuItemRadio(data));
|
||||
case Role::navigation:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Navigation(data));
|
||||
case Role::note:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Note(data));
|
||||
case Role::option:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Option(data));
|
||||
case Role::paragraph:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Paragraph(data));
|
||||
case Role::presentation:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Presentation(data));
|
||||
case Role::progressbar:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Progressbar(data));
|
||||
case Role::radio:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Radio(data));
|
||||
case Role::radiogroup:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Composite*>(new (nothrow) RadioGroup(data)));
|
||||
case Role::region:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Region(data));
|
||||
case Role::row:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Group*>(new (nothrow) Row(data)));
|
||||
case Role::rowgroup:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) RowGroup(data));
|
||||
case Role::rowheader:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Cell*>(new (nothrow) RowHeader(data)));
|
||||
case Role::scrollbar:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Range*>(new (nothrow) Scrollbar(data)));
|
||||
case Role::search:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Search(data));
|
||||
case Role::searchbox:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) SearchBox(data));
|
||||
case Role::separator:
|
||||
if (focusable)
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) FocusableSeparator(data));
|
||||
else
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) NonFocusableSeparator(data));
|
||||
case Role::slider:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Input*>(new (nothrow) Slider(data)));
|
||||
case Role::spinbutton:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Composite*>(new (nothrow) SpinButton(data)));
|
||||
case Role::status:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Status(data));
|
||||
case Role::strong:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Strong(data));
|
||||
case Role::switch_:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Switch(data));
|
||||
case Role::tab:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<SectionHead*>(new (nothrow) Tab(data)));
|
||||
case Role::table:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Table(data));
|
||||
case Role::tablist:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) TabList(data));
|
||||
case Role::tabpanel:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) TabPanel(data));
|
||||
case Role::term:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Term(data));
|
||||
case Role::textbox:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) TextBox(data));
|
||||
case Role::time:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Time(data));
|
||||
case Role::timer:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Timer(data));
|
||||
case Role::toolbar:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Toolbar(data));
|
||||
case Role::tooltip:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Tooltip(data));
|
||||
case Role::tree:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Composite*>(new (nothrow) Tree(data)));
|
||||
case Role::treegrid:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<Group*>(new (nothrow) TreeGrid(data)));
|
||||
case Role::treeitem:
|
||||
return adopt_nonnull_own_or_enomem(static_cast<ListItem*>(new (nothrow) TreeItem(data)));
|
||||
case Role::window:
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Window(data));
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
61
Userland/Libraries/LibWeb/ARIA/RoleType.h
Normal file
61
Userland/Libraries/LibWeb/ARIA/RoleType.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/JsonObjectSerializer.h>
|
||||
#include <LibWeb/ARIA/AriaData.h>
|
||||
#include <LibWeb/ARIA/Roles.h>
|
||||
#include <LibWeb/ARIA/StateAndProperties.h>
|
||||
|
||||
namespace Web::ARIA {
|
||||
|
||||
enum class NameFromSource {
|
||||
Author,
|
||||
Content,
|
||||
AuthorContent,
|
||||
Prohibited
|
||||
};
|
||||
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#roletype
|
||||
// The base role from which all other roles inherit.
|
||||
class RoleType {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<RoleType>> build_role_object(Role, bool, AriaData const&);
|
||||
|
||||
virtual ~RoleType() = default;
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#supportedState
|
||||
virtual HashTable<StateAndProperties> const& supported_states() const;
|
||||
virtual HashTable<StateAndProperties> const& supported_properties() const;
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#requiredState
|
||||
virtual HashTable<StateAndProperties> const& required_states() const;
|
||||
virtual HashTable<StateAndProperties> const& required_properties() const;
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#prohibitedattributes
|
||||
virtual HashTable<StateAndProperties> const& prohibited_properties() const;
|
||||
virtual HashTable<StateAndProperties> const& prohibited_states() const;
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#scope
|
||||
virtual HashTable<Role> const& required_context_roles() const;
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#mustContain
|
||||
virtual HashTable<Role> const& required_owned_elements() const;
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#namecalculation
|
||||
virtual NameFromSource name_from_source() const = 0;
|
||||
virtual bool accessible_name_required() const { return false; }
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#childrenArePresentational
|
||||
virtual bool children_are_presentational() const { return false; }
|
||||
// https://www.w3.org/TR/wai-aria-1.2/#implictValueForRole
|
||||
using DefaultValueType = Variant<Empty, f64, AriaOrientation, AriaLive, bool, AriaHasPopup>;
|
||||
virtual DefaultValueType default_value_for_property_or_state(StateAndProperties) const { return {}; };
|
||||
ErrorOr<void> serialize_as_json(JsonObjectSerializer<StringBuilder>& object) const;
|
||||
|
||||
protected:
|
||||
RoleType(AriaData const&);
|
||||
RoleType() { }
|
||||
|
||||
private:
|
||||
AriaData m_data;
|
||||
};
|
||||
|
||||
}
|
|
@ -5,6 +5,7 @@ set(SOURCES
|
|||
ARIA/ARIAMixin.cpp
|
||||
ARIA/ARIAMixin.idl
|
||||
ARIA/Roles.cpp
|
||||
ARIA/RoleType.cpp
|
||||
ARIA/StateAndProperties.cpp
|
||||
Bindings/AudioConstructor.cpp
|
||||
Bindings/HostDefined.cpp
|
||||
|
@ -593,9 +594,19 @@ set(SOURCES
|
|||
XML/XMLDocumentBuilder.cpp
|
||||
)
|
||||
|
||||
invoke_generator(
|
||||
"AriaRoles.cpp"
|
||||
Lagom::GenerateAriaRoles
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/ARIA/AriaRoles.json"
|
||||
"ARIA/AriaRoles.h"
|
||||
"ARIA/AriaRoles.cpp"
|
||||
arguments -j "${CMAKE_CURRENT_SOURCE_DIR}/ARIA/AriaRoles.json"
|
||||
)
|
||||
|
||||
generate_css_implementation()
|
||||
|
||||
set(GENERATED_SOURCES
|
||||
ARIA/AriaRoles.cpp
|
||||
CSS/DefaultStyleSheetSource.cpp
|
||||
CSS/Enums.cpp
|
||||
CSS/MediaFeatureID.cpp
|
||||
|
|
Loading…
Reference in a new issue