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