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