Home | History | Annotate | Download | only in editing
      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 
     34 namespace WebCore {
     35 
     36 SplitTextNodeCommand::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 
     51 void SplitTextNodeCommand::doApply()
     52 {
     53     ContainerNode* parent = m_text2->parentNode();
     54     if (!parent || !parent->rendererIsEditable())
     55         return;
     56 
     57     ExceptionCode ec = 0;
     58     String prefixText = m_text2->substringData(0, m_offset, ec);
     59     if (prefixText.isEmpty())
     60         return;
     61 
     62     m_text1 = Text::create(document(), prefixText);
     63     ASSERT(m_text1);
     64     document()->markers()->copyMarkers(m_text2.get(), 0, m_offset, m_text1.get(), 0);
     65 
     66     insertText1AndTrimText2();
     67 }
     68 
     69 void SplitTextNodeCommand::doUnapply()
     70 {
     71     if (!m_text1 || !m_text1->rendererIsEditable())
     72         return;
     73 
     74     ASSERT(m_text1->document() == document());
     75 
     76     String prefixText = m_text1->data();
     77 
     78     ExceptionCode ec = 0;
     79     m_text2->insertData(0, prefixText, ec);
     80     ASSERT(!ec);
     81 
     82     document()->markers()->copyMarkers(m_text1.get(), 0, prefixText.length(), m_text2.get(), 0);
     83     m_text1->remove(ec);
     84 }
     85 
     86 void SplitTextNodeCommand::doReapply()
     87 {
     88     if (!m_text1 || !m_text2)
     89         return;
     90 
     91     ContainerNode* parent = m_text2->parentNode();
     92     if (!parent || !parent->rendererIsEditable())
     93         return;
     94 
     95     insertText1AndTrimText2();
     96 }
     97 
     98 void SplitTextNodeCommand::insertText1AndTrimText2()
     99 {
    100     ExceptionCode ec = 0;
    101     m_text2->parentNode()->insertBefore(m_text1.get(), m_text2.get(), ec);
    102     if (ec)
    103         return;
    104     m_text2->deleteData(0, m_offset, ec);
    105 }
    106 
    107 } // namespace WebCore
    108