2010-12-03 16:40:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-12-03 16:40:23 +00:00
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2010-12-03 16:40:23 +00:00
|
|
|
#include "SVGNumberList.h"
|
2012-09-22 19:26:05 +00:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2010-12-03 16:40:23 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTextFormatter.h"
|
2012-09-22 19:26:05 +00:00
|
|
|
#include "SVGContentUtils.h"
|
2010-12-03 16:40:23 +00:00
|
|
|
|
2011-04-14 18:47:27 +00:00
|
|
|
namespace mozilla {
|
2010-12-03 16:40:23 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
SVGNumberList::CopyFrom(const SVGNumberList& rhs)
|
|
|
|
{
|
|
|
|
if (!mNumbers.SetCapacity(rhs.Length())) {
|
|
|
|
// Yes, we do want fallible alloc here
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mNumbers = rhs.mNumbers;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SVGNumberList::GetValueAsString(nsAString& aValue) const
|
|
|
|
{
|
|
|
|
aValue.Truncate();
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t buf[24];
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t last = mNumbers.Length() - 1;
|
|
|
|
for (uint32_t i = 0; i < mNumbers.Length(); ++i) {
|
2010-12-03 16:40:23 +00:00
|
|
|
// Would like to use aValue.AppendPrintf("%f", mNumbers[i]), but it's not
|
|
|
|
// possible to always avoid trailing zeros.
|
2011-10-11 05:50:08 +00:00
|
|
|
nsTextFormatter::snprintf(buf, ArrayLength(buf),
|
2013-12-13 01:50:01 +00:00
|
|
|
MOZ_UTF16("%g"),
|
2010-12-03 16:40:23 +00:00
|
|
|
double(mNumbers[i]));
|
|
|
|
// We ignore OOM, since it's not useful for us to return an error.
|
|
|
|
aValue.Append(buf);
|
|
|
|
if (i != last) {
|
|
|
|
aValue.Append(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SVGNumberList::SetValueFromString(const nsAString& aValue)
|
|
|
|
{
|
|
|
|
SVGNumberList temp;
|
|
|
|
|
|
|
|
nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
|
|
|
|
tokenizer(aValue, ',', nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
|
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
2013-10-01 07:50:40 +00:00
|
|
|
float num;
|
|
|
|
if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), num)) {
|
2010-12-03 16:40:23 +00:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
2011-11-09 12:58:34 +00:00
|
|
|
if (!temp.AppendItem(num)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2010-12-03 16:40:23 +00:00
|
|
|
}
|
2013-09-17 12:52:39 +00:00
|
|
|
if (tokenizer.separatorAfterCurrentToken()) {
|
2010-12-03 16:40:23 +00:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR; // trailing comma
|
|
|
|
}
|
|
|
|
return CopyFrom(temp);
|
|
|
|
}
|
2011-04-14 18:47:27 +00:00
|
|
|
|
|
|
|
} // namespace mozilla
|