1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Base class for DOM Core's Comment, DocumentType, Text,
9 * CDATASection and ProcessingInstruction nodes.
12 #include "mozilla/dom/CharacterData.h"
14 #include "mozilla/DebugOnly.h"
16 #include "mozilla/AsyncEventDispatcher.h"
17 #include "mozilla/MemoryReporting.h"
18 #include "mozilla/dom/BindContext.h"
19 #include "mozilla/dom/Element.h"
20 #include "mozilla/dom/HTMLSlotElement.h"
21 #include "mozilla/dom/MutationObservers.h"
22 #include "mozilla/dom/ShadowRoot.h"
23 #include "mozilla/dom/Document.h"
24 #include "nsReadableUtils.h"
25 #include "mozilla/InternalMutationEvent.h"
27 #include "nsDOMString.h"
28 #include "nsChangeHint.h"
29 #include "nsCOMArray.h"
30 #include "mozilla/dom/DirectionalityUtils.h"
31 #include "nsCCUncollectableMarker.h"
32 #include "mozAutoDocUpdate.h"
33 #include "nsIContentInlines.h"
34 #include "nsTextNode.h"
35 #include "nsBidiUtils.h"
36 #include "PLDHashTable.h"
37 #include "mozilla/Sprintf.h"
38 #include "nsWindowSizes.h"
39 #include "nsWrapperCacheInlines.h"
41 #if defined(ACCESSIBILITY) && defined(DEBUG)
42 # include "nsAccessibilityService.h"
45 namespace mozilla::dom
{
47 CharacterData::CharacterData(already_AddRefed
<dom::NodeInfo
>&& aNodeInfo
)
48 : nsIContent(std::move(aNodeInfo
)) {
49 MOZ_ASSERT(mNodeInfo
->NodeType() == TEXT_NODE
||
50 mNodeInfo
->NodeType() == CDATA_SECTION_NODE
||
51 mNodeInfo
->NodeType() == COMMENT_NODE
||
52 mNodeInfo
->NodeType() == PROCESSING_INSTRUCTION_NODE
||
53 mNodeInfo
->NodeType() == DOCUMENT_TYPE_NODE
,
54 "Bad NodeType in aNodeInfo");
57 CharacterData::~CharacterData() {
58 MOZ_ASSERT(!IsInUncomposedDoc(),
59 "Please remove this from the document properly");
65 Element
* CharacterData::GetNameSpaceElement() {
66 return Element::FromNodeOrNull(GetParentNode());
69 NS_IMPL_CYCLE_COLLECTION_CLASS(CharacterData
)
71 NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(CharacterData
)
73 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(CharacterData
)
74 return Element::CanSkip(tmp
, aRemovingAllowed
);
75 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
77 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(CharacterData
)
78 return Element::CanSkipInCC(tmp
);
79 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
81 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(CharacterData
)
82 return Element::CanSkipThis(tmp
);
83 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
85 // We purposefully don't TRAVERSE_BEGIN_INHERITED here. All the bits
86 // we should traverse should be added here or in nsINode::Traverse.
87 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(CharacterData
)
88 if (MOZ_UNLIKELY(cb
.WantDebugInfo())) {
90 SprintfLiteral(name
, "CharacterData (len=%d)", tmp
->mText
.GetLength());
91 cb
.DescribeRefCountedNode(tmp
->mRefCnt
.get(), name
);
93 NS_IMPL_CYCLE_COLLECTION_DESCRIBE(CharacterData
, tmp
->mRefCnt
.get())
96 if (!nsIContent::Traverse(tmp
, cb
)) {
97 return NS_SUCCESS_INTERRUPTED_TRAVERSE
;
99 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
101 // We purposefully don't UNLINK_BEGIN_INHERITED here.
102 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CharacterData
)
103 nsIContent::Unlink(tmp
);
105 if (nsContentSlots
* slots
= tmp
->GetExistingContentSlots()) {
108 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
110 NS_INTERFACE_MAP_BEGIN(CharacterData
)
111 NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(CharacterData
)
112 NS_INTERFACE_MAP_END_INHERITING(nsIContent
)
114 void CharacterData::GetNodeValueInternal(nsAString
& aNodeValue
) {
118 void CharacterData::SetNodeValueInternal(const nsAString
& aNodeValue
,
119 ErrorResult
& aError
) {
120 aError
= SetTextInternal(0, mText
.GetLength(), aNodeValue
.BeginReading(),
121 aNodeValue
.Length(), true);
124 //----------------------------------------------------------------------
126 // Implementation of CharacterData
128 void CharacterData::SetTextContentInternal(const nsAString
& aTextContent
,
129 nsIPrincipal
* aSubjectPrincipal
,
130 ErrorResult
& aError
) {
131 // Batch possible DOMSubtreeModified events.
132 mozAutoSubtreeModified
subtree(OwnerDoc(), nullptr);
133 return SetNodeValue(aTextContent
, aError
);
136 void CharacterData::GetData(nsAString
& aData
) const {
139 mText
.AppendTo(aData
);
141 // Must use Substring() since nsDependentCString() requires null
142 // terminated strings.
144 const char* data
= mText
.Get1b();
147 CopyASCIItoUTF16(Substring(data
, data
+ mText
.GetLength()), aData
);
154 void CharacterData::SetData(const nsAString
& aData
, ErrorResult
& aRv
) {
155 nsresult rv
= SetTextInternal(0, mText
.GetLength(), aData
.BeginReading(),
156 aData
.Length(), true);
162 void CharacterData::SubstringData(uint32_t aStart
, uint32_t aCount
,
163 nsAString
& aReturn
, ErrorResult
& rv
) {
166 uint32_t textLength
= mText
.GetLength();
167 if (aStart
> textLength
) {
168 rv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
172 uint32_t amount
= aCount
;
173 if (amount
> textLength
- aStart
) {
174 amount
= textLength
- aStart
;
178 aReturn
.Assign(mText
.Get2b() + aStart
, amount
);
180 // Must use Substring() since nsDependentCString() requires null
181 // terminated strings.
183 const char* data
= mText
.Get1b() + aStart
;
184 CopyASCIItoUTF16(Substring(data
, data
+ amount
), aReturn
);
188 //----------------------------------------------------------------------
190 void CharacterData::AppendData(const nsAString
& aData
, ErrorResult
& aRv
) {
191 InsertData(mText
.GetLength(), aData
, aRv
);
194 void CharacterData::InsertData(uint32_t aOffset
, const nsAString
& aData
,
197 SetTextInternal(aOffset
, 0, aData
.BeginReading(), aData
.Length(), true);
203 void CharacterData::DeleteData(uint32_t aOffset
, uint32_t aCount
,
205 nsresult rv
= SetTextInternal(aOffset
, aCount
, nullptr, 0, true);
211 void CharacterData::ReplaceData(uint32_t aOffset
, uint32_t aCount
,
212 const nsAString
& aData
, ErrorResult
& aRv
) {
213 nsresult rv
= SetTextInternal(aOffset
, aCount
, aData
.BeginReading(),
214 aData
.Length(), true);
220 nsresult
CharacterData::SetTextInternal(
221 uint32_t aOffset
, uint32_t aCount
, const char16_t
* aBuffer
,
222 uint32_t aLength
, bool aNotify
,
223 CharacterDataChangeInfo::Details
* aDetails
) {
224 MOZ_ASSERT(aBuffer
|| !aLength
, "Null buffer passed to SetTextInternal!");
226 // sanitize arguments
227 uint32_t textLength
= mText
.GetLength();
228 if (aOffset
> textLength
) {
229 return NS_ERROR_DOM_INDEX_SIZE_ERR
;
232 if (aCount
> textLength
- aOffset
) {
233 aCount
= textLength
- aOffset
;
236 uint32_t endOffset
= aOffset
+ aCount
;
238 // Make sure the text fragment can hold the new data.
239 if (aLength
> aCount
&& !mText
.CanGrowBy(aLength
- aCount
)) {
240 return NS_ERROR_OUT_OF_MEMORY
;
243 Document
* document
= GetComposedDoc();
244 mozAutoDocUpdate
updateBatch(document
, aNotify
);
246 bool haveMutationListeners
=
247 aNotify
&& nsContentUtils::HasMutationListeners(
248 this, NS_EVENT_BITS_MUTATION_CHARACTERDATAMODIFIED
, this);
250 RefPtr
<nsAtom
> oldValue
;
251 if (haveMutationListeners
) {
252 oldValue
= GetCurrentValueAtom();
256 CharacterDataChangeInfo info
= {aOffset
== textLength
, aOffset
, endOffset
,
258 MutationObservers::NotifyCharacterDataWillChange(this, info
);
261 Directionality oldDir
= eDir_NotSet
;
262 bool dirAffectsAncestor
=
263 (NodeType() == TEXT_NODE
&&
264 TextNodeWillChangeDirection(static_cast<nsTextNode
*>(this), &oldDir
,
267 if (aOffset
== 0 && endOffset
== textLength
) {
268 // Replacing whole text or old text was empty.
269 // If this is marked as "maybe modified frequently", the text should be
270 // stored as char16_t since converting char* to char16_t* is expensive.
271 bool ok
= mText
.SetTo(aBuffer
, aLength
, true,
272 HasFlag(NS_MAYBE_MODIFIED_FREQUENTLY
));
273 NS_ENSURE_TRUE(ok
, NS_ERROR_OUT_OF_MEMORY
);
274 } else if (aOffset
== textLength
) {
275 // Appending to existing.
276 bool ok
= mText
.Append(aBuffer
, aLength
, !mText
.IsBidi(),
277 HasFlag(NS_MAYBE_MODIFIED_FREQUENTLY
));
278 NS_ENSURE_TRUE(ok
, NS_ERROR_OUT_OF_MEMORY
);
280 // Merging old and new
282 bool bidi
= mText
.IsBidi();
284 // Allocate new buffer
285 int32_t newLength
= textLength
- aCount
+ aLength
;
286 // Use nsString and not nsAutoString so that we get a nsStringBuffer which
287 // can be just AddRefed in nsTextFragment.
289 to
.SetCapacity(newLength
);
291 // Copy over appropriate data
293 mText
.AppendTo(to
, 0, aOffset
);
296 to
.Append(aBuffer
, aLength
);
298 bidi
= HasRTLChars(Span(aBuffer
, aLength
));
301 if (endOffset
!= textLength
) {
302 mText
.AppendTo(to
, endOffset
, textLength
- endOffset
);
305 // If this is marked as "maybe modified frequently", the text should be
306 // stored as char16_t since converting char* to char16_t* is expensive.
307 // Use char16_t also when we have bidi characters.
308 bool use2b
= HasFlag(NS_MAYBE_MODIFIED_FREQUENTLY
) || bidi
;
309 bool ok
= mText
.SetTo(to
, false, use2b
);
312 NS_ENSURE_TRUE(ok
, NS_ERROR_OUT_OF_MEMORY
);
315 UnsetFlags(NS_CACHED_TEXT_IS_ONLY_WHITESPACE
);
317 if (document
&& mText
.IsBidi()) {
318 // If we found bidi characters in mText.SetTo() above, indicate that the
319 // document contains bidi characters.
320 document
->SetBidiEnabled();
323 if (dirAffectsAncestor
) {
324 // dirAffectsAncestor being true implies that we have a text node, see
326 MOZ_ASSERT(NodeType() == TEXT_NODE
);
327 TextNodeChangedDirection(static_cast<nsTextNode
*>(this), oldDir
, aNotify
);
332 CharacterDataChangeInfo info
= {aOffset
== textLength
, aOffset
, endOffset
,
334 MutationObservers::NotifyCharacterDataChanged(this, info
);
336 if (haveMutationListeners
) {
337 InternalMutationEvent
mutation(true, eLegacyCharacterDataModified
);
339 mutation
.mPrevAttrValue
= oldValue
;
343 mutation
.mNewAttrValue
= NS_Atomize(val
);
346 mozAutoSubtreeModified
subtree(OwnerDoc(), this);
347 (new AsyncEventDispatcher(this, mutation
))->RunDOMEventWhenSafe();
354 //----------------------------------------------------------------------
356 // Implementation of nsIContent
359 void CharacterData::ToCString(nsAString
& aBuf
, int32_t aOffset
,
360 int32_t aLen
) const {
362 const char16_t
* cp
= mText
.Get2b() + aOffset
;
363 const char16_t
* end
= cp
+ aLen
;
368 aBuf
.AppendLiteral("&");
369 } else if (ch
== '<') {
370 aBuf
.AppendLiteral("<");
371 } else if (ch
== '>') {
372 aBuf
.AppendLiteral(">");
373 } else if ((ch
< ' ') || (ch
>= 127)) {
374 aBuf
.AppendPrintf("\\u%04x", ch
);
380 unsigned char* cp
= (unsigned char*)mText
.Get1b() + aOffset
;
381 const unsigned char* end
= cp
+ aLen
;
386 aBuf
.AppendLiteral("&");
387 } else if (ch
== '<') {
388 aBuf
.AppendLiteral("<");
389 } else if (ch
== '>') {
390 aBuf
.AppendLiteral(">");
391 } else if ((ch
< ' ') || (ch
>= 127)) {
392 aBuf
.AppendPrintf("\\u%04x", ch
);
401 nsresult
CharacterData::BindToTree(BindContext
& aContext
, nsINode
& aParent
) {
402 MOZ_ASSERT(aParent
.IsContent() || aParent
.IsDocument(),
403 "Must have content or document parent!");
404 MOZ_ASSERT(aParent
.OwnerDoc() == OwnerDoc(),
405 "Must have the same owner document");
406 MOZ_ASSERT(OwnerDoc() == &aContext
.OwnerDoc(), "These should match too");
407 MOZ_ASSERT(!IsInUncomposedDoc(), "Already have a document. Unbind first!");
408 MOZ_ASSERT(!IsInComposedDoc(), "Already have a document. Unbind first!");
409 // Note that as we recurse into the kids, they'll have a non-null parent. So
410 // only assert if our parent is _changing_ while we have a parent.
411 MOZ_ASSERT(!GetParentNode() || &aParent
== GetParentNode(),
412 "Already have a parent. Unbind first!");
414 const bool hadParent
= !!GetParentNode();
416 if (aParent
.IsInNativeAnonymousSubtree()) {
417 SetFlags(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE
);
419 if (aParent
.HasFlag(NODE_HAS_BEEN_IN_UA_WIDGET
)) {
420 SetFlags(NODE_HAS_BEEN_IN_UA_WIDGET
);
422 if (IsRootOfNativeAnonymousSubtree()) {
423 aParent
.SetMayHaveAnonymousChildren();
428 if (!hadParent
&& aParent
.IsContent()) {
429 SetParentIsContent(true);
432 MOZ_ASSERT(!!GetParent() == aParent
.IsContent());
434 if (aParent
.IsInUncomposedDoc() || aParent
.IsInShadowTree()) {
435 // We no longer need to track the subtree pointer (and in fact we'll assert
436 // if we do this any later).
437 ClearSubtreeRootPointer();
438 SetIsConnected(aParent
.IsInComposedDoc());
440 if (aParent
.IsInUncomposedDoc()) {
443 SetFlags(NODE_IS_IN_SHADOW_TREE
);
444 MOZ_ASSERT(aParent
.IsContent() &&
445 aParent
.AsContent()->GetContainingShadow());
446 ExtendedContentSlots()->mContainingShadow
=
447 aParent
.AsContent()->GetContainingShadow();
450 if (IsInComposedDoc() && mText
.IsBidi()) {
451 aContext
.OwnerDoc().SetBidiEnabled();
454 // Clear the lazy frame construction bits.
455 UnsetFlags(NODE_NEEDS_FRAME
| NODE_DESCENDANTS_NEED_FRAMES
);
457 // If we're not in the doc and not in a shadow tree,
458 // update our subtree pointer.
459 SetSubtreeRootPointer(aParent
.SubtreeRoot());
462 MutationObservers::NotifyParentChainChanged(this);
463 if (!hadParent
&& IsRootOfNativeAnonymousSubtree()) {
464 MutationObservers::NotifyNativeAnonymousChildListChange(this, false);
467 UpdateEditableState(false);
469 // Ensure we only do these once, in the case we move the shadow host around.
470 if (aContext
.SubtreeRootChanges()) {
471 HandleShadowDOMRelatedInsertionSteps(hadParent
);
474 MOZ_ASSERT(OwnerDoc() == aParent
.OwnerDoc(), "Bound to wrong document");
475 MOZ_ASSERT(IsInComposedDoc() == aContext
.InComposedDoc());
476 MOZ_ASSERT(IsInUncomposedDoc() == aContext
.InUncomposedDoc());
477 MOZ_ASSERT(&aParent
== GetParentNode(), "Bound to wrong parent node");
478 MOZ_ASSERT(aParent
.IsInUncomposedDoc() == IsInUncomposedDoc());
479 MOZ_ASSERT(aParent
.IsInComposedDoc() == IsInComposedDoc());
480 MOZ_ASSERT(aParent
.IsInShadowTree() == IsInShadowTree());
481 MOZ_ASSERT(aParent
.SubtreeRoot() == SubtreeRoot());
485 void CharacterData::UnbindFromTree(bool aNullParent
) {
486 // Unset frame flags; if we need them again later, they'll get set again.
487 UnsetFlags(NS_CREATE_FRAME_IF_NON_WHITESPACE
| NS_REFRAME_IF_WHITESPACE
);
489 HandleShadowDOMRelatedRemovalSteps(aNullParent
);
492 if (IsRootOfNativeAnonymousSubtree()) {
493 MutationObservers::NotifyNativeAnonymousChildListChange(this, true);
500 SetParentIsContent(false);
503 SetIsConnected(false);
505 if (aNullParent
|| !mParent
->IsInShadowTree()) {
506 UnsetFlags(NODE_IS_IN_SHADOW_TREE
);
508 // Begin keeping track of our subtree root.
509 SetSubtreeRootPointer(aNullParent
? this : mParent
->SubtreeRoot());
512 if (nsExtendedContentSlots
* slots
= GetExistingExtendedContentSlots()) {
513 if (aNullParent
|| !mParent
->IsInShadowTree()) {
514 slots
->mContainingShadow
= nullptr;
518 MutationObservers::NotifyParentChainChanged(this);
520 #if defined(ACCESSIBILITY) && defined(DEBUG)
521 MOZ_ASSERT(!GetAccService() || !GetAccService()->HasAccessible(this),
522 "An accessible for this element still exists!");
526 //----------------------------------------------------------------------
528 // Implementation of the nsIContent interface text functions
530 nsresult
CharacterData::SetText(const char16_t
* aBuffer
, uint32_t aLength
,
532 return SetTextInternal(0, mText
.GetLength(), aBuffer
, aLength
, aNotify
);
535 nsresult
CharacterData::AppendText(const char16_t
* aBuffer
, uint32_t aLength
,
537 return SetTextInternal(mText
.GetLength(), 0, aBuffer
, aLength
, aNotify
);
540 bool CharacterData::TextIsOnlyWhitespace() {
541 MOZ_ASSERT(NS_IsMainThread());
542 if (!ThreadSafeTextIsOnlyWhitespace()) {
543 UnsetFlags(NS_TEXT_IS_ONLY_WHITESPACE
);
544 SetFlags(NS_CACHED_TEXT_IS_ONLY_WHITESPACE
);
548 SetFlags(NS_CACHED_TEXT_IS_ONLY_WHITESPACE
| NS_TEXT_IS_ONLY_WHITESPACE
);
552 bool CharacterData::ThreadSafeTextIsOnlyWhitespace() const {
553 // FIXME: should this method take content language into account?
555 // The fragment contains non-8bit characters and such characters
556 // are never considered whitespace.
558 // FIXME(emilio): This is not quite true in presence of the
559 // NS_MAYBE_MODIFIED_FREQUENTLY flag... But looks like we only set that on
560 // anonymous nodes, so should be fine...
564 if (HasFlag(NS_CACHED_TEXT_IS_ONLY_WHITESPACE
)) {
565 return HasFlag(NS_TEXT_IS_ONLY_WHITESPACE
);
568 const char* cp
= mText
.Get1b();
569 const char* end
= cp
+ mText
.GetLength();
574 // NOTE(emilio): If you ever change the definition of "whitespace" here, you
575 // need to change it too in RestyleManager::CharacterDataChanged.
576 if (!dom::IsSpaceCharacter(ch
)) {
586 already_AddRefed
<nsAtom
> CharacterData::GetCurrentValueAtom() {
589 return NS_Atomize(val
);
592 void CharacterData::AddSizeOfExcludingThis(nsWindowSizes
& aSizes
,
593 size_t* aNodeSize
) const {
594 nsIContent::AddSizeOfExcludingThis(aSizes
, aNodeSize
);
595 *aNodeSize
+= mText
.SizeOfExcludingThis(aSizes
.mState
.mMallocSizeOf
);
598 } // namespace mozilla::dom