2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef SVGListPropertyTearOffHelper_h
32 #define SVGListPropertyTearOffHelper_h
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "core/svg/properties/SVGPropertyTearOff.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/TypeTraits.h"
41 template<typename ItemProperty>
42 class ListItemPropertyTraits {
44 typedef ItemProperty ItemPropertyType;
45 typedef typename ItemPropertyType::TearOffType ItemTearOffType;
47 static PassRefPtr<ItemPropertyType> getValueForInsertionFromTearOff(PassRefPtr<ItemTearOffType> passNewItem, SVGElement* contextElement, const QualifiedName& attributeName)
49 RefPtr<ItemTearOffType> newItem = passNewItem;
51 // |newItem| is immutable, OR
52 // |newItem| belongs to a SVGElement, but it does not belong to an animated list
53 // (for example: "textElement.x.baseVal.appendItem(rectElement.width.baseVal)")
54 if (newItem->isImmutable()
55 || (newItem->contextElement() && !newItem->target()->ownerList())) {
56 // We have to copy the incoming |newItem|,
57 // Otherwise we'll end up having two tearoffs that operate on the same SVGProperty. Consider the example below:
58 // SVGRectElements SVGAnimatedLength 'width' property baseVal points to the same tear off object
59 // that's inserted into SVGTextElements SVGAnimatedLengthList 'x'. textElement.x.baseVal.getItem(0).value += 150 would
60 // mutate the rectElement width _and_ the textElement x list. That's obviously wrong, take care of that.
61 return newItem->target()->clone();
64 newItem->attachToSVGElementAttribute(contextElement, attributeName);
65 return newItem->target();
68 static PassRefPtr<ItemTearOffType> createTearOff(PassRefPtr<ItemPropertyType> value, SVGElement* contextElement, PropertyIsAnimValType propertyIsAnimVal, const QualifiedName& attributeName)
70 return ItemTearOffType::create(value, contextElement, propertyIsAnimVal, attributeName);
74 template<typename Derived, typename ListProperty>
75 class SVGListPropertyTearOffHelper : public SVGPropertyTearOff<ListProperty> {
77 typedef ListProperty ListPropertyType;
78 typedef typename ListPropertyType::ItemPropertyType ItemPropertyType;
79 typedef typename ItemPropertyType::TearOffType ItemTearOffType;
80 typedef ListItemPropertyTraits<ItemPropertyType> ItemTraits;
82 // SVG*List DOM interface:
84 // WebIDL requires "unsigned long" type instead of size_t.
85 unsigned long length()
87 return toDerived()->target()->length();
90 void clear(ExceptionState& exceptionState)
92 if (toDerived()->isImmutable()) {
93 exceptionState.throwDOMException(NoModificationAllowedError, "The object is read-only.");
97 toDerived()->target()->clear();
100 PassRefPtr<ItemTearOffType> initialize(PassRefPtr<ItemTearOffType> passItem, ExceptionState& exceptionState)
102 RefPtr<ItemTearOffType> item = passItem;
104 if (toDerived()->isImmutable()) {
105 exceptionState.throwDOMException(NoModificationAllowedError, "The object is read-only.");
110 exceptionState.throwTypeError("Lists must be initialized with a valid item.");
114 RefPtr<ItemPropertyType> value = toDerived()->target()->initialize(getValueForInsertionFromTearOff(item));
115 toDerived()->commitChange();
117 return createItemTearOff(value.release());
120 PassRefPtr<ItemTearOffType> getItem(unsigned long index, ExceptionState& exceptionState)
122 RefPtr<ItemPropertyType> value = toDerived()->target()->getItem(index, exceptionState);
123 return createItemTearOff(value.release());
126 PassRefPtr<ItemTearOffType> insertItemBefore(PassRefPtr<ItemTearOffType> passItem, unsigned long index, ExceptionState& exceptionState)
128 RefPtr<ItemTearOffType> item = passItem;
130 if (toDerived()->isImmutable()) {
131 exceptionState.throwDOMException(NoModificationAllowedError, "The object is read-only.");
136 exceptionState.throwTypeError("An invalid item cannot be inserted to a list.");
140 RefPtr<ItemPropertyType> value = toDerived()->target()->insertItemBefore(getValueForInsertionFromTearOff(item), index);
141 toDerived()->commitChange();
143 return createItemTearOff(value.release());
146 PassRefPtr<ItemTearOffType> replaceItem(PassRefPtr<ItemTearOffType> passItem, unsigned long index, ExceptionState& exceptionState)
148 RefPtr<ItemTearOffType> item = passItem;
150 if (toDerived()->isImmutable()) {
151 exceptionState.throwDOMException(NoModificationAllowedError, "The object is read-only.");
156 exceptionState.throwTypeError("An invalid item cannot be replaced with an existing list item.");
160 RefPtr<ItemPropertyType> value = toDerived()->target()->replaceItem(getValueForInsertionFromTearOff(item), index, exceptionState);
161 toDerived()->commitChange();
163 return createItemTearOff(value.release());
166 bool anonymousIndexedSetter(unsigned index, PassRefPtr<ItemTearOffType> passItem, ExceptionState& exceptionState)
168 replaceItem(passItem, index, exceptionState);
172 PassRefPtr<ItemTearOffType> removeItem(unsigned long index, ExceptionState& exceptionState)
174 RefPtr<ItemPropertyType> value = toDerived()->target()->removeItem(index, exceptionState);
175 toDerived()->commitChange();
177 return createItemTearOff(value.release());
180 PassRefPtr<ItemTearOffType> appendItem(PassRefPtr<ItemTearOffType> passItem, ExceptionState& exceptionState)
182 RefPtr<ItemTearOffType> item = passItem;
184 if (toDerived()->isImmutable()) {
185 exceptionState.throwDOMException(NoModificationAllowedError, "The object is read-only.");
190 exceptionState.throwTypeError("An invalid item cannot be appended to a list.");
194 RefPtr<ItemPropertyType> value = toDerived()->target()->appendItem(getValueForInsertionFromTearOff(item));
195 toDerived()->commitChange();
197 return createItemTearOff(value.release());
201 SVGListPropertyTearOffHelper(PassRefPtr<ListPropertyType> target, SVGElement* contextElement, PropertyIsAnimValType propertyIsAnimVal, const QualifiedName& attributeName = QualifiedName::null())
202 : SVGPropertyTearOff<ListPropertyType>(target, contextElement, propertyIsAnimVal, attributeName)
206 PassRefPtr<ItemPropertyType> getValueForInsertionFromTearOff(PassRefPtr<ItemTearOffType> passNewItem)
208 return ItemTraits::getValueForInsertionFromTearOff(passNewItem, toDerived()->contextElement(), toDerived()->attributeName());
211 PassRefPtr<ItemTearOffType> createItemTearOff(PassRefPtr<ItemPropertyType> value)
216 if (value->ownerList() == toDerived()->target())
217 return ItemTraits::createTearOff(value, toDerived()->contextElement(), toDerived()->propertyIsAnimVal(), toDerived()->attributeName());
219 return ItemTraits::createTearOff(value, 0, PropertyIsNotAnimVal, QualifiedName::null());
223 Derived* toDerived() { return static_cast<Derived*>(this); }
228 #endif // SVGListPropertyTearOffHelper_h