Serenity Operating System
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
8#include <AK/TypeCasts.h>
9#include <LibWeb/Bindings/Intrinsics.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
15namespace Web::DOM {
16
17StaticRange::StaticRange(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
18 : AbstractRange(start_container, start_offset, end_container, end_offset)
19{
20}
21
22StaticRange::~StaticRange() = default;
23
24// https://dom.spec.whatwg.org/#dom-staticrange-staticrange
25WebIDL::ExceptionOr<JS::NonnullGCPtr<StaticRange>> StaticRange::construct_impl(JS::Realm& realm, StaticRangeInit& init)
26{
27 // 1. If init["startContainer"] or init["endContainer"] is a DocumentType or Attr node, then throw an "InvalidNodeTypeError" DOMException.
28 if (is<DocumentType>(*init.start_container) || is<Attr>(*init.start_container))
29 return WebIDL::InvalidNodeTypeError::create(realm, "startContainer cannot be a DocumentType or Attribute node.");
30
31 if (is<DocumentType>(*init.end_container) || is<Attr>(*init.end_container))
32 return WebIDL::InvalidNodeTypeError::create(realm, "endContainer cannot be a DocumentType or Attribute node.");
33
34 // 2. Set this’s start to (init["startContainer"], init["startOffset"]) and end to (init["endContainer"], init["endOffset"]).
35 return MUST_OR_THROW_OOM(realm.heap().allocate<StaticRange>(realm, *init.start_container, init.start_offset, *init.end_container, init.end_offset));
36}
37
38JS::ThrowCompletionOr<void> StaticRange::initialize(JS::Realm& realm)
39{
40 MUST_OR_THROW_OOM(Base::initialize(realm));
41 set_prototype(&Bindings::ensure_web_prototype<Bindings::StaticRangePrototype>(realm, "StaticRange"));
42
43 return {};
44}
45
46}