/src/serenity/Userland/Libraries/LibWeb/DOM/AdoptedStyleSheets.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/CSS/StyleComputer.h> |
8 | | #include <LibWeb/DOM/AdoptedStyleSheets.h> |
9 | | #include <LibWeb/DOM/Document.h> |
10 | | |
11 | | namespace Web::DOM { |
12 | | |
13 | | JS::NonnullGCPtr<WebIDL::ObservableArray> create_adopted_style_sheets_list(Document& document) |
14 | 0 | { |
15 | 0 | auto adopted_style_sheets = WebIDL::ObservableArray::create(document.realm()); |
16 | 0 | adopted_style_sheets->set_on_set_an_indexed_value_callback([&document](JS::Value& value) -> WebIDL::ExceptionOr<void> { |
17 | 0 | auto& vm = document.vm(); |
18 | 0 | if (!value.is_object()) |
19 | 0 | return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "CSSStyleSheet"); |
20 | 0 | auto& object = value.as_object(); |
21 | 0 | if (!is<CSS::CSSStyleSheet>(object)) |
22 | 0 | return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "CSSStyleSheet"); |
23 | 0 | auto& style_sheet = static_cast<CSS::CSSStyleSheet&>(object); |
24 | | |
25 | | // The set an indexed value algorithm for adoptedStyleSheets, given value and index, is the following: |
26 | | // 1. If value’s constructed flag is not set, or its constructor document is not equal to this |
27 | | // DocumentOrShadowRoot's node document, throw a "NotAllowedError" DOMException. |
28 | 0 | if (!style_sheet.constructed()) |
29 | 0 | return WebIDL::NotAllowedError::create(document.realm(), "StyleSheet's constructed flag is not set."_string); |
30 | 0 | if (!style_sheet.constructed() || style_sheet.constructor_document().ptr() != &document) |
31 | 0 | return WebIDL::NotAllowedError::create(document.realm(), "Sharing a StyleSheet between documents is not allowed."_string); |
32 | | |
33 | 0 | document.style_computer().load_fonts_from_sheet(style_sheet); |
34 | 0 | document.style_computer().invalidate_rule_cache(); |
35 | 0 | document.invalidate_style(DOM::StyleInvalidationReason::AdoptedStyleSheetsList); |
36 | 0 | return {}; |
37 | 0 | }); |
38 | 0 | adopted_style_sheets->set_on_delete_an_indexed_value_callback([&document]() -> WebIDL::ExceptionOr<void> { |
39 | 0 | document.style_computer().invalidate_rule_cache(); |
40 | 0 | document.invalidate_style(DOM::StyleInvalidationReason::AdoptedStyleSheetsList); |
41 | 0 | return {}; |
42 | 0 | }); |
43 | |
|
44 | 0 | return adopted_style_sheets; |
45 | 0 | } |
46 | | |
47 | | } |