Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / dom / xul / nsXULContentUtils.cpp
blob07de8f6cdb98199dd1fbe3d920784ca9ace2d4df
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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/. */
7 /*
9 A package of routines shared by the XUL content code.
13 #include "mozilla/ArrayUtils.h"
14 #include "mozilla/intl/LocaleService.h"
15 #include "mozilla/intl/Collator.h"
17 #include "nsCOMPtr.h"
18 #include "nsComponentManagerUtils.h"
19 #include "nsIContent.h"
20 #include "mozilla/dom/Document.h"
21 #include "mozilla/dom/Element.h"
22 #include "nsXULContentUtils.h"
23 #include "nsString.h"
24 #include "nsGkAtoms.h"
26 using namespace mozilla;
28 //------------------------------------------------------------------------
30 const mozilla::intl::Collator* nsXULContentUtils::gCollator;
32 //------------------------------------------------------------------------
33 // Constructors n' stuff
36 nsresult nsXULContentUtils::Finish() {
37 if (gCollator) {
38 delete gCollator;
39 gCollator = nullptr;
42 return NS_OK;
45 const mozilla::intl::Collator* nsXULContentUtils::GetCollator() {
46 if (!gCollator) {
47 // Lazily initialize the Collator.
48 auto result = mozilla::intl::LocaleService::TryCreateComponent<
49 mozilla::intl::Collator>();
50 if (result.isErr()) {
51 NS_ERROR("couldn't create a mozilla::intl::Collator");
52 return nullptr;
55 auto collator = result.unwrap();
57 // Sort in a case-insensitive way, where "base" letters are considered
58 // equal, e.g: a = á, a = A, a ≠ b.
59 mozilla::intl::Collator::Options options{};
60 options.sensitivity = mozilla::intl::Collator::Sensitivity::Base;
61 auto optResult = collator->SetOptions(options);
62 if (optResult.isErr()) {
63 NS_ERROR("couldn't set options for mozilla::intl::Collator");
64 return nullptr;
66 gCollator = collator.release();
69 return gCollator;
72 //------------------------------------------------------------------------
75 nsresult nsXULContentUtils::FindChildByTag(nsIContent* aElement,
76 int32_t aNameSpaceID, nsAtom* aTag,
77 mozilla::dom::Element** aResult) {
78 for (nsIContent* child = aElement->GetFirstChild(); child;
79 child = child->GetNextSibling()) {
80 if (child->IsElement() && child->NodeInfo()->Equals(aTag, aNameSpaceID)) {
81 NS_ADDREF(*aResult = child->AsElement());
82 return NS_OK;
86 *aResult = nullptr;
87 return NS_RDF_NO_VALUE; // not found