SVGLengthList.cpp (2130B)
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/. */ 6 7 #include "SVGLengthList.h" 8 9 #include "SVGAnimatedLengthList.h" 10 #include "SVGContentUtils.h" 11 #include "SVGElement.h" 12 #include "SVGLength.h" 13 #include "nsCharSeparatedTokenizer.h" 14 #include "nsContentUtils.h" 15 #include "nsError.h" 16 #include "nsString.h" 17 18 namespace mozilla { 19 20 nsresult SVGLengthList::CopyFrom(const SVGLengthList& rhs) { 21 if (!mLengths.Assign(rhs.mLengths, fallible)) { 22 return NS_ERROR_OUT_OF_MEMORY; 23 } 24 return NS_OK; 25 } 26 27 void SVGLengthList::GetValueAsString(nsAString& aValue) const { 28 aValue.Truncate(); 29 uint32_t last = mLengths.Length() - 1; 30 for (uint32_t i = 0; i < mLengths.Length(); ++i) { 31 nsAutoString length; 32 mLengths[i].GetValueAsString(length); 33 // We ignore OOM, since it's not useful for us to return an error. 34 aValue.Append(length); 35 if (i != last) { 36 aValue.Append(' '); 37 } 38 } 39 } 40 41 nsresult SVGLengthList::SetValueFromString(const nsAString& aValue) { 42 SVGLengthList temp; 43 44 nsCharSeparatedTokenizerTemplate<nsContentUtils::IsHTMLWhitespace, 45 nsTokenizerFlags::SeparatorOptional> 46 tokenizer(aValue, ','); 47 48 while (tokenizer.hasMoreTokens()) { 49 SVGLength length; 50 if (!length.SetValueFromString(tokenizer.nextToken())) { 51 return NS_ERROR_DOM_SYNTAX_ERR; 52 } 53 if (!temp.AppendItem(length)) { 54 return NS_ERROR_OUT_OF_MEMORY; 55 } 56 } 57 if (tokenizer.separatorAfterCurrentToken()) { 58 return NS_ERROR_DOM_SYNTAX_ERR; // trailing comma 59 } 60 mLengths = std::move(temp.mLengths); 61 return NS_OK; 62 } 63 64 bool SVGLengthList::operator==(const SVGLengthList& rhs) const { 65 if (Length() != rhs.Length()) { 66 return false; 67 } 68 for (uint32_t i = 0; i < Length(); ++i) { 69 if (!(mLengths[i] == rhs.mLengths[i])) { 70 return false; 71 } 72 } 73 return true; 74 } 75 76 } // namespace mozilla