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
TokenizeProperties(const nsAString & aProperties,AtomArray & aPropertiesArray)18 nsresult nsTreeUtils::TokenizeProperties(const nsAString& aProperties,
19 AtomArray& aPropertiesArray) {
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)) ++iter;
29
30 // If only whitespace, we're done
31 if (iter == end) break;
32
33 // Note the first non-whitespace character
34 nsAString::const_iterator first = iter;
35
36 // Advance to the next whitespace character
37 while (iter != end && !nsCRT::IsAsciiSpace(*iter)) ++iter;
38
39 // XXX this would be nonsensical
40 NS_ASSERTION(iter != first, "eh? something's wrong here");
41 if (iter == first) break;
42
43 RefPtr<nsAtom> atom = NS_Atomize(Substring(first, iter));
44 aPropertiesArray.AppendElement(atom);
45 } while (iter != end);
46
47 return NS_OK;
48 }
49
GetImmediateChild(nsIContent * aContainer,nsAtom * aTag)50 nsIContent* nsTreeUtils::GetImmediateChild(nsIContent* aContainer,
51 nsAtom* aTag) {
52 dom::FlattenedChildIterator iter(aContainer);
53 for (nsIContent* child = iter.GetNextChild(); child;
54 child = iter.GetNextChild()) {
55 if (child->IsXULElement(aTag)) {
56 return child;
57 }
58 // <slot> is in the flattened tree, but <tree> code is used to work with
59 // <xbl:children> which is not, so recurse in <slot> here.
60 if (child->IsHTMLElement(nsGkAtoms::slot)) {
61 if (nsIContent* c = GetImmediateChild(child, aTag)) {
62 return c;
63 }
64 }
65 }
66
67 return nullptr;
68 }
69
GetDescendantChild(nsIContent * aContainer,nsAtom * aTag)70 nsIContent* nsTreeUtils::GetDescendantChild(nsIContent* aContainer,
71 nsAtom* aTag) {
72 dom::FlattenedChildIterator iter(aContainer);
73 for (nsIContent* child = iter.GetNextChild(); child;
74 child = iter.GetNextChild()) {
75 if (child->IsXULElement(aTag)) {
76 return child;
77 }
78
79 child = GetDescendantChild(child, aTag);
80 if (child) {
81 return child;
82 }
83 }
84
85 return nullptr;
86 }
87
UpdateSortIndicators(dom::Element * aColumn,const nsAString & aDirection)88 nsresult nsTreeUtils::UpdateSortIndicators(dom::Element* aColumn,
89 const nsAString& aDirection) {
90 aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortDirection, aDirection,
91 true);
92 aColumn->SetAttr(kNameSpaceID_None, nsGkAtoms::sortActive,
93 NS_LITERAL_STRING("true"), true);
94
95 // Unset sort attribute(s) on the other columns
96 nsCOMPtr<nsIContent> parentContent = aColumn->GetParent();
97 if (parentContent && parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
98 kNameSpaceID_XUL)) {
99 for (nsINode* childContent = parentContent->GetFirstChild(); childContent;
100 childContent = childContent->GetNextSibling()) {
101 if (childContent != aColumn &&
102 childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
103 kNameSpaceID_XUL)) {
104 childContent->AsElement()->UnsetAttr(kNameSpaceID_None,
105 nsGkAtoms::sortDirection, true);
106 childContent->AsElement()->UnsetAttr(kNameSpaceID_None,
107 nsGkAtoms::sortActive, true);
108 }
109 }
110 }
111
112 return NS_OK;
113 }
114
GetColumnIndex(dom::Element * aColumn,int32_t * aResult)115 nsresult nsTreeUtils::GetColumnIndex(dom::Element* aColumn, int32_t* aResult) {
116 nsIContent* parentContent = aColumn->GetParent();
117 if (parentContent && parentContent->NodeInfo()->Equals(nsGkAtoms::treecols,
118 kNameSpaceID_XUL)) {
119 int32_t colIndex = 0;
120
121 for (nsINode* childContent = parentContent->GetFirstChild(); childContent;
122 childContent = childContent->GetNextSibling()) {
123 if (childContent->NodeInfo()->Equals(nsGkAtoms::treecol,
124 kNameSpaceID_XUL)) {
125 if (childContent == aColumn) {
126 *aResult = colIndex;
127 return NS_OK;
128 }
129 ++colIndex;
130 }
131 }
132 }
133
134 *aResult = -1;
135 return NS_OK;
136 }
137