| 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 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | using namespace HTMLNames; |
| 41 | |
| 42 | RemoveFormatCommand::RemoveFormatCommand(Document& document) |
| 43 | : CompositeEditCommand(document) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | static bool isElementForRemoveFormatCommand(const Element* element) |
| 48 | { |
| 49 | static const auto elements = makeNeverDestroyed(HashSet<QualifiedName> { |
| 50 | acronymTag, |
| 51 | bTag, |
| 52 | bdoTag, |
| 53 | bigTag, |
| 54 | citeTag, |
| 55 | codeTag, |
| 56 | dfnTag, |
| 57 | emTag, |
| 58 | fontTag, |
| 59 | iTag, |
| 60 | insTag, |
| 61 | kbdTag, |
| 62 | nobrTag, |
| 63 | qTag, |
| 64 | sTag, |
| 65 | sampTag, |
| 66 | smallTag, |
| 67 | strikeTag, |
| 68 | strongTag, |
| 69 | subTag, |
| 70 | supTag, |
| 71 | ttTag, |
| 72 | uTag, |
| 73 | varTag, |
| 74 | }); |
| 75 | return elements.get().contains(element->tagQName()); |
| 76 | } |
| 77 | |
| 78 | void RemoveFormatCommand::doApply() |
| 79 | { |
| 80 | if (endingSelection().isNoneOrOrphaned()) |
| 81 | return; |
| 82 | |
| 83 | // Get the default style for this editable root, it's the style that we'll give the |
| 84 | // content that we're operating on. |
| 85 | Node* root = endingSelection().rootEditableElement(); |
| 86 | auto defaultStyle = EditingStyle::create(root); |
| 87 | |
| 88 | // We want to remove everything but transparent background. |
| 89 | // FIXME: We shouldn't access style(). |
| 90 | defaultStyle->style()->setProperty(CSSPropertyBackgroundColor, CSSValueTransparent); |
| 91 | |
| 92 | applyCommandToComposite(ApplyStyleCommand::create(document(), defaultStyle.ptr(), isElementForRemoveFormatCommand, editingAction())); |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | |