layout/xul/tree/nsTreeUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/xul/tree/nsTreeUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,140 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsReadableUtils.h"
    1.10 +#include "nsTreeUtils.h"
    1.11 +#include "ChildIterator.h"
    1.12 +#include "nsCRT.h"
    1.13 +#include "nsIAtom.h"
    1.14 +#include "nsNameSpaceManager.h"
    1.15 +#include "nsGkAtoms.h"
    1.16 +#include "nsINodeInfo.h"
    1.17 +
    1.18 +using namespace mozilla;
    1.19 +
    1.20 +nsresult
    1.21 +nsTreeUtils::TokenizeProperties(const nsAString& aProperties, AtomArray & aPropertiesArray)
    1.22 +{
    1.23 +  nsAString::const_iterator end;
    1.24 +  aProperties.EndReading(end);
    1.25 +
    1.26 +  nsAString::const_iterator iter;
    1.27 +  aProperties.BeginReading(iter);
    1.28 +
    1.29 +  do {
    1.30 +    // Skip whitespace
    1.31 +    while (iter != end && nsCRT::IsAsciiSpace(*iter))
    1.32 +      ++iter;
    1.33 +
    1.34 +    // If only whitespace, we're done
    1.35 +    if (iter == end)
    1.36 +      break;
    1.37 +
    1.38 +    // Note the first non-whitespace character
    1.39 +    nsAString::const_iterator first = iter;
    1.40 +
    1.41 +    // Advance to the next whitespace character
    1.42 +    while (iter != end && ! nsCRT::IsAsciiSpace(*iter))
    1.43 +      ++iter;
    1.44 +
    1.45 +    // XXX this would be nonsensical
    1.46 +    NS_ASSERTION(iter != first, "eh? something's wrong here");
    1.47 +    if (iter == first)
    1.48 +      break;
    1.49 +
    1.50 +    nsCOMPtr<nsIAtom> atom = do_GetAtom(Substring(first, iter));
    1.51 +    aPropertiesArray.AppendElement(atom);
    1.52 +  } while (iter != end);
    1.53 +
    1.54 +  return NS_OK;
    1.55 +}
    1.56 +
    1.57 +nsIContent*
    1.58 +nsTreeUtils::GetImmediateChild(nsIContent* aContainer, nsIAtom* aTag)
    1.59 +{
    1.60 +  dom::FlattenedChildIterator iter(aContainer);
    1.61 +  for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
    1.62 +    if (child->Tag() == aTag) {
    1.63 +      return child;
    1.64 +    }
    1.65 +  }
    1.66 +
    1.67 +  return nullptr;
    1.68 +}
    1.69 +
    1.70 +nsIContent*
    1.71 +nsTreeUtils::GetDescendantChild(nsIContent* aContainer, nsIAtom* aTag)
    1.72 +{
    1.73 +  dom::FlattenedChildIterator iter(aContainer);
    1.74 +  for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
    1.75 +    if (child->Tag() == aTag) {
    1.76 +      return child;
    1.77 +    }
    1.78 +
    1.79 +    child = GetDescendantChild(child, aTag);
    1.80 +    if (child) {
    1.81 +      return child;
    1.82 +    }
    1.83 +  }
    1.84 +
    1.85 +  return nullptr;
    1.86 +}
    1.87 +
    1.88 +nsresult
    1.89 +nsTreeUtils::UpdateSortIndicators(nsIContent* aColumn, const nsAString& aDirection)
    1.90 +{
    1.91 +  aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortDirection, aDirection, true);
    1.92 +  aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortActive, NS_LITERAL_STRING("true"), true);
    1.93 +
    1.94 +  // Unset sort attribute(s) on the other columns
    1.95 +  nsCOMPtr<nsIContent> parentContent = aColumn->GetParent();
    1.96 +  if (parentContent &&
    1.97 +      parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
    1.98 +                                        kNameSpaceID_XUL)) {
    1.99 +    uint32_t i, numChildren = parentContent->GetChildCount();
   1.100 +    for (i = 0; i < numChildren; ++i) {
   1.101 +      nsCOMPtr<nsIContent> childContent = parentContent->GetChildAt(i);
   1.102 +
   1.103 +      if (childContent &&
   1.104 +          childContent != aColumn &&
   1.105 +          childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
   1.106 +                                           kNameSpaceID_XUL)) {
   1.107 +        childContent->UnsetAttr(kNameSpaceID_None,
   1.108 +                                nsGkAtoms::sortDirection, true);
   1.109 +        childContent->UnsetAttr(kNameSpaceID_None,
   1.110 +                                nsGkAtoms::sortActive, true);
   1.111 +      }
   1.112 +    }
   1.113 +  }
   1.114 +
   1.115 +  return NS_OK;
   1.116 +}
   1.117 +
   1.118 +nsresult
   1.119 +nsTreeUtils::GetColumnIndex(nsIContent* aColumn, int32_t* aResult)
   1.120 +{
   1.121 +  nsIContent* parentContent = aColumn->GetParent();
   1.122 +  if (parentContent &&
   1.123 +      parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
   1.124 +                                        kNameSpaceID_XUL)) {
   1.125 +    uint32_t i, numChildren = parentContent->GetChildCount();
   1.126 +    int32_t colIndex = 0;
   1.127 +    for (i = 0; i < numChildren; ++i) {
   1.128 +      nsIContent *childContent = parentContent->GetChildAt(i);
   1.129 +      if (childContent &&
   1.130 +          childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
   1.131 +                                           kNameSpaceID_XUL)) {
   1.132 +        if (childContent == aColumn) {
   1.133 +          *aResult = colIndex;
   1.134 +          return NS_OK;
   1.135 +        }
   1.136 +        ++colIndex;
   1.137 +      }
   1.138 +    }
   1.139 +  }
   1.140 +
   1.141 +  *aResult = -1;
   1.142 +  return NS_OK;
   1.143 +}

mercurial