StaticRange.cpp 1.8 KB

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