18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/*
28e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Copyright (C) 2005, 2008 Apple Inc. All rights reserved.
38e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
48e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without
58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions
68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met:
78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    documentation and/or other materials provided with the distribution.
128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */
258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "config.h"
278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "SplitTextNodeCommand.h"
288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "Document.h"
30cad810f21b803229eb11403f9209855525a25d57Steve Block#include "DocumentMarkerController.h"
318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "Text.h"
328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include <wtf/Assertions.h>
338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectnamespace WebCore {
358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectSplitTextNodeCommand::SplitTextNodeCommand(PassRefPtr<Text> text, int offset)
37635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    : SimpleEditCommand(text->document())
38635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    , m_text2(text)
39635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    , m_offset(offset)
408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
41635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    // NOTE: Various callers rely on the fact that the original node becomes
42635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    // the second node (i.e. the new node is inserted before the existing one).
43635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    // That is not a fundamental dependency (i.e. it could be re-coded), but
44635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    // rather is based on how this code happens to work.
458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_text2);
468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_text2->length() > 0);
47635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    ASSERT(m_offset > 0);
48635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    ASSERT(m_offset < m_text2->length());
498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid SplitTextNodeCommand::doApply()
528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
53a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    ContainerNode* parent = m_text2->parentNode();
542bde8e466a4451c7319e3a072d118917957d6554Steve Block    if (!parent || !parent->rendererIsEditable())
55643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        return;
56643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ExceptionCode ec = 0;
58635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    String prefixText = m_text2->substringData(0, m_offset, ec);
59635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    if (prefixText.isEmpty())
60635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        return;
618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
620617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    m_text1 = Text::create(document(), prefixText);
630617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    ASSERT(m_text1);
64e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    document()->markers()->copyMarkers(m_text2.get(), 0, m_offset, m_text1.get(), 0);
658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
660617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    insertText1AndTrimText2();
678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid SplitTextNodeCommand::doUnapply()
708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
712bde8e466a4451c7319e3a072d118917957d6554Steve Block    if (!m_text1 || !m_text1->rendererIsEditable())
72635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        return;
738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
74635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    ASSERT(m_text1->document() == document());
758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
760617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    String prefixText = m_text1->data();
77635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
78635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    ExceptionCode ec = 0;
79635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    m_text2->insertData(0, prefixText, ec);
800617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    ASSERT(!ec);
810617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen
82e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    document()->markers()->copyMarkers(m_text1.get(), 0, prefixText.length(), m_text2.get(), 0);
830617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    m_text1->remove(ec);
840617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen}
850617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen
860617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsenvoid SplitTextNodeCommand::doReapply()
870617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen{
880617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    if (!m_text1 || !m_text2)
89635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        return;
908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
91a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    ContainerNode* parent = m_text2->parentNode();
922bde8e466a4451c7319e3a072d118917957d6554Steve Block    if (!parent || !parent->rendererIsEditable())
930617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen        return;
940617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen
950617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    insertText1AndTrimText2();
968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
980617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsenvoid SplitTextNodeCommand::insertText1AndTrimText2()
990617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen{
1000617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    ExceptionCode ec = 0;
1010617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    m_text2->parentNode()->insertBefore(m_text1.get(), m_text2.get(), ec);
1020617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    if (ec)
1030617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen        return;
1040617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen    m_text2->deleteData(0, m_offset, ec);
1050617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen}
1060617145a89917ae7735fe1c9538688ab9a577df5Kristian Monsen
1078e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project} // namespace WebCore
108