LibWeb: Replace local name checks with simpler is<T> in editing API

This commit is contained in:
Jelle Raaijmakers 2024-12-04 00:18:06 +01:00 committed by Jelle Raaijmakers
parent e308a3fd3f
commit 9b446df7f5
Notes: github-actions[bot] 2024-12-04 05:52:48 +00:00
2 changed files with 10 additions and 7 deletions

View file

@ -15,7 +15,9 @@
#include <LibWeb/Editing/Internal/Algorithms.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLBRElement.h>
#include <LibWeb/HTML/HTMLHRElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLLIElement.h>
#include <LibWeb/HTML/HTMLTableElement.h>
#include <LibWeb/Namespace.h>
@ -152,9 +154,8 @@ bool command_delete_action(DOM::Document& document, String const&)
auto items = Vector<GC::Ref<DOM::Element>>();
GC::Ptr<DOM::Node> ancestor = node->parent();
while (ancestor) {
auto& ancestor_element = static_cast<DOM::Element&>(*ancestor);
if (ancestor_element.local_name() == HTML::TagNames::li)
items.append(ancestor_element);
if (is<HTML::HTMLLIElement>(*ancestor))
items.append(static_cast<DOM::Element&>(*ancestor));
ancestor = ancestor->parent();
}
@ -264,7 +265,7 @@ bool command_delete_action(DOM::Document& document, String const&)
if (offset == 0 && is<DOM::Element>(offset_minus_one_child.ptr())) {
auto& child_element = static_cast<DOM::Element&>(*offset_minus_one_child);
auto* previous_sibling = child_element.previous_sibling();
if (child_element.local_name() == HTML::TagNames::hr
if (is<HTML::HTMLHRElement>(child_element)
|| (is<HTML::HTMLBRElement>(child_element) && previous_sibling && (is<HTML::HTMLBRElement>(*previous_sibling) || !is_inline_node(*previous_sibling)))) {
// 1. Call collapse(start node, start offset 1) on the context object's selection.
MUST(selection.collapse(start_node, start_offset - 1));

View file

@ -13,9 +13,11 @@
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Editing/CommandNames.h>
#include <LibWeb/Editing/Internal/Algorithms.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLBRElement.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLLIElement.h>
#include <LibWeb/HTML/HTMLOListElement.h>
#include <LibWeb/HTML/HTMLUListElement.h>
#include <LibWeb/Infra/CharacterTypes.h>
@ -632,7 +634,7 @@ bool is_allowed_child_of_node(Variant<GC::Ref<DOM::Node>, FlyString> child, Vari
if (child_local_name == HTML::TagNames::a) {
DOM::Node* ancestor = &parent_html_element;
while (ancestor) {
if (is<DOM::Element>(ancestor) && static_cast<DOM::Element const&>(*ancestor).local_name() == HTML::TagNames::a)
if (is<HTML::HTMLAnchorElement>(*ancestor))
return false;
ancestor = ancestor->parent();
}
@ -940,8 +942,8 @@ bool is_extraneous_line_break(GC::Ref<DOM::Node> node)
return false;
// ...except that a br that is the sole child of an li is not extraneous.
auto parent = node->parent();
if (parent && static_cast<DOM::Element&>(*parent).local_name() == HTML::TagNames::li && parent->child_count() == 1)
GC::Ptr<DOM::Node> parent = node->parent();
if (is<HTML::HTMLLIElement>(parent.ptr()) && parent->child_count() == 1)
return false;
// FIXME: ...that has no visual effect, in that removing it from the DOM