tor-browser

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

nsTreeUtils.cpp (4582B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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 #include "nsTreeUtils.h"
      8 
      9 #include "ChildIterator.h"
     10 #include "nsAtom.h"
     11 #include "nsCRT.h"
     12 #include "nsGkAtoms.h"
     13 #include "nsIContent.h"
     14 #include "nsNameSpaceManager.h"
     15 #include "nsReadableUtils.h"
     16 
     17 using namespace mozilla;
     18 
     19 nsresult nsTreeUtils::TokenizeProperties(const nsAString& aProperties,
     20                                         AtomArray& aPropertiesArray) {
     21  nsAString::const_iterator end;
     22  aProperties.EndReading(end);
     23 
     24  nsAString::const_iterator iter;
     25  aProperties.BeginReading(iter);
     26 
     27  do {
     28    // Skip whitespace
     29    while (iter != end && nsCRT::IsAsciiSpace(*iter)) {
     30      ++iter;
     31    }
     32 
     33    // If only whitespace, we're done
     34    if (iter == end) {
     35      break;
     36    }
     37 
     38    // Note the first non-whitespace character
     39    nsAString::const_iterator first = iter;
     40 
     41    // Advance to the next whitespace character
     42    while (iter != end && !nsCRT::IsAsciiSpace(*iter)) {
     43      ++iter;
     44    }
     45 
     46    // XXX this would be nonsensical
     47    NS_ASSERTION(iter != first, "eh? something's wrong here");
     48    if (iter == first) {
     49      break;
     50    }
     51 
     52    RefPtr<nsAtom> atom = NS_Atomize(Substring(first, iter));
     53    aPropertiesArray.AppendElement(atom);
     54  } while (iter != end);
     55 
     56  return NS_OK;
     57 }
     58 
     59 nsIContent* nsTreeUtils::GetImmediateChild(nsIContent* aContainer,
     60                                           nsAtom* aTag) {
     61  dom::FlattenedChildIterator iter(aContainer);
     62  for (nsIContent* child = iter.GetNextChild(); child;
     63       child = iter.GetNextChild()) {
     64    if (child->IsXULElement(aTag)) {
     65      return child;
     66    }
     67    // <slot> is in the flattened tree, but <tree> code is used to work with
     68    // <xbl:children> which is not, so recurse in <slot> here.
     69    if (child->IsHTMLElement(nsGkAtoms::slot)) {
     70      if (nsIContent* c = GetImmediateChild(child, aTag)) {
     71        return c;
     72      }
     73    }
     74  }
     75 
     76  return nullptr;
     77 }
     78 
     79 nsIContent* nsTreeUtils::GetDescendantChild(nsIContent* aContainer,
     80                                            nsAtom* aTag) {
     81  dom::FlattenedChildIterator iter(aContainer);
     82  for (nsIContent* child = iter.GetNextChild(); child;
     83       child = iter.GetNextChild()) {
     84    if (child->IsXULElement(aTag)) {
     85      return child;
     86    }
     87 
     88    child = GetDescendantChild(child, aTag);
     89    if (child) {
     90      return child;
     91    }
     92  }
     93 
     94  return nullptr;
     95 }
     96 
     97 nsresult nsTreeUtils::UpdateSortIndicators(dom::Element* aColumn,
     98                                           const nsAString& aDirection) {
     99  aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortDirection, aDirection,
    100                   true);
    101  aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortActive, u"true"_ns, true);
    102 
    103  // Unset sort attribute(s) on the other columns
    104  nsCOMPtr<nsIContent> parentContent = aColumn->GetParent();
    105  if (parentContent && parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
    106                                                         kNameSpaceID_XUL)) {
    107    for (nsINode* childContent = parentContent->GetFirstChild(); childContent;
    108         childContent = childContent->GetNextSibling()) {
    109      if (childContent != aColumn &&
    110          childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
    111                                           kNameSpaceID_XUL)) {
    112        childContent->AsElement()->UnsetAttr(kNameSpaceID_None,
    113                                             nsGkAtoms::sortDirection, true);
    114        childContent->AsElement()->UnsetAttr(kNameSpaceID_None,
    115                                             nsGkAtoms::sortActive, true);
    116      }
    117    }
    118  }
    119 
    120  return NS_OK;
    121 }
    122 
    123 nsresult nsTreeUtils::GetColumnIndex(dom::Element* aColumn, int32_t* aResult) {
    124  nsIContent* parentContent = aColumn->GetParent();
    125  if (parentContent && parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
    126                                                         kNameSpaceID_XUL)) {
    127    int32_t colIndex = 0;
    128 
    129    for (nsINode* childContent = parentContent->GetFirstChild(); childContent;
    130         childContent = childContent->GetNextSibling()) {
    131      if (childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
    132                                           kNameSpaceID_XUL)) {
    133        if (childContent == aColumn) {
    134          *aResult = colIndex;
    135          return NS_OK;
    136        }
    137        ++colIndex;
    138      }
    139    }
    140  }
    141 
    142  *aResult = -1;
    143  return NS_OK;
    144 }