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/. */
6 
7 /* DOM object representing lists of values in DOM computed style */
8 
9 #include "nsDOMCSSValueList.h"
10 #include "mozilla/dom/CSSValueBinding.h"
11 #include "mozilla/dom/CSSValueListBinding.h"
12 #include "mozilla/Move.h"
13 
14 using namespace mozilla;
15 using namespace mozilla::dom;
16 
nsDOMCSSValueList(bool aCommaDelimited,bool aReadonly)17 nsDOMCSSValueList::nsDOMCSSValueList(bool aCommaDelimited, bool aReadonly)
18     : CSSValue(), mCommaDelimited(aCommaDelimited), mReadonly(aReadonly) {}
19 
~nsDOMCSSValueList()20 nsDOMCSSValueList::~nsDOMCSSValueList() {}
21 
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCSSValueList)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCSSValueList)23 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCSSValueList)
24 
25 // QueryInterface implementation for nsDOMCSSValueList
26 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMCSSValueList)
27   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
28   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, CSSValue)
29 NS_INTERFACE_MAP_END
30 
31 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsDOMCSSValueList, mCSSValues)
32 
33 JSObject* nsDOMCSSValueList::WrapObject(JSContext* cx,
34                                         JS::Handle<JSObject*> aGivenProto) {
35   return dom::CSSValueListBinding::Wrap(cx, this, aGivenProto);
36 }
37 
AppendCSSValue(already_AddRefed<CSSValue> aValue)38 void nsDOMCSSValueList::AppendCSSValue(already_AddRefed<CSSValue> aValue) {
39   RefPtr<CSSValue> val = aValue;
40   mCSSValues.AppendElement(Move(val));
41 }
42 
GetCssText(nsAString & aCssText)43 void nsDOMCSSValueList::GetCssText(nsAString& aCssText) {
44   aCssText.Truncate();
45 
46   uint32_t count = mCSSValues.Length();
47 
48   nsAutoString separator;
49   if (mCommaDelimited) {
50     separator.AssignLiteral(", ");
51   } else {
52     separator.Assign(char16_t(' '));
53   }
54 
55   nsAutoString tmpStr;
56   for (uint32_t i = 0; i < count; ++i) {
57     CSSValue* cssValue = mCSSValues[i];
58     NS_ASSERTION(cssValue,
59                  "Eek!  Someone filled the value list with null CSSValues!");
60     ErrorResult dummy;
61     if (cssValue) {
62       cssValue->GetCssText(tmpStr, dummy);
63 
64       if (tmpStr.IsEmpty()) {
65 #ifdef DEBUG_caillon
66         NS_ERROR("Eek!  An empty CSSValue!  Bad!");
67 #endif
68 
69         continue;
70       }
71 
72       // If this isn't the first item in the list, then
73       // it's ok to append a separator.
74       if (!aCssText.IsEmpty()) {
75         aCssText.Append(separator);
76       }
77       aCssText.Append(tmpStr);
78     }
79   }
80 }
81 
GetCssText(nsString & aCssText,ErrorResult & aRv)82 void nsDOMCSSValueList::GetCssText(nsString& aCssText, ErrorResult& aRv) {
83   GetCssText(aCssText);
84 }
85 
SetCssText(const nsAString & aText,ErrorResult & aRv)86 void nsDOMCSSValueList::SetCssText(const nsAString& aText, ErrorResult& aRv) {
87   if (mReadonly) {
88     aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
89     return;
90   }
91 
92   MOZ_ASSERT_UNREACHABLE("Can't SetCssText yet: please write me!");
93 }
94 
CssValueType() const95 uint16_t nsDOMCSSValueList::CssValueType() const {
96   return CSSValueBinding::CSS_VALUE_LIST;
97 }
98