Bug 1265584 [wpt PR 11167] - [Gecko Bug 1265584] Move wptrunner marionette usage...
[gecko.git] / dom / svg / SVGPointList.cpp
blob5c44f24625886209ce6177987659083634873b30
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 "mozilla/ArrayUtils.h"
9 #include "SVGPointList.h"
10 #include "nsCharSeparatedTokenizer.h"
11 #include "nsTextFormatter.h"
12 #include "SVGContentUtils.h"
14 namespace mozilla {
16 nsresult
17 SVGPointList::CopyFrom(const SVGPointList& rhs)
19 if (!mItems.Assign(rhs.mItems, fallible)) {
20 return NS_ERROR_OUT_OF_MEMORY;
22 return NS_OK;
25 void
26 SVGPointList::GetValueAsString(nsAString& aValue) const
28 aValue.Truncate();
29 char16_t buf[50];
30 uint32_t last = mItems.Length() - 1;
31 for (uint32_t i = 0; i < mItems.Length(); ++i) {
32 // Would like to use aValue.AppendPrintf("%f,%f", item.mX, item.mY),
33 // but it's not possible to always avoid trailing zeros.
34 nsTextFormatter::snprintf(buf, ArrayLength(buf),
35 u"%g,%g",
36 double(mItems[i].mX), double(mItems[i].mY));
37 // We ignore OOM, since it's not useful for us to return an error.
38 aValue.Append(buf);
39 if (i != last) {
40 aValue.Append(' ');
45 nsresult
46 SVGPointList::SetValueFromString(const nsAString& aValue)
48 // The spec says that the list is parsed and accepted up to the first error
49 // encountered, so we must call CopyFrom even if an error occurs. We still
50 // want to throw any error code from setAttribute if there's a problem
51 // though, so we must take care to return any error code.
53 nsresult rv = NS_OK;
55 SVGPointList temp;
57 nsCharSeparatedTokenizerTemplate<nsContentUtils::IsHTMLWhitespace>
58 tokenizer(aValue, ',', nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
60 while (tokenizer.hasMoreTokens()) {
62 const nsAString& token = tokenizer.nextToken();
64 RangedPtr<const char16_t> iter =
65 SVGContentUtils::GetStartRangedPtr(token);
66 const RangedPtr<const char16_t> end =
67 SVGContentUtils::GetEndRangedPtr(token);
69 float x;
70 if (!SVGContentUtils::ParseNumber(iter, end, x)) {
71 rv = NS_ERROR_DOM_SYNTAX_ERR;
72 break;
75 float y;
76 if (iter == end) {
77 if (!tokenizer.hasMoreTokens() ||
78 !SVGContentUtils::ParseNumber(tokenizer.nextToken(), y)) {
79 rv = NS_ERROR_DOM_SYNTAX_ERR;
80 break;
82 } else {
83 // It's possible for the token to be 10-30 which has
84 // no separator but needs to be parsed as 10, -30
85 const nsAString& leftOver = Substring(iter.get(), end.get());
86 if (leftOver[0] != '-' || !SVGContentUtils::ParseNumber(leftOver, y)) {
87 rv = NS_ERROR_DOM_SYNTAX_ERR;
88 break;
91 temp.AppendItem(SVGPoint(x, y));
93 if (tokenizer.separatorAfterCurrentToken()) {
94 rv = NS_ERROR_DOM_SYNTAX_ERR; // trailing comma
96 nsresult rv2 = CopyFrom(temp);
97 if (NS_FAILED(rv2)) {
98 return rv2; // prioritize OOM error code over syntax errors
100 return rv;
103 } // namespace mozilla