Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / parser / speculative_html_parser.lucb

#==============================================================================================
#
#   speculative_html_parser - HTML::SpeculativeHTMLParser: the look-ahead parser that issues
#                             speculative fetches
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/Parser/SpeculativeHTMLParser.cpp (lines 1-223). While a parser-blocking script loads, the
#       speculative parser tokenizes the rest of the input and fetches the scripts, style sheets,
#       preloads and images it finds. Fetching is phase 2: the steps that create and run a
#       request (create_potential_CORS_request, Fetch::Fetching::fetch) and translating a preload
#       destination are not in the phase-1 closure, so they trap "unported (P2)" in place. With
#       scripting disabled no parser-blocking script exists, so phase 1 never starts this parser.
#
#==============================================================================================

## Ported from SpeculativeHTMLParser.cpp:28 Web::HTML::SpeculativeHTMLParser::create.
pub func html_speculative_html_parser_create(realm: Realm*, document: DomDocument*, pending_input: ak.String, base_url: web_url.Url) -> HtmlSpeculativeHtmlParser*:
    return realm_create_html_speculative_html_parser(realm, document, pending_input, base_url)

## Ported from SpeculativeHTMLParser.cpp:33 Web::HTML::SpeculativeHTMLParser::SpeculativeHTMLParser.
pub func html_speculative_html_parser_construct(this: HtmlSpeculativeHtmlParser*, document: DomDocument*, pending_input: ak.String, base_url: web_url.Url):
    this.m_document = document
    this.m_input = pending_input
    html_syntax.html_tokenizer_construct_string_view(&this.m_tokenizer, ak.string_bytes_as_string_view(&this.m_input), ak.byte_string_from_str("UTF-8"))
    this.m_base_url = base_url

## Ported from SpeculativeHTMLParser.cpp:43 Web::HTML::SpeculativeHTMLParser::visit_edges (override).
pub func html_speculative_html_parser_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlSpeculativeHtmlParser*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_document)
    html_syntax.html_tokenizer_visit_edges(&this.m_tokenizer, visitor)

## Ported from SpeculativeHTMLParser.cpp:50 Web::HTML::SpeculativeHTMLParser::stop.
pub func html_speculative_html_parser_stop(this: HtmlSpeculativeHtmlParser*):
    # https://html.spec.whatwg.org/multipage/parsing.html#stop-the-speculative-html-parser
    # 3. Throw away any pending content in speculativeParser's input stream, and discard any future content
    #    that would have been added to it.
    html_syntax.html_tokenizer_abort(&this.m_tokenizer)

## Ported from SpeculativeHTMLParser.cpp:58 Web::HTML::SpeculativeHTMLParser::run.
pub func html_speculative_html_parser_run(this: HtmlSpeculativeHtmlParser*):
    while true:
        let token = html_syntax.html_tokenizer_next_token(&this.m_tokenizer) else break

        if html_syntax.html_token_is_start_tag(&token):
            html_speculative_html_parser_process_start_tag(this, token)
        elif html_syntax.html_token_is_end_tag(&token):
            html_speculative_html_parser_process_end_tag(this, token)
        elif html_syntax.html_token_is_end_of_file(&token):
            break

## Ported from SpeculativeHTMLParser.cpp:77 Web::HTML::(anonymous)::attributes_from_token.
func html_attributes_from_token(token: html_syntax.HtmlToken) -> ak.Vector[html_syntax.HtmlTokenAttribute]:
    var attributes: ak.Vector[html_syntax.HtmlTokenAttribute]
    if let token_attributes = html_syntax.html_token_tag_attributes_const(&token):
        for attribute in ak.vector_span_const(token_attributes):
            ak.vector_append_t(&attributes, attribute)
    return attributes

## Ported from SpeculativeHTMLParser.cpp:87 Web::HTML::(anonymous)::issue_speculative_fetch.
func html_issue_speculative_fetch(realm: Realm*, document: DomDocument*, url: web_url.Url, destination: FetchRequestDestination?, cors_setting: HtmlCorsSettingAttribute):
    # auto& vm = realm.vm();
    # auto request = create_potential_CORS_request(vm, url, destination, cors_setting);
    # request->set_client(&document.relevant_settings_object());
    #
    # Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
    # auto algorithms = Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input));
    #
    # // The fetch stays alive via ResourceLoader's GC::Root callbacks for the duration of the
    # // network request, so we don't need to retain the FetchController.
    # (void)Fetch::Fetching::fetch(realm, request, algorithms);
    trap("unported (P2): Web::HTML::issue_speculative_fetch (create_potential_CORS_request, Fetch::Fetching::fetch)")

## Ported from SpeculativeHTMLParser.cpp:101 Web::HTML::(anonymous)::rel_contains_keyword.
func html_rel_contains_keyword(rel: ak.StringView, keyword: ak.StringView) -> bool:
    let is_ascii_whitespace = DomElementIsAsciiWhitespace(unused = 0)
    let tokens = ak.string_view_split_view_if(&rel, &is_ascii_whitespace)
    for token in &ak.vector_span_const(&tokens):
        if ak.string_view_equals_ignoring_ascii_case(token, keyword):
            return true
    return false

## `href->is_empty()` on an Optional<String> the caller checked.
func html_speculative_optional_string_is_empty(string: ak.String?) -> bool:
    let value = string else trap("VERIFY: has_value()")
    return ak.string_is_empty(&value)

