SVGTransformList.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2024, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/SVGTransformListPrototype.h>
  8. #include <LibWeb/SVG/SVGTransformList.h>
  9. #include <LibWeb/WebIDL/ExceptionOr.h>
  10. namespace Web::SVG {
  11. GC_DEFINE_ALLOCATOR(SVGTransformList);
  12. GC::Ref<SVGTransformList> SVGTransformList::create(JS::Realm& realm)
  13. {
  14. return realm.create<SVGTransformList>(realm);
  15. }
  16. SVGTransformList::~SVGTransformList() = default;
  17. SVGTransformList::SVGTransformList(JS::Realm& realm)
  18. : PlatformObject(realm)
  19. {
  20. }
  21. // https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__length
  22. WebIDL::UnsignedLong SVGTransformList::length()
  23. {
  24. // The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the length of the list.
  25. return m_transforms.size();
  26. }
  27. // https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__numberOfItems
  28. WebIDL::UnsignedLong SVGTransformList::number_of_items()
  29. {
  30. // The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the length of the list.
  31. return m_transforms.size();
  32. }
  33. // https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__getItem
  34. WebIDL::ExceptionOr<GC::Ref<SVGTransform>> SVGTransformList::get_item(WebIDL::UnsignedLong index)
  35. {
  36. // 1. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
  37. if (index >= m_transforms.size())
  38. return WebIDL::IndexSizeError::create(realm(), "SVGTransformList index out of bounds"_string);
  39. // 2. Return the element in the list at position index.
  40. return m_transforms.at(index);
  41. }
  42. // https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__appendItem
  43. GC::Ref<SVGTransform> SVGTransformList::append_item(GC::Ref<SVGTransform> new_item)
  44. {
  45. // FIXME: This does not implement the steps from the specification.
  46. m_transforms.append(new_item);
  47. return new_item;
  48. }
  49. void SVGTransformList::initialize(JS::Realm& realm)
  50. {
  51. Base::initialize(realm);
  52. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGTransformList);
  53. }
  54. void SVGTransformList::visit_edges(Cell::Visitor& visitor)
  55. {
  56. Base::visit_edges(visitor);
  57. for (auto transform : m_transforms)
  58. transform->visit_edges(visitor);
  59. }
  60. }