LibWeb: Implement the headingoffset and headingreset attributes

:heading() now matches based on a computed heading level, which is based
on the level of the tag (h1, h2, etc) and then modified by these two new
attributes.

I'm caching this heading level on HTMLHeadingElement, based on the dom
tree version. That's more invalidation than is actually needed, but it
saves us calculating it over and over when the document hasn't changed.

The failing test cases are:
- Implicit headingreset for modal dialogs which is apparently unspecced
  and controversial.
- Not walking the flat tree properly. A flat tree ancestor of a
  slot-assigned element is its slot, which is something we don't do
  anywhere that I could find. I've made a note to look into this later.

We also don't implement the `ReflectRange` IDL attribute yet, which
means we're not clamping the read value of `headingOffset`.

Corresponds to:
e774e8e318
This commit is contained in:
Sam Atkins
2025-12-11 14:38:17 +00:00
parent 7418d262ed
commit f9f4c36f20
Notes: github-actions[bot] 2025-12-15 14:09:36 +00:00
11 changed files with 683 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -8,6 +9,7 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/ComputedProperties.h>
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/HTMLHeadingElement.h>
namespace Web::HTML {
@@ -53,4 +55,15 @@ void HTMLHeadingElement::apply_presentational_hints(GC::Ref<CSS::CascadedPropert
});
}
// https://html.spec.whatwg.org/multipage/sections.html#heading-level
WebIDL::UnsignedLong HTMLHeadingElement::heading_level() const
{
// h1h6 elements have a heading level, which is given by getting the element's computed heading level.
if (m_dom_tree_version_for_cached_heading_level < document().dom_tree_version()) {
m_dom_tree_version_for_cached_heading_level = document().dom_tree_version();
m_cached_heading_level = computed_heading_level();
}
return m_cached_heading_level;
}
}