Skip to main content

script/dom/execcommand/commands/
unlink.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use html5ever::local_name;
6use js::context::JSContext;
7use script_bindings::inheritance::Castable;
8
9use crate::dom::bindings::root::DomRoot;
10use crate::dom::element::Element;
11use crate::dom::execcommand::basecommand::CommandName;
12use crate::dom::html::htmlanchorelement::HTMLAnchorElement;
13use crate::dom::html::htmlelement::HTMLElement;
14use crate::dom::selection::Selection;
15
16/// <https://w3c.github.io/editing/docs/execCommand/#the-unlink-command>
17pub(crate) fn execute_unlink_command(cx: &mut JSContext, selection: &Selection) -> bool {
18    // Step 1. Let hyperlinks be a list of every a element that has an href attribute
19    // and is contained in the active range or is an ancestor of one of its boundary points.
20    let active_range = selection
21        .active_range()
22        .expect("Must always have an active range");
23    let mut hyperlinks = vec![];
24    active_range.for_each_effectively_contained_child(|node| {
25        if let Some(anchor) = node.downcast::<HTMLAnchorElement>() &&
26            anchor
27                .upcast::<Element>()
28                .has_attribute(&local_name!("href"))
29        {
30            hyperlinks.push(DomRoot::from_ref(anchor));
31        }
32    });
33    // Step 2. Clear the value of each member of hyperlinks.
34    for anchor in hyperlinks.iter() {
35        anchor
36            .upcast::<HTMLElement>()
37            .clear_the_value(cx, &CommandName::Unlink);
38    }
39    // Step 3. Return true.
40    true
41}