Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / html_meta_element.lucb

#==============================================================================================
#
#   html_meta_element - HTML::HTMLMetaElement: <meta>, its metadata names and pragma directives
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLMetaElement.cpp (lines 1-246) and HTMLMetaElement.h: the
#       constructor (which moves the mixin tables to HTMLMetaElement's, as C++'s vptrs move),
#       initialize, the http-equiv state, the theme-color/color-scheme/referrer metadata, and
#       the pragma directives run on insertion (the Content-Security-Policy pragma's policy
#       parsing is phase 2).
#
#==============================================================================================

## Ported from HTMLMetaElement.cpp:28 Web::HTML::HTMLMetaElement::HTMLMetaElement.
pub func html_meta_element_construct(this: HtmlMetaElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLMetaElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_meta_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_meta_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_meta_element_animatable_vtable
    element.global_event_handlers.vtable = &html_meta_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_meta_element_form_associated_element_vtable

## Ported from HTMLMetaElement.cpp:35 Web::HTML::HTMLMetaElement::initialize (override).
pub func html_meta_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_meta_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLMetaElement.cpp:41 Web::HTML::HTMLMetaElement::http_equiv_state.
pub func html_meta_element_http_equiv_state(this: HtmlMetaElement*) -> HtmlMetaElementHttpEquivAttributeState?:
    let value = dom_element_get_attribute_value(this.dom_element(), html_attribute_names_http_equiv)

    # ENUMERATE_HTML_META_HTTP_EQUIV_ATTRIBUTES
    if ak.string_equals_ignoring_ascii_case_string_view(&value, ak.sv("content-language")):
        return HtmlMetaElementHttpEquivAttributeState.content_language
    if ak.string_equals_ignoring_ascii_case_string_view(&value, ak.sv("content-type")):
        return HtmlMetaElementHttpEquivAttributeState.encoding_declaration
    if ak.string_equals_ignoring_ascii_case_string_view(&value, ak.sv("default-style")):
        return HtmlMetaElementHttpEquivAttributeState.default_style
    if ak.string_equals_ignoring_ascii_case_string_view(&value, ak.sv("refresh")):
        return HtmlMetaElementHttpEquivAttributeState.refresh
    if ak.string_equals_ignoring_ascii_case_string_view(&value, ak.sv("set-cookie")):
        return HtmlMetaElementHttpEquivAttributeState.set_cookie
    if ak.string_equals_ignoring_ascii_case_string_view(&value, ak.sv("x-ua-compatible")):
        return HtmlMetaElementHttpEquivAttributeState.xua_compatible
    if ak.string_equals_ignoring_ascii_case_string_view(&value, ak.sv("content-security-policy")):
        return HtmlMetaElementHttpEquivAttributeState.content_security_policy

    return none

## Ported from HTMLMetaElement.cpp:54 Web::HTML::HTMLMetaElement::update_metadata.
pub func html_meta_element_update_metadata(this: HtmlMetaElement*, old_name: ak.String? = none):
    let document = dom_node_document(this.dom_node())
    if let name = dom_element_name(this.dom_element()):
        if ak.fly_string_equals_ignoring_ascii_case_string_view(&name, ak.sv("theme-color")):
            dom_document_obtain_theme_color(document)
        elif ak.fly_string_equals_ignoring_ascii_case_string_view(&name, ak.sv("color-scheme")):
            dom_document_obtain_supported_color_schemes(document)
        elif ak.fly_string_equals_ignoring_ascii_case_string_view(&name, ak.sv("referrer")):
            # 2. If element does not have a name attribute whose value is an ASCII case-insensitive match for "referrer", then return.
            html_meta_element_update_referrer_policy(this)

    if let old = old_name:
        if ak.string_equals_ignoring_ascii_case_string_view(&old, ak.sv("theme-color")):
            dom_document_obtain_theme_color(document)
        elif ak.string_equals_ignoring_ascii_case_string_view(&old, ak.sv("color-scheme")):
            dom_document_obtain_supported_color_schemes(document)

        # NOTE: For historical reasons, unlike other standard metadata names, the processing model for referrer is not
        #       responsive to element removals, and does not use tree order. Only the most-recently-inserted or
        #       most-recently-modified meta element in this state has an effect.

## Ported from HTMLMetaElement.cpp:81 Web::HTML::HTMLMetaElement::update_referrer_policy.
# https://html.spec.whatwg.org/multipage/semantics.html#meta-referrer
pub func html_meta_element_update_referrer_policy(this: HtmlMetaElement*):
    # 1. If element is not in a document tree, then return.
    if not dom_node_in_a_document_tree(this.dom_node()):
        return

    # 3. If element does not have a content attribute, or that attribute's value is the empty string, then return.
    let content = dom_element_attribute(this.dom_element(), html_attribute_names_content) else return
    if ak.string_is_empty(&content):
        return

    # 4. Let value be the value of element's content attribute, converted to ASCII lowercase.
    let value = ak.string_bytes_as_string_view(&content)

    # 5. If value is one of the values given in the first column of the following table, then set value to the value given in the second column:
    var policy: ReferrerPolicyReferrerPolicy
    if ak.string_view_equals_ignoring_ascii_case(&value, ak.sv("never")):
        policy = ReferrerPolicyReferrerPolicy.no_referrer
    elif ak.string_view_equals_ignoring_ascii_case(&value, ak.sv("default")):
        # ReferrerPolicy::DEFAULT_REFERRER_POLICY
        policy = ReferrerPolicyReferrerPolicy.strict_origin_when_cross_origin
    elif ak.string_view_equals_ignoring_ascii_case(&value, ak.sv("always")):
        policy = ReferrerPolicyReferrerPolicy.unsafe_url
    elif ak.string_view_equals_ignoring_ascii_case(&value, ak.sv("origin-when-crossorigin")):
        policy = ReferrerPolicyReferrerPolicy.origin_when_cross_origin
    # 6. If value is a referrer policy, then...
    elif let parsed_policy = referrer_policy_from_string(value):
        policy = parsed_policy
    else:
        return

    # 6. ...set element's node document's policy container's referrer policy to policy.
    dom_document_policy_container(dom_node_document(this.dom_node())).referrer_policy = policy

## Ported from HTMLMetaElement.cpp:115 Web::HTML::HTMLMetaElement::inserted (override).
pub func html_meta_element_inserted_impl(base: DomNode*):
    let this = (HtmlMetaElement*)base
    html_element_inserted_impl(base)

    html_meta_element_update_metadata(this)

    # https://html.spec.whatwg.org/multipage/semantics.html#pragma-directives
    # When a meta element is inserted into the document, if its http-equiv attribute is present and represents one of
    # the above states, then the user agent must run the algorithm appropriate for that state, as described in the
    # following list:
    let http_equiv = html_meta_element_http_equiv_state(this) else return
    let element = this.dom_element()
    let document = dom_node_document(base)
    match http_equiv:
        .encoding_declaration:
            # https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-content-type
            # The Encoding declaration state is just an alternative form of setting the charset attribute: it is a character encoding declaration.
            # This state's user agent requirements are all handled by the parsing section of the specification.
            return
        .refresh:
            # https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh
            # 1. If the meta element has no content attribute, or if that attribute's value is the empty string, then return.
            # 2. Let input be the value of the element's content attribute.
            if not dom_element_has_attribute(element, html_attribute_names_content):
                return

            let input = dom_element_get_attribute_value(element, html_attribute_names_content)
            if ak.string_is_empty(&input):
                return

            # 3. Run the shared declarative refresh steps with the meta element's node document, input, and the meta element.
            dom_document_shared_declarative_refresh_steps(document, ak.string_bytes_as_string_view(&input), this)
        .set_cookie:
            # https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-set-cookie
            # This pragma is non-conforming and has no effect.
            # User agents are required to ignore this pragma.
            return
        .xua_compatible:
            # https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-x-ua-compatible
            # In practice, this pragma encourages Internet Explorer to more closely follow the specifications.
            # For meta elements with an http-equiv attribute in the X-UA-Compatible state, the content attribute must have a value that is an ASCII case-insensitive match for the string "IE=edge".
            # User agents are required to ignore this pragma.
            return
        .content_language:
            # https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-content-language
            # 1. If the meta element has no content attribute, then return.
            if not dom_element_has_attribute(element, html_attribute_names_content):
                return

            # 2. If the element's content attribute contains a U+002C COMMA character (,), then return.
            let content = dom_element_get_attribute_value(element, html_attribute_names_content)
            let content_view = ak.string_bytes_as_string_view(&content)
            if ak.string_view_contains_string_view(&content_view, ak.sv(",")):
                return

            # 3. Let input be the value of the element's content attribute.
            # 4. Let position point at the first character of input.
            var lexer = ak.generic_lexer_new(content_view)

            # 5. Skip ASCII whitespace within input given position.
            let is_ascii_whitespace = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
            ak.generic_lexer_ignore_while(&lexer, &is_ascii_whitespace)

            # 6. Collect a sequence of code points that are not ASCII whitespace from input given position.
            # 7. Let candidate be the string that resulted from the previous step.
            let candidate = ak.generic_lexer_consume_until_tt_predicate(&lexer, &is_ascii_whitespace)

            # 8. If candidate is the empty string, return.
            if ak.string_view_is_empty(&candidate):
                return

            # 9. Set the pragma-set default language to candidate.
            let language = ak.string_from_utf8_without_validation(ak.string_view_bytes(&candidate))
            dom_document_set_pragma_set_default_language(document, language)
            let document_element = dom_document_document_element(document) else trap("a document element")
            dom_element_invalidate_lang_value(document_element)
        .content_security_policy:
            # https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-content-security-policy
            # This pragma enforces a Content Security Policy on a Document. [CSP]
            # 1. If the meta element is not a child of a head element, return.
            let parent = tree_node_parent(base) else return
            if not is_html_head_element(parent):
                return

            # 2. If the meta element has no content attribute, or if that attribute's value is the empty string, then return.
            let input = dom_element_get_attribute_value(element, html_attribute_names_content)
            if ak.string_is_empty(&input):
                return

            # 3. Let policy be the result of executing Content Security Policy's parse a serialized Content Security
            #    Policy algorithm on the meta element's content attribute's value, with a source of "meta", and a
            #    disposition of "enforce".
            let realm = html_element_realm(this.html_element())
            let policy = content_security_policy_policy_parse_a_serialized_csp(realm_heap(realm), ByteStringOrString.string(input), ContentSecurityPolicyPolicySource.meta, ContentSecurityPolicyPolicyDisposition.enforce)

            # 4. Remove all occurrences of the report-uri, frame-ancestors, and sandbox directives from policy.
            content_security_policy_policy_remove_directive(policy, content_security_policy_directives_names_report_uri)
            content_security_policy_policy_remove_directive(policy, content_security_policy_directives_names_frame_ancestors)
            content_security_policy_policy_remove_directive(policy, content_security_policy_directives_names_sandbox)

            # FIXME: File spec issue stating the policy's self origin isn't set here.
            content_security_policy_policy_set_self_origin(policy, dom_document_origin(document))

            # 5. Enforce the policy policy.
            let policy_list = content_security_policy_policy_list_from_object(realm_global_object(realm)) else trap("VERIFY: a CSP list")
            content_security_policy_policy_list_enforce_policy(policy_list, policy)
        _:
            let http_equiv_value = dom_element_get_attribute_value(element, html_attribute_names_http_equiv)
            ak.dbgln(f"FIXME: Implement '{ak.string_view_text(ak.string_bytes_as_string_view(&http_equiv_value))}' http-equiv state")

## Ported from HTMLMetaElement.cpp:230 Web::HTML::HTMLMetaElement::removed_from (override).
pub func html_meta_element_removed_from_impl(base: DomNode*, is_subtree_root: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?, old_root: DomNode*):
    html_element_removed_from_impl(base, is_subtree_root, old_ancestor, old_root)
    html_meta_element_update_metadata((HtmlMetaElement*)base)

## Ported from HTMLMetaElement.cpp:236 Web::HTML::HTMLMetaElement::attribute_changed (override).
pub func html_meta_element_attribute_changed_impl(base: DomElement*, local_name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    html_element_attribute_changed_impl(base, local_name, old_value, value, namespace_)
    if ak.fly_string_eq_fly_string(&local_name, &html_attribute_names_name):
        html_meta_element_update_metadata((HtmlMetaElement*)base, old_value)
    else:
        html_meta_element_update_metadata((HtmlMetaElement*)base)