layout/xul/tree/nsTreeUtils.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial