StaticRange.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Bindings/StaticRangePrototype.h>
  10. #include <LibWeb/DOM/Attr.h>
  11. #include <LibWeb/DOM/DocumentType.h>
  12. #include <LibWeb/DOM/StaticRange.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::DOM {
  15. JS_DEFINE_ALLOCATOR(StaticRange);
  16. StaticRange::StaticRange(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
  17. : AbstractRange(start_container, start_offset, end_container, end_offset)
  18. {
  19. }
  20. StaticRange::~StaticRange() = default;
  21. // https://dom.spec.whatwg.org/#dom-staticrange-staticrange
  22. WebIDL::ExceptionOr<JS::NonnullGCPtr<StaticRange>> StaticRange::construct_impl(JS::Realm& realm, StaticRangeInit& init)
  23. {
  24. // 1. If init["startContainer"] or init["endContainer"] is a DocumentType or Attr node, then throw an "InvalidNodeTypeError" DOMException.
  25. if (is<DocumentType>(*init.start_container) || is<Attr>(*init.start_container))
  26. return WebIDL::InvalidNodeTypeError::create(realm, "startContainer cannot be a DocumentType or Attribute node."_string);
  27. if (is<DocumentType>(*init.end_container) || is<Attr>(*init.end_container))
  28. return WebIDL::InvalidNodeTypeError::create(realm, "endContainer cannot be a DocumentType or Attribute node."_string);
  29. // 2. Set this’s start to (init["startContainer"], init["startOffset"]) and end to (init["endContainer"], init["endOffset"]).
  30. return realm.heap().allocate<StaticRange>(realm, *init.start_container, init.start_offset, *init.end_container, init.end_offset);
  31. }
  32. void StaticRange::initialize(JS::Realm& realm)
  33. {
  34. Base::initialize(realm);
  35. WEB_SET_PROTOTYPE_FOR_INTERFACE(StaticRange);
  36. }
  37. }