Gracefully handle XMLDocumentParser being detached by mutation events.
[chromium-blink-merge.git] / third_party / WebKit / Source / core / editing / SplitTextNodeCommand.cpp
blobb14b958dced50280cfe8784ba582bad2ca254415
1 /*
2 * Copyright (C) 2005, 2008 Apple Inc. All rights reserved.
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.
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.
26 #include "config.h"
27 #include "core/editing/SplitTextNodeCommand.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/DocumentMarkerController.h"
33 #include "core/dom/Text.h"
34 #include "wtf/Assertions.h"
36 namespace blink {
38 SplitTextNodeCommand::SplitTextNodeCommand(PassRefPtrWillBeRawPtr<Text> text, int offset)
39 : SimpleEditCommand(text->document())
40 , m_text2(text)
41 , m_offset(offset)
43 // NOTE: Various callers rely on the fact that the original node becomes
44 // the second node (i.e. the new node is inserted before the existing one).
45 // That is not a fundamental dependency (i.e. it could be re-coded), but
46 // rather is based on how this code happens to work.
47 ASSERT(m_text2);
48 ASSERT(m_text2->length() > 0);
49 ASSERT(m_offset > 0);
50 ASSERT(m_offset < m_text2->length());
53 void SplitTextNodeCommand::doApply()
55 ContainerNode* parent = m_text2->parentNode();
56 if (!parent || !parent->hasEditableStyle())
57 return;
59 String prefixText = m_text2->substringData(0, m_offset, IGNORE_EXCEPTION);
60 if (prefixText.isEmpty())
61 return;
63 m_text1 = Text::create(document(), prefixText);
64 ASSERT(m_text1);
65 document().markers().copyMarkers(m_text2.get(), 0, m_offset, m_text1.get(), 0);
67 insertText1AndTrimText2();
70 void SplitTextNodeCommand::doUnapply()
72 if (!m_text1 || !m_text1->hasEditableStyle())
73 return;
75 ASSERT(m_text1->document() == document());
77 String prefixText = m_text1->data();
79 m_text2->insertData(0, prefixText, ASSERT_NO_EXCEPTION, CharacterData::DeprecatedRecalcStyleImmediatlelyForEditing);
81 document().markers().copyMarkers(m_text1.get(), 0, prefixText.length(), m_text2.get(), 0);
82 m_text1->remove(ASSERT_NO_EXCEPTION);
85 void SplitTextNodeCommand::doReapply()
87 if (!m_text1 || !m_text2)
88 return;
90 ContainerNode* parent = m_text2->parentNode();
91 if (!parent || !parent->hasEditableStyle())
92 return;
94 insertText1AndTrimText2();
97 void SplitTextNodeCommand::insertText1AndTrimText2()
99 TrackExceptionState exceptionState;
100 m_text2->parentNode()->insertBefore(m_text1.get(), m_text2.get(), exceptionState);
101 if (exceptionState.hadException())
102 return;
103 m_text2->deleteData(0, m_offset, exceptionState, CharacterData::DeprecatedRecalcStyleImmediatlelyForEditing);
106 DEFINE_TRACE(SplitTextNodeCommand)
108 visitor->trace(m_text1);
109 visitor->trace(m_text2);
110 SimpleEditCommand::trace(visitor);
113 } // namespace blink