LibWeb: Implement the "unlink" editing command

This commit is contained in:
Jelle Raaijmakers 2025-01-08 17:38:02 +01:00 committed by Andreas Kling
parent 043a28baeb
commit e686328cbd
Notes: github-actions[bot] 2025-01-10 22:35:59 +00:00
4 changed files with 66 additions and 0 deletions

View file

@ -1439,6 +1439,41 @@ bool command_underline_action(DOM::Document& document, String const&)
return true;
}
// https://w3c.github.io/editing/docs/execCommand/#the-unlink-command
bool command_unlink_action(DOM::Document& document, String const&)
{
// 1. Let hyperlinks be a list of every a element that has an href attribute and is contained in the active range or
// is an ancestor of one of its boundary points.
Vector<GC::Ref<DOM::Element>> hyperlinks;
if (auto range = active_range(document)) {
auto node_matches = [](GC::Ref<DOM::Node> node) {
return is<HTML::HTMLAnchorElement>(*node)
&& static_cast<HTML::HTMLAnchorElement&>(*node).has_attribute(HTML::AttributeNames::href);
};
range->for_each_contained([&](GC::Ref<DOM::Node> node) {
if (node_matches(node))
hyperlinks.append(static_cast<DOM::Element&>(*node));
return IterationDecision::Continue;
});
auto add_matching_ancestors = [&](GC::Ref<DOM::Node> node) {
node->for_each_ancestor([&](GC::Ref<DOM::Node> ancestor) {
if (node_matches(ancestor))
hyperlinks.append(static_cast<DOM::Element&>(*ancestor));
return IterationDecision::Continue;
});
};
add_matching_ancestors(range->start_container());
add_matching_ancestors(range->end_container());
}
// 2. Clear the value of each member of hyperlinks.
for (auto member : hyperlinks)
clear_the_value(CommandNames::unlink, member);
// 3. Return true.
return true;
}
static Array const commands {
// https://w3c.github.io/editing/docs/execCommand/#the-backcolor-command
CommandDefinition {
@ -1557,6 +1592,11 @@ static Array const commands {
.action = command_underline_action,
.inline_activated_values = { "underline"sv },
},
// https://w3c.github.io/editing/docs/execCommand/#the-unlink-command
CommandDefinition {
.command = CommandNames::unlink,
.action = command_unlink_action,
},
};
Optional<CommandDefinition const&> find_command_definition(FlyString const& command)

View file

@ -52,5 +52,6 @@ bool command_subscript_indeterminate(DOM::Document const&);
bool command_superscript_action(DOM::Document&, String const&);
bool command_superscript_indeterminate(DOM::Document const&);
bool command_underline_action(DOM::Document&, String const&);
bool command_unlink_action(DOM::Document&, String const&);
}

View file

@ -0,0 +1,2 @@
d1 contents: "ladybird"
d2 contents: "ladybird"

View file

@ -0,0 +1,23 @@
<script src="../include.js"></script>
<div contenteditable="true" id="d1"><a href="https://ladybird.dev">ladybird</a></div>
<div contenteditable="true" id="d2">lady<a href="https://ladybird.dev">bird</a></div>
<script>
test(() => {
const range = document.createRange();
getSelection().addRange(range);
// Unlink d1's ladybird
const div1 = document.querySelector('#d1');
range.setStart(div1.childNodes[0].childNodes[0], 0);
range.setEnd(div1.childNodes[0].childNodes[0], 8);
document.execCommand('unlink');
println(`d1 contents: "${div1.innerHTML}"`);
// Unlink d2's 'bird'
const div2 = document.querySelector('#d2');
range.setStart(div2.childNodes[1].childNodes[0], 2);
range.setEnd(div2.childNodes[1].childNodes[0], 3);
document.execCommand('unlink');
println(`d2 contents: "${div2.innerHTML}"`);
});
</script>