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
, std::size(buf
), u
"%g", double(mNumbers
[i
]));
33 // We ignore OOM, since it's not useful for us to return an error.
41 nsresult
SVGNumberList::SetValueFromString(const nsAString
& aValue
) {
44 nsCharSeparatedTokenizerTemplate
<nsContentUtils::IsHTMLWhitespace
,
45 nsTokenizerFlags::SeparatorOptional
>
46 tokenizer(aValue
, ',');
48 while (tokenizer
.hasMoreTokens()) {
50 if (!SVGContentUtils::ParseNumber(tokenizer
.nextToken(), num
)) {
51 return NS_ERROR_DOM_SYNTAX_ERR
;
53 if (!temp
.AppendItem(num
)) {
54 return NS_ERROR_OUT_OF_MEMORY
;
57 if (tokenizer
.separatorAfterCurrentToken()) {
58 return NS_ERROR_DOM_SYNTAX_ERR
; // trailing comma
60 mNumbers
= std::move(temp
.mNumbers
);
64 } // namespace mozilla