1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /*
8 
9   A package of routines shared by the XUL content code.
10 
11  */
12 
13 #include "mozilla/ArrayUtils.h"
14 
15 #include "nsCollationCID.h"
16 #include "nsCOMPtr.h"
17 #include "nsIContent.h"
18 #include "nsICollation.h"
19 #include "nsIDocument.h"
20 #include "nsIDOMElement.h"
21 #include "nsIDOMXULCommandDispatcher.h"
22 #include "nsIRDFService.h"
23 #include "nsIServiceManager.h"
24 #include "nsXULContentUtils.h"
25 #include "nsLayoutCID.h"
26 #include "nsRDFCID.h"
27 #include "nsString.h"
28 #include "nsGkAtoms.h"
29 #include "XULDocument.h"
30 
31 using namespace mozilla;
32 using dom::XULDocument;
33 
34 //------------------------------------------------------------------------
35 
36 nsIRDFService* nsXULContentUtils::gRDF;
37 nsICollation* nsXULContentUtils::gCollation;
38 
39 //------------------------------------------------------------------------
40 // Constructors n' stuff
41 //
42 
Init()43 nsresult nsXULContentUtils::Init() {
44   static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
45   nsresult rv = CallGetService(kRDFServiceCID, &gRDF);
46   if (NS_FAILED(rv)) {
47     return rv;
48   }
49 
50   return NS_OK;
51 }
52 
Finish()53 nsresult nsXULContentUtils::Finish() {
54   NS_IF_RELEASE(gRDF);
55   NS_IF_RELEASE(gCollation);
56 
57   return NS_OK;
58 }
59 
GetCollation()60 nsICollation* nsXULContentUtils::GetCollation() {
61   if (!gCollation) {
62     nsCOMPtr<nsICollationFactory> colFactory =
63         do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
64     if (colFactory) {
65       DebugOnly<nsresult> rv = colFactory->CreateCollation(&gCollation);
66       NS_ASSERTION(NS_SUCCEEDED(rv), "couldn't create collation instance");
67     } else
68       NS_ERROR("couldn't create instance of collation factory");
69   }
70 
71   return gCollation;
72 }
73 
74 //------------------------------------------------------------------------
75 //
76 
FindChildByTag(nsIContent * aElement,int32_t aNameSpaceID,nsAtom * aTag,Element ** aResult)77 nsresult nsXULContentUtils::FindChildByTag(nsIContent* aElement,
78                                            int32_t aNameSpaceID, nsAtom* aTag,
79                                            Element** aResult) {
80   for (nsIContent* child = aElement->GetFirstChild(); child;
81        child = child->GetNextSibling()) {
82     if (child->IsElement() && child->NodeInfo()->Equals(aTag, aNameSpaceID)) {
83       NS_ADDREF(*aResult = child->AsElement());
84       return NS_OK;
85     }
86   }
87 
88   *aResult = nullptr;
89   return NS_RDF_NO_VALUE;  // not found
90 }
91 
SetCommandUpdater(nsIDocument * aDocument,Element * aElement)92 nsresult nsXULContentUtils::SetCommandUpdater(nsIDocument* aDocument,
93                                               Element* aElement) {
94   // Deal with setting up a 'commandupdater'. Pulls the 'events' and
95   // 'targets' attributes off of aElement, and adds it to the
96   // document's command dispatcher.
97   NS_PRECONDITION(aDocument != nullptr, "null ptr");
98   if (!aDocument) return NS_ERROR_NULL_POINTER;
99 
100   NS_PRECONDITION(aElement != nullptr, "null ptr");
101   if (!aElement) return NS_ERROR_NULL_POINTER;
102 
103   nsresult rv;
104 
105   XULDocument* xuldoc = aDocument->AsXULDocument();
106   NS_ASSERTION(xuldoc != nullptr, "not a xul document");
107   if (!xuldoc) return NS_ERROR_UNEXPECTED;
108 
109   nsCOMPtr<nsIDOMXULCommandDispatcher> dispatcher =
110       xuldoc->GetCommandDispatcher();
111   NS_ASSERTION(dispatcher != nullptr, "no dispatcher");
112   if (!dispatcher) return NS_ERROR_UNEXPECTED;
113 
114   nsAutoString events;
115   aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::events, events);
116   if (events.IsEmpty()) events.Assign('*');
117 
118   nsAutoString targets;
119   aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::targets, targets);
120 
121   if (targets.IsEmpty()) targets.Assign('*');
122 
123   nsCOMPtr<nsIDOMElement> domelement = do_QueryInterface(aElement);
124   NS_ASSERTION(domelement != nullptr, "not a DOM element");
125   if (!domelement) return NS_ERROR_UNEXPECTED;
126 
127   rv = dispatcher->AddCommandUpdater(domelement, events, targets);
128   if (NS_FAILED(rv)) return rv;
129 
130   return NS_OK;
131 }
132