Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / element_internals.lucb

#==============================================================================================
#
#   element_internals - HTML::ElementInternals: the internals of a form-associated custom
#                       element
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/ElementInternals.cpp (lines 1-262): creation, shadowRoot,
#       setFormValue, form, setValidity, willValidate, validity, validationMessage,
#       checkValidity, reportValidity, labels, states, initialize and visit_edges. Every member
#       but shadowRoot and states throws a NotSupportedError unless the target element is a
#       form-associated custom element (custom elements are phase 3).
#
#==============================================================================================

# mark: Helpers =================================================================================

## `realm()` of ElementInternals: its platform object's realm.
func html_element_internals_realm(this: HtmlElementInternals*) -> Realm*:
    return bind_platform_object_realm(this.bind_platform_object())

## The NotSupportedError every member throws for a target that is not form-associated.
func html_element_internals_throw_not_form_associated(this: HtmlElementInternals*) -> never!:
    try idl_throw(.idl_dom_exception(idl_not_supported_error_create(html_element_internals_realm(this), ak.utf16_string_from_str("Element is not a form-associated custom element"))))

## The `value.visit(...)` of set_form_value: an ElementInternalsFormValue as a FACESubmissionValue
## (a FormData object becomes a clone of its entry list).
func html_element_internals_to_face_submission_value(value: HtmlElementInternalsElementInternalsFormValue) -> HtmlFormAssociatedElementFaceSubmissionValue:
    match value:
        .file_api_file(file):
            return HtmlFormAssociatedElementFaceSubmissionValue.file_api_file(file else trap("GC::Root<FileAPI::File> is null"))
        .string(string):
            return HtmlFormAssociatedElementFaceSubmissionValue.string(string)
        .xhr_form_data(form_data):
            return HtmlFormAssociatedElementFaceSubmissionValue.vector_xhr_form_data_entry(xhr_form_data_entry_list(form_data else trap("GC::Root<XHR::FormData> is null")))
        .empty:
            return HtmlFormAssociatedElementFaceSubmissionValue.empty

# mark: Creation ================================================================================

## Ported from ElementInternals.cpp:19 Web::HTML::ElementInternals::create.
pub func html_element_internals_create(realm: Realm*, target_element: HtmlElement*) -> HtmlElementInternals*:
    return realm_create_html_element_internals(realm, realm, target_element)

## Ported from ElementInternals.cpp:24 Web::HTML::ElementInternals::ElementInternals.
pub func html_element_internals_construct(this: HtmlElementInternals*, realm: Realm*, target_element: HtmlElement*):
    bind_platform_object_construct_realm(this.bind_platform_object(), realm)
    this.m_target_element = target_element

# mark: The members =============================================================================

## Ported from ElementInternals.cpp:31 Web::HTML::ElementInternals::shadow_root.
# https://html.spec.whatwg.org/multipage/custom-elements.html#dom-elementinternals-shadowroot
pub func html_element_internals_shadow_root(this: HtmlElementInternals*) -> DomShadowRoot*?:
    # 1. Let target be this's target element.
    let target = this.m_target_element

    # 2. If target is not a shadow host, then return null.
    if not dom_element_is_shadow_host(target.dom_element()):
        return none

    # 3. Let shadow be target's shadow root.
    let shadow = dom_element_shadow_root(target.dom_element()) else trap("a shadow host has a shadow root")

    # 4. If shadow's available to element internals is false, then return null.
    if not dom_shadow_root_available_to_element_internals(shadow):
        return none

    # 5. Return shadow.
    return shadow

## Ported from ElementInternals.cpp:52 Web::HTML::ElementInternals::set_form_value.
# https://html.spec.whatwg.org/multipage/custom-elements.html#dom-elementinternals-setformvalue
pub func html_element_internals_set_form_value(this: HtmlElementInternals*, value: HtmlElementInternalsElementInternalsFormValue, state: HtmlElementInternalsElementInternalsFormValue?) -> !:
    # 1. Let element be this's target element.
    let element = this.m_target_element

    # 2. If element is not a form-associated custom element, then throw a "NotSupportedError" DOMException.
    if not html_element_is_form_associated_custom_element(element):
        try html_element_internals_throw_not_form_associated(this)

    # 3. Set target element's submission value to value if value is not a FormData object, or to a clone of value's entry list otherwise.
    let submission_value = html_element_internals_to_face_submission_value(value)

    html_form_associated_element_set_face_submission_value(&element.form_associated_element, submission_value)

    # 4. If the state argument of the function is omitted, set element's state to its submission value.
    if let state_argument = state:
        # 5. Otherwise, if state is a FormData object, set element's state to a clone of state's entry list.
        # 6. Otherwise, set element's state to state.
        let state_value = html_element_internals_to_face_submission_value(state_argument)

        html_form_associated_element_set_face_state(&element.form_associated_element, state_value)
    else:
        html_form_associated_element_set_face_state(&element.form_associated_element, submission_value)

