mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-10 01:59:31 +00:00
LibWeb: Implement the "createLink" editing command
This commit is contained in:
parent
05386fe99c
commit
1b02e0dea3
Notes:
github-actions[bot]
2025-01-10 22:37:08 +00:00
Author: https://github.com/gmta
Commit: 1b02e0dea3
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3216
4 changed files with 64 additions and 0 deletions
|
@ -64,6 +64,39 @@ bool command_bold_action(DOM::Document& document, String const&)
|
|||
return true;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/editing/docs/execCommand/#the-createlink-command
|
||||
bool command_create_link_action(DOM::Document& document, String const& value)
|
||||
{
|
||||
// 1. If value is the empty string, return false.
|
||||
if (value.is_empty())
|
||||
return false;
|
||||
|
||||
// 2. For each editable a element that has an href attribute and is an ancestor of some node effectively contained
|
||||
// in the active range, set that a element's href attribute to value.
|
||||
HashTable<DOM::Node*> visited_ancestors;
|
||||
auto set_value_for_ancestor_anchors = [&](GC::Ref<DOM::Node> node) {
|
||||
node->for_each_ancestor([&](GC::Ref<DOM::Node> ancestor) {
|
||||
if (visited_ancestors.contains(ancestor.ptr()))
|
||||
return IterationDecision::Break;
|
||||
if (is<HTML::HTMLAnchorElement>(*ancestor) && ancestor->is_editable()
|
||||
&& static_cast<DOM::Element&>(*ancestor).has_attribute(HTML::AttributeNames::href))
|
||||
MUST(static_cast<HTML::HTMLAnchorElement&>(*ancestor).set_href(value));
|
||||
visited_ancestors.set(ancestor.ptr());
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
};
|
||||
for_each_node_effectively_contained_in_range(active_range(document), [&](GC::Ref<DOM::Node> descendant) {
|
||||
set_value_for_ancestor_anchors(descendant);
|
||||
return TraversalDecision::Continue;
|
||||
});
|
||||
|
||||
// 3. Set the selection's value to value.
|
||||
set_the_selections_value(document, CommandNames::createLink, value);
|
||||
|
||||
// 4. Return true.
|
||||
return true;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/editing/docs/execCommand/#the-defaultparagraphseparator-command
|
||||
bool command_default_paragraph_separator_action(DOM::Document& document, String const& input_value)
|
||||
{
|
||||
|
@ -1065,6 +1098,11 @@ static Array const commands {
|
|||
.relevant_css_property = CSS::PropertyID::FontWeight,
|
||||
.inline_activated_values = { "bold"sv, "600"sv, "700"sv, "800"sv, "900"sv },
|
||||
},
|
||||
// https://w3c.github.io/editing/docs/execCommand/#the-createlink-command
|
||||
CommandDefinition {
|
||||
.command = CommandNames::createLink,
|
||||
.action = command_create_link_action,
|
||||
},
|
||||
// https://w3c.github.io/editing/docs/execCommand/#the-delete-command
|
||||
CommandDefinition {
|
||||
.command = CommandNames::delete_,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue