• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 "CString.h"
26 #include "EventNames.h"
27 #include "ExceptionCode.h"
28 #include "MutationEvent.h"
29 #include "RenderText.h"
30 
31 namespace WebCore {
32 
CharacterData(Document * doc,bool isText)33 CharacterData::CharacterData(Document *doc, bool isText)
34     : Node(doc, false, false, isText)
35     , m_data(StringImpl::empty())
36 {
37 }
38 
CharacterData(Document * document,const String & text,bool isText)39 CharacterData::CharacterData(Document* document, const String& text, bool isText)
40     : Node(document, false, false, isText)
41 {
42     m_data = text.impl() ? text.impl() : StringImpl::empty();
43 }
44 
~CharacterData()45 CharacterData::~CharacterData()
46 {
47 }
48 
setData(const String & data,ExceptionCode &)49 void CharacterData::setData(const String& data, ExceptionCode&)
50 {
51     StringImpl* dataImpl = data.impl() ? data.impl() : StringImpl::empty();
52     if (equal(m_data.get(), dataImpl))
53         return;
54 
55     int oldLength = length();
56     RefPtr<StringImpl> oldStr = m_data;
57     m_data = dataImpl;
58 
59     if ((!renderer() || !rendererIsNeeded(renderer()->style())) && attached()) {
60         detach();
61         attach();
62     } else if (renderer())
63         toRenderText(renderer())->setText(m_data);
64 
65     dispatchModifiedEvent(oldStr.get());
66 
67     document()->textRemoved(this, 0, oldLength);
68 }
69 
substringData(unsigned offset,unsigned count,ExceptionCode & ec)70 String CharacterData::substringData(unsigned offset, unsigned count, ExceptionCode& ec)
71 {
72     checkCharDataOperation(offset, ec);
73     if (ec)
74         return String();
75 
76     return m_data->substring(offset, count);
77 }
78 
appendData(const String & arg,ExceptionCode &)79 void CharacterData::appendData(const String& arg, ExceptionCode&)
80 {
81     String newStr = m_data;
82     newStr.append(arg);
83 
84     RefPtr<StringImpl> oldStr = m_data;
85     m_data = newStr.impl();
86 
87     if ((!renderer() || !rendererIsNeeded(renderer()->style())) && attached()) {
88         detach();
89         attach();
90     } else if (renderer())
91         toRenderText(renderer())->setTextWithOffset(m_data, oldStr->length(), 0);
92 
93     dispatchModifiedEvent(oldStr.get());
94 }
95 
insertData(unsigned offset,const String & arg,ExceptionCode & ec)96 void CharacterData::insertData(unsigned offset, const String& arg, ExceptionCode& ec)
97 {
98     checkCharDataOperation(offset, ec);
99     if (ec)
100         return;
101 
102     String newStr = m_data;
103     newStr.insert(arg, offset);
104 
105     RefPtr<StringImpl> oldStr = m_data;
106     m_data = newStr.impl();
107 
108     if ((!renderer() || !rendererIsNeeded(renderer()->style())) && attached()) {
109         detach();
110         attach();
111     } else if (renderer())
112         toRenderText(renderer())->setTextWithOffset(m_data, offset, 0);
113 
114     dispatchModifiedEvent(oldStr.get());
115 
116     document()->textInserted(this, offset, arg.length());
117 }
118 
deleteData(unsigned offset,unsigned count,ExceptionCode & ec)119 void CharacterData::deleteData(unsigned offset, unsigned count, ExceptionCode& ec)
120 {
121     checkCharDataOperation(offset, ec);
122     if (ec)
123         return;
124 
125     unsigned realCount;
126     if (offset + count > length())
127         realCount = length() - offset;
128     else
129         realCount = count;
130 
131     String newStr = m_data;
132     newStr.remove(offset, realCount);
133 
134     RefPtr<StringImpl> oldStr = m_data;
135     m_data = newStr.impl();
136 
137     if ((!renderer() || !rendererIsNeeded(renderer()->style())) && attached()) {
138         detach();
139         attach();
140     } else if (renderer())
141         toRenderText(renderer())->setTextWithOffset(m_data, offset, count);
142 
143     dispatchModifiedEvent(oldStr.get());
144 
145     document()->textRemoved(this, offset, realCount);
146 }
147 
replaceData(unsigned offset,unsigned count,const String & arg,ExceptionCode & ec)148 void CharacterData::replaceData(unsigned offset, unsigned count, const String& arg, ExceptionCode& ec)
149 {
150     checkCharDataOperation(offset, ec);
151     if (ec)
152         return;
153 
154     unsigned realCount;
155     if (offset + count > length())
156         realCount = length() - offset;
157     else
158         realCount = count;
159 
160     String newStr = m_data;
161     newStr.remove(offset, realCount);
162     newStr.insert(arg, offset);
163 
164     RefPtr<StringImpl> oldStr = m_data;
165     m_data = newStr.impl();
166 
167     if ((!renderer() || !rendererIsNeeded(renderer()->style())) && attached()) {
168         detach();
169         attach();
170     } else if (renderer())
171         toRenderText(renderer())->setTextWithOffset(m_data, offset, count);
172 
173     dispatchModifiedEvent(oldStr.get());
174 
175     // update the markers for spell checking and grammar checking
176     document()->textRemoved(this, offset, realCount);
177     document()->textInserted(this, offset, arg.length());
178 }
179 
nodeValue() const180 String CharacterData::nodeValue() const
181 {
182     return m_data;
183 }
184 
containsOnlyWhitespace() const185 bool CharacterData::containsOnlyWhitespace() const
186 {
187     return !m_data || m_data->containsOnlyWhitespace();
188 }
189 
setNodeValue(const String & nodeValue,ExceptionCode & ec)190 void CharacterData::setNodeValue(const String& nodeValue, ExceptionCode& ec)
191 {
192     setData(nodeValue, ec);
193 }
194 
dispatchModifiedEvent(StringImpl * prevValue)195 void CharacterData::dispatchModifiedEvent(StringImpl* prevValue)
196 {
197     if (parentNode())
198         parentNode()->childrenChanged();
199     if (document()->hasListenerType(Document::DOMCHARACTERDATAMODIFIED_LISTENER)) {
200         ExceptionCode ec;
201         dispatchMutationEvent(eventNames().DOMCharacterDataModifiedEvent, true, 0, prevValue, m_data, ec);
202     }
203     dispatchSubtreeModifiedEvent();
204 }
205 
checkCharDataOperation(unsigned offset,ExceptionCode & ec)206 void CharacterData::checkCharDataOperation(unsigned offset, ExceptionCode& ec)
207 {
208     ec = 0;
209 
210     // INDEX_SIZE_ERR: Raised if the specified offset is negative or greater than the number of 16-bit
211     // units in data.
212     if (offset > length()) {
213         ec = INDEX_SIZE_ERR;
214         return;
215     }
216 }
217 
maxCharacterOffset() const218 int CharacterData::maxCharacterOffset() const
219 {
220     return static_cast<int>(length());
221 }
222 
rendererIsNeeded(RenderStyle * style)223 bool CharacterData::rendererIsNeeded(RenderStyle *style)
224 {
225     if (!m_data || !length())
226         return false;
227     return Node::rendererIsNeeded(style);
228 }
229 
offsetInCharacters() const230 bool CharacterData::offsetInCharacters() const
231 {
232     return true;
233 }
234 
235 } // namespace WebCore
236