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