Skip to content

Commit

Permalink
LibWeb: Implement hidden="until-found" IDL support
Browse files Browse the repository at this point in the history
Follow-ups will implement the actual behaviour.
  • Loading branch information
lukewarlow committed Jan 24, 2025
1 parent 51a9177 commit 52d2435
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
52 changes: 52 additions & 0 deletions Libraries/LibWeb/HTML/HTMLElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
3 changes: 3 additions & 0 deletions Libraries/LibWeb/HTML/HTMLElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class HTMLElement

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;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibWeb/HTML/HTMLElement.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<link rel=author href="mailto:[email protected]">
<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>

0 comments on commit 52d2435

Please sign in to comment.