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 static const char* sAlignStrings
[] = {
23 "none", "xMinYMin", "xMidYMin", "xMaxYMin", "xMinYMid",
24 "xMidYMid", "xMaxYMid", "xMinYMax", "xMidYMax", "xMaxYMax"};
26 static const char* sMeetOrSliceStrings
[] = {"meet", "slice"};
28 static uint16_t GetAlignForString(const nsAString
& aAlignString
) {
29 for (uint32_t i
= 0; i
< std::size(sAlignStrings
); i
++) {
30 if (aAlignString
.EqualsASCII(sAlignStrings
[i
])) {
31 return (i
+ SVG_ALIGN_MIN_VALID
);
35 return SVG_PRESERVEASPECTRATIO_UNKNOWN
;
38 static uint16_t GetMeetOrSliceForString(const nsAString
& aMeetOrSlice
) {
39 for (uint32_t i
= 0; i
< std::size(sMeetOrSliceStrings
); i
++) {
40 if (aMeetOrSlice
.EqualsASCII(sMeetOrSliceStrings
[i
])) {
41 return (i
+ SVG_MEETORSLICE_MIN_VALID
);
45 return SVG_MEETORSLICE_UNKNOWN
;
49 nsresult
SVGPreserveAspectRatio::FromString(const nsAString
& aString
,
50 SVGPreserveAspectRatio
* aValue
) {
51 nsWhitespaceTokenizerTemplate
<nsContentUtils::IsHTMLWhitespace
> tokenizer(
53 if (tokenizer
.whitespaceBeforeFirstToken() || !tokenizer
.hasMoreTokens()) {
54 return NS_ERROR_DOM_SYNTAX_ERR
;
56 const nsAString
& token
= tokenizer
.nextToken();
59 SVGPreserveAspectRatio val
;
61 rv
= val
.SetAlign(GetAlignForString(token
));
64 return NS_ERROR_DOM_SYNTAX_ERR
;
67 if (tokenizer
.hasMoreTokens()) {
68 rv
= val
.SetMeetOrSlice(GetMeetOrSliceForString(tokenizer
.nextToken()));
70 return NS_ERROR_DOM_SYNTAX_ERR
;
73 val
.SetMeetOrSlice(SVG_MEETORSLICE_MEET
);
76 if (tokenizer
.whitespaceAfterCurrentToken()) {
77 return NS_ERROR_DOM_SYNTAX_ERR
;
84 void SVGPreserveAspectRatio::ToString(nsAString
& aValueAsString
) const {
85 MOZ_ASSERT(mAlign
>= SVG_ALIGN_MIN_VALID
&& mAlign
<= SVG_ALIGN_MAX_VALID
,
87 aValueAsString
.AssignASCII(sAlignStrings
[mAlign
- SVG_ALIGN_MIN_VALID
]);
89 if (mAlign
!= uint8_t(SVG_PRESERVEASPECTRATIO_NONE
)) {
90 MOZ_ASSERT(mMeetOrSlice
>= SVG_MEETORSLICE_MIN_VALID
&&
91 mMeetOrSlice
<= SVG_MEETORSLICE_MAX_VALID
,
92 "Unknown meetOrSlice");
93 aValueAsString
.Append(' ');
94 aValueAsString
.AppendASCII(
95 sMeetOrSliceStrings
[mMeetOrSlice
- SVG_MEETORSLICE_MIN_VALID
]);
99 bool SVGPreserveAspectRatio::operator==(
100 const SVGPreserveAspectRatio
& aOther
) const {
101 return mAlign
== aOther
.mAlign
&& mMeetOrSlice
== aOther
.mMeetOrSlice
;
104 JSObject
* DOMSVGPreserveAspectRatio::WrapObject(
105 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
106 return mozilla::dom::SVGPreserveAspectRatio_Binding::Wrap(aCx
, this,
110 uint16_t DOMSVGPreserveAspectRatio::Align() {
112 return mVal
->GetBaseValue().GetAlign();
115 mSVGElement
->FlushAnimations();
116 return mVal
->GetAnimValue().GetAlign();
119 void DOMSVGPreserveAspectRatio::SetAlign(uint16_t aAlign
, ErrorResult
& rv
) {
121 rv
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
124 rv
= mVal
->SetBaseAlign(aAlign
, mSVGElement
);
127 uint16_t DOMSVGPreserveAspectRatio::MeetOrSlice() {
129 return mVal
->GetBaseValue().GetMeetOrSlice();
132 mSVGElement
->FlushAnimations();
133 return mVal
->GetAnimValue().GetMeetOrSlice();
136 void DOMSVGPreserveAspectRatio::SetMeetOrSlice(uint16_t aMeetOrSlice
,
139 rv
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
142 rv
= mVal
->SetBaseMeetOrSlice(aMeetOrSlice
, mSVGElement
);
145 } // namespace mozilla