StaticRange.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibWeb/DOM/Attribute.h>
  8. #include <LibWeb/DOM/DocumentType.h>
  9. #include <LibWeb/DOM/ExceptionOr.h>
  10. #include <LibWeb/DOM/StaticRange.h>
  11. namespace Web::DOM {
  12. StaticRange::StaticRange(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
  13. : AbstractRange(start_container, start_offset, end_container, end_offset)
  14. {
  15. }
  16. StaticRange::~StaticRange()
  17. {
  18. }
  19. // https://dom.spec.whatwg.org/#dom-staticrange-staticrange
  20. ExceptionOr<NonnullRefPtr<StaticRange>> StaticRange::create_with_global_object(JS::GlobalObject&, StaticRangeInit& init)
  21. {
  22. // 1. If init["startContainer"] or init["endContainer"] is a DocumentType or Attr node, then throw an "InvalidNodeTypeError" DOMException.
  23. if (is<DocumentType>(*init.start_container) || is<Attribute>(*init.start_container))
  24. return DOM::InvalidNodeTypeError::create("startContainer cannot be a DocumentType or Attribute node.");
  25. if (is<DocumentType>(*init.end_container) || is<Attribute>(*init.end_container))
  26. return DOM::InvalidNodeTypeError::create("endContainer cannot be a DocumentType or Attribute node.");
  27. // 2. Set this’s start to (init["startContainer"], init["startOffset"]) and end to (init["endContainer"], init["endOffset"]).
  28. return adopt_ref(*new StaticRange(*init.start_container, init.start_offset, *init.end_container, init.end_offset));
  29. }
  30. }