Bug 1698238 return default dictionary from GetUserMediaRequest#getConstraints() if...
[gecko.git] / dom / svg / SVGPreserveAspectRatio.cpp
blob32906e264f524696fa87f69a7cb9cb819231c3db
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;
17 namespace mozilla {
19 NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(DOMSVGPreserveAspectRatio,
20 mSVGElement)
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;
51 /* static */
52 nsresult SVGPreserveAspectRatio::FromString(const nsAString& aString,
53 SVGPreserveAspectRatio* aValue) {
54 nsWhitespaceTokenizerTemplate<nsContentUtils::IsHTMLWhitespace> tokenizer(
55 aString);
56 if (tokenizer.whitespaceBeforeFirstToken() || !tokenizer.hasMoreTokens()) {
57 return NS_ERROR_DOM_SYNTAX_ERR;
59 const nsAString& token = tokenizer.nextToken();
61 nsresult rv;
62 SVGPreserveAspectRatio val;
64 rv = val.SetAlign(GetAlignForString(token));
66 if (NS_FAILED(rv)) {
67 return NS_ERROR_DOM_SYNTAX_ERR;
70 if (tokenizer.hasMoreTokens()) {
71 rv = val.SetMeetOrSlice(GetMeetOrSliceForString(tokenizer.nextToken()));
72 if (NS_FAILED(rv)) {
73 return NS_ERROR_DOM_SYNTAX_ERR;
75 } else {
76 val.SetMeetOrSlice(SVG_MEETORSLICE_MEET);
79 if (tokenizer.whitespaceAfterCurrentToken()) {
80 return NS_ERROR_DOM_SYNTAX_ERR;
83 *aValue = val;
84 return NS_OK;
87 void SVGPreserveAspectRatio::ToString(nsAString& aValueAsString) const {
88 MOZ_ASSERT(mAlign >= SVG_ALIGN_MIN_VALID && mAlign <= SVG_ALIGN_MAX_VALID,
89 "Unknown align");
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,
110 aGivenProto);
113 uint16_t DOMSVGPreserveAspectRatio::Align() {
114 if (mIsBaseValue) {
115 return mVal->GetBaseValue().GetAlign();
118 mSVGElement->FlushAnimations();
119 return mVal->GetAnimValue().GetAlign();
122 void DOMSVGPreserveAspectRatio::SetAlign(uint16_t aAlign, ErrorResult& rv) {
123 if (!mIsBaseValue) {
124 rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
125 return;
127 rv = mVal->SetBaseAlign(aAlign, mSVGElement);
130 uint16_t DOMSVGPreserveAspectRatio::MeetOrSlice() {
131 if (mIsBaseValue) {
132 return mVal->GetBaseValue().GetMeetOrSlice();
135 mSVGElement->FlushAnimations();
136 return mVal->GetAnimValue().GetMeetOrSlice();
139 void DOMSVGPreserveAspectRatio::SetMeetOrSlice(uint16_t aMeetOrSlice,
140 ErrorResult& rv) {
141 if (!mIsBaseValue) {
142 rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
143 return;
145 rv = mVal->SetBaseMeetOrSlice(aMeetOrSlice, mSVGElement);
148 } // namespace mozilla