2010-12-08 04:15:53 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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-08 04:15:53 -08:00
|
|
|
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-10 22:50:08 -07:00
|
|
|
|
2010-12-08 04:15:53 -08:00
|
|
|
#include "SVGPointList.h"
|
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2012-09-22 12:26:05 -07:00
|
|
|
#include "nsTextFormatter.h"
|
|
|
|
#include "SVGContentUtils.h"
|
2010-12-08 04:15:53 -08:00
|
|
|
|
2011-04-14 11:47:27 -07:00
|
|
|
namespace mozilla {
|
2010-12-08 04:15:53 -08:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
SVGPointList::CopyFrom(const SVGPointList& rhs)
|
|
|
|
{
|
|
|
|
if (!SetCapacity(rhs.Length())) {
|
|
|
|
// Yes, we do want fallible alloc here
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mItems = rhs.mItems;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SVGPointList::GetValueAsString(nsAString& aValue) const
|
|
|
|
{
|
|
|
|
aValue.Truncate();
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t buf[50];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t last = mItems.Length() - 1;
|
|
|
|
for (uint32_t i = 0; i < mItems.Length(); ++i) {
|
2010-12-08 04:15:53 -08:00
|
|
|
// Would like to use aValue.AppendPrintf("%f,%f", item.mX, item.mY),
|
|
|
|
// but it's not possible to always avoid trailing zeros.
|
2011-10-10 22:50:08 -07:00
|
|
|
nsTextFormatter::snprintf(buf, ArrayLength(buf),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("%g,%g"),
|
2010-12-08 04:15:53 -08:00
|
|
|
double(mItems[i].mX), double(mItems[i].mY));
|
|
|
|
// We ignore OOM, since it's not useful for us to return an error.
|
|
|
|
aValue.Append(buf);
|
|
|
|
if (i != last) {
|
|
|
|
aValue.Append(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SVGPointList::SetValueFromString(const nsAString& aValue)
|
|
|
|
{
|
|
|
|
// The spec says that the list is parsed and accepted up to the first error
|
|
|
|
// encountered, so we must call CopyFrom even if an error occurs. We still
|
|
|
|
// want to throw any error code from setAttribute if there's a problem
|
|
|
|
// though, so we must take care to return any error code.
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
SVGPointList temp;
|
|
|
|
|
|
|
|
nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
|
|
|
|
tokenizer(aValue, ',', nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
|
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
2013-10-01 00:50:40 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
const nsAString& token = tokenizer.nextToken();
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
RangedPtr<const char16_t> iter =
|
2013-10-29 10:15:39 -07:00
|
|
|
SVGContentUtils::GetStartRangedPtr(token);
|
2014-01-04 07:02:17 -08:00
|
|
|
const RangedPtr<const char16_t> end =
|
2013-10-29 10:15:39 -07:00
|
|
|
SVGContentUtils::GetEndRangedPtr(token);
|
|
|
|
|
2013-10-01 00:50:40 -07:00
|
|
|
float x;
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SVGContentUtils::ParseNumber(iter, end, x)) {
|
2010-12-08 04:15:53 -08:00
|
|
|
rv = NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-01 00:50:40 -07:00
|
|
|
|
|
|
|
float y;
|
2013-10-29 10:15:39 -07:00
|
|
|
if (iter == end) {
|
2013-10-01 00:50:40 -07:00
|
|
|
if (!tokenizer.hasMoreTokens() ||
|
|
|
|
!SVGContentUtils::ParseNumber(tokenizer.nextToken(), y)) {
|
2010-12-18 12:12:13 -08:00
|
|
|
rv = NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-01 00:50:40 -07:00
|
|
|
} else {
|
|
|
|
// It's possible for the token to be 10-30 which has
|
|
|
|
// no separator but needs to be parsed as 10, -30
|
2013-10-29 10:15:39 -07:00
|
|
|
const nsAString& leftOver = Substring(iter.get(), end.get());
|
2013-10-01 00:50:40 -07:00
|
|
|
if (leftOver[0] != '-' || !SVGContentUtils::ParseNumber(leftOver, y)) {
|
2010-12-18 12:12:13 -08:00
|
|
|
rv = NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-12-08 04:15:53 -08:00
|
|
|
temp.AppendItem(SVGPoint(x, y));
|
|
|
|
}
|
2013-09-17 05:52:39 -07:00
|
|
|
if (tokenizer.separatorAfterCurrentToken()) {
|
2010-12-08 04:15:53 -08:00
|
|
|
rv = NS_ERROR_DOM_SYNTAX_ERR; // trailing comma
|
|
|
|
}
|
|
|
|
nsresult rv2 = CopyFrom(temp);
|
|
|
|
if (NS_FAILED(rv2)) {
|
|
|
|
return rv2; // prioritize OOM error code over syntax errors
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2011-04-14 11:47:27 -07:00
|
|
|
|
|
|
|
} // namespace mozilla
|