1 | /* |
2 | * Copyright (C) 2005, 2008 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "SplitElementCommand.h" |
28 | |
29 | #include "Element.h" |
30 | #include "HTMLNames.h" |
31 | #include <wtf/Assertions.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | SplitElementCommand::SplitElementCommand(Ref<Element>&& element, Ref<Node>&& atChild) |
36 | : SimpleEditCommand(element->document()) |
37 | , m_element2(WTFMove(element)) |
38 | , m_atChild(WTFMove(atChild)) |
39 | { |
40 | ASSERT(m_atChild->parentNode() == m_element2.ptr()); |
41 | } |
42 | |
43 | void SplitElementCommand::executeApply() |
44 | { |
45 | if (m_atChild->parentNode() != m_element2.ptr()) |
46 | return; |
47 | |
48 | Vector<Ref<Node>> children; |
49 | for (Node* node = m_element2->firstChild(); node != m_atChild.ptr(); node = node->nextSibling()) |
50 | children.append(*node); |
51 | |
52 | auto* parent = m_element2->parentNode(); |
53 | if (!parent || !parent->hasEditableStyle()) |
54 | return; |
55 | if (parent->insertBefore(*m_element1, m_element2.ptr()).hasException()) |
56 | return; |
57 | |
58 | // Delete id attribute from the second element because the same id cannot be used for more than one element |
59 | m_element2->removeAttribute(HTMLNames::idAttr); |
60 | |
61 | for (auto& child : children) |
62 | m_element1->appendChild(child); |
63 | } |
64 | |
65 | void SplitElementCommand::doApply() |
66 | { |
67 | m_element1 = m_element2->cloneElementWithoutChildren(document()); |
68 | |
69 | executeApply(); |
70 | } |
71 | |
72 | void SplitElementCommand::doUnapply() |
73 | { |
74 | if (!m_element1 || !m_element1->hasEditableStyle() || !m_element2->hasEditableStyle()) |
75 | return; |
76 | |
77 | Vector<Ref<Node>> children; |
78 | for (Node* node = m_element1->firstChild(); node; node = node->nextSibling()) |
79 | children.append(*node); |
80 | |
81 | RefPtr<Node> refChild = m_element2->firstChild(); |
82 | |
83 | for (auto& child : children) |
84 | m_element2->insertBefore(child, refChild.get()); |
85 | |
86 | // Recover the id attribute of the original element. |
87 | const AtomicString& id = m_element1->getIdAttribute(); |
88 | if (!id.isNull()) |
89 | m_element2->setIdAttribute(id); |
90 | |
91 | m_element1->remove(); |
92 | } |
93 | |
94 | void SplitElementCommand::doReapply() |
95 | { |
96 | if (!m_element1) |
97 | return; |
98 | |
99 | executeApply(); |
100 | } |
101 | |
102 | #ifndef NDEBUG |
103 | void SplitElementCommand::getNodesInCommand(HashSet<Node*>& nodes) |
104 | { |
105 | addNodeAndDescendants(m_element1.get(), nodes); |
106 | addNodeAndDescendants(m_element2.ptr(), nodes); |
107 | addNodeAndDescendants(m_atChild.ptr(), nodes); |
108 | } |
109 | #endif |
110 | |
111 | } // namespace WebCore |
112 | |