1/*
2 * Copyright (C) 2007 Apple Inc.  All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "RemoveFormatCommand.h"
29
30#include "ApplyStyleCommand.h"
31#include "Element.h"
32#include "Frame.h"
33#include "FrameSelection.h"
34#include "HTMLNames.h"
35#include "StyleProperties.h"
36#include <wtf/NeverDestroyed.h>
37
38namespace WebCore {
39
40using namespace HTMLNames;
41
42RemoveFormatCommand::RemoveFormatCommand(Document& document)
43    : CompositeEditCommand(document)
44{
45}
46
47static bool isElementForRemoveFormatCommand(const Element* element)
48{
49    static NeverDestroyed<HashSet<QualifiedName>> elements;
50    if (elements.get().isEmpty()) {
51        elements.get().add(acronymTag);
52        elements.get().add(bTag);
53        elements.get().add(bdoTag);
54        elements.get().add(bigTag);
55        elements.get().add(citeTag);
56        elements.get().add(codeTag);
57        elements.get().add(dfnTag);
58        elements.get().add(emTag);
59        elements.get().add(fontTag);
60        elements.get().add(iTag);
61        elements.get().add(insTag);
62        elements.get().add(kbdTag);
63        elements.get().add(nobrTag);
64        elements.get().add(qTag);
65        elements.get().add(sTag);
66        elements.get().add(sampTag);
67        elements.get().add(smallTag);
68        elements.get().add(strikeTag);
69        elements.get().add(strongTag);
70        elements.get().add(subTag);
71        elements.get().add(supTag);
72        elements.get().add(ttTag);
73        elements.get().add(uTag);
74        elements.get().add(varTag);
75    }
76    return elements.get().contains(element->tagQName());
77}
78
79void RemoveFormatCommand::doApply()
80{
81    if (!endingSelection().isNonOrphanedCaretOrRange())
82        return;
83
84    // Get the default style for this editable root, it's the style that we'll give the
85    // content that we're operating on.
86    Node* root = endingSelection().rootEditableElement();
87    RefPtr<EditingStyle> defaultStyle = EditingStyle::create(root);
88
89    // We want to remove everything but transparent background.
90    // FIXME: We shouldn't access style().
91    defaultStyle->style()->setProperty(CSSPropertyBackgroundColor, CSSValueTransparent);
92
93    applyCommandToComposite(ApplyStyleCommand::create(document(), defaultStyle.get(), isElementForRemoveFormatCommand, editingAction()));
94}
95
96}
97