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
;
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
)
28 static const char *sAlignStrings
[] =
29 { "none", "xMinYMin", "xMidYMin", "xMaxYMin", "xMinYMid", "xMidYMid",
30 "xMaxYMid", "xMinYMax", "xMidYMax", "xMaxYMax" };
32 static const char *sMeetOrSliceStrings
[] = { "meet", "slice" };
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
;
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
;
59 SVGPreserveAspectRatio::FromString(const nsAString
& aString
,
60 SVGPreserveAspectRatio
* aValue
)
62 nsWhitespaceTokenizerTemplate
<nsContentUtils::IsHTMLWhitespace
>
64 if (tokenizer
.whitespaceBeforeFirstToken() ||
65 !tokenizer
.hasMoreTokens()) {
66 return NS_ERROR_DOM_SYNTAX_ERR
;
68 const nsAString
&token
= tokenizer
.nextToken();
71 SVGPreserveAspectRatio val
;
73 rv
= val
.SetAlign(GetAlignForString(token
));
76 return NS_ERROR_DOM_SYNTAX_ERR
;
79 if (tokenizer
.hasMoreTokens()) {
80 rv
= val
.SetMeetOrSlice(GetMeetOrSliceForString(tokenizer
.nextToken()));
82 return NS_ERROR_DOM_SYNTAX_ERR
;
85 val
.SetMeetOrSlice(SVG_MEETORSLICE_MEET
);
88 if (tokenizer
.whitespaceAfterCurrentToken()) {
89 return NS_ERROR_DOM_SYNTAX_ERR
;
97 SVGPreserveAspectRatio::ToString(nsAString
& aValueAsString
) const
99 MOZ_ASSERT(mAlign
>= SVG_ALIGN_MIN_VALID
&& mAlign
<= SVG_ALIGN_MAX_VALID
,
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
]);
113 SVGPreserveAspectRatio::operator==(const SVGPreserveAspectRatio
& aOther
) const
115 return mAlign
== aOther
.mAlign
&&
116 mMeetOrSlice
== aOther
.mMeetOrSlice
;
120 DOMSVGPreserveAspectRatio::WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
)
122 return mozilla::dom::SVGPreserveAspectRatio_Binding::Wrap(aCx
, this, aGivenProto
);
126 DOMSVGPreserveAspectRatio::Align()
129 return mVal
->GetBaseValue().GetAlign();
132 mSVGElement
->FlushAnimations();
133 return mVal
->GetAnimValue().GetAlign();
137 DOMSVGPreserveAspectRatio::SetAlign(uint16_t aAlign
, ErrorResult
& rv
)
140 rv
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
143 rv
= mVal
->SetBaseAlign(aAlign
, mSVGElement
);
147 DOMSVGPreserveAspectRatio::MeetOrSlice()
150 return mVal
->GetBaseValue().GetMeetOrSlice();
153 mSVGElement
->FlushAnimations();
154 return mVal
->GetAnimValue().GetMeetOrSlice();
158 DOMSVGPreserveAspectRatio::SetMeetOrSlice(uint16_t aMeetOrSlice
, ErrorResult
& rv
)
161 rv
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
164 rv
= mVal
->SetBaseMeetOrSlice(aMeetOrSlice
, mSVGElement
);