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/. */
7 #include "mozilla/ArrayUtils.h"
9 #include "SVGPointList.h"
10 #include "nsCharSeparatedTokenizer.h"
11 #include "nsTextFormatter.h"
12 #include "SVGContentUtils.h"
17 SVGPointList::CopyFrom(const SVGPointList
& rhs
)
19 if (!mItems
.Assign(rhs
.mItems
, fallible
)) {
20 return NS_ERROR_OUT_OF_MEMORY
;
26 SVGPointList::GetValueAsString(nsAString
& aValue
) const
30 uint32_t last
= mItems
.Length() - 1;
31 for (uint32_t i
= 0; i
< mItems
.Length(); ++i
) {
32 // Would like to use aValue.AppendPrintf("%f,%f", item.mX, item.mY),
33 // but it's not possible to always avoid trailing zeros.
34 nsTextFormatter::snprintf(buf
, ArrayLength(buf
),
36 double(mItems
[i
].mX
), double(mItems
[i
].mY
));
37 // We ignore OOM, since it's not useful for us to return an error.
46 SVGPointList::SetValueFromString(const nsAString
& aValue
)
48 // The spec says that the list is parsed and accepted up to the first error
49 // encountered, so we must call CopyFrom even if an error occurs. We still
50 // want to throw any error code from setAttribute if there's a problem
51 // though, so we must take care to return any error code.
57 nsCharSeparatedTokenizerTemplate
<nsContentUtils::IsHTMLWhitespace
>
58 tokenizer(aValue
, ',', nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL
);
60 while (tokenizer
.hasMoreTokens()) {
62 const nsAString
& token
= tokenizer
.nextToken();
64 RangedPtr
<const char16_t
> iter
=
65 SVGContentUtils::GetStartRangedPtr(token
);
66 const RangedPtr
<const char16_t
> end
=
67 SVGContentUtils::GetEndRangedPtr(token
);
70 if (!SVGContentUtils::ParseNumber(iter
, end
, x
)) {
71 rv
= NS_ERROR_DOM_SYNTAX_ERR
;
77 if (!tokenizer
.hasMoreTokens() ||
78 !SVGContentUtils::ParseNumber(tokenizer
.nextToken(), y
)) {
79 rv
= NS_ERROR_DOM_SYNTAX_ERR
;
83 // It's possible for the token to be 10-30 which has
84 // no separator but needs to be parsed as 10, -30
85 const nsAString
& leftOver
= Substring(iter
.get(), end
.get());
86 if (leftOver
[0] != '-' || !SVGContentUtils::ParseNumber(leftOver
, y
)) {
87 rv
= NS_ERROR_DOM_SYNTAX_ERR
;
91 temp
.AppendItem(SVGPoint(x
, y
));
93 if (tokenizer
.separatorAfterCurrentToken()) {
94 rv
= NS_ERROR_DOM_SYNTAX_ERR
; // trailing comma
96 nsresult rv2
= CopyFrom(temp
);
98 return rv2
; // prioritize OOM error code over syntax errors
103 } // namespace mozilla