1 /*
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.
5 *
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.
10 *
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.
15 *
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.
20 */
21
22 #include "config.h"
23 #include "CharacterData.h"
24
25 #include "EventNames.h"
26 #include "ExceptionCode.h"
27 #include "InspectorInstrumentation.h"
28 #include "MutationEvent.h"
29 #include "RenderText.h"
30 #include "TextBreakIterator.h"
31
32 using namespace std;
33
34 namespace WebCore {
35
setData(const String & data,ExceptionCode &)36 void CharacterData::setData(const String& data, ExceptionCode&)
37 {
38 StringImpl* dataImpl = data.impl() ? data.impl() : StringImpl::empty();
39 if (equal(m_data.get(), dataImpl))
40 return;
41
42 unsigned oldLength = length();
43
44 setDataAndUpdate(dataImpl, 0, oldLength, dataImpl->length());
45 document()->textRemoved(this, 0, oldLength);
46 }
47
substringData(unsigned offset,unsigned count,ExceptionCode & ec)48 String CharacterData::substringData(unsigned offset, unsigned count, ExceptionCode& ec)
49 {
50 checkCharDataOperation(offset, ec);
51 if (ec)
52 return String();
53
54 return m_data->substring(offset, count);
55 }
56
parserAppendData(const UChar * data,unsigned dataLength,unsigned lengthLimit)57 unsigned CharacterData::parserAppendData(const UChar* data, unsigned dataLength, unsigned lengthLimit)
58 {
59 unsigned oldLength = m_data->length();
60
61 unsigned end = min(dataLength, lengthLimit - oldLength);
62
63 // Check that we are not on an unbreakable boundary.
64 // Some text break iterator implementations work best if the passed buffer is as small as possible,
65 // see <https://bugs.webkit.org/show_bug.cgi?id=29092>.
66 // We need at least two characters look-ahead to account for UTF-16 surrogates.
67 if (end < dataLength) {
68 TextBreakIterator* it = characterBreakIterator(data, (end + 2 > dataLength) ? dataLength : end + 2);
69 if (!isTextBreak(it, end))
70 end = textBreakPreceding(it, end);
71 }
72
73 if (!end)
74 return 0;
75
76 String newStr = m_data;
77 newStr.append(data, end);
78 m_data = newStr.impl();
79
80 updateRenderer(oldLength, 0);
81 // We don't call dispatchModifiedEvent here because we don't want the
82 // parser to dispatch DOM mutation events.
83 if (parentNode())
84 parentNode()->childrenChanged();
85
86 return end;
87 }
88
appendData(const String & data,ExceptionCode &)89 void CharacterData::appendData(const String& data, ExceptionCode&)
90 {
91 String newStr = m_data;
92 newStr.append(data);
93
94 setDataAndUpdate(newStr.impl(), m_data->length(), 0, data.length());
95
96 // FIXME: Should we call textInserted here?
97 }
98
insertData(unsigned offset,const String & data,ExceptionCode & ec)99 void CharacterData::insertData(unsigned offset, const String& data, ExceptionCode& ec)
100 {
101 checkCharDataOperation(offset, ec);
102 if (ec)
103 return;
104
105 String newStr = m_data;
106 newStr.insert(data, offset);
107
108 setDataAndUpdate(newStr.impl(), offset, 0, data.length());
109
110 document()->textInserted(this, offset, data.length());
111 }
112
deleteData(unsigned offset,unsigned count,ExceptionCode & ec)113 void CharacterData::deleteData(unsigned offset, unsigned count, ExceptionCode& ec)
114 {
115 checkCharDataOperation(offset, ec);
116 if (ec)
117 return;
118
119 unsigned realCount;
120 if (offset + count > length())
121 realCount = length() - offset;
122 else
123 realCount = count;
124
125 String newStr = m_data;
126 newStr.remove(offset, realCount);
127
128 setDataAndUpdate(newStr.impl(), offset, count, 0);
129
130 document()->textRemoved(this, offset, realCount);
131 }
132
replaceData(unsigned offset,unsigned count,const String & data,ExceptionCode & ec)133 void CharacterData::replaceData(unsigned offset, unsigned count, const String& data, ExceptionCode& ec)
134 {
135 checkCharDataOperation(offset, ec);
136 if (ec)
137 return;
138
139 unsigned realCount;
140 if (offset + count > length())
141 realCount = length() - offset;
142 else
143 realCount = count;
144
145 String newStr = m_data;
146 newStr.remove(offset, realCount);
147 newStr.insert(data, offset);
148
149 setDataAndUpdate(newStr.impl(), offset, count, data.length());
150
151 // update the markers for spell checking and grammar checking
152 document()->textRemoved(this, offset, realCount);
153 document()->textInserted(this, offset, data.length());
154 }
155
nodeValue() const156 String CharacterData::nodeValue() const
157 {
158 return m_data;
159 }
160
containsOnlyWhitespace() const161 bool CharacterData::containsOnlyWhitespace() const
162 {
163 return !m_data || m_data->containsOnlyWhitespace();
164 }
165
setNodeValue(const String & nodeValue,ExceptionCode & ec)166 void CharacterData::setNodeValue(const String& nodeValue, ExceptionCode& ec)
167 {
168 setData(nodeValue, ec);
169 }
170
setDataAndUpdate(PassRefPtr<StringImpl> newData,unsigned offsetOfReplacedData,unsigned oldLength,unsigned newLength)171 void CharacterData::setDataAndUpdate(PassRefPtr<StringImpl> newData, unsigned offsetOfReplacedData, unsigned oldLength, unsigned newLength)
172 {
173 if (document()->frame())
174 document()->frame()->selection()->textWillBeReplaced(this, offsetOfReplacedData, oldLength, newLength);
175 RefPtr<StringImpl> oldData = m_data;
176 m_data = newData;
177 updateRenderer(offsetOfReplacedData, oldLength);
178 dispatchModifiedEvent(oldData.get());
179 }
180
updateRenderer(unsigned offsetOfReplacedData,unsigned lengthOfReplacedData)181 void CharacterData::updateRenderer(unsigned offsetOfReplacedData, unsigned lengthOfReplacedData)
182 {
183 if ((!renderer() || !rendererIsNeeded(renderer()->style())) && attached()) {
184 detach();
185 attach();
186 } else if (renderer())
187 toRenderText(renderer())->setTextWithOffset(m_data, offsetOfReplacedData, lengthOfReplacedData);
188 }
189
dispatchModifiedEvent(StringImpl * oldData)190 void CharacterData::dispatchModifiedEvent(StringImpl* oldData)
191 {
192 if (parentNode())
193 parentNode()->childrenChanged();
194 if (document()->hasListenerType(Document::DOMCHARACTERDATAMODIFIED_LISTENER))
195 dispatchEvent(MutationEvent::create(eventNames().DOMCharacterDataModifiedEvent, true, 0, oldData, m_data));
196 dispatchSubtreeModifiedEvent();
197 #if ENABLE(INSPECTOR)
198 InspectorInstrumentation::characterDataModified(document(), this);
199 #endif
200 }
201
checkCharDataOperation(unsigned offset,ExceptionCode & ec)202 void CharacterData::checkCharDataOperation(unsigned offset, ExceptionCode& ec)
203 {
204 ec = 0;
205
206 // INDEX_SIZE_ERR: Raised if the specified offset is negative or greater than the number of 16-bit
207 // units in data.
208 if (offset > length()) {
209 ec = INDEX_SIZE_ERR;
210 return;
211 }
212 }
213
maxCharacterOffset() const214 int CharacterData::maxCharacterOffset() const
215 {
216 return static_cast<int>(length());
217 }
218
rendererIsNeeded(RenderStyle * style)219 bool CharacterData::rendererIsNeeded(RenderStyle *style)
220 {
221 if (!m_data || !length())
222 return false;
223 return Node::rendererIsNeeded(style);
224 }
225
offsetInCharacters() const226 bool CharacterData::offsetInCharacters() const
227 {
228 return true;
229 }
230
231 } // namespace WebCore
232