Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / dom / CharacterData.cpp
blob90b06a7dcf6f19e5a6210c33290f80c137e5e3f2
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, 2013 Apple Inc. All rights reserved.
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.
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.
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.
22 #include "config.h"
23 #include "core/dom/CharacterData.h"
25 #include "bindings/core/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"
37 namespace blink {
39 void CharacterData::atomize()
41 m_data = AtomicString(m_data);
44 void CharacterData::setData(const String& data)
46 const String& nonNullData = !data.isNull() ? data : emptyString();
47 if (m_data == nonNullData)
48 return;
50 RefPtrWillBeRawPtr<CharacterData> protect(this);
52 unsigned oldLength = length();
54 setDataAndUpdate(nonNullData, 0, oldLength, nonNullData.length(), UpdateFromNonParser);
55 document().didRemoveText(this, 0, oldLength);
58 String CharacterData::substringData(unsigned offset, unsigned count, ExceptionState& exceptionState)
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();
65 return m_data.substring(offset, count);
68 void CharacterData::parserAppendData(const String& data)
70 String newStr = m_data + data;
72 setDataAndUpdate(newStr, m_data.length(), 0, data.length(), UpdateFromParser);
75 void CharacterData::appendData(const String& data)
77 String newStr = m_data + data;
79 setDataAndUpdate(newStr, m_data.length(), 0, data.length(), UpdateFromNonParser);
81 // FIXME: Should we call textInserted here?
84 void CharacterData::insertData(unsigned offset, const String& data, ExceptionState& exceptionState, RecalcStyleBehavior recalcStyleBehavior)
86 if (offset > length()) {
87 exceptionState.throwDOMException(IndexSizeError, "The offset " + String::number(offset) + " is greater than the node's length (" + String::number(length()) + ").");
88 return;
91 String newStr = m_data;
92 newStr.insert(data, offset);
94 setDataAndUpdate(newStr, offset, 0, data.length(), UpdateFromNonParser, recalcStyleBehavior);
96 document().didInsertText(this, offset, data.length());
99 static bool validateOffsetCount(unsigned offset, unsigned count, unsigned length, unsigned& realCount, ExceptionState& exceptionState)
101 if (offset > length) {
102 exceptionState.throwDOMException(IndexSizeError, "The offset " + String::number(offset) + " is greater than the node's length (" + String::number(length) + ").");
103 return false;
106 Checked<unsigned, RecordOverflow> offsetCount = offset;
107 offsetCount += count;
109 if (offsetCount.hasOverflowed() || offset + count > length)
110 realCount = length - offset;
111 else
112 realCount = count;
114 return true;
117 void CharacterData::deleteData(unsigned offset, unsigned count, ExceptionState& exceptionState, RecalcStyleBehavior recalcStyleBehavior)
119 unsigned realCount = 0;
120 if (!validateOffsetCount(offset, count, length(), realCount, exceptionState))
121 return;
123 String newStr = m_data;
124 newStr.remove(offset, realCount);
126 setDataAndUpdate(newStr, offset, realCount, 0, UpdateFromNonParser, recalcStyleBehavior);
128 document().didRemoveText(this, offset, realCount);
131 void CharacterData::replaceData(unsigned offset, unsigned count, const String& data, ExceptionState& exceptionState)
133 unsigned realCount = 0;
134 if (!validateOffsetCount(offset, count, length(), realCount, exceptionState))
135 return;
137 String newStr = m_data;
138 newStr.remove(offset, realCount);
139 newStr.insert(data, offset);
141 setDataAndUpdate(newStr, offset, realCount, data.length(), UpdateFromNonParser);
143 // update the markers for spell checking and grammar checking
144 document().didRemoveText(this, offset, realCount);
145 document().didInsertText(this, offset, data.length());
148 String CharacterData::nodeValue() const
150 return m_data;
153 bool CharacterData::containsOnlyWhitespace() const
155 return m_data.containsOnlyWhitespace();
158 void CharacterData::setNodeValue(const String& nodeValue)
160 setData(nodeValue);
163 void CharacterData::setDataAndUpdate(const String& newData, unsigned offsetOfReplacedData, unsigned oldLength, unsigned newLength, UpdateSource source, RecalcStyleBehavior recalcStyleBehavior)
165 String oldData = m_data;
166 m_data = newData;
168 ASSERT(!layoutObject() || isTextNode());
169 if (isTextNode())
170 toText(this)->updateTextLayoutObject(offsetOfReplacedData, oldLength, recalcStyleBehavior);
172 if (source != UpdateFromParser) {
173 if (nodeType() == PROCESSING_INSTRUCTION_NODE)
174 toProcessingInstruction(this)->didAttributeChanged();
176 if (document().frame())
177 document().frame()->selection().didUpdateCharacterData(this, offsetOfReplacedData, oldLength, newLength);
180 document().incDOMTreeVersion();
181 didModifyData(oldData, source);
184 void CharacterData::didModifyData(const String& oldData, UpdateSource source)
186 if (OwnPtrWillBeRawPtr<MutationObserverInterestGroup> mutationRecipients = MutationObserverInterestGroup::createForCharacterDataMutation(*this))
187 mutationRecipients->enqueueMutationRecord(MutationRecord::createCharacterData(this, oldData));
189 if (parentNode()) {
190 ContainerNode::ChildrenChange change = {ContainerNode::TextChanged, previousSibling(), nextSibling(), ContainerNode::ChildrenChangeSourceAPI};
191 parentNode()->childrenChanged(change);
194 // Skip DOM mutation events if the modification is from parser.
195 // Note that mutation observer events will still fire.
196 // Spec: https://html.spec.whatwg.org/multipage/syntax.html#insert-a-character
197 if (source != UpdateFromParser && !isInShadowTree()) {
198 if (document().hasListenerType(Document::DOMCHARACTERDATAMODIFIED_LISTENER))
199 dispatchScopedEvent(MutationEvent::create(EventTypeNames::DOMCharacterDataModified, true, nullptr, oldData, m_data));
200 dispatchSubtreeModifiedEvent();
202 InspectorInstrumentation::characterDataModified(this);
205 int CharacterData::maxCharacterOffset() const
207 return static_cast<int>(length());
210 } // namespace blink