SVGStringList.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* -*- Mode: C++; tab-width: 8; 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 "SVGStringList.h"
  6. #include "nsError.h"
  7. #include "nsCharSeparatedTokenizer.h"
  8. #include "nsString.h"
  9. #include "nsWhitespaceTokenizer.h"
  10. #include "SVGContentUtils.h"
  11. namespace mozilla {
  12. nsresult
  13. SVGStringList::CopyFrom(const SVGStringList& rhs)
  14. {
  15. if (!mStrings.Assign(rhs.mStrings, fallible)) {
  16. return NS_ERROR_OUT_OF_MEMORY;
  17. }
  18. mIsSet = true;
  19. return NS_OK;
  20. }
  21. void
  22. SVGStringList::GetValue(nsAString& aValue) const
  23. {
  24. aValue.Truncate();
  25. uint32_t last = mStrings.Length() - 1;
  26. for (uint32_t i = 0; i < mStrings.Length(); ++i) {
  27. aValue.Append(mStrings[i]);
  28. if (i != last) {
  29. if (mIsCommaSeparated) {
  30. aValue.Append(',');
  31. }
  32. aValue.Append(' ');
  33. }
  34. }
  35. }
  36. nsresult
  37. SVGStringList::SetValue(const nsAString& aValue)
  38. {
  39. SVGStringList temp;
  40. if (mIsCommaSeparated) {
  41. nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
  42. tokenizer(aValue, ',');
  43. while (tokenizer.hasMoreTokens()) {
  44. if (!temp.AppendItem(tokenizer.nextToken())) {
  45. return NS_ERROR_OUT_OF_MEMORY;
  46. }
  47. }
  48. if (tokenizer.separatorAfterCurrentToken()) {
  49. return NS_ERROR_DOM_SYNTAX_ERR; // trailing comma
  50. }
  51. } else {
  52. nsWhitespaceTokenizerTemplate<IsSVGWhitespace> tokenizer(aValue);
  53. while (tokenizer.hasMoreTokens()) {
  54. if (!temp.AppendItem(tokenizer.nextToken())) {
  55. return NS_ERROR_OUT_OF_MEMORY;
  56. }
  57. }
  58. }
  59. return CopyFrom(temp);
  60. }
  61. } // namespace mozilla