| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 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 | |
| 13 * distribution. | |
| 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. | |
| 17 * | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #ifndef NewSVGListPropertyTearOffHelper_h | |
| 32 #define NewSVGListPropertyTearOffHelper_h | |
| 33 | |
| 34 #include "bindings/v8/ExceptionState.h" | |
| 35 #include "core/svg/properties/NewSVGPropertyTearOff.h" | |
| 36 #include "wtf/HashMap.h" | |
| 37 #include "wtf/TypeTraits.h" | |
| 38 | |
| 39 namespace WebCore { | |
| 40 | |
| 41 template<typename ItemProperty> | |
| 42 class ListItemPropertyTraits { | |
| 43 public: | |
| 44 typedef ItemProperty ItemPropertyType; | |
| 45 typedef typename ItemPropertyType::TearOffType ItemTearOffType; | |
| 46 | |
| 47 static PassRefPtr<ItemPropertyType> getValueForInsertionFromTearOff(PassRefP
tr<ItemTearOffType> passNewItem) | |
| 48 { | |
| 49 RefPtr<ItemTearOffType> newItem = passNewItem; | |
| 50 | |
| 51 // |newItem| is immutable, OR | |
| 52 // |newItem| belongs to a SVGElement, but it does not belong to an anima
ted list | |
| 53 // (for example: "textElement.x.baseVal.appendItem(rectElement.width.bas
eVal)") | |
| 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 sa
me SVGProperty. Consider the example above: | |
| 58 // SVGRectElements SVGAnimatedLength 'width' property baseVal points
to the same tear off object | |
| 59 // that's inserted into SVGTextElements SVGAnimatedLengthList 'x'. t
extElement.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(); | |
| 62 } | |
| 63 | |
| 64 return newItem->target(); | |
| 65 } | |
| 66 | |
| 67 static PassRefPtr<ItemTearOffType> createTearOff(PassRefPtr<ItemPropertyType
> value, SVGElement* contextElement, PropertyIsAnimValType propertyIsAnimVal, co
nst QualifiedName& attributeName) | |
| 68 { | |
| 69 return ItemTearOffType::create(value, contextElement, propertyIsAnimVal,
attributeName); | |
| 70 } | |
| 71 }; | |
| 72 | |
| 73 template<typename Derived, typename ListProperty> | |
| 74 class NewSVGListPropertyTearOffHelper : public NewSVGPropertyTearOff<ListPropert
y> { | |
| 75 public: | |
| 76 typedef ListProperty ListPropertyType; | |
| 77 typedef typename ListPropertyType::ItemPropertyType ItemPropertyType; | |
| 78 typedef typename ItemPropertyType::TearOffType ItemTearOffType; | |
| 79 typedef ListItemPropertyTraits<ItemPropertyType> ItemTraits; | |
| 80 | |
| 81 // SVG*List DOM interface: | |
| 82 | |
| 83 // WebIDL requires "unsigned long" type instead of size_t. | |
| 84 unsigned long length() | |
| 85 { | |
| 86 return toDerived()->target()->length(); | |
| 87 } | |
| 88 | |
| 89 void clear(ExceptionState& exceptionState) | |
| 90 { | |
| 91 if (toDerived()->isImmutable()) { | |
| 92 exceptionState.throwDOMException(NoModificationAllowedError, "The ob
ject is read-only."); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 toDerived()->target()->clear(); | |
| 97 } | |
| 98 | |
| 99 PassRefPtr<ItemTearOffType> initialize(PassRefPtr<ItemTearOffType> passItem,
ExceptionState& exceptionState) | |
| 100 { | |
| 101 RefPtr<ItemTearOffType> item = passItem; | |
| 102 | |
| 103 if (toDerived()->isImmutable()) { | |
| 104 exceptionState.throwDOMException(NoModificationAllowedError, "The ob
ject is read-only."); | |
| 105 return nullptr; | |
| 106 } | |
| 107 | |
| 108 if (!item) { | |
| 109 exceptionState.throwTypeError("Lists must be initialized with a vali
d item."); | |
| 110 return nullptr; | |
| 111 } | |
| 112 | |
| 113 RefPtr<ItemPropertyType> value = toDerived()->target()->initialize(getVa
lueForInsertionFromTearOff(item)); | |
| 114 toDerived()->commitChange(); | |
| 115 | |
| 116 return createItemTearOff(value.release()); | |
| 117 } | |
| 118 | |
| 119 PassRefPtr<ItemTearOffType> getItem(unsigned long index, ExceptionState& exc
eptionState) | |
| 120 { | |
| 121 RefPtr<ItemPropertyType> value = toDerived()->target()->getItem(index, e
xceptionState); | |
| 122 return createItemTearOff(value.release()); | |
| 123 } | |
| 124 | |
| 125 PassRefPtr<ItemTearOffType> insertItemBefore(PassRefPtr<ItemTearOffType> pas
sItem, unsigned long index, ExceptionState& exceptionState) | |
| 126 { | |
| 127 RefPtr<ItemTearOffType> item = passItem; | |
| 128 | |
| 129 if (toDerived()->isImmutable()) { | |
| 130 exceptionState.throwDOMException(NoModificationAllowedError, "The ob
ject is read-only."); | |
| 131 return nullptr; | |
| 132 } | |
| 133 | |
| 134 if (!item) { | |
| 135 exceptionState.throwTypeError("An invalid item cannot be inserted to
a list."); | |
| 136 return nullptr; | |
| 137 } | |
| 138 | |
| 139 RefPtr<ItemPropertyType> value = toDerived()->target()->insertItemBefore
(getValueForInsertionFromTearOff(item), index); | |
| 140 toDerived()->commitChange(); | |
| 141 | |
| 142 return createItemTearOff(value.release()); | |
| 143 } | |
| 144 | |
| 145 PassRefPtr<ItemTearOffType> replaceItem(PassRefPtr<ItemTearOffType> passItem
, unsigned long index, ExceptionState& exceptionState) | |
| 146 { | |
| 147 RefPtr<ItemTearOffType> item = passItem; | |
| 148 | |
| 149 if (toDerived()->isImmutable()) { | |
| 150 exceptionState.throwDOMException(NoModificationAllowedError, "The ob
ject is read-only."); | |
| 151 return nullptr; | |
| 152 } | |
| 153 | |
| 154 if (!item) { | |
| 155 exceptionState.throwTypeError("An invalid item cannot be replaced wi
th an existing list item."); | |
| 156 return nullptr; | |
| 157 } | |
| 158 | |
| 159 RefPtr<ItemPropertyType> value = toDerived()->target()->replaceItem(getV
alueForInsertionFromTearOff(item), index, exceptionState); | |
| 160 toDerived()->commitChange(); | |
| 161 | |
| 162 return createItemTearOff(value.release()); | |
| 163 } | |
| 164 | |
| 165 bool anonymousIndexedSetter(unsigned index, PassRefPtr<ItemTearOffType> pass
Item, ExceptionState& exceptionState) | |
| 166 { | |
| 167 replaceItem(passItem, index, exceptionState); | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 171 PassRefPtr<ItemTearOffType> removeItem(unsigned long index, ExceptionState&
exceptionState) | |
| 172 { | |
| 173 RefPtr<ItemPropertyType> value = toDerived()->target()->removeItem(index
, exceptionState); | |
| 174 toDerived()->commitChange(); | |
| 175 | |
| 176 return createItemTearOff(value.release()); | |
| 177 } | |
| 178 | |
| 179 PassRefPtr<ItemTearOffType> appendItem(PassRefPtr<ItemTearOffType> passItem,
ExceptionState& exceptionState) | |
| 180 { | |
| 181 RefPtr<ItemTearOffType> item = passItem; | |
| 182 | |
| 183 if (toDerived()->isImmutable()) { | |
| 184 exceptionState.throwDOMException(NoModificationAllowedError, "The ob
ject is read-only."); | |
| 185 return nullptr; | |
| 186 } | |
| 187 | |
| 188 if (!item) { | |
| 189 exceptionState.throwTypeError("An invalid item cannot be appended to
a list."); | |
| 190 return nullptr; | |
| 191 } | |
| 192 | |
| 193 RefPtr<ItemPropertyType> value = toDerived()->target()->appendItem(getVa
lueForInsertionFromTearOff(item)); | |
| 194 toDerived()->commitChange(); | |
| 195 | |
| 196 return createItemTearOff(value.release()); | |
| 197 } | |
| 198 | |
| 199 protected: | |
| 200 NewSVGListPropertyTearOffHelper(PassRefPtr<ListPropertyType> target, SVGElem
ent* contextElement, PropertyIsAnimValType propertyIsAnimVal, const QualifiedNam
e& attributeName = nullQName()) | |
| 201 : NewSVGPropertyTearOff<ListPropertyType>(target, contextElement, proper
tyIsAnimVal, attributeName) | |
| 202 { | |
| 203 } | |
| 204 | |
| 205 static PassRefPtr<ItemPropertyType> getValueForInsertionFromTearOff(PassRefP
tr<ItemTearOffType> passNewItem) | |
| 206 { | |
| 207 return ItemTraits::getValueForInsertionFromTearOff(passNewItem); | |
| 208 } | |
| 209 | |
| 210 PassRefPtr<ItemTearOffType> createItemTearOff(PassRefPtr<ItemPropertyType> v
alue) | |
| 211 { | |
| 212 if (!value) | |
| 213 return nullptr; | |
| 214 | |
| 215 return ItemTraits::createTearOff(value, toDerived()->contextElement(), t
oDerived()->propertyIsAnimVal(), toDerived()->attributeName()); | |
| 216 } | |
| 217 | |
| 218 private: | |
| 219 Derived* toDerived() { return static_cast<Derived*>(this); } | |
| 220 }; | |
| 221 | |
| 222 } | |
| 223 | |
| 224 #endif // NewSVGListPropertyTearOffHelper_h | |
| OLD | NEW |