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 "SVGNumberList.h"
9 #include "mozilla/ArrayUtils.h"
10 #include "nsCharSeparatedTokenizer.h"
11 #include "nsContentUtils.h"
13 #include "nsTextFormatter.h"
14 #include "SVGContentUtils.h"
18 nsresult
SVGNumberList::CopyFrom(const SVGNumberList
& rhs
) {
19 if (!mNumbers
.Assign(rhs
.mNumbers
, fallible
)) {
20 return NS_ERROR_OUT_OF_MEMORY
;
25 void SVGNumberList::GetValueAsString(nsAString
& aValue
) const {
28 uint32_t last
= mNumbers
.Length() - 1;
29 for (uint32_t i
= 0; i
< mNumbers
.Length(); ++i
) {
30 // Would like to use aValue.AppendPrintf("%f", mNumbers[i]), but it's not
31 // possible to always avoid trailing zeros.
32 nsTextFormatter::snprintf(buf
, ArrayLength(buf
), u
"%g",
34 // We ignore OOM, since it's not useful for us to return an error.
42 nsresult
SVGNumberList::SetValueFromString(const nsAString
& aValue
) {
45 nsCharSeparatedTokenizerTemplate
<nsContentUtils::IsHTMLWhitespace
,
46 nsTokenizerFlags::SeparatorOptional
>
47 tokenizer(aValue
, ',');
49 while (tokenizer
.hasMoreTokens()) {
51 if (!SVGContentUtils::ParseNumber(tokenizer
.nextToken(), num
)) {
52 return NS_ERROR_DOM_SYNTAX_ERR
;
54 if (!temp
.AppendItem(num
)) {
55 return NS_ERROR_OUT_OF_MEMORY
;
58 if (tokenizer
.separatorAfterCurrentToken()) {
59 return NS_ERROR_DOM_SYNTAX_ERR
; // trailing comma
61 return CopyFrom(temp
);
64 } // namespace mozilla