## Ported from ElementInternals.cpp:107 Web::HTML::ElementInternals::form.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-elementinternals-form
pub func html_element_internals_form(this: HtmlElementInternals*) -> HtmlFormElement*?!:
    # Form-associated custom elements don't have form IDL attribute. Instead, their ElementInternals object has a form IDL attribute.
    # On getting, it must throw a "NotSupportedError" DOMException if the target element is not a form-associated custom element.
    # Otherwise, it must return the element's form owner, or null if there isn't one.
    if not html_element_is_form_associated_custom_element(this.m_target_element):
        try html_element_internals_throw_not_form_associated(this)

    return html_form_associated_element_form(&this.m_target_element.form_associated_element)

## Ported from ElementInternals.cpp:119 Web::HTML::ElementInternals::set_validity.
# https://html.spec.whatwg.org/multipage/custom-elements.html#dom-elementinternals-setvalidity
pub func html_element_internals_set_validity(this: HtmlElementInternals*, flags: HtmlValidityStateFlags, message: ak.String?, anchor: HtmlElement*?) -> !:
    # 1. Let element be this's target element.
    let element = this.m_target_element

    # 2. If element is not a form-associated custom element, then throw a "NotSupportedError" DOMException.
    if not html_element_is_form_associated_custom_element(element):
        try html_element_internals_throw_not_form_associated(this)

    # 3. If flags contains one or more true values and message is not given or is the empty string, then throw a TypeError.
    if html_validity_state_flags_has_one_or_more_true_values(&flags) and (message == none or html_element_string_is_empty(message)):
        try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Invalid flag(s) and empty message")))

    # 4. For each entry flag → value of flags, set element's validity flag with the name flag to value.
    html_form_associated_element_set_face_validity_flags(&element.form_associated_element, flags)

    # 5. Set element's validation message to the empty string if message is not given or all of element's validity flags are false, or to message otherwise.
    var validation_message = ak.string_new()
    if let given_message = message:
        if html_validity_state_flags_has_one_or_more_true_values(&flags):
            validation_message = given_message

    html_form_associated_element_set_face_validation_message(&element.form_associated_element, validation_message)

    # 6. If element's customError validity flag is true, then set element's custom validity error message to element's
    #    validation message. Otherwise, set element's custom validity error message to the empty string.
    html_form_associated_element_set_custom_validity_error_message(&element.form_associated_element, validation_message if flags.custom_error else ak.string_from_str(""))

    # 7. If anchor is not given, then set it to element.
    var validation_anchor = anchor
    if let given_anchor = anchor:
        # 8. Otherwise, if anchor is not a shadow-including inclusive descendant of element, then throw a "NotFoundError" DOMException.
        if not dom_node_is_shadow_including_inclusive_descendant_of(given_anchor.dom_node(), element.dom_node()):
            try idl_throw(.idl_dom_exception(idl_not_found_error_create(html_element_internals_realm(this), ak.utf16_string_from_str("Anchor is not a shadow-including descendant of element"))))
    else:
        validation_anchor = element

    # 9. Set element's validation anchor to anchor.
    html_form_associated_element_set_face_validation_anchor(&element.form_associated_element, validation_anchor)

## Ported from ElementInternals.cpp:166 Web::HTML::ElementInternals::will_validate.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-elementinternals-willvalidate
pub func html_element_internals_will_validate(this: HtmlElementInternals*) -> bool!:
    # The willValidate attribute of ElementInternals interface, on getting, must throw a "NotSupportedError" DOMException if
    # the target element is not a form-associated custom element. Otherwise, it must return true if the target element is a
    # candidate for constraint validation, and false otherwise.
    if not html_element_is_form_associated_custom_element(this.m_target_element):
        try html_element_internals_throw_not_form_associated(this)

    return html_form_associated_element_is_candidate_for_constraint_validation(&this.m_target_element.form_associated_element)

