mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 09:09:43 +00:00
LibWeb/DOM: Move "text node directionality" algorithm into DOM::Text
This commit is contained in:
parent
eaea80007c
commit
2a5390ef4f
Notes:
github-actions[bot]
2024-09-18 13:19:11 +00:00
Author: https://github.com/AtkinsSJ
Commit: 2a5390ef4f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1426
3 changed files with 32 additions and 27 deletions
|
@ -2575,31 +2575,6 @@ bool Element::is_auto_directionality_form_associated_element() const
|
||||||
// https://html.spec.whatwg.org/multipage/dom.html#auto-directionality
|
// https://html.spec.whatwg.org/multipage/dom.html#auto-directionality
|
||||||
Optional<Element::Directionality> Element::auto_directionality() const
|
Optional<Element::Directionality> Element::auto_directionality() const
|
||||||
{
|
{
|
||||||
// https://html.spec.whatwg.org/multipage/dom.html#text-node-directionality
|
|
||||||
auto text_node_directionality = [](Text const& text_node) -> Optional<Directionality> {
|
|
||||||
// 1. If text's data does not contain a code point whose bidirectional character type is L, AL, or R, then return null.
|
|
||||||
// 2. Let codePoint be the first code point in text's data whose bidirectional character type is L, AL, or R.
|
|
||||||
Optional<Unicode::BidiClass> found_character_bidi_class;
|
|
||||||
for (auto code_point : Utf8View(text_node.data())) {
|
|
||||||
auto bidi_class = Unicode::bidirectional_class(code_point);
|
|
||||||
if (first_is_one_of(bidi_class, Unicode::BidiClass::LeftToRight, Unicode::BidiClass::RightToLeftArabic, Unicode::BidiClass::RightToLeft)) {
|
|
||||||
found_character_bidi_class = bidi_class;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found_character_bidi_class.has_value())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
// 3. If codePoint is of bidirectional character type AL or R, then return 'rtl'.
|
|
||||||
if (first_is_one_of(*found_character_bidi_class, Unicode::BidiClass::RightToLeftArabic, Unicode::BidiClass::RightToLeft))
|
|
||||||
return Directionality::Rtl;
|
|
||||||
|
|
||||||
// 4. If codePoint is of bidirectional character type L, then return 'ltr'.
|
|
||||||
// NOTE: codePoint should always be of bidirectional character type L by this point, so we can just return 'ltr' here.
|
|
||||||
VERIFY(*found_character_bidi_class == Unicode::BidiClass::LeftToRight);
|
|
||||||
return Directionality::Ltr;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 1. If element is an auto-directionality form-associated element:
|
// 1. If element is an auto-directionality form-associated element:
|
||||||
if (is_auto_directionality_form_associated_element()) {
|
if (is_auto_directionality_form_associated_element()) {
|
||||||
auto const* form_associated_element = dynamic_cast<HTML::FormAssociatedElement const*>(this);
|
auto const* form_associated_element = dynamic_cast<HTML::FormAssociatedElement const*>(this);
|
||||||
|
@ -2635,7 +2610,7 @@ Optional<Element::Directionality> Element::auto_directionality() const
|
||||||
|
|
||||||
// 2. If child is a Text node, then set childDirection to the text node directionality of child.
|
// 2. If child is a Text node, then set childDirection to the text node directionality of child.
|
||||||
if (child->is_text())
|
if (child->is_text())
|
||||||
child_direction = text_node_directionality(static_cast<Text const&>(*child));
|
child_direction = static_cast<Text const&>(*child).directionality();
|
||||||
|
|
||||||
// 3. Otherwise:
|
// 3. Otherwise:
|
||||||
else {
|
else {
|
||||||
|
@ -2689,7 +2664,7 @@ Optional<Element::Directionality> Element::auto_directionality() const
|
||||||
return TraversalDecision::Continue;
|
return TraversalDecision::Continue;
|
||||||
|
|
||||||
// 4. Let result be the text node directionality of descendant.
|
// 4. Let result be the text node directionality of descendant.
|
||||||
result = text_node_directionality(static_cast<Text const&>(descendant));
|
result = static_cast<Text const&>(descendant).directionality();
|
||||||
|
|
||||||
// 5. If result is not null, then return result.
|
// 5. If result is not null, then return result.
|
||||||
if (result.has_value())
|
if (result.has_value())
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibUnicode/CharacterTypes.h>
|
||||||
#include <LibWeb/Bindings/Intrinsics.h>
|
#include <LibWeb/Bindings/Intrinsics.h>
|
||||||
#include <LibWeb/Bindings/TextPrototype.h>
|
#include <LibWeb/Bindings/TextPrototype.h>
|
||||||
#include <LibWeb/DOM/Range.h>
|
#include <LibWeb/DOM/Range.h>
|
||||||
|
@ -149,4 +150,30 @@ String Text::whole_text()
|
||||||
return MUST(builder.to_string());
|
return MUST(builder.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/dom.html#text-node-directionality
|
||||||
|
Optional<Element::Directionality> Text::directionality() const
|
||||||
|
{
|
||||||
|
// 1. If text's data does not contain a code point whose bidirectional character type is L, AL, or R, then return null.
|
||||||
|
// 2. Let codePoint be the first code point in text's data whose bidirectional character type is L, AL, or R.
|
||||||
|
Optional<Unicode::BidiClass> found_character_bidi_class;
|
||||||
|
for (auto code_point : Utf8View(data())) {
|
||||||
|
auto bidi_class = Unicode::bidirectional_class(code_point);
|
||||||
|
if (first_is_one_of(bidi_class, Unicode::BidiClass::LeftToRight, Unicode::BidiClass::RightToLeftArabic, Unicode::BidiClass::RightToLeft)) {
|
||||||
|
found_character_bidi_class = bidi_class;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found_character_bidi_class.has_value())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. If codePoint is of bidirectional character type AL or R, then return 'rtl'.
|
||||||
|
if (first_is_one_of(*found_character_bidi_class, Unicode::BidiClass::RightToLeftArabic, Unicode::BidiClass::RightToLeft))
|
||||||
|
return Element::Directionality::Rtl;
|
||||||
|
|
||||||
|
// 4. If codePoint is of bidirectional character type L, then return 'ltr'.
|
||||||
|
// NOTE: codePoint should always be of bidirectional character type L by this point, so we can just return 'ltr' here.
|
||||||
|
VERIFY(*found_character_bidi_class == Unicode::BidiClass::LeftToRight);
|
||||||
|
return Element::Directionality::Ltr;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibWeb/DOM/CharacterData.h>
|
#include <LibWeb/DOM/CharacterData.h>
|
||||||
|
#include <LibWeb/DOM/Element.h>
|
||||||
#include <LibWeb/DOM/Slottable.h>
|
#include <LibWeb/DOM/Slottable.h>
|
||||||
|
|
||||||
namespace Web::DOM {
|
namespace Web::DOM {
|
||||||
|
@ -48,6 +49,8 @@ public:
|
||||||
bool is_password_input() const { return m_is_password_input; }
|
bool is_password_input() const { return m_is_password_input; }
|
||||||
void set_is_password_input(Badge<HTML::HTMLInputElement>, bool b) { m_is_password_input = b; }
|
void set_is_password_input(Badge<HTML::HTMLInputElement>, bool b) { m_is_password_input = b; }
|
||||||
|
|
||||||
|
Optional<Element::Directionality> directionality() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Text(Document&, String const&);
|
Text(Document&, String const&);
|
||||||
Text(Document&, NodeType, String const&);
|
Text(Document&, NodeType, String const&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue