Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 | 164 |
| 165 bool adjustFromToListValues(PassRefPtrWillBeRawPtr<Derived> fromList, PassRe fPtrWillBeRawPtr<Derived> toList, float percentage, AnimationMode); | 165 bool adjustFromToListValues(PassRefPtrWillBeRawPtr<Derived> fromList, PassRe fPtrWillBeRawPtr<Derived> toList, float percentage, AnimationMode); |
| 166 | 166 |
| 167 virtual PassRefPtrWillBeRawPtr<ItemPropertyType> createPaddingItem() const | 167 virtual PassRefPtrWillBeRawPtr<ItemPropertyType> createPaddingItem() const |
| 168 { | 168 { |
| 169 return ItemPropertyType::create(); | 169 return ItemPropertyType::create(); |
| 170 } | 170 } |
| 171 | 171 |
| 172 private: | 172 private: |
| 173 inline bool checkIndexBound(size_t, ExceptionState&); | 173 inline bool checkIndexBound(size_t, ExceptionState&); |
| 174 bool removeFromOldOwnerListAndAdjustIndex(PassRefPtrWillBeRawPtr<ItemPropert yType>, size_t* indexToModify); | |
| 175 size_t findItem(PassRefPtrWillBeRawPtr<ItemPropertyType>); | 174 size_t findItem(PassRefPtrWillBeRawPtr<ItemPropertyType>); |
| 176 | 175 |
| 177 WillBeHeapVector<RefPtrWillBeMember<ItemPropertyType>> m_values; | 176 WillBeHeapVector<RefPtrWillBeMember<ItemPropertyType>> m_values; |
| 178 | 177 |
| 179 static PassRefPtrWillBeRawPtr<Derived> toDerived(PassRefPtrWillBeRawPtr<SVGP ropertyBase> passBase) | 178 static PassRefPtrWillBeRawPtr<Derived> toDerived(PassRefPtrWillBeRawPtr<SVGP ropertyBase> passBase) |
| 180 { | 179 { |
| 181 if (!passBase) | 180 if (!passBase) |
| 182 return nullptr; | 181 return nullptr; |
| 183 | 182 |
| 184 RefPtrWillBeRawPtr<SVGPropertyBase> base = passBase; | 183 RefPtrWillBeRawPtr<SVGPropertyBase> base = passBase; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 214 } | 213 } |
| 215 | 214 |
| 216 m_values.clear(); | 215 m_values.clear(); |
| 217 } | 216 } |
| 218 | 217 |
| 219 template<typename Derived, typename ItemProperty> | 218 template<typename Derived, typename ItemProperty> |
| 220 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::initialize(PassRefPtrWillBeRawPtr<ItemProperty> passNewItem) | 219 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::initialize(PassRefPtrWillBeRawPtr<ItemProperty> passNewItem) |
| 221 { | 220 { |
| 222 RefPtrWillBeRawPtr<ItemPropertyType> newItem = passNewItem; | 221 RefPtrWillBeRawPtr<ItemPropertyType> newItem = passNewItem; |
| 223 | 222 |
| 224 // Spec: If the inserted item is already in a list, it is removed from its p revious list before it is inserted into this list. | 223 // SVG2-Draft Spec: If newItem is already in a list, then a new object is cr eated with the same values as newItem and this item is inserted into the list. |
| 225 removeFromOldOwnerListAndAdjustIndex(newItem, 0); | 224 // Otherwise, newItem itself is inserted into the list. |
| 225 if (newItem->ownerList()) | |
| 226 newItem = newItem->clone(); | |
|
fs
2015/06/17 16:02:05
Should we handle this in the generic ListItemPrope
Shanmuga Pandi
2015/06/18 11:30:45
Done
| |
| 226 | 227 |
| 227 // Spec: Clears all existing current items from the list and re-initializes the list to hold the single item specified by the parameter. | 228 // Spec: Clears all existing current items from the list and re-initializes the list to hold the single item specified by the parameter. |
| 228 clear(); | 229 clear(); |
| 229 append(newItem); | 230 append(newItem); |
| 230 return newItem.release(); | 231 return newItem.release(); |
| 231 } | 232 } |
| 232 | 233 |
| 233 template<typename Derived, typename ItemProperty> | 234 template<typename Derived, typename ItemProperty> |
| 234 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::getItem(size_t index, ExceptionState& exceptionState) | 235 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::getItem(size_t index, ExceptionState& exceptionState) |
| 235 { | 236 { |
| 236 if (!checkIndexBound(index, exceptionState)) | 237 if (!checkIndexBound(index, exceptionState)) |
| 237 return nullptr; | 238 return nullptr; |
| 238 | 239 |
| 239 ASSERT(index < m_values.size()); | 240 ASSERT(index < m_values.size()); |
| 240 ASSERT(m_values.at(index)->ownerList() == this); | 241 ASSERT(m_values.at(index)->ownerList() == this); |
| 241 return m_values.at(index); | 242 return m_values.at(index); |
| 242 } | 243 } |
| 243 | 244 |
| 244 template<typename Derived, typename ItemProperty> | 245 template<typename Derived, typename ItemProperty> |
| 245 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::insertItemBefore(PassRefPtrWillBeRawPtr<ItemProperty> passNewItem, size_t ind ex) | 246 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::insertItemBefore(PassRefPtrWillBeRawPtr<ItemProperty> passNewItem, size_t ind ex) |
| 246 { | 247 { |
| 247 // Spec: If the index is greater than or equal to length, then the new item is appended to the end of the list. | 248 // Spec: If the index is greater than or equal to length, then the new item is appended to the end of the list. |
| 248 if (index > m_values.size()) | 249 if (index > m_values.size()) |
| 249 index = m_values.size(); | 250 index = m_values.size(); |
| 250 | 251 |
| 251 RefPtrWillBeRawPtr<ItemPropertyType> newItem = passNewItem; | 252 RefPtrWillBeRawPtr<ItemPropertyType> newItem = passNewItem; |
| 252 | 253 |
| 253 // Spec: If newItem is already in a list, it is removed from its previous li st before it is inserted into this list. | 254 // SVG2-Draft Spec: If newItem is already in a list, then a new object is cr eated with the same values as newItem and this item is inserted into the list. |
| 254 if (!removeFromOldOwnerListAndAdjustIndex(newItem, &index)) { | 255 // Otherwise, newItem itself is inserted into the list. |
| 255 // Inserting the item before itself is a no-op. | 256 if (newItem->ownerList()) |
| 256 return newItem.release(); | 257 newItem = newItem->clone(); |
| 257 } | |
| 258 | 258 |
| 259 // Spec: Inserts a new item into the list at the specified position. The ind ex of the item before which the new item is to be | 259 // Spec: Inserts a new item into the list at the specified position. The ind ex of the item before which the new item is to be |
| 260 // inserted. The first item is number 0. If the index is equal to 0, then th e new item is inserted at the front of the list. | 260 // inserted. The first item is number 0. If the index is equal to 0, then th e new item is inserted at the front of the list. |
| 261 m_values.insert(index, newItem); | 261 m_values.insert(index, newItem); |
| 262 newItem->setOwnerList(this); | 262 newItem->setOwnerList(this); |
| 263 | 263 |
| 264 return newItem.release(); | 264 return newItem.release(); |
| 265 } | 265 } |
| 266 | 266 |
| 267 template<typename Derived, typename ItemProperty> | 267 template<typename Derived, typename ItemProperty> |
| 268 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::removeItem(size_t index, ExceptionState& exceptionState) | 268 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::removeItem(size_t index, ExceptionState& exceptionState) |
| 269 { | 269 { |
| 270 if (index >= m_values.size()) { | 270 if (index >= m_values.size()) { |
| 271 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::inde xExceedsMaximumBound("index", index, m_values.size())); | 271 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::inde xExceedsMaximumBound("index", index, m_values.size())); |
| 272 return nullptr; | 272 return nullptr; |
| 273 } | 273 } |
| 274 ASSERT(m_values.at(index)->ownerList() == this); | 274 ASSERT(m_values.at(index)->ownerList() == this); |
| 275 RefPtrWillBeRawPtr<ItemPropertyType> oldItem = m_values.at(index); | 275 RefPtrWillBeRawPtr<ItemPropertyType> oldItem = m_values.at(index); |
| 276 m_values.remove(index); | 276 m_values.remove(index); |
| 277 oldItem->setOwnerList(0); | 277 oldItem->setOwnerList(0); |
| 278 return oldItem.release(); | 278 return oldItem.release(); |
| 279 } | 279 } |
| 280 | 280 |
| 281 template<typename Derived, typename ItemProperty> | 281 template<typename Derived, typename ItemProperty> |
| 282 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::appendItem(PassRefPtrWillBeRawPtr<ItemProperty> passNewItem) | 282 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::appendItem(PassRefPtrWillBeRawPtr<ItemProperty> passNewItem) |
| 283 { | 283 { |
| 284 RefPtrWillBeRawPtr<ItemPropertyType> newItem = passNewItem; | 284 RefPtrWillBeRawPtr<ItemPropertyType> newItem = passNewItem; |
| 285 | 285 |
| 286 // Spec: If newItem is already in a list, it is removed from its previous li st before it is inserted into this list. | 286 // SVG2-Draft Spec: If newItem is already in a list, then a new object is cr eated with the same values as newItem and this item is inserted into the list. |
| 287 removeFromOldOwnerListAndAdjustIndex(newItem, 0); | 287 // Otherwise, newItem itself is inserted into the list. |
| 288 if (newItem->ownerList()) | |
| 289 newItem = newItem->clone(); | |
| 288 | 290 |
| 289 // Append the value and wrapper at the end of the list. | 291 // Append the value and wrapper at the end of the list. |
| 290 append(newItem); | 292 append(newItem); |
| 291 | 293 |
| 292 return newItem.release(); | 294 return newItem.release(); |
| 293 } | 295 } |
| 294 | 296 |
| 295 template<typename Derived, typename ItemProperty> | 297 template<typename Derived, typename ItemProperty> |
| 296 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::replaceItem(PassRefPtrWillBeRawPtr<ItemProperty> passNewItem, size_t index, E xceptionState& exceptionState) | 298 PassRefPtrWillBeRawPtr<ItemProperty> SVGListPropertyHelper<Derived, ItemProperty >::replaceItem(PassRefPtrWillBeRawPtr<ItemProperty> passNewItem, size_t index, E xceptionState& exceptionState) |
| 297 { | 299 { |
| 298 if (!checkIndexBound(index, exceptionState)) | 300 if (!checkIndexBound(index, exceptionState)) |
| 299 return nullptr; | 301 return nullptr; |
| 300 | 302 |
| 301 RefPtrWillBeRawPtr<ItemPropertyType> newItem = passNewItem; | 303 RefPtrWillBeRawPtr<ItemPropertyType> newItem = passNewItem; |
| 302 | 304 |
| 303 // Spec: If newItem is already in a list, it is removed from its previous li st before it is inserted into this list. | 305 // SVG2-Draft Spec: If newItem is already in a list, then a new object is cr eated with the same values as newItem and this item is inserted into the list. |
| 304 // Spec: If the item is already in this list, note that the index of the ite m to replace is before the removal of the item. | 306 // Otherwise, newItem itself is inserted into the list. |
| 305 if (!removeFromOldOwnerListAndAdjustIndex(newItem, &index)) { | 307 if (newItem->ownerList()) |
| 306 // Replacing the item with itself is a no-op. | 308 newItem = newItem->clone(); |
| 307 return newItem.release(); | |
| 308 } | |
| 309 | 309 |
| 310 if (m_values.isEmpty()) { | 310 if (m_values.isEmpty()) { |
| 311 // 'newItem' already lived in our list, we removed it, and now we're emp ty, which means there's nothing to replace. | 311 // 'newItem' already lived in our list, we removed it, and now we're emp ty, which means there's nothing to replace. |
| 312 exceptionState.throwDOMException(IndexSizeError, String::format("Failed to replace the provided item at index %zu.", index)); | 312 exceptionState.throwDOMException(IndexSizeError, String::format("Failed to replace the provided item at index %zu.", index)); |
| 313 return nullptr; | 313 return nullptr; |
| 314 } | 314 } |
| 315 | 315 |
| 316 // Update the value at the desired position 'index'. | 316 // Update the value at the desired position 'index'. |
| 317 RefPtrWillBeMember<ItemPropertyType>& position = m_values[index]; | 317 RefPtrWillBeMember<ItemPropertyType>& position = m_values[index]; |
| 318 ASSERT(position->ownerList() == this); | 318 ASSERT(position->ownerList() == this); |
| 319 position->setOwnerList(0); | 319 position->setOwnerList(0); |
| 320 position = newItem; | 320 position = newItem; |
| 321 newItem->setOwnerList(this); | 321 newItem->setOwnerList(this); |
| 322 | 322 |
| 323 return newItem.release(); | 323 return newItem.release(); |
| 324 } | 324 } |
| 325 | 325 |
| 326 template<typename Derived, typename ItemProperty> | 326 template<typename Derived, typename ItemProperty> |
| 327 bool SVGListPropertyHelper<Derived, ItemProperty>::checkIndexBound(size_t index, ExceptionState& exceptionState) | 327 bool SVGListPropertyHelper<Derived, ItemProperty>::checkIndexBound(size_t index, ExceptionState& exceptionState) |
| 328 { | 328 { |
| 329 if (index >= m_values.size()) { | 329 if (index >= m_values.size()) { |
| 330 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::inde xExceedsMaximumBound("index", index, m_values.size())); | 330 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::inde xExceedsMaximumBound("index", index, m_values.size())); |
| 331 return false; | 331 return false; |
| 332 } | 332 } |
| 333 | 333 |
| 334 return true; | 334 return true; |
| 335 } | 335 } |
| 336 | 336 |
| 337 template<typename Derived, typename ItemProperty> | 337 template<typename Derived, typename ItemProperty> |
| 338 bool SVGListPropertyHelper<Derived, ItemProperty>::removeFromOldOwnerListAndAdju stIndex(PassRefPtrWillBeRawPtr<ItemPropertyType> passItem, size_t* indexToModify ) | |
| 339 { | |
| 340 RefPtrWillBeRawPtr<ItemPropertyType> item = passItem; | |
| 341 ASSERT(item); | |
| 342 RefPtrWillBeRawPtr<Derived> ownerList = toDerived(item->ownerList()); | |
| 343 if (!ownerList) | |
| 344 return true; | |
| 345 | |
| 346 // Spec: If newItem is already in a list, it is removed from its previous li st before it is inserted into this list. | |
| 347 // 'newItem' is already living in another list. If it's not our list, synchr onize the other lists wrappers after the removal. | |
| 348 bool livesInOtherList = ownerList.get() != this; | |
| 349 size_t indexToRemove = ownerList->findItem(item); | |
| 350 ASSERT(indexToRemove != WTF::kNotFound); | |
| 351 | |
| 352 // Do not remove newItem if already in this list at the target index. | |
| 353 if (!livesInOtherList && indexToModify && indexToRemove == *indexToModify) | |
| 354 return false; | |
| 355 | |
| 356 ownerList->removeItem(indexToRemove, ASSERT_NO_EXCEPTION); | |
| 357 | |
| 358 if (!indexToModify) | |
| 359 return true; | |
| 360 | |
| 361 // If the item lived in our list, adjust the insertion index. | |
| 362 if (!livesInOtherList) { | |
| 363 size_t& index = *indexToModify; | |
| 364 // Spec: If the item is already in this list, note that the index of the item to (replace|insert before) is before the removal of the item. | |
| 365 if (static_cast<size_t>(indexToRemove) < index) | |
| 366 --index; | |
| 367 } | |
| 368 | |
| 369 return true; | |
| 370 } | |
| 371 | |
| 372 template<typename Derived, typename ItemProperty> | |
| 373 size_t SVGListPropertyHelper<Derived, ItemProperty>::findItem(PassRefPtrWillBeRa wPtr<ItemPropertyType> item) | 338 size_t SVGListPropertyHelper<Derived, ItemProperty>::findItem(PassRefPtrWillBeRa wPtr<ItemPropertyType> item) |
| 374 { | 339 { |
| 375 return m_values.find(item); | 340 return m_values.find(item); |
| 376 } | 341 } |
| 377 | 342 |
| 378 template<typename Derived, typename ItemProperty> | 343 template<typename Derived, typename ItemProperty> |
| 379 void SVGListPropertyHelper<Derived, ItemProperty>::deepCopy(PassRefPtrWillBeRawP tr<Derived> passFrom) | 344 void SVGListPropertyHelper<Derived, ItemProperty>::deepCopy(PassRefPtrWillBeRawP tr<Derived> passFrom) |
| 380 { | 345 { |
| 381 RefPtrWillBeRawPtr<Derived> from = passFrom; | 346 RefPtrWillBeRawPtr<Derived> from = passFrom; |
| 382 | 347 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 for (size_t i = 0; i < paddingCount; ++i) | 383 for (size_t i = 0; i < paddingCount; ++i) |
| 419 append(createPaddingItem()); | 384 append(createPaddingItem()); |
| 420 } | 385 } |
| 421 | 386 |
| 422 return true; | 387 return true; |
| 423 } | 388 } |
| 424 | 389 |
| 425 } | 390 } |
| 426 | 391 |
| 427 #endif // SVGListPropertyHelper_h | 392 #endif // SVGListPropertyHelper_h |
| OLD | NEW |