Bug 1265584 [wpt PR 11167] - [Gecko Bug 1265584] Move wptrunner marionette usage...
[gecko.git] / dom / svg / SVGPreserveAspectRatio.cpp
blob2a912098785bcf78fea7521dff5c79219ba3a8b7
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 "SVGPreserveAspectRatio.h"
9 #include "mozilla/dom/SVGPreserveAspectRatioBinding.h"
10 #include "nsContentUtils.h"
11 #include "nsWhitespaceTokenizer.h"
12 #include "SVGAnimatedPreserveAspectRatio.h"
14 using namespace mozilla;
15 using namespace dom;
16 using namespace SVGPreserveAspectRatio_Binding;
18 NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(DOMSVGPreserveAspectRatio, mSVGElement)
20 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGPreserveAspectRatio)
21 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGPreserveAspectRatio)
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGPreserveAspectRatio)
24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
25 NS_INTERFACE_MAP_ENTRY(nsISupports)
26 NS_INTERFACE_MAP_END
28 static const char *sAlignStrings[] =
29 { "none", "xMinYMin", "xMidYMin", "xMaxYMin", "xMinYMid", "xMidYMid",
30 "xMaxYMid", "xMinYMax", "xMidYMax", "xMaxYMax" };
32 static const char *sMeetOrSliceStrings[] = { "meet", "slice" };
34 static uint16_t
35 GetAlignForString(const nsAString &aAlignString)
37 for (uint32_t i = 0 ; i < ArrayLength(sAlignStrings) ; i++) {
38 if (aAlignString.EqualsASCII(sAlignStrings[i])) {
39 return (i + SVG_ALIGN_MIN_VALID);
43 return SVG_PRESERVEASPECTRATIO_UNKNOWN;
46 static uint16_t
47 GetMeetOrSliceForString(const nsAString &aMeetOrSlice)
49 for (uint32_t i = 0 ; i < ArrayLength(sMeetOrSliceStrings) ; i++) {
50 if (aMeetOrSlice.EqualsASCII(sMeetOrSliceStrings[i])) {
51 return (i + SVG_MEETORSLICE_MIN_VALID);
55 return SVG_MEETORSLICE_UNKNOWN;
58 /* static */ nsresult
59 SVGPreserveAspectRatio::FromString(const nsAString& aString,
60 SVGPreserveAspectRatio* aValue)
62 nsWhitespaceTokenizerTemplate<nsContentUtils::IsHTMLWhitespace>
63 tokenizer(aString);
64 if (tokenizer.whitespaceBeforeFirstToken() ||
65 !tokenizer.hasMoreTokens()) {
66 return NS_ERROR_DOM_SYNTAX_ERR;
68 const nsAString &token = tokenizer.nextToken();
70 nsresult rv;
71 SVGPreserveAspectRatio val;
73 rv = val.SetAlign(GetAlignForString(token));
75 if (NS_FAILED(rv)) {
76 return NS_ERROR_DOM_SYNTAX_ERR;
79 if (tokenizer.hasMoreTokens()) {
80 rv = val.SetMeetOrSlice(GetMeetOrSliceForString(tokenizer.nextToken()));
81 if (NS_FAILED(rv)) {
82 return NS_ERROR_DOM_SYNTAX_ERR;
84 } else {
85 val.SetMeetOrSlice(SVG_MEETORSLICE_MEET);
88 if (tokenizer.whitespaceAfterCurrentToken()) {
89 return NS_ERROR_DOM_SYNTAX_ERR;
92 *aValue = val;
93 return NS_OK;
96 void
97 SVGPreserveAspectRatio::ToString(nsAString& aValueAsString) const
99 MOZ_ASSERT(mAlign >= SVG_ALIGN_MIN_VALID && mAlign <= SVG_ALIGN_MAX_VALID,
100 "Unknown align");
101 aValueAsString.AssignASCII(sAlignStrings[mAlign - SVG_ALIGN_MIN_VALID]);
103 if (mAlign != uint8_t(SVG_PRESERVEASPECTRATIO_NONE)) {
104 MOZ_ASSERT(mMeetOrSlice >= SVG_MEETORSLICE_MIN_VALID &&
105 mMeetOrSlice <= SVG_MEETORSLICE_MAX_VALID,
106 "Unknown meetOrSlice");
107 aValueAsString.Append(' ');
108 aValueAsString.AppendASCII(sMeetOrSliceStrings[mMeetOrSlice - SVG_MEETORSLICE_MIN_VALID]);
112 bool
113 SVGPreserveAspectRatio::operator==(const SVGPreserveAspectRatio& aOther) const
115 return mAlign == aOther.mAlign &&
116 mMeetOrSlice == aOther.mMeetOrSlice;
119 JSObject*
120 DOMSVGPreserveAspectRatio::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
122 return mozilla::dom::SVGPreserveAspectRatio_Binding::Wrap(aCx, this, aGivenProto);
125 uint16_t
126 DOMSVGPreserveAspectRatio::Align()
128 if (mIsBaseValue) {
129 return mVal->GetBaseValue().GetAlign();
132 mSVGElement->FlushAnimations();
133 return mVal->GetAnimValue().GetAlign();
136 void
137 DOMSVGPreserveAspectRatio::SetAlign(uint16_t aAlign, ErrorResult& rv)
139 if (!mIsBaseValue) {
140 rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
141 return;
143 rv = mVal->SetBaseAlign(aAlign, mSVGElement);
146 uint16_t
147 DOMSVGPreserveAspectRatio::MeetOrSlice()
149 if (mIsBaseValue) {
150 return mVal->GetBaseValue().GetMeetOrSlice();
153 mSVGElement->FlushAnimations();
154 return mVal->GetAnimValue().GetMeetOrSlice();
157 void
158 DOMSVGPreserveAspectRatio::SetMeetOrSlice(uint16_t aMeetOrSlice, ErrorResult& rv)
160 if (!mIsBaseValue) {
161 rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
162 return;
164 rv = mVal->SetBaseMeetOrSlice(aMeetOrSlice, mSVGElement);