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 "nsContentUtils.h"
12 #include "nsTextFormatter.h"
13 #include "SVGContentUtils.h"
17 nsresult
SVGPointList::CopyFrom(const SVGPointList
& rhs
) {
18 if (!mItems
.Assign(rhs
.mItems
, fallible
)) {
19 return NS_ERROR_OUT_OF_MEMORY
;
24 void SVGPointList::GetValueAsString(nsAString
& aValue
) const {
27 uint32_t last
= mItems
.Length() - 1;
28 for (uint32_t i
= 0; i
< mItems
.Length(); ++i
) {
29 // Would like to use aValue.AppendPrintf("%f,%f", item.mX, item.mY),
30 // but it's not possible to always avoid trailing zeros.
31 nsTextFormatter::snprintf(buf
, ArrayLength(buf
), u
"%g,%g",
32 double(mItems
[i
].mX
), double(mItems
[i
].mY
));
33 // We ignore OOM, since it's not useful for us to return an error.
41 nsresult
SVGPointList::SetValueFromString(const nsAString
& aValue
) {
42 // The spec says that the list is parsed and accepted up to the first error
43 // encountered, so we must call CopyFrom even if an error occurs. We still
44 // want to throw any error code from setAttribute if there's a problem
45 // though, so we must take care to return any error code.
51 nsCharSeparatedTokenizerTemplate
<nsContentUtils::IsHTMLWhitespace
,
52 nsTokenizerFlags::SeparatorOptional
>
53 tokenizer(aValue
, ',');
55 while (tokenizer
.hasMoreTokens()) {
56 const nsAString
& token
= tokenizer
.nextToken();
58 RangedPtr
<const char16_t
> iter
= SVGContentUtils::GetStartRangedPtr(token
);
59 const RangedPtr
<const char16_t
> end
=
60 SVGContentUtils::GetEndRangedPtr(token
);
63 if (!SVGContentUtils::ParseNumber(iter
, end
, x
)) {
64 rv
= NS_ERROR_DOM_SYNTAX_ERR
;
70 if (!tokenizer
.hasMoreTokens() ||
71 !SVGContentUtils::ParseNumber(tokenizer
.nextToken(), y
)) {
72 rv
= NS_ERROR_DOM_SYNTAX_ERR
;
76 // It's possible for the token to be 10-30 which has
77 // no separator but needs to be parsed as 10, -30
78 const nsAString
& leftOver
= Substring(iter
.get(), end
.get());
79 if (leftOver
[0] != '-' || !SVGContentUtils::ParseNumber(leftOver
, y
)) {
80 rv
= NS_ERROR_DOM_SYNTAX_ERR
;
84 temp
.AppendItem(SVGPoint(x
, y
));
86 if (tokenizer
.separatorAfterCurrentToken()) {
87 rv
= NS_ERROR_DOM_SYNTAX_ERR
; // trailing comma
89 nsresult rv2
= CopyFrom(temp
);
91 return rv2
; // prioritize OOM error code over syntax errors
96 } // namespace mozilla