Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "nsReadableUtils.h"
7 : #include "nsTreeUtils.h"
8 : #include "ChildIterator.h"
9 : #include "nsCRT.h"
10 : #include "nsIAtom.h"
11 : #include "nsNameSpaceManager.h"
12 : #include "nsGkAtoms.h"
13 : #include "nsIContent.h"
14 :
15 : using namespace mozilla;
16 :
17 : nsresult
18 0 : nsTreeUtils::TokenizeProperties(const nsAString& aProperties, AtomArray & aPropertiesArray)
19 : {
20 0 : nsAString::const_iterator end;
21 0 : aProperties.EndReading(end);
22 :
23 0 : nsAString::const_iterator iter;
24 0 : aProperties.BeginReading(iter);
25 :
26 0 : do {
27 : // Skip whitespace
28 0 : while (iter != end && nsCRT::IsAsciiSpace(*iter))
29 0 : ++iter;
30 :
31 : // If only whitespace, we're done
32 0 : if (iter == end)
33 0 : break;
34 :
35 : // Note the first non-whitespace character
36 0 : nsAString::const_iterator first = iter;
37 :
38 : // Advance to the next whitespace character
39 0 : while (iter != end && ! nsCRT::IsAsciiSpace(*iter))
40 0 : ++iter;
41 :
42 : // XXX this would be nonsensical
43 0 : NS_ASSERTION(iter != first, "eh? something's wrong here");
44 0 : if (iter == first)
45 0 : break;
46 :
47 0 : nsCOMPtr<nsIAtom> atom = NS_Atomize(Substring(first, iter));
48 0 : aPropertiesArray.AppendElement(atom);
49 : } while (iter != end);
50 :
51 0 : return NS_OK;
52 : }
53 :
54 : nsIContent*
55 0 : nsTreeUtils::GetImmediateChild(nsIContent* aContainer, nsIAtom* aTag)
56 : {
57 0 : dom::FlattenedChildIterator iter(aContainer);
58 0 : for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
59 0 : if (child->IsXULElement(aTag)) {
60 0 : return child;
61 : }
62 : }
63 :
64 0 : return nullptr;
65 : }
66 :
67 : nsIContent*
68 0 : nsTreeUtils::GetDescendantChild(nsIContent* aContainer, nsIAtom* aTag)
69 : {
70 0 : dom::FlattenedChildIterator iter(aContainer);
71 0 : for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
72 0 : if (child->IsXULElement(aTag)) {
73 0 : return child;
74 : }
75 :
76 0 : child = GetDescendantChild(child, aTag);
77 0 : if (child) {
78 0 : return child;
79 : }
80 : }
81 :
82 0 : return nullptr;
83 : }
84 :
85 : nsresult
86 0 : nsTreeUtils::UpdateSortIndicators(nsIContent* aColumn, const nsAString& aDirection)
87 : {
88 0 : aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortDirection, aDirection, true);
89 0 : aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortActive, NS_LITERAL_STRING("true"), true);
90 :
91 : // Unset sort attribute(s) on the other columns
92 0 : nsCOMPtr<nsIContent> parentContent = aColumn->GetParent();
93 0 : if (parentContent &&
94 0 : parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
95 0 : kNameSpaceID_XUL)) {
96 0 : uint32_t i, numChildren = parentContent->GetChildCount();
97 0 : for (i = 0; i < numChildren; ++i) {
98 0 : nsCOMPtr<nsIContent> childContent = parentContent->GetChildAt(i);
99 :
100 0 : if (childContent &&
101 0 : childContent != aColumn &&
102 0 : childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
103 : kNameSpaceID_XUL)) {
104 0 : childContent->UnsetAttr(kNameSpaceID_None,
105 0 : nsGkAtoms::sortDirection, true);
106 0 : childContent->UnsetAttr(kNameSpaceID_None,
107 0 : nsGkAtoms::sortActive, true);
108 : }
109 : }
110 : }
111 :
112 0 : return NS_OK;
113 : }
114 :
115 : nsresult
116 0 : nsTreeUtils::GetColumnIndex(nsIContent* aColumn, int32_t* aResult)
117 : {
118 0 : nsIContent* parentContent = aColumn->GetParent();
119 0 : if (parentContent &&
120 0 : parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
121 : kNameSpaceID_XUL)) {
122 0 : uint32_t i, numChildren = parentContent->GetChildCount();
123 0 : int32_t colIndex = 0;
124 0 : for (i = 0; i < numChildren; ++i) {
125 0 : nsIContent *childContent = parentContent->GetChildAt(i);
126 0 : if (childContent &&
127 0 : childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
128 : kNameSpaceID_XUL)) {
129 0 : if (childContent == aColumn) {
130 0 : *aResult = colIndex;
131 0 : return NS_OK;
132 : }
133 0 : ++colIndex;
134 : }
135 : }
136 : }
137 :
138 0 : *aResult = -1;
139 0 : return NS_OK;
140 : }
|