Files
ladybird/Libraries/LibWeb/HTML/HTMLHeadingElement.h
Sam Atkins f9f4c36f20 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
2025-12-15 14:08:24 +00:00

49 lines
1.4 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/ARIA/Roles.h>
#include <LibWeb/HTML/HTMLElement.h>
namespace Web::HTML {
class HTMLHeadingElement final : public HTMLElement {
WEB_PLATFORM_OBJECT(HTMLHeadingElement, HTMLElement);
GC_DECLARE_ALLOCATOR(HTMLHeadingElement);
public:
virtual ~HTMLHeadingElement() override;
virtual bool is_presentational_hint(FlyString const&) const override;
virtual void apply_presentational_hints(GC::Ref<CSS::CascadedProperties>) const override;
// https://www.w3.org/TR/html-aria/#el-h1-h6
virtual Optional<ARIA::Role> default_role() const override { return ARIA::Role::heading; }
WebIDL::UnsignedLong heading_level() const;
virtual Optional<String> aria_level() const override
{
if (auto const attr = get_attribute(ARIA::AttributeNames::aria_level); attr.has_value())
return attr;
// Implicit defaults to the number in the element's tag name.
return MUST(local_name().to_string().substring_from_byte_offset(1));
}
private:
HTMLHeadingElement(DOM::Document&, DOM::QualifiedName);
virtual void initialize(JS::Realm&) override;
mutable WebIDL::UnsignedLong m_cached_heading_level { 0 };
mutable u64 m_dom_tree_version_for_cached_heading_level { 0 };
};
}