Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / svg / SVGNumberList.cpp
blobcbb9aa80c26e776d3f46afb0814a751476f0f523
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"
12 #include "nsString.h"
13 #include "nsTextFormatter.h"
14 #include "SVGContentUtils.h"
16 namespace mozilla {
18 nsresult SVGNumberList::CopyFrom(const SVGNumberList& rhs) {
19 if (!mNumbers.Assign(rhs.mNumbers, fallible)) {
20 return NS_ERROR_OUT_OF_MEMORY;
22 return NS_OK;
25 void SVGNumberList::GetValueAsString(nsAString& aValue) const {
26 aValue.Truncate();
27 char16_t buf[24];
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.
34 aValue.Append(buf);
35 if (i != last) {
36 aValue.Append(' ');
41 nsresult SVGNumberList::SetValueFromString(const nsAString& aValue) {
42 SVGNumberList temp;
44 nsCharSeparatedTokenizerTemplate<nsContentUtils::IsHTMLWhitespace,
45 nsTokenizerFlags::SeparatorOptional>
46 tokenizer(aValue, ',');
48 while (tokenizer.hasMoreTokens()) {
49 float num;
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);
61 return NS_OK;
64 } // namespace mozilla