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 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 "SplitTextNodeCommand.h" 28 29#include "Document.h" 30#include "DocumentMarkerController.h" 31#include "Text.h" 32#include <wtf/Assertions.h> 33 34namespace WebCore { 35 36SplitTextNodeCommand::SplitTextNodeCommand(PassRefPtr<Text> text, int offset) 37 : SimpleEditCommand(text->document()) 38 , m_text2(text) 39 , m_offset(offset) 40{ 41 // NOTE: Various callers rely on the fact that the original node becomes 42 // the second node (i.e. the new node is inserted before the existing one). 43 // That is not a fundamental dependency (i.e. it could be re-coded), but 44 // rather is based on how this code happens to work. 45 ASSERT(m_text2); 46 ASSERT(m_text2->length() > 0); 47 ASSERT(m_offset > 0); 48 ASSERT(m_offset < m_text2->length()); 49} 50 51void SplitTextNodeCommand::doApply() 52{ 53 ContainerNode* parent = m_text2->parentNode(); 54 if (!parent || !parent->rendererIsEditable()) 55 return; 56 57 String prefixText = m_text2->substringData(0, m_offset, IGNORE_EXCEPTION); 58 if (prefixText.isEmpty()) 59 return; 60 61 m_text1 = Text::create(document(), prefixText); 62 ASSERT(m_text1); 63 document()->markers()->copyMarkers(m_text2.get(), 0, m_offset, m_text1.get(), 0); 64 65 insertText1AndTrimText2(); 66} 67 68void SplitTextNodeCommand::doUnapply() 69{ 70 if (!m_text1 || !m_text1->rendererIsEditable()) 71 return; 72 73 ASSERT(m_text1->document() == document()); 74 75 String prefixText = m_text1->data(); 76 77 m_text2->insertData(0, prefixText, ASSERT_NO_EXCEPTION); 78 79 document()->markers()->copyMarkers(m_text1.get(), 0, prefixText.length(), m_text2.get(), 0); 80 m_text1->remove(ASSERT_NO_EXCEPTION); 81} 82 83void SplitTextNodeCommand::doReapply() 84{ 85 if (!m_text1 || !m_text2) 86 return; 87 88 ContainerNode* parent = m_text2->parentNode(); 89 if (!parent || !parent->rendererIsEditable()) 90 return; 91 92 insertText1AndTrimText2(); 93} 94 95void SplitTextNodeCommand::insertText1AndTrimText2() 96{ 97 ExceptionCode ec = 0; 98 m_text2->parentNode()->insertBefore(m_text1.get(), m_text2.get(), ec); 99 if (ec) 100 return; 101 m_text2->deleteData(0, m_offset, ec); 102} 103 104#ifndef NDEBUG 105void SplitTextNodeCommand::getNodesInCommand(HashSet<Node*>& nodes) 106{ 107 addNodeAndDescendants(m_text1.get(), nodes); 108 addNodeAndDescendants(m_text2.get(), nodes); 109} 110#endif 111 112} // namespace WebCore 113