BindingsGenerator: Add codegen for reflected nullable Element attributes

This commit is contained in:
Nathan van der Kamp 2024-11-23 22:36:37 +01:00 committed by Tim Ledbetter
parent 32dddc8140
commit 158acabd21

View File

@ -3794,6 +3794,54 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.getter_callback@)
if (content_attribute_value.has_value())
retval = MUST(Infra::convert_to_scalar_value_string(*content_attribute_value));
)~~~");
}
// If a reflected IDL attribute has the type T?, where T is either Element
// FIXME: or an interface that inherits from Element,
// then with attr being the reflected content attribute name:
else if (attribute.type->is_nullable() && attribute.type->name() == "Element") {
// The getter steps are to return the result of running this's get the attr-associated element.
attribute_generator.append(R"~~~(
auto retval = GC::Ptr<Element> {};
)~~~");
// 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@);
)~~~");
// 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.
// 2. Return null.
attribute_generator.append(R"~~~(
auto const explicitly_set_attr = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->get_@attribute.cpp_name@(); }));
if (explicitly_set_attr) {
if (&impl->shadow_including_root() == &explicitly_set_attr->shadow_including_root()) {
retval = explicitly_set_attr;
} else {
retval = GC::Ptr<Element> {};
}
}
)~~~");
// 4. Otherwise, if contentAttributeValue is not null, return the first element candidate, in tree order, that meets the following criteria:
// candidate's root is the same as element's root;
// candidate's ID is contentAttributeValue; and
// FIXME: candidate implements T.
// If no such element exists, then return null.
// 5. Return null.
// FIXME: This works when T is Element but will need adjustment when we handle subtypes too
attribute_generator.append(R"~~~(
else if (contentAttributeValue.has_value()) {
impl->root().for_each_in_inclusive_subtree_of_type<DOM::Element>([&](auto& candidate) {
if (candidate.attribute(HTML::AttributeNames::id) == contentAttributeValue.value()) {
retval = &candidate;
return TraversalDecision::Break;
}
return TraversalDecision::Continue;
});
}
)~~~");
} else {
attribute_generator.append(R"~~~(
auto retval = impl->get_attribute_value(HTML::AttributeNames::@attribute.reflect_name@);
@ -3894,6 +3942,31 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
} 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)));
)~~~");
}
// If a reflected IDL attribute has the type T?, where T is either Element
// FIXME: or an interface that inherits from Element,
// then with attr being the reflected content attribute name:
else if (attribute.type->is_nullable() && attribute.type->name() == "Element") {
// The setter steps are:
// 1. If the given value is null, then:
// 1. Set this's explicitly set attr-element to null.
// 2. Run this's delete the content attribute.
// 3. Return.
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@);
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 {}));
)~~~");
// 3. Set this's explicitly set attr-element to a weak reference to the given value.
attribute_generator.append(R"~~~(
TRY(throw_dom_exception_if_needed(vm, [&] { return impl->set_@attribute.cpp_name@(cpp_value); }));
)~~~");
} else if (attribute.type->is_nullable()) {
attribute_generator.append(R"~~~(