2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 15:15:53 +03:00
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2010-12-08 15:15:53 +03:00
|
|
|
#include "SVGPointList.h"
|
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
#include "nsTextFormatter.h"
|
|
|
|
#include "SVGContentUtils.h"
|
2010-12-08 15:15:53 +03:00
|
|
|
|
2011-04-14 22:47:27 +04:00
|
|
|
namespace mozilla {
|
2010-12-08 15:15:53 +03: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 19:02:17 +04:00
|
|
|
char16_t buf[50];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t last = mItems.Length() - 1;
|
|
|
|
for (uint32_t i = 0; i < mItems.Length(); ++i) {
|
2010-12-08 15:15:53 +03: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-11 09:50:08 +04:00
|
|
|
nsTextFormatter::snprintf(buf, ArrayLength(buf),
|
2013-12-13 05:50:01 +04:00
|
|
|
MOZ_UTF16("%g,%g"),
|
2010-12-08 15:15:53 +03: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 11:50:40 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
const nsAString& token = tokenizer.nextToken();
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
RangedPtr<const char16_t> iter =
|
2013-10-29 21:15:39 +04:00
|
|
|
SVGContentUtils::GetStartRangedPtr(token);
|
2014-01-04 19:02:17 +04:00
|
|
|
const RangedPtr<const char16_t> end =
|
2013-10-29 21:15:39 +04:00
|
|
|
SVGContentUtils::GetEndRangedPtr(token);
|
|
|
|
|
2013-10-01 11:50:40 +04:00
|
|
|
float x;
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!SVGContentUtils::ParseNumber(iter, end, x)) {
|
2010-12-08 15:15:53 +03:00
|
|
|
rv = NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-01 11:50:40 +04:00
|
|
|
|
|
|
|
float y;
|
2013-10-29 21:15:39 +04:00
|
|
|
if (iter == end) {
|
2013-10-01 11:50:40 +04:00
|
|
|
if (!tokenizer.hasMoreTokens() ||
|
|
|
|
!SVGContentUtils::ParseNumber(tokenizer.nextToken(), y)) {
|
2010-12-18 23:12:13 +03:00
|
|
|
rv = NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-01 11:50:40 +04: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 21:15:39 +04:00
|
|
|
const nsAString& leftOver = Substring(iter.get(), end.get());
|
2013-10-01 11:50:40 +04:00
|
|
|
if (leftOver[0] != '-' || !SVGContentUtils::ParseNumber(leftOver, y)) {
|
2010-12-18 23:12:13 +03:00
|
|
|
rv = NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-12-08 15:15:53 +03:00
|
|
|
temp.AppendItem(SVGPoint(x, y));
|
|
|
|
}
|
2013-09-17 16:52:39 +04:00
|
|
|
if (tokenizer.separatorAfterCurrentToken()) {
|
2010-12-08 15:15:53 +03: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 22:47:27 +04:00
|
|
|
|
|
|
|
} // namespace mozilla
|