nsTreeUtils.cpp 4.0 KB

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