Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / adopted_style_sheets.lucb

#==============================================================================================
#
#   adopted_style_sheets - DOM::create_adopted_style_sheets_list: adoptedStyleSheets
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/AdoptedStyleSheets.cpp (lines 1-43): the ObservableArray of a
#       document or shadow root, whose set/delete callbacks validate and invalidate style.
#
#       The C++ callbacks answer WebIDL::ExceptionOr<void>; WebIDL::ObservableArray's ported
#       callback slots (region r14) take non-fallible functions, so the callbacks' bodies are
#       fallible private functions and an exception they throw traps until the array exists
#       for JavaScript (phase 3).
#
#==============================================================================================

## The set-an-indexed-value lambda's body.
func dom_adopted_style_sheets_set_an_indexed_value(document_or_shadow_root: DomNode*, value: JsValue*) -> !:
    let vm = js_cell_vm(document_or_shadow_root.js_cell())
    var style_sheet: CssCssStyleSheet*? = none
    if js_value_is_object(*value):
        style_sheet = as_if_css_css_style_sheet((gc.Cell*)js_value_as_object(*value))
    if style_sheet == none:
        try vm_throw_completion(vm, IdlSimpleExceptionType.type_error, .string_view(ak.sv("Not an object of type CSSStyleSheet")))
    let sheet = style_sheet else trap("unreachable")

    # The set an indexed value algorithm for adoptedStyleSheets, given value and index, is the following:
    # 1. If value’s constructed flag is not set, or its constructor document is not equal to this
    #    DocumentOrShadowRoot's node document, throw a "NotAllowedError" DOMException.
    let realm = bind_platform_object_realm(document_or_shadow_root.bind_platform_object())
    if not css_css_style_sheet_constructed(sheet):
        try idl_throw(.idl_dom_exception(idl_not_allowed_error_create(realm, ak.utf16_string_from_str("StyleSheet's constructed flag is not set."))))

    if not css_css_style_sheet_constructed(sheet) or css_css_style_sheet_constructor_document(sheet) != dom_node_document(document_or_shadow_root):
        try idl_throw(.idl_dom_exception(idl_not_allowed_error_create(realm, ak.utf16_string_from_str("Sharing a StyleSheet between documents is not allowed."))))

    css_invalidation_invalidate_style_after_adopting_style_sheet(document_or_shadow_root, sheet)

## The set-an-indexed-value callback (captures: the document or shadow root).
func dom_adopted_style_sheets_on_set_an_indexed_value(context: void*?, value: JsValue*):
    let document_or_shadow_root = (DomNode*)(context else trap("no node"))
    dom_adopted_style_sheets_set_an_indexed_value(document_or_shadow_root, value) else trap("unported (P3): an exception from an adoptedStyleSheets set callback")

## The delete-an-indexed-value callback (captures: the document or shadow root).
func dom_adopted_style_sheets_on_delete_an_indexed_value(context: void*?, value: JsValue):
    let document_or_shadow_root = (DomNode*)(context else trap("no node"))
    let style_sheet = as_css_css_style_sheet((gc.Cell*)js_value_as_object(value))
    css_invalidation_invalidate_style_after_removing_adopted_style_sheet(document_or_shadow_root, style_sheet)

## Ported from AdoptedStyleSheets.cpp:14 Web::DOM::create_adopted_style_sheets_list.
pub func dom_create_adopted_style_sheets_list(document_or_shadow_root: DomNode*) -> IdlObservableArray*:
    let adopted_style_sheets = idl_observable_array_create(bind_platform_object_realm(document_or_shadow_root.bind_platform_object()))
    idl_observable_array_set_on_set_an_indexed_value_callback(adopted_style_sheets, ak.Function1[JsValue*, unit](call = dom_adopted_style_sheets_on_set_an_indexed_value, context = document_or_shadow_root))
    idl_observable_array_set_on_delete_an_indexed_value_callback(adopted_style_sheets, ak.Function1[JsValue, unit](call = dom_adopted_style_sheets_on_delete_an_indexed_value, context = document_or_shadow_root))
    return adopted_style_sheets