tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsXULContentUtils.cpp (2538B)


      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 "nsXULContentUtils.h"
     14 
     15 #include "mozilla/dom/Document.h"
     16 #include "mozilla/dom/Element.h"
     17 #include "mozilla/intl/Collator.h"
     18 #include "mozilla/intl/LocaleService.h"
     19 #include "nsCOMPtr.h"
     20 #include "nsComponentManagerUtils.h"
     21 #include "nsGkAtoms.h"
     22 #include "nsIContent.h"
     23 #include "nsString.h"
     24 
     25 using namespace mozilla;
     26 
     27 //------------------------------------------------------------------------
     28 
     29 const mozilla::intl::Collator* nsXULContentUtils::gCollator;
     30 
     31 //------------------------------------------------------------------------
     32 // Constructors n' stuff
     33 //
     34 
     35 nsresult nsXULContentUtils::Finish() {
     36  if (gCollator) {
     37    delete gCollator;
     38    gCollator = nullptr;
     39  }
     40 
     41  return NS_OK;
     42 }
     43 
     44 const mozilla::intl::Collator* nsXULContentUtils::GetCollator() {
     45  if (!gCollator) {
     46    // Lazily initialize the Collator.
     47    auto result = mozilla::intl::LocaleService::TryCreateComponent<
     48        mozilla::intl::Collator>();
     49    if (result.isErr()) {
     50      NS_ERROR("couldn't create a mozilla::intl::Collator");
     51      return nullptr;
     52    }
     53 
     54    auto collator = result.unwrap();
     55 
     56    // Sort in a case-insensitive way, where "base" letters are considered
     57    // equal, e.g: a = á, a = A, a ≠ b.
     58    mozilla::intl::Collator::Options options{};
     59    options.sensitivity = mozilla::intl::Collator::Sensitivity::Base;
     60    auto optResult = collator->SetOptions(options);
     61    if (optResult.isErr()) {
     62      NS_ERROR("couldn't set options for mozilla::intl::Collator");
     63      return nullptr;
     64    }
     65    gCollator = collator.release();
     66  }
     67 
     68  return gCollator;
     69 }
     70 
     71 //------------------------------------------------------------------------
     72 //
     73 
     74 nsresult nsXULContentUtils::FindChildByTag(nsIContent* aElement,
     75                                           int32_t aNameSpaceID, nsAtom* aTag,
     76                                           mozilla::dom::Element** aResult) {
     77  for (nsIContent* child = aElement->GetFirstChild(); child;
     78       child = child->GetNextSibling()) {
     79    if (child->IsElement() && child->NodeInfo()->Equals(aTag, aNameSpaceID)) {
     80      NS_ADDREF(*aResult = child->AsElement());
     81      return NS_OK;
     82    }
     83  }
     84 
     85  *aResult = nullptr;
     86  return NS_RDF_NO_VALUE;  // not found
     87 }