## Ported from ElementInternals.cpp:178 Web::HTML::ElementInternals::validity.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-elementinternals-validity
pub func html_element_internals_validity(this: HtmlElementInternals*) -> HtmlValidityState*!:
    # The validity attribute of ElementInternals interface, on getting, must throw a "NotSupportedError" DOMException if
    # the target element is not a form-associated custom element. Otherwise, it must return a ValidityState object that
    # represents the validity states of the target element. This object is live.
    if not html_element_is_form_associated_custom_element(this.m_target_element):
        try html_element_internals_throw_not_form_associated(this)

    return html_validity_state_create(html_element_internals_realm(this), &this.m_target_element.form_associated_element)

## Ported from ElementInternals.cpp:190 Web::HTML::ElementInternals::validation_message.
# https://html.spec.whatwg.org/multipage/custom-elements.html#dom-elementinternals-validationmessage
pub func html_element_internals_validation_message(this: HtmlElementInternals*) -> ak.String!:
    # 1. Let element be this's target element.
    let element = this.m_target_element

    # 2. If element is not a form-associated custom element, then throw a "NotSupportedError" DOMException.
    if not html_element_is_form_associated_custom_element(element):
        try html_element_internals_throw_not_form_associated(this)

    # 3. Return element's validation message.
    return html_form_associated_element_face_validation_message(&element.form_associated_element)

## Ported from ElementInternals.cpp:204 Web::HTML::ElementInternals::check_validity.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-elementinternals-checkvalidity
pub func html_element_internals_check_validity(this: HtmlElementInternals*) -> bool!:
    # 1. Let element be this ElementInternals's target element.
    let element = this.m_target_element

    # 2. If element is not a form-associated custom element, then throw a "NotSupportedError" DOMException.
    if not html_element_is_form_associated_custom_element(element):
        try html_element_internals_throw_not_form_associated(this)

    # 3. Run the check validity steps on element.
    return html_form_associated_element_check_validity_steps(&element.form_associated_element)

## Ported from ElementInternals.cpp:218 Web::HTML::ElementInternals::report_validity.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-elementinternals-reportvalidity
pub func html_element_internals_report_validity(this: HtmlElementInternals*) -> bool!:
    # 1. Let element be this ElementInternals's target element
    let element = this.m_target_element

    # 2. If element is not a form-associated custom element, then throw a "NotSupportedError" DOMException.
    if not html_element_is_form_associated_custom_element(element):
        try html_element_internals_throw_not_form_associated(this)

    # 3. Run the report validity steps on element.
    return html_form_associated_element_report_validity_steps(&element.form_associated_element)

## Ported from ElementInternals.cpp:232 Web::HTML::ElementInternals::labels.
# https://html.spec.whatwg.org/multipage/forms.html#dom-elementinternals-labels
pub func html_element_internals_labels(this: HtmlElementInternals*) -> DomNodeList*?!:
    # Form-associated custom elements don't have a labels IDL attribute. Instead, their ElementInternals object has a labels IDL attribute.
    # On getting, it must throw a "NotSupportedError" DOMException if the target element is not a form-associated custom element.
    # Otherwise, it must return that NodeList object, and that same value must always be returned.
    if not html_element_is_form_associated_custom_element(this.m_target_element):
        try html_element_internals_throw_not_form_associated(this)

    return html_element_labels(this.m_target_element)

## Ported from ElementInternals.cpp:244 Web::HTML::ElementInternals::states.
# https://html.spec.whatwg.org/multipage/custom-elements.html#dom-elementinternals-states
pub func html_element_internals_states(this: HtmlElementInternals*) -> HtmlCustomStateSet*?:
    # The states getter steps are to return this's target element's states set.
    return dom_element_ensure_custom_state_set(this.m_target_element.dom_element())

## Ported from ElementInternals.cpp:250 Web::HTML::ElementInternals::initialize (override).
pub func html_element_internals_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.element_internals)
    # Base::initialize(realm): PlatformObject inherits JS::Object's.
    js_cell_initialize_impl(base, realm)

## Ported from ElementInternals.cpp:256 Web::HTML::ElementInternals::visit_edges (override).
pub func html_element_internals_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlElementInternals*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t[HtmlElement](visitor, this.m_target_element)