1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 "mozilla/dom/Document.h"
20 #include "nsXULContentUtils.h"
21 #include "nsLayoutCID.h"
22 #include "nsString.h"
23 #include "nsGkAtoms.h"
24 
25 using namespace mozilla;
26 
27 //------------------------------------------------------------------------
28 
29 nsICollation* nsXULContentUtils::gCollation;
30 
31 //------------------------------------------------------------------------
32 // Constructors n' stuff
33 //
34 
Finish()35 nsresult nsXULContentUtils::Finish() {
36   NS_IF_RELEASE(gCollation);
37 
38   return NS_OK;
39 }
40 
GetCollation()41 nsICollation* nsXULContentUtils::GetCollation() {
42   if (!gCollation) {
43     nsCOMPtr<nsICollationFactory> colFactory =
44         do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
45     if (colFactory) {
46       DebugOnly<nsresult> rv = colFactory->CreateCollation(&gCollation);
47       NS_ASSERTION(NS_SUCCEEDED(rv), "couldn't create collation instance");
48     } else
49       NS_ERROR("couldn't create instance of collation factory");
50   }
51 
52   return gCollation;
53 }
54 
55 //------------------------------------------------------------------------
56 //
57 
FindChildByTag(nsIContent * aElement,int32_t aNameSpaceID,nsAtom * aTag,Element ** aResult)58 nsresult nsXULContentUtils::FindChildByTag(nsIContent* aElement,
59                                            int32_t aNameSpaceID, nsAtom* aTag,
60                                            Element** aResult) {
61   for (nsIContent* child = aElement->GetFirstChild(); child;
62        child = child->GetNextSibling()) {
63     if (child->IsElement() && child->NodeInfo()->Equals(aTag, aNameSpaceID)) {
64       NS_ADDREF(*aResult = child->AsElement());
65       return NS_OK;
66     }
67   }
68 
69   *aResult = nullptr;
70   return NS_RDF_NO_VALUE;  // not found
71 }
72