~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/editing/EditCommand.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2005, 2006 Apple Computer, 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 COMPUTER, 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 COMPUTER, 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 "EditCommand.h"
 
28
 
 
29
#include "CompositeEditCommand.h"
 
30
#include "CSSComputedStyleDeclaration.h"
 
31
#include "CSSMutableStyleDeclaration.h"
 
32
#include "DeleteButtonController.h"
 
33
#include "Document.h"
 
34
#include "Editor.h"
 
35
#include "Element.h"
 
36
#include "EventNames.h"
 
37
#include "Frame.h"
 
38
#include "SelectionController.h"
 
39
#include "VisiblePosition.h"
 
40
#include "htmlediting.h"
 
41
 
 
42
namespace WebCore {
 
43
 
 
44
using namespace EventNames;
 
45
 
 
46
EditCommand::EditCommand(Document* document) 
 
47
    : m_document(document)
 
48
    , m_startingSelection(document->frame()->selectionController()->selection())
 
49
    , m_endingSelection(m_startingSelection)
 
50
    , m_startingRootEditableElement(m_startingSelection.rootEditableElement())
 
51
    , m_endingRootEditableElement(m_startingRootEditableElement)
 
52
    , m_parent(0)
 
53
{
 
54
    ASSERT(m_document);
 
55
    ASSERT(m_document->frame());
 
56
    setStartingSelection(m_document->frame()->selectionController()->selection());
 
57
    setEndingSelection(m_startingSelection);
 
58
}
 
59
 
 
60
EditCommand::~EditCommand()
 
61
{
 
62
}
 
63
 
 
64
void EditCommand::apply()
 
65
{
 
66
    ASSERT(m_document);
 
67
    ASSERT(m_document->frame());
 
68
 
 
69
    Frame* frame = m_document->frame();
 
70
    
 
71
    if (!m_parent) {
 
72
        if (!endingSelection().isContentRichlyEditable()) {
 
73
            switch (editingAction()) {
 
74
                case EditActionTyping:
 
75
                case EditActionPaste:
 
76
                case EditActionDrag:
 
77
                case EditActionSetWritingDirection:
 
78
                case EditActionCut:
 
79
                case EditActionUnspecified:
 
80
                    break;
 
81
                default:
 
82
                    ASSERT_NOT_REACHED();
 
83
                    return;
 
84
            }
 
85
        }
 
86
    }
 
87
 
 
88
    doApply();
 
89
 
 
90
    // FIXME: Improve typing style.
 
91
    // See this bug: <rdar://problem/3769899> Implementation of typing style needs improvement
 
92
    if (!preservesTypingStyle()) {
 
93
        setTypingStyle(0);
 
94
        if (!m_parent)
 
95
            frame->editor()->setRemovedAnchor(0);
 
96
    }
 
97
 
 
98
    if (!m_parent) {
 
99
        updateLayout();
 
100
        frame->editor()->appliedEditing(this);
 
101
    }
 
102
}
 
103
 
 
104
void EditCommand::unapply()
 
105
{
 
106
    ASSERT(m_document);
 
107
    ASSERT(m_document->frame());
 
108
 
 
109
    Frame* frame = m_document->frame();
 
110
    
 
111
    doUnapply();
 
112
 
 
113
    if (!m_parent) {
 
114
        updateLayout();
 
115
        frame->editor()->unappliedEditing(this);
 
116
    }
 
117
}
 
118
 
 
119
void EditCommand::reapply()
 
120
{
 
121
    ASSERT(m_document);
 
122
    ASSERT(m_document->frame());
 
123
 
 
124
    Frame* frame = m_document->frame();
 
125
 
 
126
    doReapply();
 
127
 
 
128
    if (!m_parent) {
 
129
        updateLayout();
 
130
        frame->editor()->reappliedEditing(this);
 
131
    }
 
132
}
 
133
 
 
134
void EditCommand::doReapply()
 
135
{
 
136
    doApply();
 
137
}
 
138
 
 
139
EditAction EditCommand::editingAction() const
 
140
{
 
141
    return EditActionUnspecified;
 
142
}
 
143
 
 
144
void EditCommand::setStartingSelection(const Selection& s)
 
145
{
 
146
    Element* root = s.rootEditableElement();
 
147
    for (EditCommand* cmd = this; cmd; cmd = cmd->m_parent) {
 
148
        cmd->m_startingSelection = s;
 
149
        cmd->m_startingRootEditableElement = root;
 
150
        if (!cmd->m_parent || cmd->m_parent->isFirstCommand(cmd))
 
151
            break;
 
152
    }
 
153
}
 
154
 
 
155
void EditCommand::setEndingSelection(const Selection &s)
 
156
{
 
157
    Element* root = s.rootEditableElement();
 
158
    for (EditCommand* cmd = this; cmd; cmd = cmd->m_parent) {
 
159
        cmd->m_endingSelection = s;
 
160
        cmd->m_endingRootEditableElement = root;
 
161
    }
 
162
}
 
163
 
 
164
void EditCommand::setTypingStyle(PassRefPtr<CSSMutableStyleDeclaration> style)
 
165
{
 
166
    // FIXME: Improve typing style.
 
167
    // See this bug: <rdar://problem/3769899> Implementation of typing style needs improvement
 
168
    if (!m_parent) {
 
169
        // Special more-efficient case for where there's only one command that
 
170
        // takes advantage of the ability of PassRefPtr to pass its ref to a RefPtr.
 
171
        m_typingStyle = style;
 
172
        return;
 
173
    }
 
174
    for (EditCommand* cmd = this; cmd; cmd = cmd->m_parent)
 
175
        cmd->m_typingStyle = style.get(); // must use get() to avoid setting parent styles to 0
 
176
}
 
177
 
 
178
bool EditCommand::preservesTypingStyle() const
 
179
{
 
180
    return false;
 
181
}
 
182
 
 
183
bool EditCommand::isInsertTextCommand() const
 
184
{
 
185
    return false;
 
186
}
 
187
 
 
188
bool EditCommand::isTypingCommand() const
 
189
{
 
190
    return false;
 
191
}
 
192
 
 
193
PassRefPtr<CSSMutableStyleDeclaration> EditCommand::styleAtPosition(const Position &pos)
 
194
{
 
195
    RefPtr<CSSMutableStyleDeclaration> style = positionBeforeTabSpan(pos).computedStyle()->copyInheritableProperties();
 
196
 
 
197
    // FIXME: Improve typing style.
 
198
    // See this bug: <rdar://problem/3769899> Implementation of typing style needs improvement
 
199
    CSSMutableStyleDeclaration* typingStyle = document()->frame()->typingStyle();
 
200
    if (typingStyle)
 
201
        style->merge(typingStyle);
 
202
 
 
203
    return style.release();
 
204
}
 
205
 
 
206
void EditCommand::updateLayout() const
 
207
{
 
208
    document()->updateLayoutIgnorePendingStylesheets();
 
209
}
 
210
 
 
211
void EditCommand::setParent(CompositeEditCommand* parent)
 
212
{
 
213
    ASSERT(parent);
 
214
    ASSERT(!m_parent);
 
215
    m_parent = parent;
 
216
    m_startingSelection = parent->m_endingSelection;
 
217
    m_endingSelection = parent->m_endingSelection;
 
218
    m_startingRootEditableElement = parent->m_endingRootEditableElement;
 
219
    m_endingRootEditableElement = parent->m_endingRootEditableElement;
 
220
}
 
221
 
 
222
void applyCommand(PassRefPtr<EditCommand> command)
 
223
{
 
224
    DeleteButtonController *deleteButtonController = command->document()->frame()->editor()->deleteButtonController();
 
225
    deleteButtonController->disable();
 
226
    command->apply();
 
227
    deleteButtonController->enable();
 
228
}
 
229
 
 
230
} // namespace WebCore