script/dom/execcommand/commands/
defaultparagraphseparator.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 crate::dom::bindings::str::DOMString;
6use crate::dom::document::Document;
7use crate::dom::execcommand::basecommand::{BaseCommand, DefaultSingleLineContainerName};
8use crate::dom::selection::Selection;
9
10impl From<DefaultSingleLineContainerName> for DOMString {
11    fn from(default_single_line_container_name: DefaultSingleLineContainerName) -> Self {
12        match default_single_line_container_name {
13            DefaultSingleLineContainerName::Div => DOMString::from("div"),
14            DefaultSingleLineContainerName::Paragraph => DOMString::from("p"),
15        }
16    }
17}
18
19pub(crate) struct DefaultParagraphSeparatorCommand {}
20
21impl BaseCommand for DefaultParagraphSeparatorCommand {
22    /// <https://w3c.github.io/editing/docs/execCommand/#the-defaultparagraphseparator-command>
23    fn execute(
24        &self,
25        _cx: &mut js::context::JSContext,
26        document: &Document,
27        _selection: &Selection,
28        value: DOMString,
29    ) -> bool {
30        // > Let value be converted to ASCII lowercase. If value is then equal to "p" or "div",
31        // > set the context object's default single-line container name to value, then return true. Otherwise, return false.
32        let value = match value.to_ascii_lowercase().as_str() {
33            "div" => DefaultSingleLineContainerName::Div,
34            "p" => DefaultSingleLineContainerName::Paragraph,
35            _ => return false,
36        };
37
38        document.set_default_single_line_container_name(value);
39
40        true
41    }
42
43    /// <https://w3c.github.io/editing/docs/execCommand/#the-defaultparagraphseparator-command>
44    fn current_value(&self, document: &Document) -> Option<DOMString> {
45        // > Return the context object's default single-line container name.
46        Some(document.default_single_line_container_name().into())
47    }
48}