• 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 "core/dom/CharacterData.h"
24 
25 #include "bindings/v8/ExceptionState.h"
26 #include "core/dom/Document.h"
27 #include "core/dom/ExceptionCode.h"
28 #include "core/dom/MutationObserverInterestGroup.h"
29 #include "core/dom/MutationRecord.h"
30 #include "core/dom/ProcessingInstruction.h"
31 #include "core/dom/Text.h"
32 #include "core/editing/FrameSelection.h"
33 #include "core/events/MutationEvent.h"
34 #include "core/inspector/InspectorInstrumentation.h"
35 #include "wtf/CheckedArithmetic.h"
36 
37 namespace WebCore {
38 
atomize()39 void CharacterData::atomize()
40 {
41     m_data = AtomicString(m_data);
42 }
43 
setData(const String & data)44 void CharacterData::setData(const String& data)
45 {
46     const String& nonNullData = !data.isNull() ? data : emptyString();
47     if (m_data == nonNullData)
48         return;
49 
50     RefPtrWillBeRawPtr<CharacterData> protect(this);
51 
52     unsigned oldLength = length();
53 
54     setDataAndUpdate(nonNullData, 0, oldLength, nonNullData.length());
55     document().didRemoveText(this, 0, oldLength);
56 }
57 
substringData(unsigned offset,unsigned count,ExceptionState & exceptionState)58 String CharacterData::substringData(unsigned offset, unsigned count, ExceptionState& exceptionState)
59 {
60     if (offset > length()) {
61         exceptionState.throwDOMException(IndexSizeError, "The offset " + String::number(offset) + " is greater than the node's length (" + String::number(length()) + ").");
62         return String();
63     }
64 
65     return m_data.substring(offset, count);
66 }
67 
parserAppendData(const String & string)68 void CharacterData::parserAppendData(const String& string)
69 {
70     unsigned oldLength = m_data.length();
71     m_data = m_data + string;
72 
73     ASSERT(!renderer() || isTextNode());
74     if (isTextNode())
75         toText(this)->updateTextRenderer(oldLength, 0);
76 
77     document().incDOMTreeVersion();
78 
79     if (parentNode())
80         parentNode()->childrenChanged();
81 }
82 
appendData(const String & data)83 void CharacterData::appendData(const String& data)
84 {
85     String newStr = m_data + data;
86 
87     setDataAndUpdate(newStr, m_data.length(), 0, data.length());
88 
89     // FIXME: Should we call textInserted here?
90 }
91 
insertData(unsigned offset,const String & data,ExceptionState & exceptionState,RecalcStyleBehavior recalcStyleBehavior)92 void CharacterData::insertData(unsigned offset, const String& data, ExceptionState& exceptionState, RecalcStyleBehavior recalcStyleBehavior)
93 {
94     if (offset > length()) {
95         exceptionState.throwDOMException(IndexSizeError, "The offset " + String::number(offset) + " is greater than the node's length (" + String::number(length()) + ").");
96         return;
97     }
98 
99     String newStr = m_data;
100     newStr.insert(data, offset);
101 
102     setDataAndUpdate(newStr, offset, 0, data.length(), recalcStyleBehavior);
103 
104     document().didInsertText(this, offset, data.length());
105 }
106 
validateOffsetCount(unsigned offset,unsigned count,unsigned length,unsigned & realCount,ExceptionState & exceptionState)107 static bool validateOffsetCount(unsigned offset, unsigned count, unsigned length, unsigned& realCount, ExceptionState& exceptionState)
108 {
109     if (offset > length) {
110         exceptionState.throwDOMException(IndexSizeError, "The offset " + String::number(offset) + " is greater than the node's length (" + String::number(length) + ").");
111         return false;
112     }
113 
114     Checked<unsigned, RecordOverflow> offsetCount = offset;
115     offsetCount += count;
116 
117     if (offsetCount.hasOverflowed() || offset + count > length)
118         realCount = length - offset;
119     else
120         realCount = count;
121 
122     return true;
123 }
124 
deleteData(unsigned offset,unsigned count,ExceptionState & exceptionState,RecalcStyleBehavior recalcStyleBehavior)125 void CharacterData::deleteData(unsigned offset, unsigned count, ExceptionState& exceptionState, RecalcStyleBehavior recalcStyleBehavior)
126 {
127     unsigned realCount = 0;
128     if (!validateOffsetCount(offset, count, length(), realCount, exceptionState))
129         return;
130 
131     String newStr = m_data;
132     newStr.remove(offset, realCount);
133 
134     setDataAndUpdate(newStr, offset, realCount, 0, recalcStyleBehavior);
135 
136     document().didRemoveText(this, offset, realCount);
137 }
138 
replaceData(unsigned offset,unsigned count,const String & data,ExceptionState & exceptionState)139 void CharacterData::replaceData(unsigned offset, unsigned count, const String& data, ExceptionState& exceptionState)
140 {
141     unsigned realCount = 0;
142     if (!validateOffsetCount(offset, count, length(), realCount, exceptionState))
143         return;
144 
145     String newStr = m_data;
146     newStr.remove(offset, realCount);
147     newStr.insert(data, offset);
148 
149     setDataAndUpdate(newStr, offset, realCount, data.length());
150 
151     // update the markers for spell checking and grammar checking
152     document().didRemoveText(this, offset, realCount);
153     document().didInsertText(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.containsOnlyWhitespace();
164 }
165 
setNodeValue(const String & nodeValue)166 void CharacterData::setNodeValue(const String& nodeValue)
167 {
168     setData(nodeValue);
169 }
170 
setDataAndUpdate(const String & newData,unsigned offsetOfReplacedData,unsigned oldLength,unsigned newLength,RecalcStyleBehavior recalcStyleBehavior)171 void CharacterData::setDataAndUpdate(const String& newData, unsigned offsetOfReplacedData, unsigned oldLength, unsigned newLength, RecalcStyleBehavior recalcStyleBehavior)
172 {
173     String oldData = m_data;
174     m_data = newData;
175 
176     ASSERT(!renderer() || isTextNode());
177     if (isTextNode())
178         toText(this)->updateTextRenderer(offsetOfReplacedData, oldLength, recalcStyleBehavior);
179 
180     if (nodeType() == PROCESSING_INSTRUCTION_NODE)
181         toProcessingInstruction(this)->didAttributeChanged();
182 
183     if (document().frame())
184         document().frame()->selection().didUpdateCharacterData(this, offsetOfReplacedData, oldLength, newLength);
185 
186     document().incDOMTreeVersion();
187     didModifyData(oldData);
188 }
189 
didModifyData(const String & oldData)190 void CharacterData::didModifyData(const String& oldData)
191 {
192     if (OwnPtrWillBeRawPtr<MutationObserverInterestGroup> mutationRecipients = MutationObserverInterestGroup::createForCharacterDataMutation(*this))
193         mutationRecipients->enqueueMutationRecord(MutationRecord::createCharacterData(this, oldData));
194 
195     if (parentNode())
196         parentNode()->childrenChanged();
197 
198     if (!isInShadowTree()) {
199         if (document().hasListenerType(Document::DOMCHARACTERDATAMODIFIED_LISTENER))
200             dispatchScopedEvent(MutationEvent::create(EventTypeNames::DOMCharacterDataModified, true, nullptr, oldData, m_data));
201         dispatchSubtreeModifiedEvent();
202     }
203     InspectorInstrumentation::characterDataModified(this);
204 }
205 
maxCharacterOffset() const206 int CharacterData::maxCharacterOffset() const
207 {
208     return static_cast<int>(length());
209 }
210 
offsetInCharacters() const211 bool CharacterData::offsetInCharacters() const
212 {
213     return true;
214 }
215 
216 } // namespace WebCore
217