This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnsXULElement.cpp
2241 lines (1923 loc) · 74 KB
/
nsXULElement.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: 4; 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/. */
#include "nsCOMPtr.h"
#include "nsDOMCID.h"
#include "nsError.h"
#include "nsDOMString.h"
#include "nsAtom.h"
#include "nsIBaseWindow.h"
#include "nsIDOMEventListener.h"
#include "nsIDOMXULCommandDispatcher.h"
#include "nsIDOMXULSelectCntrlItemEl.h"
#include "mozilla/dom/BindContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/EventListenerManager.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/EventStates.h"
#include "mozilla/DeclarationBlock.h"
#include "mozilla/PresShell.h"
#include "js/CompilationAndEvaluation.h"
#include "js/SourceText.h"
#include "nsFocusManager.h"
#include "nsHTMLStyleSheet.h"
#include "nsNameSpaceManager.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include "nsIPrincipal.h"
#include "nsIScriptContext.h"
#include "nsIScriptError.h"
#include "nsIScriptSecurityManager.h"
#include "nsIServiceManager.h"
#include "nsIURL.h"
#include "nsViewManager.h"
#include "nsIWidget.h"
#include "nsLayoutCID.h"
#include "nsContentCID.h"
#include "mozilla/dom/Event.h"
#include "nsStyleConsts.h"
#include "nsString.h"
#include "nsXULControllers.h"
#include "XULDocument.h"
#include "nsXULPopupListener.h"
#include "nsContentUtils.h"
#include "nsContentList.h"
#include "mozilla/InternalMutationEvent.h"
#include "mozilla/MouseEvents.h"
#include "nsPIDOMWindow.h"
#include "nsJSPrincipals.h"
#include "nsDOMAttributeMap.h"
#include "nsGkAtoms.h"
#include "nsNodeUtils.h"
#include "nsFrameLoader.h"
#include "mozilla/Logging.h"
#include "nsIControllers.h"
#include "nsAttrValueOrString.h"
#include "nsAttrValueInlines.h"
#include "mozilla/Attributes.h"
#include "nsIController.h"
#include "nsQueryObject.h"
#include <algorithm>
#include "nsIDOMChromeWindow.h"
#include "nsReadableUtils.h"
#include "nsIFrame.h"
#include "nsNodeInfoManager.h"
#include "nsXBLBinding.h"
#include "nsXULTooltipListener.h"
#include "mozilla/EventDispatcher.h"
#include "mozAutoDocUpdate.h"
#include "nsCCUncollectableMarker.h"
#include "nsICSSDeclaration.h"
#include "nsLayoutUtils.h"
#include "XULFrameElement.h"
#include "XULMenuElement.h"
#include "XULPopupElement.h"
#include "XULTreeElement.h"
#include "mozilla/dom/XULElementBinding.h"
#include "mozilla/dom/XULBroadcastManager.h"
#include "mozilla/dom/MouseEventBinding.h"
#include "mozilla/dom/MutationEventBinding.h"
#include "mozilla/dom/XULCommandEvent.h"
using namespace mozilla;
using namespace mozilla::dom;
#ifdef XUL_PROTOTYPE_ATTRIBUTE_METERING
uint32_t nsXULPrototypeAttribute::gNumElements;
uint32_t nsXULPrototypeAttribute::gNumAttributes;
uint32_t nsXULPrototypeAttribute::gNumCacheTests;
uint32_t nsXULPrototypeAttribute::gNumCacheHits;
uint32_t nsXULPrototypeAttribute::gNumCacheSets;
uint32_t nsXULPrototypeAttribute::gNumCacheFills;
#endif
#define NS_DISPATCH_XUL_COMMAND (1 << 0)
//----------------------------------------------------------------------
// nsXULElement
//
nsXULElement::nsXULElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: nsStyledElement(std::move(aNodeInfo)), mBindingParent(nullptr) {
XUL_PROTOTYPE_ATTRIBUTE_METER(gNumElements);
// We may be READWRITE by default; check.
if (IsReadWriteTextElement()) {
AddStatesSilently(NS_EVENT_STATE_MOZ_READWRITE);
RemoveStatesSilently(NS_EVENT_STATE_MOZ_READONLY);
}
}
nsXULElement::~nsXULElement() {}
void nsXULElement::MaybeUpdatePrivateLifetime() {
if (AttrValueIs(kNameSpaceID_None, nsGkAtoms::windowtype,
NS_LITERAL_STRING("navigator:browser"), eCaseMatters)) {
return;
}
nsPIDOMWindowOuter* win = OwnerDoc()->GetWindow();
nsCOMPtr<nsIDocShell> docShell = win ? win->GetDocShell() : nullptr;
if (docShell) {
docShell->SetAffectPrivateSessionLifetime(false);
}
}
/* static */
nsXULElement* NS_NewBasicXULElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) {
return new nsXULElement(std::move(aNodeInfo));
}
/* static */
nsXULElement* nsXULElement::Construct(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) {
RefPtr<mozilla::dom::NodeInfo> nodeInfo = aNodeInfo;
if (nodeInfo->Equals(nsGkAtoms::label) ||
nodeInfo->Equals(nsGkAtoms::description)) {
return new XULTextElement(nodeInfo.forget());
}
if (nodeInfo->Equals(nsGkAtoms::menupopup) ||
nodeInfo->Equals(nsGkAtoms::popup) ||
nodeInfo->Equals(nsGkAtoms::panel)) {
return NS_NewXULPopupElement(nodeInfo.forget());
}
if (nodeInfo->Equals(nsGkAtoms::tooltip)) {
return NS_NewXULTooltipElement(nodeInfo.forget());
}
if (nodeInfo->Equals(nsGkAtoms::iframe) ||
nodeInfo->Equals(nsGkAtoms::browser) ||
nodeInfo->Equals(nsGkAtoms::editor)) {
return new XULFrameElement(nodeInfo.forget());
}
if (nodeInfo->Equals(nsGkAtoms::menu) ||
nodeInfo->Equals(nsGkAtoms::menulist)) {
return new XULMenuElement(nodeInfo.forget());
}
if (nodeInfo->Equals(nsGkAtoms::tree)) {
return new XULTreeElement(nodeInfo.forget());
}
return NS_NewBasicXULElement(nodeInfo.forget());
}
/* static */
already_AddRefed<nsXULElement> nsXULElement::CreateFromPrototype(
nsXULPrototypeElement* aPrototype, mozilla::dom::NodeInfo* aNodeInfo,
bool aIsScriptable, bool aIsRoot) {
RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
nsCOMPtr<Element> baseElement;
NS_NewXULElement(getter_AddRefs(baseElement), ni.forget(),
dom::FROM_PARSER_NETWORK, aPrototype->mIsAtom);
if (baseElement) {
nsXULElement* element = FromNode(baseElement);
if (aPrototype->mHasIdAttribute) {
element->SetHasID();
}
if (aPrototype->mHasClassAttribute) {
element->SetMayHaveClass();
}
if (aPrototype->mHasStyleAttribute) {
element->SetMayHaveStyle();
}
element->MakeHeavyweight(aPrototype);
if (aIsScriptable) {
// Check each attribute on the prototype to see if we need to do
// any additional processing and hookup that would otherwise be
// done 'automagically' by SetAttr().
for (uint32_t i = 0; i < aPrototype->mNumAttributes; ++i) {
element->AddListenerFor(aPrototype->mAttributes[i].mName);
}
}
if (aIsRoot && aPrototype->mNodeInfo->Equals(nsGkAtoms::window)) {
for (uint32_t i = 0; i < aPrototype->mNumAttributes; ++i) {
if (aPrototype->mAttributes[i].mName.Equals(nsGkAtoms::windowtype)) {
element->MaybeUpdatePrivateLifetime();
}
}
}
return baseElement.forget().downcast<nsXULElement>();
}
return nullptr;
}
nsresult nsXULElement::CreateFromPrototype(nsXULPrototypeElement* aPrototype,
Document* aDocument,
bool aIsScriptable, bool aIsRoot,
Element** aResult) {
// Create an nsXULElement from a prototype
MOZ_ASSERT(aPrototype != nullptr, "null ptr");
if (!aPrototype) return NS_ERROR_NULL_POINTER;
MOZ_ASSERT(aResult != nullptr, "null ptr");
if (!aResult) return NS_ERROR_NULL_POINTER;
RefPtr<mozilla::dom::NodeInfo> nodeInfo;
if (aDocument) {
mozilla::dom::NodeInfo* ni = aPrototype->mNodeInfo;
nodeInfo = aDocument->NodeInfoManager()->GetNodeInfo(
ni->NameAtom(), ni->GetPrefixAtom(), ni->NamespaceID(), ELEMENT_NODE);
} else {
nodeInfo = aPrototype->mNodeInfo;
}
RefPtr<nsXULElement> element =
CreateFromPrototype(aPrototype, nodeInfo, aIsScriptable, aIsRoot);
element.forget(aResult);
return NS_OK;
}
nsresult NS_NewXULElement(Element** aResult,
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
FromParser aFromParser, nsAtom* aIsAtom,
mozilla::dom::CustomElementDefinition* aDefinition) {
RefPtr<mozilla::dom::NodeInfo> nodeInfo = aNodeInfo;
MOZ_ASSERT(nodeInfo, "need nodeinfo for non-proto Create");
NS_ASSERTION(
nodeInfo->NamespaceEquals(kNameSpaceID_XUL),
"Trying to create XUL elements that don't have the XUL namespace");
Document* doc = nodeInfo->GetDocument();
if (doc && !doc->AllowXULXBL()) {
return NS_ERROR_NOT_AVAILABLE;
}
return nsContentUtils::NewXULOrHTMLElement(aResult, nodeInfo, aFromParser,
aIsAtom, aDefinition);
}
void NS_TrustedNewXULElement(
Element** aResult, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) {
RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
MOZ_ASSERT(ni, "need nodeinfo for non-proto Create");
// Create an nsXULElement with the specified namespace and tag.
NS_ADDREF(*aResult = nsXULElement::Construct(ni.forget()));
}
//----------------------------------------------------------------------
// nsISupports interface
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsXULElement, nsStyledElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBindingParent);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsXULElement, nsStyledElement)
// Why aren't we unlinking the prototype?
tmp->ClearHasID();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBindingParent);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_ADDREF_INHERITED(nsXULElement, nsStyledElement)
NS_IMPL_RELEASE_INHERITED(nsXULElement, nsStyledElement)
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsXULElement)
NS_ELEMENT_INTERFACE_TABLE_TO_MAP_SEGUE
NS_INTERFACE_MAP_END_INHERITING(nsStyledElement)
//----------------------------------------------------------------------
// nsINode interface
nsresult nsXULElement::Clone(mozilla::dom::NodeInfo* aNodeInfo,
nsINode** aResult) const {
*aResult = nullptr;
RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
RefPtr<nsXULElement> element = Construct(ni.forget());
nsresult rv = element->mAttrs.EnsureCapacityToClone(mAttrs);
NS_ENSURE_SUCCESS(rv, rv);
// Note that we're _not_ copying mControllers.
uint32_t count = mAttrs.AttrCount();
rv = NS_OK;
for (uint32_t i = 0; i < count; ++i) {
const nsAttrName* originalName = mAttrs.AttrNameAt(i);
const nsAttrValue* originalValue = mAttrs.AttrAt(i);
nsAttrValue attrValue;
// Style rules need to be cloned.
if (originalValue->Type() == nsAttrValue::eCSSDeclaration) {
DeclarationBlock* decl = originalValue->GetCSSDeclarationValue();
RefPtr<DeclarationBlock> declClone = decl->Clone();
nsString stringValue;
originalValue->ToString(stringValue);
attrValue.SetTo(declClone.forget(), &stringValue);
} else {
attrValue.SetTo(*originalValue);
}
bool oldValueSet;
if (originalName->IsAtom()) {
rv = element->mAttrs.SetAndSwapAttr(originalName->Atom(), attrValue,
&oldValueSet);
} else {
rv = element->mAttrs.SetAndSwapAttr(originalName->NodeInfo(), attrValue,
&oldValueSet);
}
NS_ENSURE_SUCCESS(rv, rv);
element->AddListenerFor(*originalName);
if (originalName->Equals(nsGkAtoms::id) &&
!originalValue->IsEmptyString()) {
element->SetHasID();
}
if (originalName->Equals(nsGkAtoms::_class)) {
element->SetMayHaveClass();
}
if (originalName->Equals(nsGkAtoms::style)) {
element->SetMayHaveStyle();
}
}
element.forget(aResult);
return rv;
}
//----------------------------------------------------------------------
EventListenerManager* nsXULElement::GetEventListenerManagerForAttr(
nsAtom* aAttrName, bool* aDefer) {
// XXXbz sXBL/XBL2 issue: should we instead use GetComposedDoc()
// here, override BindToTree for those classes and munge event
// listeners there?
Document* doc = OwnerDoc();
nsPIDOMWindowInner* window;
Element* root = doc->GetRootElement();
if ((!root || root == this) && (window = doc->GetInnerWindow())) {
nsCOMPtr<EventTarget> piTarget = do_QueryInterface(window);
*aDefer = false;
return piTarget->GetOrCreateListenerManager();
}
return nsStyledElement::GetEventListenerManagerForAttr(aAttrName, aDefer);
}
// returns true if the element is not a list
static bool IsNonList(mozilla::dom::NodeInfo* aNodeInfo) {
return !aNodeInfo->Equals(nsGkAtoms::tree) &&
!aNodeInfo->Equals(nsGkAtoms::richlistbox);
}
bool nsXULElement::IsFocusableInternal(int32_t* aTabIndex, bool aWithMouse) {
/*
* Returns true if an element may be focused, and false otherwise. The inout
* argument aTabIndex will be set to the tab order index to be used; -1 for
* elements that should not be part of the tab order and a greater value to
* indicate its tab order.
*
* Confusingly, the supplied value for the aTabIndex argument may indicate
* whether the element may be focused as a result of the -moz-user-focus
* property, where -1 means no and 0 means yes.
*
* For controls, the element cannot be focused and is not part of the tab
* order if it is disabled.
*
* -moz-user-focus is overridden if a tabindex (even -1) is specified.
*
* Specifically, the behaviour for all XUL elements is as follows:
* *aTabIndex = -1 no tabindex Not focusable or tabbable
* *aTabIndex = -1 tabindex="-1" Focusable but not tabbable
* *aTabIndex = -1 tabindex=">=0" Focusable and tabbable
* *aTabIndex >= 0 no tabindex Focusable and tabbable
* *aTabIndex >= 0 tabindex="-1" Focusable but not tabbable
* *aTabIndex >= 0 tabindex=">=0" Focusable and tabbable
*
* If aTabIndex is null, then the tabindex is not computed, and
* true is returned for non-disabled controls and false otherwise.
*/
// elements are not focusable by default
bool shouldFocus = false;
#ifdef XP_MACOSX
// on Mac, mouse interactions only focus the element if it's a list,
// or if it's a remote target, since the remote target must handle
// the focus.
if (aWithMouse && IsNonList(mNodeInfo) &&
!EventStateManager::IsRemoteTarget(this)) {
return false;
}
#endif
nsCOMPtr<nsIDOMXULControlElement> xulControl = AsXULControl();
if (xulControl) {
// a disabled element cannot be focused and is not part of the tab order
bool disabled;
xulControl->GetDisabled(&disabled);
if (disabled) {
if (aTabIndex) *aTabIndex = -1;
return false;
}
shouldFocus = true;
}
if (aTabIndex) {
if (HasAttr(kNameSpaceID_None, nsGkAtoms::tabindex)) {
// The tabindex attribute was specified, so the element becomes
// focusable.
shouldFocus = true;
*aTabIndex = TabIndex();
} else {
// otherwise, if there is no tabindex attribute, just use the value of
// *aTabIndex to indicate focusability. Reset any supplied tabindex to 0.
shouldFocus = *aTabIndex >= 0;
if (shouldFocus) {
*aTabIndex = 0;
}
}
if (xulControl && shouldFocus && sTabFocusModelAppliesToXUL &&
!(sTabFocusModel & eTabFocus_formElementsMask)) {
// By default, the tab focus model doesn't apply to xul element on any
// system but OS X. on OS X we're following it for UI elements (XUL) as
// sTabFocusModel is based on "Full Keyboard Access" system setting (see
// mac/nsILookAndFeel). both textboxes and list elements (i.e. trees and
// list) should always be focusable (textboxes are handled as html:input)
// For compatibility, we only do this for controls, otherwise elements
// like <browser> cannot take this focus.
if (IsNonList(mNodeInfo)) {
*aTabIndex = -1;
}
}
}
return shouldFocus;
}
int32_t nsXULElement::ScreenX() {
nsIFrame* frame = GetPrimaryFrame(FlushType::Layout);
return frame ? frame->GetScreenRect().x : 0;
}
int32_t nsXULElement::ScreenY() {
nsIFrame* frame = GetPrimaryFrame(FlushType::Layout);
return frame ? frame->GetScreenRect().y : 0;
}
bool nsXULElement::HasMenu() {
nsMenuFrame* menu = do_QueryFrame(GetPrimaryFrame());
return menu != nullptr;
}
void nsXULElement::OpenMenu(bool aOpenFlag) {
nsMenuFrame* menu = do_QueryFrame(GetPrimaryFrame(FlushType::Frames));
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm) {
if (aOpenFlag) {
// Nothing will happen if this element isn't a menu.
pm->ShowMenu(this, false, false);
} else if (menu) {
nsMenuPopupFrame* popupFrame = menu->GetPopup();
if (popupFrame) {
pm->HidePopup(popupFrame->GetContent(), false, true, false, false);
}
}
}
}
bool nsXULElement::PerformAccesskey(bool aKeyCausesActivation,
bool aIsTrustedEvent) {
RefPtr<Element> content(this);
if (IsXULElement(nsGkAtoms::label)) {
nsAutoString control;
GetAttr(kNameSpaceID_None, nsGkAtoms::control, control);
if (control.IsEmpty()) {
return false;
}
// XXXsmaug Should we use ShadowRoot::GetElementById in case
// content is in Shadow DOM?
nsCOMPtr<Document> document = content->GetUncomposedDoc();
if (!document) {
return false;
}
content = document->GetElementById(control);
if (!content) {
return false;
}
}
nsIFrame* frame = content->GetPrimaryFrame();
if (!frame || !frame->IsVisibleConsideringAncestors()) {
return false;
}
bool focused = false;
nsXULElement* elm = FromNode(content);
if (elm) {
// Define behavior for each type of XUL element.
if (!content->IsXULElement(nsGkAtoms::toolbarbutton)) {
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm) {
nsCOMPtr<Element> elementToFocus;
// for radio buttons, focus the radiogroup instead
if (content->IsXULElement(nsGkAtoms::radio)) {
nsCOMPtr<nsIDOMXULSelectControlItemElement> controlItem =
content->AsXULSelectControlItem();
if (controlItem) {
bool disabled;
controlItem->GetDisabled(&disabled);
if (!disabled) {
controlItem->GetControl(getter_AddRefs(elementToFocus));
}
}
} else {
elementToFocus = content;
}
if (elementToFocus) {
fm->SetFocus(elementToFocus, nsIFocusManager::FLAG_BYKEY);
// Return true if the element became focused.
nsPIDOMWindowOuter* window = OwnerDoc()->GetWindow();
focused = (window && window->GetFocusedElement());
}
}
}
if (aKeyCausesActivation &&
!content->IsAnyOfXULElements(nsGkAtoms::textbox, nsGkAtoms::menulist)) {
elm->ClickWithInputSource(MouseEvent_Binding::MOZ_SOURCE_KEYBOARD,
aIsTrustedEvent);
}
} else {
return content->PerformAccesskey(aKeyCausesActivation, aIsTrustedEvent);
}
return focused;
}
//----------------------------------------------------------------------
void nsXULElement::AddListenerFor(const nsAttrName& aName) {
// If appropriate, add a popup listener and/or compile the event
// handler. Called when we change the element's document, create a
// new element, change an attribute's value, etc.
// Eventlistenener-attributes are always in the null namespace
if (aName.IsAtom()) {
nsAtom* attr = aName.Atom();
MaybeAddPopupListener(attr);
if (nsContentUtils::IsEventAttributeName(attr, EventNameType_XUL)) {
nsAutoString value;
GetAttr(kNameSpaceID_None, attr, value);
SetEventHandler(attr, value, true);
}
}
}
void nsXULElement::MaybeAddPopupListener(nsAtom* aLocalName) {
// If appropriate, add a popup listener. Called when we change the
// element's document, create a new element, change an attribute's
// value, etc.
if (aLocalName == nsGkAtoms::menu || aLocalName == nsGkAtoms::contextmenu ||
// XXXdwh popup and context are deprecated
aLocalName == nsGkAtoms::popup || aLocalName == nsGkAtoms::context) {
AddPopupListener(aLocalName);
}
}
//----------------------------------------------------------------------
//
// nsIContent interface
//
void nsXULElement::UpdateEditableState(bool aNotify) {
// Don't call through to Element here because the things
// it does don't work for cases when we're an editable control.
nsIContent* parent = GetParent();
SetEditableFlag(parent && parent->HasFlag(NODE_IS_EDITABLE));
UpdateState(aNotify);
}
class XULInContentErrorReporter : public Runnable {
public:
explicit XULInContentErrorReporter(Document& aDocument)
: mozilla::Runnable("XULInContentErrorReporter"), mDocument(aDocument) {}
NS_IMETHOD Run() override {
mDocument->WarnOnceAbout(Document::eImportXULIntoContent, false);
return NS_OK;
}
private:
OwningNonNull<Document> mDocument;
};
static bool NeedTooltipSupport(const nsXULElement& aXULElement) {
if (aXULElement.NodeInfo()->Equals(nsGkAtoms::treechildren)) {
// treechildren always get tooltip support, since cropped tree cells show
// their full text in a tooltip.
return true;
}
return aXULElement.GetBoolAttr(nsGkAtoms::tooltip) ||
aXULElement.GetBoolAttr(nsGkAtoms::tooltiptext);
}
nsresult nsXULElement::BindToTree(BindContext& aContext, nsINode& aParent) {
nsresult rv = nsStyledElement::BindToTree(aContext, aParent);
NS_ENSURE_SUCCESS(rv, rv);
Document& doc = aContext.OwnerDoc();
// FIXME(emilio): Could use IsInComposedDoc().
if (!aContext.GetBindingParent() && IsInUncomposedDoc() &&
!doc.IsLoadedAsInteractiveData() && !doc.AllowXULXBL() &&
!doc.HasWarnedAbout(Document::eImportXULIntoContent)) {
nsContentUtils::AddScriptRunner(new XULInContentErrorReporter(doc));
}
if (!IsInComposedDoc()) {
return rv;
}
#ifdef DEBUG
if (!doc.AllowXULXBL() && !doc.IsUnstyledDocument()) {
// To save CPU cycles and memory, non-XUL documents only load the user
// agent style sheet rules for a minimal set of XUL elements such as
// 'scrollbar' that may be created implicitly for their content (those
// rules being in minimal-xul.css).
//
// This assertion makes sure no other XUL element is used in a non-XUL
// document.
nsAtom* tag = NodeInfo()->NameAtom();
MOZ_ASSERT(
// scrollbar parts
tag == nsGkAtoms::scrollbar || tag == nsGkAtoms::scrollbarbutton ||
tag == nsGkAtoms::scrollcorner || tag == nsGkAtoms::slider ||
tag == nsGkAtoms::thumb ||
// other
tag == nsGkAtoms::resizer || tag == nsGkAtoms::label,
"Unexpected XUL element in non-XUL doc");
}
#endif
if (NodeInfo()->Equals(nsGkAtoms::keyset, kNameSpaceID_XUL)) {
// Create our XUL key listener and hook it up.
nsXBLService::AttachGlobalKeyHandler(this);
}
if (NeedTooltipSupport(*this)) {
AddTooltipSupport();
}
if (XULBroadcastManager::MayNeedListener(*this)) {
if (!doc.HasXULBroadcastManager()) {
doc.InitializeXULBroadcastManager();
}
XULBroadcastManager* broadcastManager = doc.GetXULBroadcastManager();
broadcastManager->AddListener(this);
}
return rv;
}
void nsXULElement::UnbindFromTree(bool aNullParent) {
if (NodeInfo()->Equals(nsGkAtoms::keyset, kNameSpaceID_XUL)) {
nsXBLService::DetachGlobalKeyHandler(this);
}
if (NeedTooltipSupport(*this)) {
RemoveTooltipSupport();
}
Document* doc = GetComposedDoc();
if (doc && doc->HasXULBroadcastManager() &&
XULBroadcastManager::MayNeedListener(*this)) {
RefPtr<XULBroadcastManager> broadcastManager =
doc->GetXULBroadcastManager();
broadcastManager->RemoveListener(this);
}
// mControllers can own objects that are implemented
// in JavaScript (such as some implementations of
// nsIControllers. These objects prevent their global
// object's script object from being garbage collected,
// which means JS continues to hold an owning reference
// to the nsGlobalWindow, which owns the document,
// which owns this content. That's a cycle, so we break
// it here. (It might be better to break this by releasing
// mDocument in nsGlobalWindow::SetDocShell, but I'm not
// sure whether that would fix all possible cycles through
// mControllers.)
nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
if (slots) {
slots->mControllers = nullptr;
}
nsStyledElement::UnbindFromTree(aNullParent);
}
void nsXULElement::DoneAddingChildren(bool aHaveNotified) {
if (IsXULElement(nsGkAtoms::linkset)) {
Document* doc = GetComposedDoc();
if (doc) {
doc->OnL10nResourceContainerParsed();
}
}
}
void nsXULElement::UnregisterAccessKey(const nsAString& aOldValue) {
// If someone changes the accesskey, unregister the old one
//
Document* doc = GetComposedDoc();
if (doc && !aOldValue.IsEmpty()) {
PresShell* presShell = doc->GetPresShell();
if (presShell) {
Element* element = this;
// find out what type of content node this is
if (mNodeInfo->Equals(nsGkAtoms::label)) {
// For anonymous labels the unregistering must
// occur on the binding parent control.
// XXXldb: And what if the binding parent is null?
nsIContent* bindingParent = GetBindingParent();
element = bindingParent ? bindingParent->AsElement() : nullptr;
}
if (element) {
presShell->GetPresContext()->EventStateManager()->UnregisterAccessKey(
element, aOldValue.First());
}
}
}
}
nsresult nsXULElement::BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
const nsAttrValueOrString* aValue,
bool aNotify) {
if (aNamespaceID == kNameSpaceID_None && aName == nsGkAtoms::accesskey &&
IsInUncomposedDoc()) {
nsAutoString oldValue;
if (GetAttr(aNamespaceID, aName, oldValue)) {
UnregisterAccessKey(oldValue);
}
} else if (aNamespaceID == kNameSpaceID_None &&
(aName == nsGkAtoms::command || aName == nsGkAtoms::observes) &&
IsInUncomposedDoc()) {
// XXX sXBL/XBL2 issue! Owner or current document?
// XXX Why does this not also remove broadcast listeners if the
// "element" attribute was changed on an <observer>?
nsAutoString oldValue;
GetAttr(kNameSpaceID_None, nsGkAtoms::observes, oldValue);
if (oldValue.IsEmpty()) {
GetAttr(kNameSpaceID_None, nsGkAtoms::command, oldValue);
}
Document* doc = GetUncomposedDoc();
if (!oldValue.IsEmpty() && doc->HasXULBroadcastManager()) {
RefPtr<XULBroadcastManager> broadcastManager =
doc->GetXULBroadcastManager();
broadcastManager->RemoveListener(this);
}
} else if (aNamespaceID == kNameSpaceID_None && aValue &&
mNodeInfo->Equals(nsGkAtoms::window) &&
aName == nsGkAtoms::chromemargin) {
nsAttrValue attrValue;
// Make sure the margin format is valid first
if (!attrValue.ParseIntMarginValue(aValue->String())) {
return NS_ERROR_INVALID_ARG;
}
} else if (aNamespaceID == kNameSpaceID_None &&
aName == nsGkAtoms::usercontextid) {
nsAutoString oldValue;
bool hasAttribute =
GetAttr(kNameSpaceID_None, nsGkAtoms::usercontextid, oldValue);
if (hasAttribute && (!aValue || !aValue->String().Equals(oldValue))) {
MOZ_ASSERT(false, "Changing usercontextid is not allowed.");
return NS_ERROR_INVALID_ARG;
}
}
return nsStyledElement::BeforeSetAttr(aNamespaceID, aName, aValue, aNotify);
}
nsresult nsXULElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
const nsAttrValue* aValue,
const nsAttrValue* aOldValue,
nsIPrincipal* aSubjectPrincipal,
bool aNotify) {
if (aNamespaceID == kNameSpaceID_None) {
if (aValue) {
// Add popup and event listeners. We can't call AddListenerFor since
// the attribute isn't set yet.
MaybeAddPopupListener(aName);
if (nsContentUtils::IsEventAttributeName(aName, EventNameType_XUL)) {
if (aValue->Type() == nsAttrValue::eString) {
SetEventHandler(aName, aValue->GetStringValue(), true);
} else {
nsAutoString body;
aValue->ToString(body);
SetEventHandler(aName, body, true);
}
}
Document* document = GetUncomposedDoc();
// Hide chrome if needed
if (mNodeInfo->Equals(nsGkAtoms::window)) {
if (aName == nsGkAtoms::hidechrome) {
HideWindowChrome(
aValue->Equals(NS_LITERAL_STRING("true"), eCaseMatters));
} else if (aName == nsGkAtoms::chromemargin) {
SetChromeMargins(aValue);
} else if (aName == nsGkAtoms::windowtype && document &&
document->GetRootElement() == this) {
MaybeUpdatePrivateLifetime();
}
}
// title and drawintitlebar are settable on
// any root node (windows, dialogs, etc)
if (document && document->GetRootElement() == this) {
if (aName == nsGkAtoms::title) {
document->NotifyPossibleTitleChange(false);
} else if (aName == nsGkAtoms::drawintitlebar) {
SetDrawsInTitlebar(
aValue->Equals(NS_LITERAL_STRING("true"), eCaseMatters));
} else if (aName == nsGkAtoms::drawtitle) {
SetDrawsTitle(
aValue->Equals(NS_LITERAL_STRING("true"), eCaseMatters));
} else if (aName == nsGkAtoms::localedir) {
// if the localedir changed on the root element, reset the document
// direction
document->ResetDocumentDirection();
} else if (aName == nsGkAtoms::lwtheme ||
aName == nsGkAtoms::lwthemetextcolor) {
// if the lwtheme changed, make sure to reset the document lwtheme
// cache
document->ResetDocumentLWTheme();
UpdateBrightTitlebarForeground(document);
} else if (aName == nsGkAtoms::brighttitlebarforeground) {
UpdateBrightTitlebarForeground(document);
}
}
} else {
if (mNodeInfo->Equals(nsGkAtoms::window)) {
if (aName == nsGkAtoms::hidechrome) {
HideWindowChrome(false);
} else if (aName == nsGkAtoms::chromemargin) {
ResetChromeMargins();
}
}
Document* doc = GetUncomposedDoc();
if (doc && doc->GetRootElement() == this) {
if (aName == nsGkAtoms::localedir) {
// if the localedir changed on the root element, reset the document
// direction
doc->ResetDocumentDirection();
} else if ((aName == nsGkAtoms::lwtheme ||
aName == nsGkAtoms::lwthemetextcolor)) {
// if the lwtheme changed, make sure to restyle appropriately
doc->ResetDocumentLWTheme();
UpdateBrightTitlebarForeground(doc);
} else if (aName == nsGkAtoms::brighttitlebarforeground) {
UpdateBrightTitlebarForeground(doc);
} else if (aName == nsGkAtoms::drawintitlebar) {
SetDrawsInTitlebar(false);
} else if (aName == nsGkAtoms::drawtitle) {
SetDrawsTitle(false);
}
}
}
if (aName == nsGkAtoms::tooltip || aName == nsGkAtoms::tooltiptext) {
if (!!aValue != !!aOldValue && IsInComposedDoc() &&
!NodeInfo()->Equals(nsGkAtoms::treechildren)) {
if (aValue) {
AddTooltipSupport();
} else {
RemoveTooltipSupport();
}
}
}
Document* doc = GetComposedDoc();
if (doc && doc->HasXULBroadcastManager()) {
RefPtr<XULBroadcastManager> broadcastManager =
doc->GetXULBroadcastManager();
broadcastManager->AttributeChanged(this, aNamespaceID, aName);
}
if (doc && XULBroadcastManager::MayNeedListener(*this)) {
if (!doc->HasXULBroadcastManager()) {
doc->InitializeXULBroadcastManager();
}
XULBroadcastManager* broadcastManager = doc->GetXULBroadcastManager();
broadcastManager->AddListener(this);
}
// XXX need to check if they're changing an event handler: if
// so, then we need to unhook the old one. Or something.
}
return nsStyledElement::AfterSetAttr(aNamespaceID, aName, aValue, aOldValue,
aSubjectPrincipal, aNotify);
}
void nsXULElement::AddTooltipSupport() {
nsXULTooltipListener* listener = nsXULTooltipListener::GetInstance();
if (!listener) {
return;
}
listener->AddTooltipSupport(this);
}
void nsXULElement::RemoveTooltipSupport() {
nsXULTooltipListener* listener = nsXULTooltipListener::GetInstance();
if (!listener) {
return;
}
listener->RemoveTooltipSupport(this);
}
bool nsXULElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult) {
if (aNamespaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::tabindex) {
return aResult.ParseIntValue(aValue);
}
// Parse into a nsAttrValue
if (!nsStyledElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
aMaybeScriptedPrincipal, aResult)) {
// Fall back to parsing as atom for short values
aResult.ParseStringOrAtom(aValue);
}
return true;
}
void nsXULElement::DestroyContent() {
nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
if (slots) {
slots->mControllers = nullptr;
}
nsStyledElement::DestroyContent();
}
#ifdef DEBUG
void nsXULElement::List(FILE* out, int32_t aIndent) const {
nsCString prefix("XUL");
if (HasSlots()) {
prefix.Append('*');
}
prefix.Append(' ');
nsStyledElement::List(out, aIndent, prefix);
}
#endif
bool nsXULElement::IsEventStoppedFromAnonymousScrollbar(EventMessage aMessage) {