Coverage Report

Created: 2018-09-25 14:53

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