Skip to main content

script/dom/execcommand/commands/
backcolor.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 js::context::JSContext;
6
7use crate::dom::bindings::str::{DOMString, FromInputValueString};
8use crate::dom::document::Document;
9use crate::dom::execcommand::basecommand::CommandName;
10use crate::dom::selection::Selection;
11
12/// <https://w3c.github.io/editing/docs/execCommand/#the-backcolor-command>
13pub(crate) fn execute_backcolor_command(
14    cx: &mut JSContext,
15    document: &Document,
16    selection: &Selection,
17    value: DOMString,
18) -> bool {
19    // Step 1. If value is not a valid CSS color, prepend "#" to it.
20    let value = if !value.str().is_valid_simple_color_string() {
21        ("#".to_owned() + &*value.str()).into()
22    } else {
23        value
24    };
25    // Step 2. If value is still not a valid CSS color, or if it is currentColor, return false.
26    //
27    // TODO: figure out what to do with currentColor
28    if !value.str().is_valid_simple_color_string() {
29        return false;
30    }
31    // Step 3. Set the selection's value to value.
32    selection.set_the_selection_value(cx, Some(value), CommandName::BackColor, document);
33    // Step 4. Return true.
34    true
35}