Meta: Make IDLGenerators handle non-HTML and dash-containing attributes

This change updates the BindingsGenerator/IDLGenerators.cpp code to
handle reflected non-HTML::AttributeNames content-attribute names, and
to handle such names as-is — including names with dashes.
This commit is contained in:
sideshowbarker 2024-12-31 16:53:45 +09:00 committed by Tim Ledbetter
parent 35b636acb3
commit f9d4852a22
Notes: github-actions[bot] 2025-01-01 11:02:00 +00:00

View file

@ -3001,7 +3001,6 @@ static void collect_attribute_values_of_an_inheritance_stack(SourceGenerator& fu
auto attribute_name = attribute.extended_attributes.get("Reflect").value();
if (attribute_name.is_empty())
attribute_name = attribute.name;
attribute_name = make_input_acceptable_cpp(attribute_name);
attribute_generator.set("attribute.reflect_name", attribute_name);
} else {
@ -3011,11 +3010,11 @@ static void collect_attribute_values_of_an_inheritance_stack(SourceGenerator& fu
if (attribute.extended_attributes.contains("Reflect")) {
if (attribute.type->name() != "boolean") {
attribute_generator.append(R"~~~(
auto @attribute.return_value_name@ = impl->get_attribute_value(HTML::AttributeNames::@attribute.reflect_name@);
auto @attribute.return_value_name@ = impl->get_attribute_value("@attribute.reflect_name@"_fly_string);
)~~~");
} else {
attribute_generator.append(R"~~~(
auto @attribute.return_value_name@ = impl->has_attribute(HTML::AttributeNames::@attribute.reflect_name@);
auto @attribute.return_value_name@ = impl->has_attribute("@attribute.reflect_name@"_fly_string);
)~~~");
}
} else {
@ -3549,7 +3548,6 @@ void @class_name@::initialize(JS::Realm& realm)
auto attribute_name = attribute.extended_attributes.get("Reflect").value();
if (attribute_name.is_empty())
attribute_name = attribute.name;
attribute_name = make_input_acceptable_cpp(attribute_name);
attribute_generator.set("attribute.reflect_name", attribute_name);
} else {
@ -3587,7 +3585,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
// 2. Let contentAttributeValue be the result of running this's get the content attribute.
attribute_generator.append(R"~~~(
auto contentAttributeValue = impl->attribute(HTML::AttributeNames::@attribute.reflect_name@);
auto contentAttributeValue = impl->attribute("@attribute.reflect_name@"_fly_string);
)~~~");
// 3. Let attributeDefinition be the attribute definition of element's content attribute whose namespace is null
@ -3652,7 +3650,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
// 8. Return the canonical keyword for the state of attributeDefinition that contentAttributeValue corresponds to.
// NOTE: We run step 8 here to have a field to assign to
attribute_generator.append(R"~~~(
auto retval = impl->attribute(HTML::AttributeNames::@attribute.reflect_name@);
auto retval = impl->attribute("@attribute.reflect_name@"_fly_string);
)~~~");
// 3. Let attributeDefinition be the attribute definition of element's content attribute whose namespace is null
@ -3717,7 +3715,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
// 1. Let contentAttributeValue be the result of running this's get the content attribute.
// 2. If contentAttributeValue is null, then return false
attribute_generator.append(R"~~~(
auto retval = impl->has_attribute(HTML::AttributeNames::@attribute.reflect_name@);
auto retval = impl->has_attribute("@attribute.reflect_name@"_fly_string);
)~~~");
}
// If a reflected IDL attribute has the type long:
@ -3730,7 +3728,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
// 2. If parsedValue is not an error and is within the long range, then return parsedValue.
attribute_generator.append(R"~~~(
i32 retval = 0;
auto content_attribute_value = impl->get_attribute(HTML::AttributeNames::@attribute.reflect_name@);
auto content_attribute_value = impl->get_attribute("@attribute.reflect_name@"_fly_string);
if (content_attribute_value.has_value()) {
auto maybe_parsed_value = Web::HTML::parse_integer(*content_attribute_value);
if (maybe_parsed_value.has_value())
@ -3755,7 +3753,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
// FIXME: 2. Return maximum.
attribute_generator.append(R"~~~(
u32 retval = 0;
auto content_attribute_value = impl->get_attribute(HTML::AttributeNames::@attribute.reflect_name@);
auto content_attribute_value = impl->get_attribute("@attribute.reflect_name@"_fly_string);
u32 minimum = 0;
u32 maximum = 2147483647;
if (content_attribute_value.has_value()) {
@ -3776,7 +3774,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
// NOTE: this is "impl" above
// 2. Let contentAttributeValue be the result of running this's get the content attribute.
attribute_generator.append(R"~~~(
auto content_attribute_value = impl->attribute(HTML::AttributeNames::@attribute.reflect_name@);
auto content_attribute_value = impl->attribute("@attribute.reflect_name@"_fly_string);
)~~~");
// 3. Let attributeDefinition be the attribute definition of element's content attribute whose namespace is null and local name is the reflected content attribute name.
// NOTE: this is "attribute" above
@ -3815,7 +3813,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
// 1. Let element be the result of running reflectedTarget's get the element.
// 2. Let contentAttributeValue be the result of running reflectedTarget's get the content attribute.
attribute_generator.append(R"~~~(
auto contentAttributeValue = impl->attribute(HTML::AttributeNames::@attribute.reflect_name@);
auto contentAttributeValue = impl->attribute("@attribute.reflect_name@"_fly_string);
)~~~");
// 3. If reflectedTarget's explicitly set attr-element is not null:
// 1. If reflectedTarget's explicitly set attr-element is a descendant of any of element's shadow-including ancestors, then return reflectedTarget's explicitly set attr-element.
@ -3852,7 +3850,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
} else {
attribute_generator.append(R"~~~(
auto retval = impl->get_attribute_value(HTML::AttributeNames::@attribute.reflect_name@);
auto retval = impl->get_attribute_value("@attribute.reflect_name@"_fly_string);
)~~~");
}
@ -3929,9 +3927,9 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
if (attribute.type->name() == "boolean") {
attribute_generator.append(R"~~~(
if (!cpp_value)
impl->remove_attribute(HTML::AttributeNames::@attribute.reflect_name@);
impl->remove_attribute("@attribute.reflect_name@"_fly_string);
else
MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, String {}));
MUST(impl->set_attribute("@attribute.reflect_name@"_fly_string, String {}));
)~~~");
} else if (attribute.type->name() == "unsigned long") {
// The setter steps are:
@ -3947,11 +3945,11 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
u32 new_value = minimum;
if (cpp_value >= minimum && cpp_value <= 2147483647)
new_value = cpp_value;
MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, String::number(new_value)));
MUST(impl->set_attribute("@attribute.reflect_name@"_fly_string, String::number(new_value)));
)~~~");
} else if (attribute.type->is_integer() && !attribute.type->is_nullable()) {
attribute_generator.append(R"~~~(
MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, String::number(cpp_value)));
MUST(impl->set_attribute("@attribute.reflect_name@"_fly_string, String::number(cpp_value)));
)~~~");
}
// If a reflected IDL attribute has the type T?, where T is either Element
@ -3966,13 +3964,13 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
attribute_generator.append(R"~~~(
if (!cpp_value) {
TRY(throw_dom_exception_if_needed(vm, [&] { return impl->set_@attribute.cpp_name@(nullptr); }));
impl->remove_attribute(HTML::AttributeNames::@attribute.reflect_name@);
impl->remove_attribute("@attribute.reflect_name@"_fly_string);
return JS::js_undefined();
}
)~~~");
// 2. Run this's set the content attribute with the empty string.
attribute_generator.append(R"~~~(
MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, String {}));
MUST(impl->set_attribute("@attribute.reflect_name@"_fly_string, String {}));
)~~~");
// 3. Set this's explicitly set attr-element to a weak reference to the given value.
attribute_generator.append(R"~~~(
@ -3981,13 +3979,13 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
} else if (attribute.type->is_nullable()) {
attribute_generator.append(R"~~~(
if (!cpp_value.has_value())
impl->remove_attribute(HTML::AttributeNames::@attribute.reflect_name@);
impl->remove_attribute("@attribute.reflect_name@"_fly_string);
else
MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, cpp_value.value()));
MUST(impl->set_attribute("@attribute.reflect_name@"_fly_string, cpp_value.value()));
)~~~");
} else {
attribute_generator.append(R"~~~(
MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, cpp_value));
MUST(impl->set_attribute("@attribute.reflect_name@"_fly_string, cpp_value));
)~~~");
}