forked from classilla/tenfourfox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFragmentOrElement.cpp
2347 lines (2052 loc) · 68.6 KB
/
FragmentOrElement.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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 all element classes; this provides an implementation
* of DOM Core's nsIDOMElement, implements nsIContent, provides
* utility methods for subclasses, and so forth.
*/
#include "mozilla/ArrayUtils.h"
#include "mozilla/Likely.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/FragmentOrElement.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/EffectSet.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/EventListenerManager.h"
#include "mozilla/EventStates.h"
#include "mozilla/dom/Attr.h"
#include "nsDOMAttributeMap.h"
#include "nsIAtom.h"
#include "mozilla/dom/NodeInfo.h"
#include "mozilla/dom/Event.h"
#include "nsIDocumentInlines.h"
#include "nsIDocumentEncoder.h"
#include "nsIDOMNodeList.h"
#include "nsIContentIterator.h"
#include "nsFocusManager.h"
#include "nsIScriptGlobalObject.h"
#include "nsIURL.h"
#include "nsNetUtil.h"
#include "nsIFrame.h"
#include "nsIAnonymousContentCreator.h"
#include "nsIPresShell.h"
#include "nsPresContext.h"
#include "nsStyleConsts.h"
#include "nsString.h"
#include "nsUnicharUtils.h"
#include "nsIDOMEvent.h"
#include "nsDOMCID.h"
#include "nsIServiceManager.h"
#include "nsIDOMCSSStyleDeclaration.h"
#include "nsDOMCSSAttrDeclaration.h"
#include "nsNameSpaceManager.h"
#include "nsContentList.h"
#include "nsDOMTokenList.h"
#include "nsXBLPrototypeBinding.h"
#include "nsError.h"
#include "nsDOMString.h"
#include "nsIScriptSecurityManager.h"
#include "nsIDOMMutationEvent.h"
#include "mozilla/InternalMutationEvent.h"
#include "mozilla/MouseEvents.h"
#include "nsNodeUtils.h"
#include "nsDocument.h"
#include "nsAttrValueOrString.h"
#ifdef MOZ_XUL
#include "nsXULElement.h"
#endif /* MOZ_XUL */
#include "nsFrameSelection.h"
#ifdef DEBUG
#include "nsRange.h"
#endif
#include "nsBindingManager.h"
#include "nsXBLBinding.h"
#include "nsPIDOMWindow.h"
#include "nsPIBoxObject.h"
#include "nsSVGUtils.h"
#include "nsLayoutUtils.h"
#include "nsGkAtoms.h"
#include "nsContentUtils.h"
#include "nsTextFragment.h"
#include "nsContentCID.h"
#include "nsIDOMEventListener.h"
#include "nsIWebNavigation.h"
#include "nsIBaseWindow.h"
#include "nsIWidget.h"
#include "js/GCAPI.h"
#include "nsNodeInfoManager.h"
#include "nsICategoryManager.h"
#include "nsGenericHTMLElement.h"
#include "nsIEditor.h"
#include "nsIEditorIMESupport.h"
#include "nsContentCreatorFunctions.h"
#include "nsIControllers.h"
#include "nsView.h"
#include "nsViewManager.h"
#include "nsIScrollableFrame.h"
#include "ChildIterator.h"
#include "mozilla/css/StyleRule.h" /* For nsCSSSelectorList */
#include "nsRuleProcessorData.h"
#include "nsTextNode.h"
#include "mozilla/dom/NodeListBinding.h"
#include "mozilla/dom/UndoManager.h"
#ifdef MOZ_XUL
#include "nsIXULDocument.h"
#endif /* MOZ_XUL */
#include "nsCCUncollectableMarker.h"
#include "mozAutoDocUpdate.h"
#include "prprf.h"
#include "nsDOMMutationObserver.h"
#include "nsWrapperCacheInlines.h"
#include "nsCycleCollector.h"
#include "xpcpublic.h"
#include "nsIScriptError.h"
#include "mozilla/Telemetry.h"
#include "mozilla/CORSMode.h"
#include "mozilla/dom/ShadowRoot.h"
#include "mozilla/dom/HTMLTemplateElement.h"
#include "nsStyledElement.h"
#include "nsIContentInlines.h"
#include "nsChildContentList.h"
using namespace mozilla;
using namespace mozilla::dom;
int32_t nsIContent::sTabFocusModel = eTabFocus_any;
bool nsIContent::sTabFocusModelAppliesToXUL = false;
uint64_t nsMutationGuard::sGeneration = 0;
nsIContent*
nsIContent::FindFirstNonChromeOnlyAccessContent() const
{
// This handles also nested native anonymous content.
for (const nsIContent *content = this; content;
content = content->GetBindingParent()) {
if (!content->ChromeOnlyAccess()) {
// Oops, this function signature allows casting const to
// non-const. (Then again, so does GetChildAt(0)->GetParent().)
return const_cast<nsIContent*>(content);
}
}
return nullptr;
}
nsIContent*
nsIContent::GetFlattenedTreeParent() const
{
nsIContent* parent = GetParent();
if (parent && nsContentUtils::HasDistributedChildren(parent) &&
nsContentUtils::IsInSameAnonymousTree(parent, this)) {
// This node is distributed to insertion points, thus we
// need to consult the destination insertion points list to
// figure out where this node was inserted in the flattened tree.
// It may be the case that |parent| distributes its children
// but the child does not match any insertion points, thus
// the flattened tree parent is nullptr.
nsTArray<nsIContent*>* destInsertionPoints = GetExistingDestInsertionPoints();
parent = destInsertionPoints && !destInsertionPoints->IsEmpty() ?
destInsertionPoints->LastElement()->GetParent() : nullptr;
} else if (HasFlag(NODE_MAY_BE_IN_BINDING_MNGR)) {
nsIContent* insertionParent = GetXBLInsertionParent();
if (insertionParent) {
parent = insertionParent;
}
}
// Shadow roots never shows up in the flattened tree. Return the host
// instead.
if (parent && parent->IsInShadowTree()) {
ShadowRoot* parentShadowRoot = ShadowRoot::FromNode(parent);
if (parentShadowRoot) {
return parentShadowRoot->GetHost();
}
}
return parent;
}
nsIContent::IMEState
nsIContent::GetDesiredIMEState()
{
if (!IsEditableInternal()) {
// Check for the special case where we're dealing with elements which don't
// have the editable flag set, but are readwrite (such as text controls).
if (!IsElement() ||
!AsElement()->State().HasState(NS_EVENT_STATE_MOZ_READWRITE)) {
return IMEState(IMEState::DISABLED);
}
}
// NOTE: The content for independent editors (e.g., input[type=text],
// textarea) must override this method, so, we don't need to worry about
// that here.
nsIContent *editableAncestor = GetEditingHost();
// This is in another editable content, use the result of it.
if (editableAncestor && editableAncestor != this) {
return editableAncestor->GetDesiredIMEState();
}
nsIDocument* doc = GetComposedDoc();
if (!doc) {
return IMEState(IMEState::DISABLED);
}
nsIPresShell* ps = doc->GetShell();
if (!ps) {
return IMEState(IMEState::DISABLED);
}
nsPresContext* pc = ps->GetPresContext();
if (!pc) {
return IMEState(IMEState::DISABLED);
}
nsIEditor* editor = nsContentUtils::GetHTMLEditor(pc);
nsCOMPtr<nsIEditorIMESupport> imeEditor = do_QueryInterface(editor);
if (!imeEditor) {
return IMEState(IMEState::DISABLED);
}
IMEState state;
imeEditor->GetPreferredIMEState(&state);
return state;
}
bool
nsIContent::HasIndependentSelection()
{
nsIFrame* frame = GetPrimaryFrame();
return (frame && frame->GetStateBits() & NS_FRAME_INDEPENDENT_SELECTION);
}
dom::Element*
nsIContent::GetEditingHost()
{
// If this isn't editable, return nullptr.
NS_ENSURE_TRUE(IsEditableInternal(), nullptr);
nsIDocument* doc = GetComposedDoc();
NS_ENSURE_TRUE(doc, nullptr);
// If this is in designMode, we should return <body>
if (doc->HasFlag(NODE_IS_EDITABLE) && !IsInShadowTree()) {
return doc->GetBodyElement();
}
nsIContent* content = this;
for (dom::Element* parent = GetParentElement();
parent && parent->HasFlag(NODE_IS_EDITABLE);
parent = content->GetParentElement()) {
content = parent;
}
return content->AsElement();
}
nsresult
nsIContent::LookupNamespaceURIInternal(const nsAString& aNamespacePrefix,
nsAString& aNamespaceURI) const
{
if (aNamespacePrefix.EqualsLiteral("xml")) {
// Special-case for xml prefix
aNamespaceURI.AssignLiteral("http://www.w3.org/XML/1998/namespace");
return NS_OK;
}
if (aNamespacePrefix.EqualsLiteral("xmlns")) {
// Special-case for xmlns prefix
aNamespaceURI.AssignLiteral("http://www.w3.org/2000/xmlns/");
return NS_OK;
}
nsCOMPtr<nsIAtom> name;
if (!aNamespacePrefix.IsEmpty()) {
name = do_GetAtom(aNamespacePrefix);
NS_ENSURE_TRUE(name, NS_ERROR_OUT_OF_MEMORY);
}
else {
name = nsGkAtoms::xmlns;
}
// Trace up the content parent chain looking for the namespace
// declaration that declares aNamespacePrefix.
const nsIContent* content = this;
do {
if (content->GetAttr(kNameSpaceID_XMLNS, name, aNamespaceURI))
return NS_OK;
} while ((content = content->GetParent()));
return NS_ERROR_FAILURE;
}
already_AddRefed<nsIURI>
nsIContent::GetBaseURI(bool aTryUseXHRDocBaseURI) const
{
nsIDocument* doc = OwnerDoc();
// Start with document base
nsCOMPtr<nsIURI> base = doc->GetBaseURI(aTryUseXHRDocBaseURI);
// Collect array of xml:base attribute values up the parent chain. This
// is slightly slower for the case when there are xml:base attributes, but
// faster for the far more common case of there not being any such
// attributes.
// Also check for SVG elements which require special handling
nsAutoTArray<nsString, 5> baseAttrs;
nsString attr;
const nsIContent *elem = this;
do {
// First check for SVG specialness (why is this SVG specific?)
if (elem->IsSVGElement()) {
nsIContent* bindingParent = elem->GetBindingParent();
if (bindingParent) {
nsXBLBinding* binding = bindingParent->GetXBLBinding();
if (binding) {
// XXX sXBL/XBL2 issue
// If this is an anonymous XBL element use the binding
// document for the base URI.
// XXX Will fail with xml:base
base = binding->PrototypeBinding()->DocURI();
break;
}
}
}
nsIURI* explicitBaseURI = elem->GetExplicitBaseURI();
if (explicitBaseURI) {
base = explicitBaseURI;
break;
}
// Otherwise check for xml:base attribute
elem->GetAttr(kNameSpaceID_XML, nsGkAtoms::base, attr);
if (!attr.IsEmpty()) {
baseAttrs.AppendElement(attr);
}
elem = elem->GetParent();
} while(elem);
// Now resolve against all xml:base attrs
for (uint32_t i = baseAttrs.Length() - 1; i != uint32_t(-1); --i) {
nsCOMPtr<nsIURI> newBase;
nsresult rv = NS_NewURI(getter_AddRefs(newBase), baseAttrs[i],
doc->GetDocumentCharacterSet().get(), base);
// Do a security check, almost the same as nsDocument::SetBaseURL()
// Only need to do this on the final uri
if (NS_SUCCEEDED(rv) && i == 0) {
rv = nsContentUtils::GetSecurityManager()->
CheckLoadURIWithPrincipal(NodePrincipal(), newBase,
nsIScriptSecurityManager::STANDARD);
}
if (NS_SUCCEEDED(rv)) {
base.swap(newBase);
}
}
return base.forget();
}
//----------------------------------------------------------------------
static inline JSObject*
GetJSObjectChild(nsWrapperCache* aCache)
{
return aCache->PreservingWrapper() ? aCache->GetWrapperPreserveColor() : nullptr;
}
static bool
NeedsScriptTraverse(nsINode* aNode)
{
return aNode->PreservingWrapper() && aNode->GetWrapperPreserveColor() &&
!aNode->IsBlackAndDoesNotNeedTracing(aNode);
}
//----------------------------------------------------------------------
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsChildContentList)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsChildContentList)
// If nsChildContentList is changed so that any additional fields are
// traversed by the cycle collector, then CAN_SKIP must be updated to
// check that the additional fields are null.
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsChildContentList)
// nsChildContentList only ever has a single child, its wrapper, so if
// the wrapper is black, the list can't be part of a garbage cycle.
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsChildContentList)
return tmp->IsBlack();
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsChildContentList)
return tmp->IsBlackAndDoesNotNeedTracing(tmp);
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
// CanSkipThis returns false to avoid problems with incomplete unlinking.
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsChildContentList)
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
NS_INTERFACE_TABLE_HEAD(nsChildContentList)
NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
NS_INTERFACE_TABLE(nsChildContentList, nsINodeList, nsIDOMNodeList)
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(nsChildContentList)
NS_INTERFACE_MAP_END
JSObject*
nsChildContentList::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto)
{
return NodeListBinding::Wrap(cx, this, aGivenProto);
}
NS_IMETHODIMP
nsChildContentList::GetLength(uint32_t* aLength)
{
*aLength = mNode ? mNode->GetChildCount() : 0;
return NS_OK;
}
NS_IMETHODIMP
nsChildContentList::Item(uint32_t aIndex, nsIDOMNode** aReturn)
{
nsINode* node = Item(aIndex);
if (!node) {
*aReturn = nullptr;
return NS_OK;
}
return CallQueryInterface(node, aReturn);
}
nsIContent*
nsChildContentList::Item(uint32_t aIndex)
{
if (mNode) {
return mNode->GetChildAt(aIndex);
}
return nullptr;
}
int32_t
nsChildContentList::IndexOf(nsIContent* aContent)
{
if (mNode) {
return mNode->IndexOf(aContent);
}
return -1;
}
//----------------------------------------------------------------------
nsIHTMLCollection*
FragmentOrElement::Children()
{
FragmentOrElement::nsDOMSlots *slots = DOMSlots();
if (!slots->mChildrenList) {
slots->mChildrenList = new nsContentList(this, kNameSpaceID_Wildcard,
nsGkAtoms::_asterisk, nsGkAtoms::_asterisk,
false);
}
return slots->mChildrenList;
}
//----------------------------------------------------------------------
NS_IMPL_ISUPPORTS(nsNodeWeakReference,
nsIWeakReference)
nsNodeWeakReference::~nsNodeWeakReference()
{
if (mNode) {
NS_ASSERTION(mNode->Slots()->mWeakReference == this,
"Weak reference has wrong value");
mNode->Slots()->mWeakReference = nullptr;
}
}
NS_IMETHODIMP
nsNodeWeakReference::QueryReferent(const nsIID& aIID, void** aInstancePtr)
{
return mNode ? mNode->QueryInterface(aIID, aInstancePtr) :
NS_ERROR_NULL_POINTER;
}
size_t
nsNodeWeakReference::SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
return aMallocSizeOf(this);
}
NS_IMPL_CYCLE_COLLECTION(nsNodeSupportsWeakRefTearoff, mNode)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsNodeSupportsWeakRefTearoff)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END_AGGREGATED(mNode)
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsNodeSupportsWeakRefTearoff)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsNodeSupportsWeakRefTearoff)
NS_IMETHODIMP
nsNodeSupportsWeakRefTearoff::GetWeakReference(nsIWeakReference** aInstancePtr)
{
nsINode::nsSlots* slots = mNode->Slots();
if (!slots->mWeakReference) {
slots->mWeakReference = new nsNodeWeakReference(mNode);
}
NS_ADDREF(*aInstancePtr = slots->mWeakReference);
return NS_OK;
}
//----------------------------------------------------------------------
FragmentOrElement::nsDOMSlots::nsDOMSlots()
: nsINode::nsSlots(),
mDataset(nullptr),
mUndoManager(nullptr),
mBindingParent(nullptr)
{
}
FragmentOrElement::nsDOMSlots::~nsDOMSlots()
{
if (mAttributeMap) {
mAttributeMap->DropReference();
}
}
void
FragmentOrElement::nsDOMSlots::Traverse(nsCycleCollectionTraversalCallback &cb, bool aIsXUL)
{
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mStyle");
cb.NoteXPCOMChild(mStyle.get());
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mSMILOverrideStyle");
cb.NoteXPCOMChild(mSMILOverrideStyle.get());
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mAttributeMap");
cb.NoteXPCOMChild(mAttributeMap.get());
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mUndoManager");
cb.NoteXPCOMChild(mUndoManager.get());
if (aIsXUL) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mControllers");
cb.NoteXPCOMChild(mControllers);
}
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mXBLBinding");
cb.NoteNativeChild(mXBLBinding, NS_CYCLE_COLLECTION_PARTICIPANT(nsXBLBinding));
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mXBLInsertionParent");
cb.NoteXPCOMChild(mXBLInsertionParent.get());
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mShadowRoot");
cb.NoteXPCOMChild(NS_ISUPPORTS_CAST(nsIContent*, mShadowRoot));
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mContainingShadow");
cb.NoteXPCOMChild(NS_ISUPPORTS_CAST(nsIContent*, mContainingShadow));
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mChildrenList");
cb.NoteXPCOMChild(NS_ISUPPORTS_CAST(nsIDOMNodeList*, mChildrenList));
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mClassList");
cb.NoteXPCOMChild(mClassList.get());
if (mCustomElementData) {
for (uint32_t i = 0; i < mCustomElementData->mCallbackQueue.Length(); i++) {
mCustomElementData->mCallbackQueue[i]->Traverse(cb);
}
}
}
void
FragmentOrElement::nsDOMSlots::Unlink(bool aIsXUL)
{
mStyle = nullptr;
mSMILOverrideStyle = nullptr;
if (mAttributeMap) {
mAttributeMap->DropReference();
mAttributeMap = nullptr;
}
if (aIsXUL)
NS_IF_RELEASE(mControllers);
mXBLBinding = nullptr;
mXBLInsertionParent = nullptr;
mShadowRoot = nullptr;
mContainingShadow = nullptr;
mChildrenList = nullptr;
mUndoManager = nullptr;
mCustomElementData = nullptr;
mClassList = nullptr;
}
size_t
FragmentOrElement::nsDOMSlots::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
{
size_t n = aMallocSizeOf(this);
if (mAttributeMap) {
n += mAttributeMap->SizeOfIncludingThis(aMallocSizeOf);
}
// Measurement of the following members may be added later if DMD finds it is
// worthwhile:
// - Superclass members (nsINode::nsSlots)
// - mStyle
// - mDataSet
// - mSMILOverrideStyle
// - mSMILOverrideStyleDeclaration
// - mChildrenList
// - mClassList
// The following members are not measured:
// - mBindingParent / mControllers: because they're non-owning
return n;
}
FragmentOrElement::FragmentOrElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
: nsIContent(aNodeInfo)
{
}
FragmentOrElement::FragmentOrElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: nsIContent(aNodeInfo)
{
}
FragmentOrElement::~FragmentOrElement()
{
NS_PRECONDITION(!IsInDoc(),
"Please remove this from the document properly");
if (GetParent()) {
NS_RELEASE(mParent);
}
}
already_AddRefed<nsINodeList>
FragmentOrElement::GetChildren(uint32_t aFilter)
{
RefPtr<nsSimpleContentList> list = new nsSimpleContentList(this);
AllChildrenIterator iter(this, aFilter);
while (nsIContent* kid = iter.GetNextChild()) {
list->AppendElement(kid);
}
return list.forget();
}
static nsIContent*
FindChromeAccessOnlySubtreeOwner(nsIContent* aContent)
{
if (aContent->ChromeOnlyAccess()) {
bool chromeAccessOnly = false;
while (aContent && !chromeAccessOnly) {
chromeAccessOnly = aContent->IsRootOfChromeAccessOnlySubtree();
aContent = aContent->GetParent();
}
}
return aContent;
}
nsresult
nsIContent::PreHandleEvent(EventChainPreVisitor& aVisitor)
{
//FIXME! Document how this event retargeting works, Bug 329124.
aVisitor.mCanHandle = true;
aVisitor.mMayHaveListenerManager = HasListenerManager();
// Don't propagate mouseover and mouseout events when mouse is moving
// inside chrome access only content.
bool isAnonForEvents = IsRootOfChromeAccessOnlySubtree();
if ((aVisitor.mEvent->mMessage == eMouseOver ||
aVisitor.mEvent->mMessage == eMouseOut ||
aVisitor.mEvent->mMessage == ePointerOver ||
aVisitor.mEvent->mMessage == ePointerOut) &&
// Check if we should stop event propagation when event has just been
// dispatched or when we're about to propagate from
// chrome access only subtree or if we are about to propagate out of
// a shadow root to a shadow root host.
((this == aVisitor.mEvent->originalTarget &&
!ChromeOnlyAccess()) || isAnonForEvents || GetShadowRoot())) {
nsCOMPtr<nsIContent> relatedTarget =
do_QueryInterface(aVisitor.mEvent->AsMouseEvent()->relatedTarget);
if (relatedTarget &&
relatedTarget->OwnerDoc() == OwnerDoc()) {
// In the web components case, we may need to stop propagation of events
// at shadow root host.
if (GetShadowRoot()) {
nsIContent* adjustedTarget =
Event::GetShadowRelatedTarget(this, relatedTarget);
if (this == adjustedTarget) {
aVisitor.mParentTarget = nullptr;
aVisitor.mCanHandle = false;
return NS_OK;
}
}
// If current target is anonymous for events or we know that related
// target is descendant of an element which is anonymous for events,
// we may want to stop event propagation.
// If this is the original target, aVisitor.mRelatedTargetIsInAnon
// must be updated.
if (isAnonForEvents || aVisitor.mRelatedTargetIsInAnon ||
(aVisitor.mEvent->originalTarget == this &&
(aVisitor.mRelatedTargetIsInAnon =
relatedTarget->ChromeOnlyAccess()))) {
nsIContent* anonOwner = FindChromeAccessOnlySubtreeOwner(this);
if (anonOwner) {
nsIContent* anonOwnerRelated =
FindChromeAccessOnlySubtreeOwner(relatedTarget);
if (anonOwnerRelated) {
// Note, anonOwnerRelated may still be inside some other
// native anonymous subtree. The case where anonOwner is still
// inside native anonymous subtree will be handled when event
// propagates up in the DOM tree.
while (anonOwner != anonOwnerRelated &&
anonOwnerRelated->ChromeOnlyAccess()) {
anonOwnerRelated = FindChromeAccessOnlySubtreeOwner(anonOwnerRelated);
}
if (anonOwner == anonOwnerRelated) {
#ifdef DEBUG_smaug
nsCOMPtr<nsIContent> originalTarget =
do_QueryInterface(aVisitor.mEvent->originalTarget);
nsAutoString ot, ct, rt;
if (originalTarget) {
originalTarget->NodeInfo()->NameAtom()->ToString(ot);
}
NodeInfo()->NameAtom()->ToString(ct);
relatedTarget->NodeInfo()->NameAtom()->ToString(rt);
printf("Stopping %s propagation:"
"\n\toriginalTarget=%s \n\tcurrentTarget=%s %s"
"\n\trelatedTarget=%s %s \n%s",
(aVisitor.mEvent->mMessage == eMouseOver)
? "mouseover" : "mouseout",
NS_ConvertUTF16toUTF8(ot).get(),
NS_ConvertUTF16toUTF8(ct).get(),
isAnonForEvents
? "(is native anonymous)"
: (ChromeOnlyAccess()
? "(is in native anonymous subtree)" : ""),
NS_ConvertUTF16toUTF8(rt).get(),
relatedTarget->ChromeOnlyAccess()
? "(is in native anonymous subtree)" : "",
(originalTarget &&
relatedTarget->FindFirstNonChromeOnlyAccessContent() ==
originalTarget->FindFirstNonChromeOnlyAccessContent())
? "" : "Wrong event propagation!?!\n");
#endif
aVisitor.mParentTarget = nullptr;
// Event should not propagate to non-anon content.
aVisitor.mCanHandle = isAnonForEvents;
return NS_OK;
}
}
}
}
}
}
nsIContent* parent = GetParent();
// Web components have a special event chain that need to account
// for destination insertion points where nodes have been distributed.
nsTArray<nsIContent*>* destPoints = GetExistingDestInsertionPoints();
if (destPoints && !destPoints->IsEmpty()) {
// Push destination insertion points to aVisitor.mDestInsertionPoints
// excluding shadow insertion points.
bool didPushNonShadowInsertionPoint = false;
for (uint32_t i = 0; i < destPoints->Length(); i++) {
nsIContent* point = destPoints->ElementAt(i);
if (!ShadowRoot::IsShadowInsertionPoint(point)) {
aVisitor.mDestInsertionPoints.AppendElement(point);
didPushNonShadowInsertionPoint = true;
}
}
// Next node in the event path is the final destination
// (non-shadow) insertion point that was pushed.
if (didPushNonShadowInsertionPoint) {
parent = aVisitor.mDestInsertionPoints.LastElement();
aVisitor.mDestInsertionPoints.SetLength(
aVisitor.mDestInsertionPoints.Length() - 1);
}
}
ShadowRoot* thisShadowRoot = ShadowRoot::FromNode(this);
if (thisShadowRoot) {
// The following events must always be stopped at the root node of the node tree:
// abort
// error
// select
// change
// load
// reset
// resize
// scroll
// selectstart
bool stopEvent = false;
switch (aVisitor.mEvent->mMessage) {
case eImageAbort:
case eLoadError:
case eFormSelect:
case eFormChange:
case eLoad:
case eFormReset:
case eResize:
case eScroll:
case eSelectStart:
stopEvent = true;
break;
case eUnidentifiedEvent:
if (aVisitor.mDOMEvent) {
nsAutoString eventType;
aVisitor.mDOMEvent->GetType(eventType);
if (eventType.EqualsLiteral("abort") ||
eventType.EqualsLiteral("error") ||
eventType.EqualsLiteral("select") ||
eventType.EqualsLiteral("change") ||
eventType.EqualsLiteral("load") ||
eventType.EqualsLiteral("reset") ||
eventType.EqualsLiteral("resize") ||
eventType.EqualsLiteral("scroll")) {
stopEvent = true;
}
}
break;
default:
break;
}
if (stopEvent) {
// If we do stop propagation, we still want to propagate
// the event to chrome (nsPIDOMWindow::GetParentTarget()).
// The load event is special in that we don't ever propagate it
// to chrome.
nsCOMPtr<nsPIDOMWindow> win = OwnerDoc()->GetWindow();
EventTarget* parentTarget = win && aVisitor.mEvent->mMessage != eLoad
? win->GetParentTarget() : nullptr;
aVisitor.mParentTarget = parentTarget;
return NS_OK;
}
if (!aVisitor.mDestInsertionPoints.IsEmpty()) {
parent = aVisitor.mDestInsertionPoints.LastElement();
aVisitor.mDestInsertionPoints.SetLength(
aVisitor.mDestInsertionPoints.Length() - 1);
} else {
// The pool host for the youngest shadow root is shadow DOM host,
// for older shadow roots, it is the shadow insertion point
// where the shadow root is projected, nullptr if none exists.
parent = thisShadowRoot->GetPoolHost();
}
}
// Event may need to be retargeted if this is the root of a native
// anonymous content subtree or event is dispatched somewhere inside XBL.
if (isAnonForEvents) {
#ifdef DEBUG
// If a DOM event is explicitly dispatched using node.dispatchEvent(), then
// all the events are allowed even in the native anonymous content..
nsCOMPtr<nsIContent> t = do_QueryInterface(aVisitor.mEvent->originalTarget);
NS_ASSERTION(!t || !t->ChromeOnlyAccess() ||
aVisitor.mEvent->mClass != eMutationEventClass ||
aVisitor.mDOMEvent,
"Mutation event dispatched in native anonymous content!?!");
#endif
aVisitor.mEventTargetAtParent = parent;
} else if (parent && aVisitor.mOriginalTargetIsInAnon) {
nsCOMPtr<nsIContent> content(do_QueryInterface(aVisitor.mEvent->target));
if (content && content->GetBindingParent() == parent) {
aVisitor.mEventTargetAtParent = parent;
}
}
// check for an anonymous parent
// XXX XBL2/sXBL issue
if (HasFlag(NODE_MAY_BE_IN_BINDING_MNGR)) {
nsIContent* insertionParent = GetXBLInsertionParent();
NS_ASSERTION(!(aVisitor.mEventTargetAtParent && insertionParent &&
aVisitor.mEventTargetAtParent != insertionParent),
"Retargeting and having insertion parent!");
if (insertionParent) {
parent = insertionParent;
}
}
if (parent) {
aVisitor.mParentTarget = parent;
} else {
aVisitor.mParentTarget = GetComposedDoc();
}
return NS_OK;
}
bool
nsIContent::GetAttr(int32_t aNameSpaceID, nsIAtom* aName,
nsAString& aResult) const
{
if (IsElement()) {
return AsElement()->GetAttr(aNameSpaceID, aName, aResult);
}
aResult.Truncate();
return false;
}
bool
nsIContent::HasAttr(int32_t aNameSpaceID, nsIAtom* aName) const
{
return IsElement() && AsElement()->HasAttr(aNameSpaceID, aName);
}
bool
nsIContent::AttrValueIs(int32_t aNameSpaceID,
nsIAtom* aName,
const nsAString& aValue,
nsCaseTreatment aCaseSensitive) const
{
return IsElement() &&
AsElement()->AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
}
bool
nsIContent::AttrValueIs(int32_t aNameSpaceID,
nsIAtom* aName,
nsIAtom* aValue,
nsCaseTreatment aCaseSensitive) const
{
return IsElement() &&
AsElement()->AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
}
bool
nsIContent::IsFocusable(int32_t* aTabIndex, bool aWithMouse)
{
bool focusable = IsFocusableInternal(aTabIndex, aWithMouse);
// Ensure that the return value and aTabIndex are consistent in the case
// we're in userfocusignored context.
if (focusable || (aTabIndex && *aTabIndex != -1)) {
if (nsContentUtils::IsUserFocusIgnored(this)) {
if (aTabIndex) {
*aTabIndex = -1;
}
return false;
}
return focusable;
}
return false;
}
bool
nsIContent::IsFocusableInternal(int32_t* aTabIndex, bool aWithMouse)
{
if (aTabIndex) {
*aTabIndex = -1; // Default, not tabbable
}
return false;
}
NS_IMETHODIMP
FragmentOrElement::WalkContentStyleRules(nsRuleWalker* aRuleWalker)
{
return NS_OK;
}
bool
FragmentOrElement::IsLink(nsIURI** aURI) const
{
*aURI = nullptr;
return false;
}
nsIContent*
FragmentOrElement::GetBindingParent() const
{
nsDOMSlots *slots = GetExistingDOMSlots();
if (slots) {
return slots->mBindingParent;
}
return nullptr;
}
nsXBLBinding*
FragmentOrElement::GetXBLBinding() const
{
if (HasFlag(NODE_MAY_BE_IN_BINDING_MNGR)) {
nsDOMSlots *slots = GetExistingDOMSlots();
if (slots) {
return slots->mXBLBinding;
}
}