LibWeb: Implement lang() function for DOM::Element
This is in accordance with: https://html.spec.whatwg.org/multipage/dom.html#the-lang-and-xml:lang-attributes
This commit is contained in:
parent
5c1bbd3eff
commit
5ac1a24255
Notes:
github-actions[bot]
2024-10-28 21:56:19 +00:00
Author: https://github.com/Grubre 🔰 Commit: https://github.com/LadybirdBrowser/ladybird/commit/5ac1a24255b Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1974 Reviewed-by: https://github.com/trflynn89
2 changed files with 42 additions and 0 deletions
|
@ -9,6 +9,7 @@
|
|||
#include <AK/Debug.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibUnicode/CharacterTypes.h>
|
||||
#include <LibUnicode/Locale.h>
|
||||
#include <LibWeb/Bindings/ElementPrototype.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||
|
@ -2901,4 +2902,43 @@ void Element::inherit_counters()
|
|||
m_counters_set = move(element_counters);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/dom.html#the-lang-and-xml:lang-attributes
|
||||
Optional<String> Element::lang() const
|
||||
{
|
||||
// 1. If the node is an element that has a lang attribute in the XML namespace set
|
||||
// Use the value of that attribute.
|
||||
auto maybe_xml_lang = get_attribute_ns(Namespace::XML, HTML::AttributeNames::lang);
|
||||
if (maybe_xml_lang.has_value())
|
||||
return maybe_xml_lang.release_value();
|
||||
|
||||
// 2. If the node is an HTML element or an element in the SVG namespace, and it has a lang in no namespace attribute set
|
||||
// Use the value of that attribute.
|
||||
if (is_html_element() || namespace_uri() == Namespace::SVG) {
|
||||
auto maybe_lang = get_attribute(HTML::AttributeNames::lang);
|
||||
if (maybe_lang.has_value())
|
||||
return maybe_lang.release_value();
|
||||
}
|
||||
|
||||
// 3. If the node's parent is a shadow root
|
||||
// Use the language of that shadow root's host.
|
||||
if (auto const* parent = parent_element()) {
|
||||
if (parent->is_shadow_root())
|
||||
return parent->shadow_root()->host()->lang();
|
||||
}
|
||||
|
||||
// 4. If the node's parent element is not null
|
||||
// Use the language of that parent element.
|
||||
if (auto const* parent = parent_element())
|
||||
return parent->lang();
|
||||
|
||||
// 5. Otherwise
|
||||
// - If there is a pragma-set default language set, then that is the language of the node.
|
||||
// - If there is no pragma-set default language set, then language information from a higher-level protocol (such as HTTP),
|
||||
// if any, must be used as the final fallback language instead.
|
||||
// - In the absence of any such language information, and in cases where the higher-level protocol reports multiple languages,
|
||||
// the language of the node is unknown, and the corresponding language tag is the empty string.
|
||||
// Default locale sounds like a reasonable fallback here.
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -123,6 +123,8 @@ public:
|
|||
Optional<String> get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& name) const;
|
||||
String get_attribute_value(FlyString const& local_name, Optional<FlyString> const& namespace_ = {}) const;
|
||||
|
||||
Optional<String> lang() const;
|
||||
|
||||
WebIDL::ExceptionOr<void> set_attribute(FlyString const& name, String const& value);
|
||||
|
||||
WebIDL::ExceptionOr<void> set_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& qualified_name, String const& value);
|
||||
|
|
Loading…
Add table
Reference in a new issue