2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "CharacterData.h"
26 #include "EventNames.h"
27 #include "ExceptionCode.h"
28 #include "InspectorInstrumentation.h"
29 #include "MutationEvent.h"
30 #include "MutationRecord.h"
31 #include "NodeRenderingContext.h"
32 #include "RenderText.h"
33 #include "TextBreakIterator.h"
34 #include "WebKitMutationObserver.h"
40 void CharacterData::setData(const String& data, ExceptionCode&)
42 const String& nonNullData = !data.isNull() ? data : emptyString();
43 if (m_data == nonNullData)
46 unsigned oldLength = length();
48 setDataAndUpdate(nonNullData, 0, oldLength, nonNullData.length());
49 document()->textRemoved(this, 0, oldLength);
52 String CharacterData::substringData(unsigned offset, unsigned count, ExceptionCode& ec)
54 checkCharDataOperation(offset, ec);
58 return m_data.substring(offset, count);
61 unsigned CharacterData::parserAppendData(const UChar* data, unsigned dataLength, unsigned lengthLimit)
63 unsigned oldLength = m_data.length();
65 unsigned end = min(dataLength, lengthLimit - oldLength);
67 // Check that we are not on an unbreakable boundary.
68 // Some text break iterator implementations work best if the passed buffer is as small as possible,
69 // see <https://bugs.webkit.org/show_bug.cgi?id=29092>.
70 // We need at least two characters look-ahead to account for UTF-16 surrogates.
71 if (end < dataLength) {
72 TextBreakIterator* it = characterBreakIterator(data, (end + 2 > dataLength) ? dataLength : end + 2);
73 if (!isTextBreak(it, end))
74 end = textBreakPreceding(it, end);
80 m_data.append(data, end);
82 updateRenderer(oldLength, 0);
83 // We don't call dispatchModifiedEvent here because we don't want the
84 // parser to dispatch DOM mutation events.
86 parentNode()->childrenChanged();
91 void CharacterData::appendData(const String& data, ExceptionCode&)
93 String newStr = m_data;
96 setDataAndUpdate(newStr, m_data.length(), 0, data.length());
98 // FIXME: Should we call textInserted here?
101 void CharacterData::insertData(unsigned offset, const String& data, ExceptionCode& ec)
103 checkCharDataOperation(offset, ec);
107 String newStr = m_data;
108 newStr.insert(data, offset);
110 setDataAndUpdate(newStr, offset, 0, data.length());
112 document()->textInserted(this, offset, data.length());
115 void CharacterData::deleteData(unsigned offset, unsigned count, ExceptionCode& ec)
117 checkCharDataOperation(offset, ec);
122 if (offset + count > length())
123 realCount = length() - offset;
127 String newStr = m_data;
128 newStr.remove(offset, realCount);
130 setDataAndUpdate(newStr, offset, count, 0);
132 document()->textRemoved(this, offset, realCount);
135 void CharacterData::replaceData(unsigned offset, unsigned count, const String& data, ExceptionCode& ec)
137 checkCharDataOperation(offset, ec);
142 if (offset + count > length())
143 realCount = length() - offset;
147 String newStr = m_data;
148 newStr.remove(offset, realCount);
149 newStr.insert(data, offset);
151 setDataAndUpdate(newStr, offset, count, data.length());
153 // update the markers for spell checking and grammar checking
154 document()->textRemoved(this, offset, realCount);
155 document()->textInserted(this, offset, data.length());
158 String CharacterData::nodeValue() const
163 bool CharacterData::containsOnlyWhitespace() const
165 return m_data.containsOnlyWhitespace();
168 void CharacterData::setNodeValue(const String& nodeValue, ExceptionCode& ec)
170 setData(nodeValue, ec);
173 void CharacterData::setDataAndUpdate(const String& newData, unsigned offsetOfReplacedData, unsigned oldLength, unsigned newLength)
175 if (document()->frame())
176 document()->frame()->selection()->textWillBeReplaced(this, offsetOfReplacedData, oldLength, newLength);
177 String oldData = m_data;
179 updateRenderer(offsetOfReplacedData, oldLength);
180 dispatchModifiedEvent(oldData);
183 void CharacterData::updateRenderer(unsigned offsetOfReplacedData, unsigned lengthOfReplacedData)
185 if ((!renderer() || !rendererIsNeeded(NodeRenderingContext(this, renderer()->style()))) && attached())
188 toRenderText(renderer())->setTextWithOffset(m_data.impl(), offsetOfReplacedData, lengthOfReplacedData);
191 void CharacterData::dispatchModifiedEvent(const String& oldData)
193 #if ENABLE(MUTATION_OBSERVERS)
194 OwnPtr<MutationObserverInterestGroup> mutationRecipients = MutationObserverInterestGroup::createForCharacterDataMutation(this);
195 mutationRecipients->enqueueMutationRecord(MutationRecord::createCharacterData(this, oldData));
198 parentNode()->childrenChanged();
199 if (document()->hasListenerType(Document::DOMCHARACTERDATAMODIFIED_LISTENER))
200 dispatchEvent(MutationEvent::create(eventNames().DOMCharacterDataModifiedEvent, true, 0, oldData, m_data));
201 dispatchSubtreeModifiedEvent();
202 #if ENABLE(INSPECTOR)
203 InspectorInstrumentation::characterDataModified(document(), this);
207 void CharacterData::checkCharDataOperation(unsigned offset, ExceptionCode& ec)
211 // INDEX_SIZE_ERR: Raised if the specified offset is negative or greater than the number of 16-bit
213 if (offset > length()) {
219 int CharacterData::maxCharacterOffset() const
221 return static_cast<int>(length());
224 bool CharacterData::rendererIsNeeded(const NodeRenderingContext& context)
226 if (!m_data || !length())
228 return Node::rendererIsNeeded(context);
231 bool CharacterData::offsetInCharacters() const
236 } // namespace WebCore