1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla SVG Project code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Robert Longson <longsonr@gmail.com>
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "mozilla/Util.h"
39 :
40 : #include "SVGStringList.h"
41 : #include "nsSVGElement.h"
42 : #include "nsDOMError.h"
43 : #include "nsString.h"
44 : #include "nsSVGUtils.h"
45 : #include "nsTextFormatter.h"
46 : #include "nsWhitespaceTokenizer.h"
47 : #include "nsCharSeparatedTokenizer.h"
48 : #include "nsMathUtils.h"
49 :
50 : namespace mozilla {
51 :
52 : nsresult
53 0 : SVGStringList::CopyFrom(const SVGStringList& rhs)
54 : {
55 0 : if (!mStrings.SetCapacity(rhs.Length())) {
56 : // Yes, we do want fallible alloc here
57 0 : return NS_ERROR_OUT_OF_MEMORY;
58 : }
59 0 : mStrings = rhs.mStrings;
60 0 : mIsSet = true;
61 0 : return NS_OK;
62 : }
63 :
64 : void
65 0 : SVGStringList::GetValue(nsAString& aValue) const
66 : {
67 0 : aValue.Truncate();
68 0 : PRUint32 last = mStrings.Length() - 1;
69 0 : for (PRUint32 i = 0; i < mStrings.Length(); ++i) {
70 0 : aValue.Append(mStrings[i]);
71 0 : if (i != last) {
72 0 : if (mIsCommaSeparated) {
73 0 : aValue.Append(',');
74 : }
75 0 : aValue.Append(' ');
76 : }
77 : }
78 0 : }
79 :
80 : nsresult
81 0 : SVGStringList::SetValue(const nsAString& aValue)
82 : {
83 0 : SVGStringList temp;
84 :
85 0 : if (mIsCommaSeparated) {
86 : nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
87 0 : tokenizer(aValue, ',');
88 :
89 0 : while (tokenizer.hasMoreTokens()) {
90 0 : if (!temp.AppendItem(tokenizer.nextToken())) {
91 0 : return NS_ERROR_OUT_OF_MEMORY;
92 : }
93 : }
94 0 : if (tokenizer.lastTokenEndedWithSeparator()) {
95 0 : return NS_ERROR_DOM_SYNTAX_ERR; // trailing comma
96 : }
97 : } else {
98 0 : nsWhitespaceTokenizer tokenizer(aValue);
99 :
100 0 : while (tokenizer.hasMoreTokens()) {
101 0 : if (!temp.AppendItem(tokenizer.nextToken())) {
102 0 : return NS_ERROR_OUT_OF_MEMORY;
103 : }
104 : }
105 : }
106 :
107 0 : return CopyFrom(temp);
108 : }
109 :
110 : } // namespace mozilla
|