## Ported from SpeculativeHTMLParser.cpp:111 Web::HTML::(anonymous)::speculative_fetch.
# https://html.spec.whatwg.org/multipage/parsing.html#speculative-fetch
func html_speculative_fetch(element: HtmlSpeculativeMockElement*, document: DomDocument*, base_url: web_url.Url*):
    let realm = bind_platform_object_realm(document.bind_platform_object())

    # 1. If the speculative HTML parser encounters one of the following elements, then act as if that
    #    element is processed for the purpose of its effect on subsequent speculative fetches.
    #    - A base element.
    if ak.fly_string_eq_fly_string(&element.local_name, &html_tag_names_base):
        if let href = html_speculative_mock_element_attribute(element, html_attribute_names_href):
            if not ak.string_is_empty(&href):
                if let parsed = dom_document_encoding_parse_url(document, ak.string_bytes_as_string_view(&href)):
                    *base_url = parsed
        return
    # FIXME: A meta element whose http-equiv attribute is in the Content security policy state.
    # FIXME: A meta element whose name attribute is referrer.
    # FIXME: A meta element whose name attribute is viewport.

    # 2. Let url be the URL that element would fetch if it was processed normally. If there is no such
    #    URL or if it is the empty string, then do nothing.
    # We resolve URLs against the speculative parser's tracked base_url (which may have been updated
    # by an earlier speculative <base href>); this is why we use complete_url here rather than
    # document.encoding_parse_url, which would resolve against the document's base instead.
    var url: web_url.Url? = none
    var destination: FetchRequestDestination? = none
    let cors_setting = html_cors_setting_attribute_from_keyword(html_speculative_mock_element_attribute(element, html_attribute_names_crossorigin))

    if ak.fly_string_eq_fly_string(&element.local_name, &html_tag_names_script):
        let src = html_speculative_mock_element_attribute(element, html_attribute_names_src) else return
        if ak.string_is_empty(&src):
            return
        url = web_url.url_complete_url(base_url, ak.string_bytes_as_string_view(&src))
        destination = FetchRequestDestination.script
    elif ak.fly_string_eq_fly_string(&element.local_name, &html_tag_names_link):
        let rel = html_speculative_mock_element_attribute(element, html_attribute_names_rel)
        let href = html_speculative_mock_element_attribute(element, html_attribute_names_href)
        if href == none or html_speculative_optional_string_is_empty(href) or rel == none:
            return
        let href_string = href else trap("checked")
        let rel_string = rel else trap("checked")
        let rel_view = ak.string_bytes_as_string_view(&rel_string)
        if html_rel_contains_keyword(rel_view, ak.sv("stylesheet")):
            url = web_url.url_complete_url(base_url, ak.string_bytes_as_string_view(&href_string))
            destination = FetchRequestDestination.style
        elif html_rel_contains_keyword(rel_view, ak.sv("preload")):
            # auto translated = translate_a_preload_destination(element.attribute(HTML::AttributeNames::as));
            # if (translated.has<Empty>())
            #     return;
            # destination = translated.get<Optional<Fetch::Infrastructure::Request::Destination>>();
            # url = base_url.complete_url(*href);
            trap("unported (P2): Web::HTML::translate_a_preload_destination")
        else:
            return
    elif ak.fly_string_eq_fly_string(&element.local_name, &html_tag_names_img):
        let src = html_speculative_mock_element_attribute(element, html_attribute_names_src) else return
        if ak.string_is_empty(&src):
            return
        url = web_url.url_complete_url(base_url, ak.string_bytes_as_string_view(&src))
        destination = FetchRequestDestination.image
    else:
        return

    let actual_url = url else return

    # 3. Otherwise, if url is already in the list of speculative fetch URLs, then do nothing.
    if dom_document_has_speculative_fetch_url(document, actual_url):
        return

    # 4. Otherwise, fetch url as if the element was processed normally, and add url to the list of
    #    speculative fetch URLs.
    dom_document_add_speculative_fetch_url(document, actual_url)
    html_issue_speculative_fetch(realm, document, actual_url, destination, cors_setting)

## Ported from SpeculativeHTMLParser.cpp:187 Web::HTML::SpeculativeHTMLParser::process_start_tag.
pub func html_speculative_html_parser_process_start_tag(this: HtmlSpeculativeHtmlParser*, token: html_syntax.HtmlToken):
    let tag_name = html_syntax.html_token_tag_name(&token)

    if ak.fly_string_eq_fly_string(&tag_name, &html_tag_names_template):
        this.m_template_depth += 1
        return

    if ak.fly_string_eq_fly_string(&tag_name, &html_tag_names_svg) or ak.fly_string_eq_fly_string(&tag_name, &html_tag_names_math):
        this.m_foreign_depth += 1
        return

    if this.m_template_depth > 0 or this.m_foreign_depth > 0:
        return

    if not dom_fly_string_is_one_of_fly_strings(tag_name, [html_tag_names_script, html_tag_names_link, html_tag_names_img, html_tag_names_base]):
        return

    var element = html_create_a_speculative_mock_element(tag_name, html_attributes_from_token(token))

    # 6. Optionally, perform a speculative fetch for element.
    html_speculative_fetch(&element, this.m_document, &this.m_base_url)

## Ported from SpeculativeHTMLParser.cpp:213 Web::HTML::SpeculativeHTMLParser::process_end_tag.
pub func html_speculative_html_parser_process_end_tag(this: HtmlSpeculativeHtmlParser*, token: html_syntax.HtmlToken):
    let tag_name = html_syntax.html_token_tag_name(&token)
    if ak.fly_string_eq_fly_string(&tag_name, &html_tag_names_template) and this.m_template_depth > 0:
        this.m_template_depth -= 1
    elif (ak.fly_string_eq_fly_string(&tag_name, &html_tag_names_svg) or ak.fly_string_eq_fly_string(&tag_name, &html_tag_names_math)) and this.m_foreign_depth > 0:
        this.m_foreign_depth -= 1