LibWeb: Implement hidden="until-found" IDL support

Follow-ups will implement the actual behaviour.
This commit is contained in:
Luke Warlow 2025-01-24 00:53:31 +00:00 committed by Jelle Raaijmakers
parent 15fe8e7906
commit f5860a3b7d
Notes: github-actions[bot] 2025-01-24 08:09:38 +00:00
5 changed files with 127 additions and 1 deletions

View file

@ -683,6 +683,58 @@ GC::Ptr<DOM::NodeList> HTMLElement::labels()
return m_labels;
}
Variant<bool, double, String> HTMLElement::hidden() const
{
// 1. If the hidden attribute is in the hidden until found state, then return "until-found".
if (get_attribute(HTML::AttributeNames::hidden) == "until-found")
return "until-found"_string;
// 2. If the hidden attribute is set, then return true.
if (has_attribute(HTML::AttributeNames::hidden))
return true;
// 3. Return false.
return false;
}
void HTMLElement::set_hidden(Variant<bool, double, String> const& given_value)
{
// 1. If the given value is a string that is an ASCII case-insensitive match for "until-found", then set the hidden attribute to "until-found".
if (given_value.has<String>()) {
auto const& string = given_value.get<String>();
if (string.equals_ignoring_ascii_case("until-found"sv)) {
MUST(set_attribute(HTML::AttributeNames::hidden, "until-found"_string));
return;
}
// 3. Otherwise, if the given value is the empty string, then remove the hidden attribute.
if (string.is_empty()) {
remove_attribute(HTML::AttributeNames::hidden);
return;
}
// 4. Otherwise, if the given value is null, then remove the hidden attribute.
if (string.equals_ignoring_ascii_case("null"sv) || string.equals_ignoring_ascii_case("undefined"sv)) {
remove_attribute(HTML::AttributeNames::hidden);
return;
}
}
// 2. Otherwise, if the given value is false, then remove the hidden attribute.
else if (given_value.has<bool>()) {
if (!given_value.get<bool>()) {
remove_attribute(HTML::AttributeNames::hidden);
return;
}
}
// 5. Otherwise, if the given value is 0, then remove the hidden attribute.
// 6. Otherwise, if the given value is NaN, then remove the hidden attribute.
else if (given_value.has<double>()) {
auto const& double_value = given_value.get<double>();
if (double_value == 0 || isnan(double_value)) {
remove_attribute(HTML::AttributeNames::hidden);
return;
}
}
// 7. Otherwise, set the hidden attribute to the empty string.
MUST(set_attribute(HTML::AttributeNames::hidden, ""_string));
}
// https://html.spec.whatwg.org/multipage/interaction.html#dom-click
void HTMLElement::click()
{

View file

@ -95,6 +95,9 @@ public:
bool cannot_navigate() const;
Variant<bool, double, String> hidden() const;
void set_hidden(Variant<bool, double, String> const&);
void click();
[[nodiscard]] String access_key_label() const;

View file

@ -18,7 +18,7 @@ interface HTMLElement : Element {
[CEReactions] attribute DOMString dir;
// user interaction
[Reflect, CEReactions] attribute boolean hidden;
[CEReactions] attribute (boolean or unrestricted double or DOMString)? hidden;
[Reflect, CEReactions] attribute boolean inert;
undefined click();
[Reflect=accesskey, CEReactions] attribute DOMString accessKey;

View file

@ -0,0 +1,22 @@
Harness status: OK
Found 17 tests
17 Pass
Pass div.hidden = false
Pass div.hidden = true
Pass div.hidden = "foo"
Pass div.hidden = "false"
Pass div.hidden = ""
Pass div.setAttribute('hidden', "false") should make div.hidden return true
Pass div.setAttribute('hidden', "foo") should make div.hidden return true
Pass div.hidden = "until-found"
Pass div.hidden = "UNTIL-FOUND"
Pass div.hidden = "UnTiL-FoUnD"
Pass div.hidden = "untıl-found"
Pass div.hidden = "untİl-found"
Pass div.hidden = null
Pass div.hidden = undefined
Pass div.hidden = 1
Pass div.hidden = 0
Pass div.hidden = NaN

View file

@ -0,0 +1,49 @@
<!DOCTYPE html>
<link rel=author href="mailto:jarhar@chromium.org">
<link rel=help href="https://github.com/whatwg/html/pull/7475">
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<div>hello</div>
<script>
const div = document.querySelector('div');
function runPropertyTest(assignedValue, expectedValue, expectedAttribute) {
test(() => {
div.hidden = assignedValue;
assert_equals(div.hidden, expectedValue,
`div.hidden = ${JSON.stringify(assignedValue)} should return ${JSON.stringify(expectedValue)}`);
assert_equals(div.getAttribute('hidden'), expectedAttribute,
`div.hidden = ${JSON.stringify(assignedValue)} should set the hidden attribute to ${JSON.stringify(expectedAttribute)}`);
}, `div.hidden = ${Number.isNaN(assignedValue) ? 'NaN' : JSON.stringify(assignedValue)}`);
}
function runAttributeTest(assignedAttribute, expectedValue) {
test(() => {
div.setAttribute('hidden', assignedAttribute);
assert_equals(div.hidden, expectedValue);
}, `div.setAttribute('hidden', ${JSON.stringify(assignedAttribute)}) should make div.hidden return ${JSON.stringify(expectedValue)}`);
}
runPropertyTest(false, false, null);
runPropertyTest(true, true, '');
runPropertyTest('foo', true, '');
runPropertyTest('false', true, '');
runPropertyTest('', false, null);
runAttributeTest('false', true);
runAttributeTest('foo', true);
runPropertyTest('until-found', 'until-found', 'until-found');
runPropertyTest('UNTIL-FOUND', 'until-found', 'until-found');
runPropertyTest('UnTiL-FoUnD', 'until-found', 'until-found');
runPropertyTest('unt\u0131l-found', true, '');
runPropertyTest('unt\u0130l-found', true, '');
runPropertyTest(null, false, null);
runPropertyTest(undefined, false, null);
runPropertyTest(1, true, '');
runPropertyTest(0, false, null);
runPropertyTest(NaN, false, null);
</script>