1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla SVG Project code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Robert Longson <longsonr@gmail.com>
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "DOMSVGStringList.h"
39 : #include "DOMSVGTests.h"
40 : #include "nsDOMError.h"
41 : #include "nsCOMPtr.h"
42 : #include "nsSVGAttrTearoffTable.h"
43 :
44 : // See the architecture comment in this file's header.
45 :
46 : namespace mozilla {
47 :
48 : static nsSVGAttrTearoffTable<SVGStringList, DOMSVGStringList>
49 1464 : sSVGStringListTearoffTable;
50 :
51 1464 : NS_SVG_VAL_IMPL_CYCLE_COLLECTION(DOMSVGStringList, mElement)
52 :
53 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGStringList)
54 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGStringList)
55 :
56 : } // namespace mozilla
57 :
58 : DOMCI_DATA(SVGStringList, mozilla::DOMSVGStringList)
59 : namespace mozilla {
60 :
61 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGStringList)
62 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMSVGStringList)
63 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
64 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(SVGStringList)
65 0 : NS_INTERFACE_MAP_END
66 :
67 :
68 : /* static */ already_AddRefed<DOMSVGStringList>
69 0 : DOMSVGStringList::GetDOMWrapper(SVGStringList *aList,
70 : nsSVGElement *aElement,
71 : bool aIsConditionalProcessingAttribute,
72 : PRUint8 aAttrEnum)
73 : {
74 : DOMSVGStringList *wrapper =
75 0 : sSVGStringListTearoffTable.GetTearoff(aList);
76 0 : if (!wrapper) {
77 : wrapper = new DOMSVGStringList(aElement,
78 : aIsConditionalProcessingAttribute,
79 0 : aAttrEnum);
80 0 : sSVGStringListTearoffTable.AddTearoff(aList, wrapper);
81 : }
82 0 : NS_ADDREF(wrapper);
83 0 : return wrapper;
84 : }
85 :
86 0 : DOMSVGStringList::~DOMSVGStringList()
87 : {
88 : // Script no longer has any references to us.
89 0 : sSVGStringListTearoffTable.RemoveTearoff(&InternalList());
90 0 : }
91 :
92 : // ----------------------------------------------------------------------------
93 : // nsIDOMSVGStringList implementation:
94 :
95 : NS_IMETHODIMP
96 0 : DOMSVGStringList::GetNumberOfItems(PRUint32 *aNumberOfItems)
97 : {
98 0 : *aNumberOfItems = InternalList().Length();
99 0 : return NS_OK;
100 : }
101 :
102 : NS_IMETHODIMP
103 0 : DOMSVGStringList::GetLength(PRUint32 *aLength)
104 : {
105 0 : return GetNumberOfItems(aLength);
106 : }
107 :
108 : NS_IMETHODIMP
109 0 : DOMSVGStringList::Clear()
110 : {
111 0 : if (InternalList().IsExplicitlySet()) {
112 : nsAttrValue emptyOrOldValue =
113 : mElement->WillChangeStringList(mIsConditionalProcessingAttribute,
114 0 : mAttrEnum);
115 0 : InternalList().Clear();
116 : mElement->DidChangeStringList(mIsConditionalProcessingAttribute,
117 0 : mAttrEnum, emptyOrOldValue);
118 : }
119 0 : return NS_OK;
120 : }
121 :
122 : NS_IMETHODIMP
123 0 : DOMSVGStringList::Initialize(const nsAString & newItem, nsAString & _retval)
124 : {
125 0 : if (InternalList().IsExplicitlySet()) {
126 0 : InternalList().Clear();
127 : }
128 0 : return InsertItemBefore(newItem, 0, _retval);
129 : }
130 :
131 : NS_IMETHODIMP
132 0 : DOMSVGStringList::GetItem(PRUint32 index,
133 : nsAString & _retval)
134 : {
135 0 : if (index >= InternalList().Length()) {
136 0 : return NS_ERROR_DOM_INDEX_SIZE_ERR;
137 : }
138 0 : _retval = InternalList()[index];
139 0 : return NS_OK;
140 : }
141 :
142 : NS_IMETHODIMP
143 0 : DOMSVGStringList::InsertItemBefore(const nsAString & newItem,
144 : PRUint32 index,
145 : nsAString & _retval)
146 : {
147 0 : if (newItem.IsEmpty()) { // takes care of DOMStringIsNull too
148 0 : return NS_ERROR_DOM_SVG_INVALID_VALUE_ERR;
149 : }
150 0 : index = NS_MIN(index, InternalList().Length());
151 :
152 : // Ensure we have enough memory so we can avoid complex error handling below:
153 0 : if (!InternalList().SetCapacity(InternalList().Length() + 1)) {
154 0 : return NS_ERROR_OUT_OF_MEMORY;
155 : }
156 :
157 : nsAttrValue emptyOrOldValue =
158 : mElement->WillChangeStringList(mIsConditionalProcessingAttribute,
159 0 : mAttrEnum);
160 0 : InternalList().InsertItem(index, newItem);
161 :
162 : mElement->DidChangeStringList(mIsConditionalProcessingAttribute, mAttrEnum,
163 0 : emptyOrOldValue);
164 0 : _retval = newItem;
165 0 : return NS_OK;
166 : }
167 :
168 : NS_IMETHODIMP
169 0 : DOMSVGStringList::ReplaceItem(const nsAString & newItem,
170 : PRUint32 index,
171 : nsAString & _retval)
172 : {
173 0 : if (newItem.IsEmpty()) { // takes care of DOMStringIsNull too
174 0 : return NS_ERROR_DOM_SVG_INVALID_VALUE_ERR;
175 : }
176 0 : if (index >= InternalList().Length()) {
177 0 : return NS_ERROR_DOM_INDEX_SIZE_ERR;
178 : }
179 :
180 0 : _retval = InternalList()[index];
181 : nsAttrValue emptyOrOldValue =
182 : mElement->WillChangeStringList(mIsConditionalProcessingAttribute,
183 0 : mAttrEnum);
184 0 : InternalList().ReplaceItem(index, newItem);
185 :
186 : mElement->DidChangeStringList(mIsConditionalProcessingAttribute, mAttrEnum,
187 0 : emptyOrOldValue);
188 0 : return NS_OK;
189 : }
190 :
191 : NS_IMETHODIMP
192 0 : DOMSVGStringList::RemoveItem(PRUint32 index,
193 : nsAString & _retval)
194 : {
195 0 : if (index >= InternalList().Length()) {
196 0 : return NS_ERROR_DOM_INDEX_SIZE_ERR;
197 : }
198 :
199 : nsAttrValue emptyOrOldValue =
200 : mElement->WillChangeStringList(mIsConditionalProcessingAttribute,
201 0 : mAttrEnum);
202 0 : InternalList().RemoveItem(index);
203 :
204 : mElement->DidChangeStringList(mIsConditionalProcessingAttribute, mAttrEnum,
205 0 : emptyOrOldValue);
206 0 : return NS_OK;
207 : }
208 :
209 : NS_IMETHODIMP
210 0 : DOMSVGStringList::AppendItem(const nsAString & newItem,
211 : nsAString & _retval)
212 : {
213 0 : return InsertItemBefore(newItem, InternalList().Length(), _retval);
214 : }
215 :
216 : SVGStringList &
217 0 : DOMSVGStringList::InternalList()
218 : {
219 0 : if (mIsConditionalProcessingAttribute) {
220 0 : nsCOMPtr<DOMSVGTests> tests = do_QueryInterface(mElement);
221 0 : return tests->mStringListAttributes[mAttrEnum];
222 : }
223 0 : return mElement->GetStringListInfo().mStringLists[mAttrEnum];
224 : }
225 :
226 4392 : } // namespace mozilla
|