Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / svg / SVGPreserveAspectRatio.cpp
blob964824496268a2bb25b335a15f9963b4d824dd87
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 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;
48 /* static */
49 nsresult SVGPreserveAspectRatio::FromString(const nsAString& aString,
50 SVGPreserveAspectRatio* aValue) {
51 nsWhitespaceTokenizerTemplate<nsContentUtils::IsHTMLWhitespace> tokenizer(
52 aString);
53 if (tokenizer.whitespaceBeforeFirstToken() || !tokenizer.hasMoreTokens()) {
54 return NS_ERROR_DOM_SYNTAX_ERR;
56 const nsAString& token = tokenizer.nextToken();
58 nsresult rv;
59 SVGPreserveAspectRatio val;
61 rv = val.SetAlign(GetAlignForString(token));
63 if (NS_FAILED(rv)) {
64 return NS_ERROR_DOM_SYNTAX_ERR;
67 if (tokenizer.hasMoreTokens()) {
68 rv = val.SetMeetOrSlice(GetMeetOrSliceForString(tokenizer.nextToken()));
69 if (NS_FAILED(rv)) {
70 return NS_ERROR_DOM_SYNTAX_ERR;
72 } else {
73 val.SetMeetOrSlice(SVG_MEETORSLICE_MEET);
76 if (tokenizer.whitespaceAfterCurrentToken()) {
77 return NS_ERROR_DOM_SYNTAX_ERR;
80 *aValue = val;
81 return NS_OK;
84 void SVGPreserveAspectRatio::ToString(nsAString& aValueAsString) const {
85 MOZ_ASSERT(mAlign >= SVG_ALIGN_MIN_VALID && mAlign <= SVG_ALIGN_MAX_VALID,
86 "Unknown align");
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,
107 aGivenProto);
110 uint16_t DOMSVGPreserveAspectRatio::Align() {
111 if (mIsBaseValue) {
112 return mVal->GetBaseValue().GetAlign();
115 mSVGElement->FlushAnimations();
116 return mVal->GetAnimValue().GetAlign();
119 void DOMSVGPreserveAspectRatio::SetAlign(uint16_t aAlign, ErrorResult& rv) {
120 if (!mIsBaseValue) {
121 rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
122 return;
124 rv = mVal->SetBaseAlign(aAlign, mSVGElement);
127 uint16_t DOMSVGPreserveAspectRatio::MeetOrSlice() {
128 if (mIsBaseValue) {
129 return mVal->GetBaseValue().GetMeetOrSlice();
132 mSVGElement->FlushAnimations();
133 return mVal->GetAnimValue().GetMeetOrSlice();
136 void DOMSVGPreserveAspectRatio::SetMeetOrSlice(uint16_t aMeetOrSlice,
137 ErrorResult& rv) {
138 if (!mIsBaseValue) {
139 rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
140 return;
142 rv = mVal->SetBaseMeetOrSlice(aMeetOrSlice, mSVGElement);
145 } // namespace mozilla