summaryrefslogtreecommitdiffstats
path: root/layout/xul/tree/nsTreeUtils.cpp
blob: 10767e22d2d9e7b636b435abd7fd55f28711311e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsReadableUtils.h"
#include "nsTreeUtils.h"
#include "ChildIterator.h"
#include "nsCRT.h"
#include "nsAtom.h"
#include "nsNameSpaceManager.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"

using namespace mozilla;

nsresult nsTreeUtils::TokenizeProperties(const nsAString& aProperties,
                                         AtomArray& aPropertiesArray) {
  nsAString::const_iterator end;
  aProperties.EndReading(end);

  nsAString::const_iterator iter;
  aProperties.BeginReading(iter);

  do {
    // Skip whitespace
    while (iter != end && nsCRT::IsAsciiSpace(*iter)) ++iter;

    // If only whitespace, we're done
    if (iter == end) break;

    // Note the first non-whitespace character
    nsAString::const_iterator first = iter;

    // Advance to the next whitespace character
    while (iter != end && !nsCRT::IsAsciiSpace(*iter)) ++iter;

    // XXX this would be nonsensical
    NS_ASSERTION(iter != first, "eh? something's wrong here");
    if (iter == first) break;

    RefPtr<nsAtom> atom = NS_Atomize(Substring(first, iter));
    aPropertiesArray.AppendElement(atom);
  } while (iter != end);

  return NS_OK;
}

nsIContent* nsTreeUtils::GetImmediateChild(nsIContent* aContainer,
                                           nsAtom* aTag) {
  dom::FlattenedChildIterator iter(aContainer);
  for (nsIContent* child = iter.GetNextChild(); child;
       child = iter.GetNextChild()) {
    if (child->IsXULElement(aTag)) {
      return child;
    }
    // <slot> is in the flattened tree, but <tree> code is used to work with
    // <xbl:children> which is not, so recurse in <slot> here.
    if (child->IsHTMLElement(nsGkAtoms::slot)) {
      if (nsIContent* c = GetImmediateChild(child, aTag)) {
        return c;
      }
    }
  }

  return nullptr;
}

nsIContent* nsTreeUtils::GetDescendantChild(nsIContent* aContainer,
                                            nsAtom* aTag) {
  dom::FlattenedChildIterator iter(aContainer);
  for (nsIContent* child = iter.GetNextChild(); child;
       child = iter.GetNextChild()) {
    if (child->IsXULElement(aTag)) {
      return child;
    }

    child = GetDescendantChild(child, aTag);
    if (child) {
      return child;
    }
  }

  return nullptr;
}

nsresult nsTreeUtils::UpdateSortIndicators(dom::Element* aColumn,
                                           const nsAString& aDirection) {
  aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortDirection, aDirection,
                   true);
  aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortActive, u"true"_ns, true);

  // Unset sort attribute(s) on the other columns
  nsCOMPtr<nsIContent> parentContent = aColumn->GetParent();
  if (parentContent && parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
                                                         kNameSpaceID_XUL)) {
    for (nsINode* childContent = parentContent->GetFirstChild(); childContent;
         childContent = childContent->GetNextSibling()) {
      if (childContent != aColumn &&
          childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
                                           kNameSpaceID_XUL)) {
        childContent->AsElement()->UnsetAttr(kNameSpaceID_None,
                                             nsGkAtoms::sortDirection, true);
        childContent->AsElement()->UnsetAttr(kNameSpaceID_None,
                                             nsGkAtoms::sortActive, true);
      }
    }
  }

  return NS_OK;
}

nsresult nsTreeUtils::GetColumnIndex(dom::Element* aColumn, int32_t* aResult) {
  nsIContent* parentContent = aColumn->GetParent();
  if (parentContent && parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
                                                         kNameSpaceID_XUL)) {
    int32_t colIndex = 0;

    for (nsINode* childContent = parentContent->GetFirstChild(); childContent;
         childContent = childContent->GetNextSibling()) {
      if (childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
                                           kNameSpaceID_XUL)) {
        if (childContent == aColumn) {
          *aResult = colIndex;
          return NS_OK;
        }
        ++colIndex;
      }
    }
  }

  *aResult = -1;
  return NS_OK;
}