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