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::dom
;
15 using namespace mozilla::dom::SVGPreserveAspectRatio_Binding
;
19 NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(DOMSVGPreserveAspectRatio
,
22 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMSVGPreserveAspectRatio
, AddRef
)
23 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMSVGPreserveAspectRatio
, Release
)
25 static const char* sAlignStrings
[] = {
26 "none", "xMinYMin", "xMidYMin", "xMaxYMin", "xMinYMid",
27 "xMidYMid", "xMaxYMid", "xMinYMax", "xMidYMax", "xMaxYMax"};
29 static const char* sMeetOrSliceStrings
[] = {"meet", "slice"};
31 static uint16_t GetAlignForString(const nsAString
& aAlignString
) {
32 for (uint32_t i
= 0; i
< ArrayLength(sAlignStrings
); i
++) {
33 if (aAlignString
.EqualsASCII(sAlignStrings
[i
])) {
34 return (i
+ SVG_ALIGN_MIN_VALID
);
38 return SVG_PRESERVEASPECTRATIO_UNKNOWN
;
41 static uint16_t GetMeetOrSliceForString(const nsAString
& aMeetOrSlice
) {
42 for (uint32_t i
= 0; i
< ArrayLength(sMeetOrSliceStrings
); i
++) {
43 if (aMeetOrSlice
.EqualsASCII(sMeetOrSliceStrings
[i
])) {
44 return (i
+ SVG_MEETORSLICE_MIN_VALID
);
48 return SVG_MEETORSLICE_UNKNOWN
;
52 nsresult
SVGPreserveAspectRatio::FromString(const nsAString
& aString
,
53 SVGPreserveAspectRatio
* aValue
) {
54 nsWhitespaceTokenizerTemplate
<nsContentUtils::IsHTMLWhitespace
> tokenizer(
56 if (tokenizer
.whitespaceBeforeFirstToken() || !tokenizer
.hasMoreTokens()) {
57 return NS_ERROR_DOM_SYNTAX_ERR
;
59 const nsAString
& token
= tokenizer
.nextToken();
62 SVGPreserveAspectRatio val
;
64 rv
= val
.SetAlign(GetAlignForString(token
));
67 return NS_ERROR_DOM_SYNTAX_ERR
;
70 if (tokenizer
.hasMoreTokens()) {
71 rv
= val
.SetMeetOrSlice(GetMeetOrSliceForString(tokenizer
.nextToken()));
73 return NS_ERROR_DOM_SYNTAX_ERR
;
76 val
.SetMeetOrSlice(SVG_MEETORSLICE_MEET
);
79 if (tokenizer
.whitespaceAfterCurrentToken()) {
80 return NS_ERROR_DOM_SYNTAX_ERR
;
87 void SVGPreserveAspectRatio::ToString(nsAString
& aValueAsString
) const {
88 MOZ_ASSERT(mAlign
>= SVG_ALIGN_MIN_VALID
&& mAlign
<= SVG_ALIGN_MAX_VALID
,
90 aValueAsString
.AssignASCII(sAlignStrings
[mAlign
- SVG_ALIGN_MIN_VALID
]);
92 if (mAlign
!= uint8_t(SVG_PRESERVEASPECTRATIO_NONE
)) {
93 MOZ_ASSERT(mMeetOrSlice
>= SVG_MEETORSLICE_MIN_VALID
&&
94 mMeetOrSlice
<= SVG_MEETORSLICE_MAX_VALID
,
95 "Unknown meetOrSlice");
96 aValueAsString
.Append(' ');
97 aValueAsString
.AppendASCII(
98 sMeetOrSliceStrings
[mMeetOrSlice
- SVG_MEETORSLICE_MIN_VALID
]);
102 bool SVGPreserveAspectRatio::operator==(
103 const SVGPreserveAspectRatio
& aOther
) const {
104 return mAlign
== aOther
.mAlign
&& mMeetOrSlice
== aOther
.mMeetOrSlice
;
107 JSObject
* DOMSVGPreserveAspectRatio::WrapObject(
108 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
109 return mozilla::dom::SVGPreserveAspectRatio_Binding::Wrap(aCx
, this,
113 uint16_t DOMSVGPreserveAspectRatio::Align() {
115 return mVal
->GetBaseValue().GetAlign();
118 mSVGElement
->FlushAnimations();
119 return mVal
->GetAnimValue().GetAlign();
122 void DOMSVGPreserveAspectRatio::SetAlign(uint16_t aAlign
, ErrorResult
& rv
) {
124 rv
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
127 rv
= mVal
->SetBaseAlign(aAlign
, mSVGElement
);
130 uint16_t DOMSVGPreserveAspectRatio::MeetOrSlice() {
132 return mVal
->GetBaseValue().GetMeetOrSlice();
135 mSVGElement
->FlushAnimations();
136 return mVal
->GetAnimValue().GetMeetOrSlice();
139 void DOMSVGPreserveAspectRatio::SetMeetOrSlice(uint16_t aMeetOrSlice
,
142 rv
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
145 rv
= mVal
->SetBaseMeetOrSlice(aMeetOrSlice
, mSVGElement
);
148 } // namespace mozilla