forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsGenericDOMDataNode.cpp
1117 lines (942 loc) · 29.5 KB
/
nsGenericDOMDataNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Base class for DOM Core's nsIDOMComment, nsIDOMDocumentType, nsIDOMText,
* nsIDOMCDATASection, and nsIDOMProcessingInstruction nodes.
*/
#include "mozilla/DebugOnly.h"
#include "nsGenericDOMDataNode.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ShadowRoot.h"
#include "nsIDocument.h"
#include "nsIDOMDocument.h"
#include "nsReadableUtils.h"
#include "mozilla/InternalMutationEvent.h"
#include "nsIURI.h"
#include "nsIDOMEvent.h"
#include "nsIDOMText.h"
#include "nsCOMPtr.h"
#include "nsDOMString.h"
#include "nsChangeHint.h"
#include "nsCOMArray.h"
#include "nsNodeUtils.h"
#include "mozilla/dom/DirectionalityUtils.h"
#include "nsBindingManager.h"
#include "nsCCUncollectableMarker.h"
#include "mozAutoDocUpdate.h"
#include "pldhash.h"
#include "prprf.h"
#include "nsWrapperCacheInlines.h"
using namespace mozilla;
using namespace mozilla::dom;
nsGenericDOMDataNode::nsGenericDOMDataNode(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
: nsIContent(aNodeInfo)
{
NS_ABORT_IF_FALSE(mNodeInfo->NodeType() == nsIDOMNode::TEXT_NODE ||
mNodeInfo->NodeType() == nsIDOMNode::CDATA_SECTION_NODE ||
mNodeInfo->NodeType() == nsIDOMNode::COMMENT_NODE ||
mNodeInfo->NodeType() ==
nsIDOMNode::PROCESSING_INSTRUCTION_NODE ||
mNodeInfo->NodeType() == nsIDOMNode::DOCUMENT_TYPE_NODE,
"Bad NodeType in aNodeInfo");
}
nsGenericDOMDataNode::nsGenericDOMDataNode(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: nsIContent(aNodeInfo)
{
NS_ABORT_IF_FALSE(mNodeInfo->NodeType() == nsIDOMNode::TEXT_NODE ||
mNodeInfo->NodeType() == nsIDOMNode::CDATA_SECTION_NODE ||
mNodeInfo->NodeType() == nsIDOMNode::COMMENT_NODE ||
mNodeInfo->NodeType() ==
nsIDOMNode::PROCESSING_INSTRUCTION_NODE ||
mNodeInfo->NodeType() == nsIDOMNode::DOCUMENT_TYPE_NODE,
"Bad NodeType in aNodeInfo");
}
nsGenericDOMDataNode::~nsGenericDOMDataNode()
{
NS_PRECONDITION(!IsInDoc(),
"Please remove this from the document properly");
if (GetParent()) {
NS_RELEASE(mParent);
}
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsGenericDOMDataNode)
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsGenericDOMDataNode)
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsGenericDOMDataNode)
return Element::CanSkip(tmp, aRemovingAllowed);
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsGenericDOMDataNode)
return Element::CanSkipInCC(tmp);
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsGenericDOMDataNode)
return Element::CanSkipThis(tmp);
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsGenericDOMDataNode)
if (MOZ_UNLIKELY(cb.WantDebugInfo())) {
char name[40];
PR_snprintf(name, sizeof(name), "nsGenericDOMDataNode (len=%d)",
tmp->mText.GetLength());
cb.DescribeRefCountedNode(tmp->mRefCnt.get(), name);
} else {
NS_IMPL_CYCLE_COLLECTION_DESCRIBE(nsGenericDOMDataNode, tmp->mRefCnt.get())
}
// Always need to traverse script objects, so do that before we check
// if we're uncollectable.
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
if (!nsINode::Traverse(tmp, cb)) {
return NS_SUCCESS_INTERRUPTED_TRAVERSE;
}
nsDataSlots *slots = tmp->GetExistingDataSlots();
if (slots) {
slots->Traverse(cb);
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsGenericDOMDataNode)
nsINode::Unlink(tmp);
nsDataSlots *slots = tmp->GetExistingDataSlots();
if (slots) {
slots->Unlink();
}
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN(nsGenericDOMDataNode)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsGenericDOMDataNode)
NS_INTERFACE_MAP_ENTRY(nsIContent)
NS_INTERFACE_MAP_ENTRY(nsINode)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
NS_INTERFACE_MAP_ENTRY(mozilla::dom::EventTarget)
NS_INTERFACE_MAP_ENTRY_TEAROFF(nsISupportsWeakReference,
new nsNodeSupportsWeakRefTearoff(this))
// DOM bindings depend on the identity pointer being the
// same as nsINode (which nsIContent inherits).
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContent)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsGenericDOMDataNode)
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(nsGenericDOMDataNode,
nsNodeUtils::LastRelease(this))
void
nsGenericDOMDataNode::GetNodeValueInternal(nsAString& aNodeValue)
{
DebugOnly<nsresult> rv = GetData(aNodeValue);
NS_ASSERTION(NS_SUCCEEDED(rv), "GetData() failed!");
}
void
nsGenericDOMDataNode::SetNodeValueInternal(const nsAString& aNodeValue,
ErrorResult& aError)
{
aError = SetTextInternal(0, mText.GetLength(), aNodeValue.BeginReading(),
aNodeValue.Length(), true);
}
//----------------------------------------------------------------------
// Implementation of nsIDOMCharacterData
nsresult
nsGenericDOMDataNode::GetData(nsAString& aData) const
{
if (mText.Is2b()) {
aData.Assign(mText.Get2b(), mText.GetLength());
} else {
// Must use Substring() since nsDependentCString() requires null
// terminated strings.
const char *data = mText.Get1b();
if (data) {
CopyASCIItoUTF16(Substring(data, data + mText.GetLength()), aData);
} else {
aData.Truncate();
}
}
return NS_OK;
}
nsresult
nsGenericDOMDataNode::SetData(const nsAString& aData)
{
return SetTextInternal(0, mText.GetLength(), aData.BeginReading(),
aData.Length(), true);
}
nsresult
nsGenericDOMDataNode::GetLength(uint32_t* aLength)
{
*aLength = mText.GetLength();
return NS_OK;
}
nsresult
nsGenericDOMDataNode::SubstringData(uint32_t aStart, uint32_t aCount,
nsAString& aReturn)
{
ErrorResult rv;
SubstringData(aStart, aCount, aReturn, rv);
return rv.ErrorCode();
}
void
nsGenericDOMDataNode::SubstringData(uint32_t aStart, uint32_t aCount,
nsAString& aReturn, ErrorResult& rv)
{
aReturn.Truncate();
uint32_t textLength = mText.GetLength();
if (aStart > textLength) {
rv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
return;
}
uint32_t amount = aCount;
if (amount > textLength - aStart) {
amount = textLength - aStart;
}
if (mText.Is2b()) {
aReturn.Assign(mText.Get2b() + aStart, amount);
} else {
// Must use Substring() since nsDependentCString() requires null
// terminated strings.
const char *data = mText.Get1b() + aStart;
CopyASCIItoUTF16(Substring(data, data + amount), aReturn);
}
}
NS_IMETHODIMP
nsGenericDOMDataNode::MozRemove()
{
Remove();
return NS_OK;
}
//----------------------------------------------------------------------
nsresult
nsGenericDOMDataNode::AppendData(const nsAString& aData)
{
return SetTextInternal(mText.GetLength(), 0, aData.BeginReading(),
aData.Length(), true);
}
nsresult
nsGenericDOMDataNode::InsertData(uint32_t aOffset,
const nsAString& aData)
{
return SetTextInternal(aOffset, 0, aData.BeginReading(),
aData.Length(), true);
}
nsresult
nsGenericDOMDataNode::DeleteData(uint32_t aOffset, uint32_t aCount)
{
return SetTextInternal(aOffset, aCount, nullptr, 0, true);
}
nsresult
nsGenericDOMDataNode::ReplaceData(uint32_t aOffset, uint32_t aCount,
const nsAString& aData)
{
return SetTextInternal(aOffset, aCount, aData.BeginReading(),
aData.Length(), true);
}
nsresult
nsGenericDOMDataNode::SetTextInternal(uint32_t aOffset, uint32_t aCount,
const char16_t* aBuffer,
uint32_t aLength, bool aNotify,
CharacterDataChangeInfo::Details* aDetails)
{
NS_PRECONDITION(aBuffer || !aLength,
"Null buffer passed to SetTextInternal!");
// sanitize arguments
uint32_t textLength = mText.GetLength();
if (aOffset > textLength) {
return NS_ERROR_DOM_INDEX_SIZE_ERR;
}
if (aCount > textLength - aOffset) {
aCount = textLength - aOffset;
}
uint32_t endOffset = aOffset + aCount;
// Make sure the text fragment can hold the new data.
if (aLength > aCount && !mText.CanGrowBy(aLength - aCount)) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsIDocument *document = GetComposedDoc();
mozAutoDocUpdate updateBatch(document, UPDATE_CONTENT_MODEL, aNotify);
bool haveMutationListeners = aNotify &&
nsContentUtils::HasMutationListeners(this,
NS_EVENT_BITS_MUTATION_CHARACTERDATAMODIFIED,
this);
nsCOMPtr<nsIAtom> oldValue;
if (haveMutationListeners) {
oldValue = GetCurrentValueAtom();
}
if (aNotify) {
CharacterDataChangeInfo info = {
aOffset == textLength,
aOffset,
endOffset,
aLength,
aDetails
};
nsNodeUtils::CharacterDataWillChange(this, &info);
}
Directionality oldDir = eDir_NotSet;
bool dirAffectsAncestor = (NodeType() == nsIDOMNode::TEXT_NODE &&
TextNodeWillChangeDirection(this, &oldDir, aOffset));
if (aOffset == 0 && endOffset == textLength) {
// Replacing whole text or old text was empty. Don't bother to check for
// bidi in this string if the document already has bidi enabled.
bool ok = mText.SetTo(aBuffer, aLength, !document || !document->GetBidiEnabled());
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
}
else if (aOffset == textLength) {
// Appending to existing
bool ok = mText.Append(aBuffer, aLength, !document || !document->GetBidiEnabled());
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
}
else {
// Merging old and new
// Allocate new buffer
int32_t newLength = textLength - aCount + aLength;
char16_t* to = new char16_t[newLength];
NS_ENSURE_TRUE(to, NS_ERROR_OUT_OF_MEMORY);
// Copy over appropriate data
if (aOffset) {
mText.CopyTo(to, 0, aOffset);
}
if (aLength) {
memcpy(to + aOffset, aBuffer, aLength * sizeof(char16_t));
}
if (endOffset != textLength) {
mText.CopyTo(to + aOffset + aLength, endOffset, textLength - endOffset);
}
bool ok = mText.SetTo(to, newLength, !document || !document->GetBidiEnabled());
delete [] to;
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
}
UnsetFlags(NS_CACHED_TEXT_IS_ONLY_WHITESPACE);
if (document && mText.IsBidi()) {
// If we found bidi characters in mText.SetTo() above, indicate that the
// document contains bidi characters.
document->SetBidiEnabled();
}
if (dirAffectsAncestor) {
TextNodeChangedDirection(this, oldDir, aNotify);
}
// Notify observers
if (aNotify) {
CharacterDataChangeInfo info = {
aOffset == textLength,
aOffset,
endOffset,
aLength,
aDetails
};
nsNodeUtils::CharacterDataChanged(this, &info);
if (haveMutationListeners) {
InternalMutationEvent mutation(true, NS_MUTATION_CHARACTERDATAMODIFIED);
mutation.mPrevAttrValue = oldValue;
if (aLength > 0) {
nsAutoString val;
mText.AppendTo(val);
mutation.mNewAttrValue = do_GetAtom(val);
}
mozAutoSubtreeModified subtree(OwnerDoc(), this);
(new AsyncEventDispatcher(this, mutation))->RunDOMEventWhenSafe();
}
}
return NS_OK;
}
//----------------------------------------------------------------------
// Implementation of nsIContent
#ifdef DEBUG
void
nsGenericDOMDataNode::ToCString(nsAString& aBuf, int32_t aOffset,
int32_t aLen) const
{
if (mText.Is2b()) {
const char16_t* cp = mText.Get2b() + aOffset;
const char16_t* end = cp + aLen;
while (cp < end) {
char16_t ch = *cp++;
if (ch == '&') {
aBuf.AppendLiteral("&");
} else if (ch == '<') {
aBuf.AppendLiteral("<");
} else if (ch == '>') {
aBuf.AppendLiteral(">");
} else if ((ch < ' ') || (ch >= 127)) {
char buf[10];
PR_snprintf(buf, sizeof(buf), "\\u%04x", ch);
AppendASCIItoUTF16(buf, aBuf);
} else {
aBuf.Append(ch);
}
}
} else {
unsigned char* cp = (unsigned char*)mText.Get1b() + aOffset;
const unsigned char* end = cp + aLen;
while (cp < end) {
char16_t ch = *cp++;
if (ch == '&') {
aBuf.AppendLiteral("&");
} else if (ch == '<') {
aBuf.AppendLiteral("<");
} else if (ch == '>') {
aBuf.AppendLiteral(">");
} else if ((ch < ' ') || (ch >= 127)) {
char buf[10];
PR_snprintf(buf, sizeof(buf), "\\u%04x", ch);
AppendASCIItoUTF16(buf, aBuf);
} else {
aBuf.Append(ch);
}
}
}
}
#endif
nsresult
nsGenericDOMDataNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
nsIContent* aBindingParent,
bool aCompileEventHandlers)
{
NS_PRECONDITION(aParent || aDocument, "Must have document if no parent!");
NS_PRECONDITION(NODE_FROM(aParent, aDocument)->OwnerDoc() == OwnerDoc(),
"Must have the same owner document");
NS_PRECONDITION(!aParent || aDocument == aParent->GetUncomposedDoc(),
"aDocument must be current doc of aParent");
NS_PRECONDITION(!GetUncomposedDoc() && !IsInDoc(),
"Already have a document. Unbind first!");
// Note that as we recurse into the kids, they'll have a non-null parent. So
// only assert if our parent is _changing_ while we have a parent.
NS_PRECONDITION(!GetParent() || aParent == GetParent(),
"Already have a parent. Unbind first!");
NS_PRECONDITION(!GetBindingParent() ||
aBindingParent == GetBindingParent() ||
(!aBindingParent && aParent &&
aParent->GetBindingParent() == GetBindingParent()),
"Already have a binding parent. Unbind first!");
NS_PRECONDITION(aBindingParent != this,
"Content must not be its own binding parent");
NS_PRECONDITION(!IsRootOfNativeAnonymousSubtree() ||
aBindingParent == aParent,
"Native anonymous content must have its parent as its "
"own binding parent");
if (!aBindingParent && aParent) {
aBindingParent = aParent->GetBindingParent();
}
// First set the binding parent
if (aBindingParent) {
NS_ASSERTION(IsRootOfNativeAnonymousSubtree() ||
!HasFlag(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE) ||
(aParent && aParent->IsInNativeAnonymousSubtree()),
"Trying to re-bind content from native anonymous subtree to "
"non-native anonymous parent!");
DataSlots()->mBindingParent = aBindingParent; // Weak, so no addref happens.
if (aParent->IsInNativeAnonymousSubtree()) {
SetFlags(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE);
}
if (aParent->HasFlag(NODE_CHROME_ONLY_ACCESS)) {
SetFlags(NODE_CHROME_ONLY_ACCESS);
}
if (aParent->IsInShadowTree()) {
ClearSubtreeRootPointer();
SetFlags(NODE_IS_IN_SHADOW_TREE);
}
ShadowRoot* parentContainingShadow = aParent->GetContainingShadow();
if (parentContainingShadow) {
DataSlots()->mContainingShadow = parentContainingShadow;
}
}
// Set parent
if (aParent) {
if (!GetParent()) {
NS_ADDREF(aParent);
}
mParent = aParent;
}
else {
mParent = aDocument;
}
SetParentIsContent(aParent);
// XXXbz sXBL/XBL2 issue!
// Set document
if (aDocument) {
// We no longer need to track the subtree pointer (and in fact we'll assert
// if we do this any later).
ClearSubtreeRootPointer();
// XXX See the comment in Element::BindToTree
SetInDocument();
if (mText.IsBidi()) {
aDocument->SetBidiEnabled();
}
// Clear the lazy frame construction bits.
UnsetFlags(NODE_NEEDS_FRAME | NODE_DESCENDANTS_NEED_FRAMES);
} else if (!IsInShadowTree()) {
// If we're not in the doc and not in a shadow tree,
// update our subtree pointer.
SetSubtreeRootPointer(aParent->SubtreeRoot());
}
nsNodeUtils::ParentChainChanged(this);
UpdateEditableState(false);
NS_POSTCONDITION(aDocument == GetUncomposedDoc(), "Bound to wrong document");
NS_POSTCONDITION(aParent == GetParent(), "Bound to wrong parent");
NS_POSTCONDITION(aBindingParent == GetBindingParent(),
"Bound to wrong binding parent");
return NS_OK;
}
void
nsGenericDOMDataNode::UnbindFromTree(bool aDeep, bool aNullParent)
{
// Unset frame flags; if we need them again later, they'll get set again.
UnsetFlags(NS_CREATE_FRAME_IF_NON_WHITESPACE |
NS_REFRAME_IF_WHITESPACE);
nsIDocument* document =
HasFlag(NODE_FORCE_XBL_BINDINGS) || IsInShadowTree() ?
OwnerDoc() : GetUncomposedDoc();
if (aNullParent) {
if (GetParent()) {
NS_RELEASE(mParent);
} else {
mParent = nullptr;
}
SetParentIsContent(false);
}
ClearInDocument();
if (aNullParent || !mParent->IsInShadowTree()) {
UnsetFlags(NODE_IS_IN_SHADOW_TREE);
// Begin keeping track of our subtree root.
SetSubtreeRootPointer(aNullParent ? this : mParent->SubtreeRoot());
}
if (document && !GetContainingShadow()) {
// Notify XBL- & nsIAnonymousContentCreator-generated
// anonymous content that the document is changing.
// Unlike XBL, bindings for web components shadow DOM
// do not get uninstalled.
if (HasFlag(NODE_MAY_BE_IN_BINDING_MNGR)) {
nsContentUtils::AddScriptRunner(
new RemoveFromBindingManagerRunnable(document->BindingManager(), this,
document));
}
}
nsDataSlots *slots = GetExistingDataSlots();
if (slots) {
slots->mBindingParent = nullptr;
if (aNullParent || !mParent->IsInShadowTree()) {
slots->mContainingShadow = nullptr;
}
}
nsNodeUtils::ParentChainChanged(this);
}
already_AddRefed<nsINodeList>
nsGenericDOMDataNode::GetChildren(uint32_t aFilter)
{
return nullptr;
}
nsresult
nsGenericDOMDataNode::SetAttr(int32_t aNameSpaceID, nsIAtom* aAttr,
nsIAtom* aPrefix, const nsAString& aValue,
bool aNotify)
{
return NS_OK;
}
nsresult
nsGenericDOMDataNode::UnsetAttr(int32_t aNameSpaceID, nsIAtom* aAttr,
bool aNotify)
{
return NS_OK;
}
const nsAttrName*
nsGenericDOMDataNode::GetAttrNameAt(uint32_t aIndex) const
{
return nullptr;
}
uint32_t
nsGenericDOMDataNode::GetAttrCount() const
{
return 0;
}
uint32_t
nsGenericDOMDataNode::GetChildCount() const
{
return 0;
}
nsIContent *
nsGenericDOMDataNode::GetChildAt(uint32_t aIndex) const
{
return nullptr;
}
nsIContent * const *
nsGenericDOMDataNode::GetChildArray(uint32_t* aChildCount) const
{
*aChildCount = 0;
return nullptr;
}
int32_t
nsGenericDOMDataNode::IndexOf(const nsINode* aPossibleChild) const
{
return -1;
}
nsresult
nsGenericDOMDataNode::InsertChildAt(nsIContent* aKid, uint32_t aIndex,
bool aNotify)
{
return NS_OK;
}
void
nsGenericDOMDataNode::RemoveChildAt(uint32_t aIndex, bool aNotify)
{
}
nsIContent *
nsGenericDOMDataNode::GetBindingParent() const
{
nsDataSlots *slots = GetExistingDataSlots();
return slots ? slots->mBindingParent : nullptr;
}
ShadowRoot *
nsGenericDOMDataNode::GetShadowRoot() const
{
return nullptr;
}
ShadowRoot *
nsGenericDOMDataNode::GetContainingShadow() const
{
nsDataSlots *slots = GetExistingDataSlots();
return slots ? slots->mContainingShadow : nullptr;
}
void
nsGenericDOMDataNode::SetShadowRoot(ShadowRoot* aShadowRoot)
{
}
nsTArray<nsIContent*>&
nsGenericDOMDataNode::DestInsertionPoints()
{
nsDataSlots *slots = DataSlots();
return slots->mDestInsertionPoints;
}
nsTArray<nsIContent*>*
nsGenericDOMDataNode::GetExistingDestInsertionPoints() const
{
nsDataSlots *slots = GetExistingDataSlots();
if (slots) {
return &slots->mDestInsertionPoints;
}
return nullptr;
}
nsXBLBinding *
nsGenericDOMDataNode::GetXBLBinding() const
{
return nullptr;
}
void
nsGenericDOMDataNode::SetXBLBinding(nsXBLBinding* aBinding,
nsBindingManager* aOldBindingManager)
{
}
nsIContent *
nsGenericDOMDataNode::GetXBLInsertionParent() const
{
if (HasFlag(NODE_MAY_BE_IN_BINDING_MNGR)) {
nsDataSlots *slots = GetExistingDataSlots();
if (slots) {
return slots->mXBLInsertionParent;
}
}
return nullptr;
}
void
nsGenericDOMDataNode::SetXBLInsertionParent(nsIContent* aContent)
{
if (aContent) {
nsDataSlots *slots = DataSlots();
SetFlags(NODE_MAY_BE_IN_BINDING_MNGR);
slots->mXBLInsertionParent = aContent;
} else {
nsDataSlots *slots = GetExistingDataSlots();
if (slots) {
slots->mXBLInsertionParent = nullptr;
}
}
}
CustomElementData *
nsGenericDOMDataNode::GetCustomElementData() const
{
return nullptr;
}
void
nsGenericDOMDataNode::SetCustomElementData(CustomElementData* aData)
{
}
bool
nsGenericDOMDataNode::IsNodeOfType(uint32_t aFlags) const
{
return !(aFlags & ~(eCONTENT | eDATA_NODE));
}
void
nsGenericDOMDataNode::SaveSubtreeState()
{
}
void
nsGenericDOMDataNode::DestroyContent()
{
// XXX We really should let cycle collection do this, but that currently still
// leaks (see https://bugzilla.mozilla.org/show_bug.cgi?id=406684).
ReleaseWrapper(this);
}
#ifdef DEBUG
void
nsGenericDOMDataNode::List(FILE* out, int32_t aIndent) const
{
}
void
nsGenericDOMDataNode::DumpContent(FILE* out, int32_t aIndent,
bool aDumpAll) const
{
}
#endif
bool
nsGenericDOMDataNode::IsLink(nsIURI** aURI) const
{
*aURI = nullptr;
return false;
}
nsINode::nsSlots*
nsGenericDOMDataNode::CreateSlots()
{
return new nsDataSlots();
}
nsGenericDOMDataNode::nsDataSlots::nsDataSlots()
: nsINode::nsSlots(), mBindingParent(nullptr)
{
}
void
nsGenericDOMDataNode::nsDataSlots::Traverse(nsCycleCollectionTraversalCallback &cb)
{
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mXBLInsertionParent");
cb.NoteXPCOMChild(mXBLInsertionParent.get());
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mContainingShadow");
cb.NoteXPCOMChild(NS_ISUPPORTS_CAST(nsIContent*, mContainingShadow));
}
void
nsGenericDOMDataNode::nsDataSlots::Unlink()
{
mXBLInsertionParent = nullptr;
mContainingShadow = nullptr;
}
//----------------------------------------------------------------------
// Implementation of the nsIDOMText interface
nsresult
nsGenericDOMDataNode::SplitData(uint32_t aOffset, nsIContent** aReturn,
bool aCloneAfterOriginal)
{
*aReturn = nullptr;
nsresult rv = NS_OK;
nsAutoString cutText;
uint32_t length = TextLength();
if (aOffset > length) {
return NS_ERROR_DOM_INDEX_SIZE_ERR;
}
uint32_t cutStartOffset = aCloneAfterOriginal ? aOffset : 0;
uint32_t cutLength = aCloneAfterOriginal ? length - aOffset : aOffset;
rv = SubstringData(cutStartOffset, cutLength, cutText);
if (NS_FAILED(rv)) {
return rv;
}
nsIDocument* document = GetComposedDoc();
mozAutoDocUpdate updateBatch(document, UPDATE_CONTENT_MODEL, true);
// Use Clone for creating the new node so that the new node is of same class
// as this node!
nsCOMPtr<nsIContent> newContent = CloneDataNode(mNodeInfo, false);
if (!newContent) {
return NS_ERROR_OUT_OF_MEMORY;
}
newContent->SetText(cutText, true); // XXX should be false?
CharacterDataChangeInfo::Details details = {
CharacterDataChangeInfo::Details::eSplit, newContent
};
rv = SetTextInternal(cutStartOffset, cutLength, nullptr, 0, true,
aCloneAfterOriginal ? &details : nullptr);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsINode> parent = GetParentNode();
if (parent) {
int32_t insertionIndex = parent->IndexOf(this);
if (aCloneAfterOriginal) {
++insertionIndex;
}
parent->InsertChildAt(newContent, insertionIndex, true);
}
newContent.swap(*aReturn);
return rv;
}
nsresult
nsGenericDOMDataNode::SplitText(uint32_t aOffset, nsIDOMText** aReturn)
{
nsCOMPtr<nsIContent> newChild;
nsresult rv = SplitData(aOffset, getter_AddRefs(newChild));
if (NS_SUCCEEDED(rv)) {
rv = CallQueryInterface(newChild, aReturn);
}
return rv;
}
/* static */ int32_t
nsGenericDOMDataNode::FirstLogicallyAdjacentTextNode(nsIContent* aParent,
int32_t aIndex)
{
while (aIndex-- > 0) {
nsIContent* sibling = aParent->GetChildAt(aIndex);
if (!sibling->IsNodeOfType(nsINode::eTEXT))
return aIndex + 1;
}
return 0;
}
/* static */ int32_t
nsGenericDOMDataNode::LastLogicallyAdjacentTextNode(nsIContent* aParent,
int32_t aIndex,
uint32_t aCount)
{
while (++aIndex < int32_t(aCount)) {
nsIContent* sibling = aParent->GetChildAt(aIndex);
if (!sibling->IsNodeOfType(nsINode::eTEXT))
return aIndex - 1;
}
return aCount - 1;
}
nsresult
nsGenericDOMDataNode::GetWholeText(nsAString& aWholeText)
{
nsIContent* parent = GetParent();
// Handle parent-less nodes
if (!parent)
return GetData(aWholeText);
int32_t index = parent->IndexOf(this);
NS_WARN_IF_FALSE(index >= 0,
"Trying to use .wholeText with an anonymous"
"text node child of a binding parent?");
NS_ENSURE_TRUE(index >= 0, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
int32_t first =
FirstLogicallyAdjacentTextNode(parent, index);
int32_t last =
LastLogicallyAdjacentTextNode(parent, index, parent->GetChildCount());
aWholeText.Truncate();
nsCOMPtr<nsIDOMText> node;
nsAutoString tmp;
do {
node = do_QueryInterface(parent->GetChildAt(first));
node->GetData(tmp);
aWholeText.Append(tmp);
} while (first++ < last);
return NS_OK;
}
//----------------------------------------------------------------------
// Implementation of the nsIContent interface text functions
const nsTextFragment *
nsGenericDOMDataNode::GetText()
{
return &mText;
}
uint32_t
nsGenericDOMDataNode::TextLength() const
{
return mText.GetLength();
}
nsresult
nsGenericDOMDataNode::SetText(const char16_t* aBuffer,
uint32_t aLength,
bool aNotify)
{
return SetTextInternal(0, mText.GetLength(), aBuffer, aLength, aNotify);
}
nsresult
nsGenericDOMDataNode::AppendText(const char16_t* aBuffer,
uint32_t aLength,
bool aNotify)
{
return SetTextInternal(mText.GetLength(), 0, aBuffer, aLength, aNotify);
}
bool
nsGenericDOMDataNode::TextIsOnlyWhitespace()
{
// FIXME: should this method take content language into account?