/* * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" #include "core/editing/CompositeEditCommand.h" #include "HTMLNames.h" #include "bindings/v8/ExceptionStatePlaceholder.h" #include "core/dom/Document.h" #include "core/dom/DocumentFragment.h" #include "core/dom/DocumentMarkerController.h" #include "core/dom/NodeTraversal.h" #include "core/dom/Range.h" #include "core/events/ScopedEventQueue.h" #include "core/dom/Text.h" #include "core/editing/AppendNodeCommand.h" #include "core/editing/ApplyStyleCommand.h" #include "core/editing/DeleteFromTextNodeCommand.h" #include "core/editing/DeleteSelectionCommand.h" #include "core/editing/Editor.h" #include "core/editing/InsertIntoTextNodeCommand.h" #include "core/editing/InsertLineBreakCommand.h" #include "core/editing/InsertNodeBeforeCommand.h" #include "core/editing/InsertParagraphSeparatorCommand.h" #include "core/editing/MergeIdenticalElementsCommand.h" #include "core/editing/PlainTextRange.h" #include "core/editing/RemoveCSSPropertyCommand.h" #include "core/editing/RemoveNodeCommand.h" #include "core/editing/RemoveNodePreservingChildrenCommand.h" #include "core/editing/ReplaceNodeWithSpanCommand.h" #include "core/editing/ReplaceSelectionCommand.h" #include "core/editing/SetNodeAttributeCommand.h" #include "core/editing/SpellChecker.h" #include "core/editing/SplitElementCommand.h" #include "core/editing/SplitTextNodeCommand.h" #include "core/editing/SplitTextNodeContainingElementCommand.h" #include "core/editing/TextIterator.h" #include "core/editing/VisibleUnits.h" #include "core/editing/WrapContentsInDummySpanCommand.h" #include "core/editing/htmlediting.h" #include "core/editing/markup.h" #include "core/html/HTMLElement.h" #include "core/frame/Frame.h" #include "core/rendering/InlineTextBox.h" #include "core/rendering/RenderBlock.h" #include "core/rendering/RenderText.h" using namespace std; namespace WebCore { using namespace HTMLNames; namespace { class ReentrancyGuard { public: static bool isRecursiveCall() { return s_nestingCounter; } class Scope { public: Scope() { ++s_nestingCounter; } ~Scope() { --s_nestingCounter; } }; friend class Scope; private: static int s_nestingCounter; }; int ReentrancyGuard::s_nestingCounter; } PassRefPtr EditCommandComposition::create(Document* document, const VisibleSelection& startingSelection, const VisibleSelection& endingSelection, EditAction editAction) { return adoptRef(new EditCommandComposition(document, startingSelection, endingSelection, editAction)); } EditCommandComposition::EditCommandComposition(Document* document, const VisibleSelection& startingSelection, const VisibleSelection& endingSelection, EditAction editAction) : m_document(document) , m_startingSelection(startingSelection) , m_endingSelection(endingSelection) , m_startingRootEditableElement(startingSelection.rootEditableElement()) , m_endingRootEditableElement(endingSelection.rootEditableElement()) , m_editAction(editAction) { } bool EditCommandComposition::belongsTo(const Frame& frame) const { ASSERT(m_document); return m_document->frame() == &frame; } void EditCommandComposition::unapply() { ASSERT(m_document); RefPtr frame = m_document->frame(); ASSERT(frame); // Changes to the document may have been made since the last editing operation that require a layout, as in . // Low level operations, like RemoveNodeCommand, don't require a layout because the high level operations that use them perform one // if one is necessary (like for the creation of VisiblePositions). m_document->updateLayoutIgnorePendingStylesheets(); { size_t size = m_commands.size(); for (size_t i = size; i; --i) m_commands[i - 1]->doUnapply(); } frame->editor().unappliedEditing(this); } void EditCommandComposition::reapply() { ASSERT(m_document); RefPtr frame = m_document->frame(); ASSERT(frame); // Changes to the document may have been made since the last editing operation that require a layout, as in . // Low level operations, like RemoveNodeCommand, don't require a layout because the high level operations that use them perform one // if one is necessary (like for the creation of VisiblePositions). m_document->updateLayoutIgnorePendingStylesheets(); { size_t size = m_commands.size(); for (size_t i = 0; i != size; ++i) m_commands[i]->doReapply(); } frame->editor().reappliedEditing(this); } void EditCommandComposition::append(SimpleEditCommand* command) { m_commands.append(command); } void EditCommandComposition::setStartingSelection(const VisibleSelection& selection) { m_startingSelection = selection; m_startingRootEditableElement = selection.rootEditableElement(); } void EditCommandComposition::setEndingSelection(const VisibleSelection& selection) { m_endingSelection = selection; m_endingRootEditableElement = selection.rootEditableElement(); } CompositeEditCommand::CompositeEditCommand(Document& document) : EditCommand(document) { } CompositeEditCommand::~CompositeEditCommand() { ASSERT(isTopLevelCommand() || !m_composition); } void CompositeEditCommand::apply() { // We don't allow recusrive |apply()| to protect against attack code. // Recursive call of |apply()| could be happened by moving iframe // with script triggered by insertion, e.g.