Bug 1825212 [wpt PR 39266] - [@scope] Propagate proximity from SubResult, a=testonly
[gecko.git] / dom / xul / nsXULContentUtils.cpp
blob109aa1975a0db8064aafc33c675c4b6f6fa6c052
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 "nsLayoutCID.h"
24 #include "nsString.h"
25 #include "nsGkAtoms.h"
27 using namespace mozilla;
29 //------------------------------------------------------------------------
31 const mozilla::intl::Collator* nsXULContentUtils::gCollator;
33 //------------------------------------------------------------------------
34 // Constructors n' stuff
37 nsresult nsXULContentUtils::Finish() {
38 if (gCollator) {
39 delete gCollator;
40 gCollator = nullptr;
43 return NS_OK;
46 const mozilla::intl::Collator* nsXULContentUtils::GetCollator() {
47 if (!gCollator) {
48 // Lazily initialize the Collator.
49 auto result = mozilla::intl::LocaleService::TryCreateComponent<
50 mozilla::intl::Collator>();
51 if (result.isErr()) {
52 NS_ERROR("couldn't create a mozilla::intl::Collator");
53 return nullptr;
56 auto collator = result.unwrap();
58 // Sort in a case-insensitive way, where "base" letters are considered
59 // equal, e.g: a = á, a = A, a ≠ b.
60 mozilla::intl::Collator::Options options{};
61 options.sensitivity = mozilla::intl::Collator::Sensitivity::Base;
62 auto optResult = collator->SetOptions(options);
63 if (optResult.isErr()) {
64 NS_ERROR("couldn't set options for mozilla::intl::Collator");
65 return nullptr;
67 gCollator = collator.release();
70 return gCollator;
73 //------------------------------------------------------------------------
76 nsresult nsXULContentUtils::FindChildByTag(nsIContent* aElement,
77 int32_t aNameSpaceID, nsAtom* aTag,
78 mozilla::dom::Element** aResult) {
79 for (nsIContent* child = aElement->GetFirstChild(); child;
80 child = child->GetNextSibling()) {
81 if (child->IsElement() && child->NodeInfo()->Equals(aTag, aNameSpaceID)) {
82 NS_ADDREF(*aResult = child->AsElement());
83 return NS_OK;
87 *aResult = nullptr;
88 return NS_RDF_NO_VALUE; // not found