dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / html_hyperlink_element_utils.lucb
#==============================================================================================
#
# html_hyperlink_element_utils - HTML::HTMLHyperlinkElementUtils: the URL decomposition IDL
# attributes of a and area elements
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLHyperlinkElementUtils.cpp (lines 1-468) and the virtuals of
# HTMLHyperlinkElementUtils.h: reinitializing and setting the url, origin, href, protocol,
# username, password, host, hostname, port, pathname, search and hash (getters and
# setters), updating href, and extracting an origin. C++ mutates `m_url` through a
# reference to the optional's value; here the URL is taken out, changed and stored back
# (the same URL data: a plain copy does not share a CopyOnWrite reference).
#
#==============================================================================================
# mark: Setting the url =========================================================================
## Ported from HTMLHyperlinkElementUtils.cpp:19 Web::HTML::HTMLHyperlinkElementUtils::reinitialize_url.
# https://html.spec.whatwg.org/multipage/links.html#reinitialise-url
pub func html_hyperlink_element_utils_reinitialize_url(this: HtmlHyperlinkElementUtils*):
# 1. If the element's url is non-null, its scheme is "blob", and it has an opaque path, then terminate these steps.
if let url = this.m_url:
let scheme = web_url.url_scheme(&url)
if ak.string_eq_string_view(&scheme, ak.sv("blob")) and web_url.url_has_an_opaque_path(&url):
return
# 2. Set the url.
html_hyperlink_element_utils_set_the_url(this)
## `Optional<URL::URL> != Optional<URL::URL>`.
func html_hyperlink_element_utils_optional_urls_differ(a: web_url.Url?, b: web_url.Url?) -> bool:
if let a_url = a:
if let b_url = b:
return not web_url.url_eq(&a_url, b_url)
return true
return b != none
## set_the_url's ScopeGuard: invalidates style when the url changed.
func html_hyperlink_element_utils_invalidate_style_if_needed(this: HtmlHyperlinkElementUtils*, old_url: web_url.Url?):
if html_hyperlink_element_utils_optional_urls_differ(this.m_url, old_url):
css_invalidation_invalidate_style_after_hyperlink_state_change(html_hyperlink_element_utils_hyperlink_element_utils_element(this))
## Ported from HTMLHyperlinkElementUtils.cpp:30 Web::HTML::HTMLHyperlinkElementUtils::set_the_url.
# https://html.spec.whatwg.org/multipage/links.html#concept-hyperlink-url-set
pub func html_hyperlink_element_utils_set_the_url(this: HtmlHyperlinkElementUtils*):
let old_url = this.m_url
defer html_hyperlink_element_utils_invalidate_style_if_needed(this, old_url)
let element = html_hyperlink_element_utils_hyperlink_element_utils_element(this)
# 1. Set this element's url to null.
this.m_url = none
# 2. If this element's href content attribute is absent, then return.
let href_content_attribute = dom_element_attribute(element, html_attribute_names_href) else return
# 3. Let url be the result of encoding-parsing a URL given this element's href content attribute's value, relative to this element's node document.
let url = dom_document_encoding_parse_url(dom_element_document(element), ak.string_bytes_as_string_view(&href_content_attribute))
# 4. If url is not failure, then set this element's url to url.
if let parsed = url:
this.m_url = parsed
# mark: origin and protocol =====================================================================
## Ported from HTMLHyperlinkElementUtils.cpp:57 Web::HTML::HTMLHyperlinkElementUtils::origin.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-origin
pub func html_hyperlink_element_utils_origin(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. If this element's url is null, return the empty string.
let url = this.m_url else return ak.string_new()
# 3. Return the serialization of this element's url's origin.
let origin = web_url.url_origin(&url)
return web_url.origin_serialize(&origin)
## Ported from HTMLHyperlinkElementUtils.cpp:71 Web::HTML::HTMLHyperlinkElementUtils::protocol.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
pub func html_hyperlink_element_utils_protocol(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. If this element's url is null, return ":".
let url = this.m_url else return ak.string_from_str(":")
# 3. Return this element's url's scheme, followed by ":".
let scheme = web_url.url_scheme(&url)
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&scheme))
ak.string_builder_append(&builder, ":")
return ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
## Ported from HTMLHyperlinkElementUtils.cpp:85 Web::HTML::HTMLHyperlinkElementUtils::set_protocol.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
pub func html_hyperlink_element_utils_set_protocol(this: HtmlHyperlinkElementUtils*, protocol: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. If this element's url is null, terminate these steps.
var url = this.m_url else return
# 3. Basic URL parse the given value, followed by ":", with this element's url as url and scheme start state as state override.
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, protocol)
ak.string_builder_append(&builder, ":")
let input = ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
_ = web_url.parser_basic_parse(ak.string_bytes_as_string_view(&input), none, &url, web_url.ParserState.scheme_start)
this.m_url = url
# 4. Update href.
html_hyperlink_element_utils_update_href(this)
# mark: username and password ===================================================================
## Ported from HTMLHyperlinkElementUtils.cpp:102 Web::HTML::HTMLHyperlinkElementUtils::username.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
pub func html_hyperlink_element_utils_username(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. If this element's url is null, return the empty string.
let url = this.m_url else return ak.string_new()
# 3. Return this element's url's username.
return web_url.url_username(&url)
## Ported from HTMLHyperlinkElementUtils.cpp:116 Web::HTML::HTMLHyperlinkElementUtils::set_username.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
pub func html_hyperlink_element_utils_set_username(this: HtmlHyperlinkElementUtils*, username: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null or url cannot have a username/password/port, then return.
var url = this.m_url else return
if web_url.url_cannot_have_a_username_or_password_or_port(&url):
return
# 4. Set the username given this’s URL and the given value.
web_url.url_set_username(&url, username)
this.m_url = url
# 5. Update href.
html_hyperlink_element_utils_update_href(this)
## Ported from HTMLHyperlinkElementUtils.cpp:136 Web::HTML::HTMLHyperlinkElementUtils::password.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
pub func html_hyperlink_element_utils_password(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null, then return the empty string.
let url = this.m_url else return ak.string_new()
# 4. Return url's password.
return web_url.url_password(&url)
## Ported from HTMLHyperlinkElementUtils.cpp:153 Web::HTML::HTMLHyperlinkElementUtils::set_password.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
pub func html_hyperlink_element_utils_set_password(this: HtmlHyperlinkElementUtils*, password: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null or url cannot have a username/password/port, then return.
var url = this.m_url else return
if web_url.url_cannot_have_a_username_or_password_or_port(&url):
return
# 4. Set the password, given url and the given value.
web_url.url_set_password(&url, password)
this.m_url = url
# 5. Update href.
html_hyperlink_element_utils_update_href(this)
# mark: host, hostname and port =================================================================
## Ported from HTMLHyperlinkElementUtils.cpp:173 Web::HTML::HTMLHyperlinkElementUtils::host.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
pub func html_hyperlink_element_utils_host(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url or url's host is null, return the empty string.
let url = this.m_url else return ak.string_new()
if web_url.url_host(&url) == none:
return ak.string_new()
# 4. If url's port is null, return url's host, serialized.
let port = web_url.url_port(&url) else return web_url.url_serialized_host(&url)
# 5. Return url's host, serialized, followed by ":" and url's port, serialized.
let serialized_host = web_url.url_serialized_host(&url)
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&serialized_host))
ak.string_builder_append(&builder, ":")
let port_string = ak.string_number_u64(port)
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&port_string))
return ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
## Ported from HTMLHyperlinkElementUtils.cpp:194 Web::HTML::HTMLHyperlinkElementUtils::set_host.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
pub func html_hyperlink_element_utils_set_host(this: HtmlHyperlinkElementUtils*, host: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null or url has an opaque path, then return.
var url = this.m_url else return
if web_url.url_has_an_opaque_path(&url):
return
# 4. Basic URL parse the given value, with url as url and host state as state override.
_ = web_url.parser_basic_parse(host, none, &url, web_url.ParserState.host)
this.m_url = url
# 5. Update href.
html_hyperlink_element_utils_update_href(this)
## Ported from HTMLHyperlinkElementUtils.cpp:214 Web::HTML::HTMLHyperlinkElementUtils::hostname.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-hostname
pub func html_hyperlink_element_utils_hostname(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url or url's host is null, return the empty string.
let url = this.m_url else return ak.string_new()
if web_url.url_host(&url) == none:
return ak.string_new()
# 4. Return url's host, serialized.
return web_url.url_serialized_host(&url)
## Ported from HTMLHyperlinkElementUtils.cpp:231 Web::HTML::HTMLHyperlinkElementUtils::set_hostname.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-hostname
pub func html_hyperlink_element_utils_set_hostname(this: HtmlHyperlinkElementUtils*, hostname: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null or url has an opaque path, then return.
var url = this.m_url else return
if web_url.url_has_an_opaque_path(&url):
return
# 4. Basic URL parse the given value, with url as url and hostname state as state override.
_ = web_url.parser_basic_parse(hostname, none, &url, web_url.ParserState.hostname)
this.m_url = url
# 5. Update href.
html_hyperlink_element_utils_update_href(this)
## Ported from HTMLHyperlinkElementUtils.cpp:251 Web::HTML::HTMLHyperlinkElementUtils::port.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
pub func html_hyperlink_element_utils_port(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url or url's port is null, return the empty string.
let url = this.m_url else return ak.string_new()
let port = web_url.url_port(&url) else return ak.string_new()
# 4. Return url's port, serialized.
return ak.string_number_u64(port)
## Ported from HTMLHyperlinkElementUtils.cpp:268 Web::HTML::HTMLHyperlinkElementUtils::set_port.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
pub func html_hyperlink_element_utils_set_port(this: HtmlHyperlinkElementUtils*, port: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null or url cannot have a username/password/port, then return.
var url = this.m_url else return
if web_url.url_cannot_have_a_username_or_password_or_port(&url):
return
# 4. If the given value is the empty string, then set url's port to null.
if ak.string_view_is_empty(&port):
web_url.url_set_port(&url, none)
# 5. Otherwise, basic URL parse the given value, with url as url and port state as state override.
else:
_ = web_url.parser_basic_parse(port, none, &url, web_url.ParserState.port)
this.m_url = url
# 6. Update href.
html_hyperlink_element_utils_update_href(this)
# mark: pathname, search and hash ===============================================================
## Ported from HTMLHyperlinkElementUtils.cpp:293 Web::HTML::HTMLHyperlinkElementUtils::pathname.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
pub func html_hyperlink_element_utils_pathname(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null, return the empty string.
let url = this.m_url else return ak.string_new()
# 4. Return the result of URL path serializing url.
return web_url.url_serialize_path(&url)
## Ported from HTMLHyperlinkElementUtils.cpp:309 Web::HTML::HTMLHyperlinkElementUtils::set_pathname.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
pub func html_hyperlink_element_utils_set_pathname(this: HtmlHyperlinkElementUtils*, pathname: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null or url has an opaque path, then return.
var url = this.m_url else return
if web_url.url_has_an_opaque_path(&url):
return
# 4. Set url's path to the empty list.
var no_paths: ak.Vector[ak.ByteString]
web_url.url_set_paths(&url, no_paths)
# 5. Basic URL parse the given value, with url as url and path start state as state override.
_ = web_url.parser_basic_parse(pathname, none, &url, web_url.ParserState.path_start)
this.m_url = url
# 6. Update href.
html_hyperlink_element_utils_update_href(this)
## Ported from HTMLHyperlinkElementUtils.cpp:331 Web::HTML::HTMLHyperlinkElementUtils::search.
pub func html_hyperlink_element_utils_search(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null, or url's query is either null or the empty string, return the empty string.
let url = this.m_url else return ak.string_new()
let query = web_url.url_query(&url) else return ak.string_new()
if ak.string_is_empty(&query):
return ak.string_new()
# 4. Return "?", followed by url's query.
var builder = ak.string_builder_new()
ak.string_builder_append(&builder, "?")
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&query))
return ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
## Ported from HTMLHyperlinkElementUtils.cpp:346 Web::HTML::HTMLHyperlinkElementUtils::set_search.
pub func html_hyperlink_element_utils_set_search(this: HtmlHyperlinkElementUtils*, search: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null, terminate these steps.
var url = this.m_url else return
# 4. If the given value is the empty string, set url's query to null.
if ak.string_view_is_empty(&search):
web_url.url_set_query(&url, none)
else:
# 5. Otherwise:
# 1. Let input be the given value with a single leading "?" removed, if any.
let input = ak.string_view_substring_view(&search, (usize)ak.string_view_starts_with_u8(&search, '?'))
# 2. Set url's query to the empty string.
web_url.url_set_query(&url, ak.string_new())
# 3. Basic URL parse input, with null, this element's node document's document's character encoding, url as url, and query state as state override.
_ = web_url.parser_basic_parse(input, none, &url, web_url.ParserState.query)
this.m_url = url
# 6. Update href.
html_hyperlink_element_utils_update_href(this)
## Ported from HTMLHyperlinkElementUtils.cpp:376 Web::HTML::HTMLHyperlinkElementUtils::hash.
pub func html_hyperlink_element_utils_hash(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null, or url's fragment is either null or the empty string, return the empty string.
let url = this.m_url else return ak.string_new()
let fragment = web_url.url_fragment(&url) else return ak.string_new()
if ak.string_is_empty(&fragment):
return ak.string_new()
# 4. Return "#", followed by url's fragment.
var builder = ak.string_builder_new()
ak.string_builder_append(&builder, "#")
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&fragment))
return ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
## Ported from HTMLHyperlinkElementUtils.cpp:391 Web::HTML::HTMLHyperlinkElementUtils::set_hash.
pub func html_hyperlink_element_utils_set_hash(this: HtmlHyperlinkElementUtils*, hash_: ak.StringView):
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
# 3. If url is null, then return.
var url = this.m_url else return
# 4. If the given value is the empty string, set url's fragment to null.
if ak.string_view_is_empty(&hash_):
web_url.url_set_fragment(&url, none)
else:
# 5. Otherwise:
# 1. Let input be the given value with a single leading "#" removed, if any.
let input = ak.string_view_substring_view(&hash_, (usize)ak.string_view_starts_with_u8(&hash_, '#'))
# 2. Set url's fragment to the empty string.
web_url.url_set_fragment(&url, ak.string_new())
# 3. Basic URL parse input, with url as url and fragment state as state override.
_ = web_url.parser_basic_parse(input, none, &url, web_url.ParserState.fragment)
this.m_url = url
# 6. Update href.
html_hyperlink_element_utils_update_href(this)
# mark: href ====================================================================================
## Ported from HTMLHyperlinkElementUtils.cpp:422 Web::HTML::HTMLHyperlinkElementUtils::href.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
pub func html_hyperlink_element_utils_href(this: HtmlHyperlinkElementUtils*) -> ak.String:
# 1. Reinitialize url.
html_hyperlink_element_utils_reinitialize_url(this)
# 2. Let url be this element's url.
let url = this.m_url
# 3. If url is null and this element has no href content attribute, return the empty string.
let href_content_attribute = dom_element_attribute(html_hyperlink_element_utils_hyperlink_element_utils_element_const(this), html_attribute_names_href)
if url == none and href_content_attribute == none:
return ak.string_new()
# 4. Otherwise, if url is null, return this element's href content attribute's value.
if url == none:
return href_content_attribute else trap("unreachable")
let present_url = url else trap("unreachable")
# 5. Return url, serialized.
return web_url.url_serialize(&present_url)
## Ported from HTMLHyperlinkElementUtils.cpp:444 Web::HTML::HTMLHyperlinkElementUtils::set_href.
# https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
pub func html_hyperlink_element_utils_set_href(this: HtmlHyperlinkElementUtils*, href: ak.String):
# The href attribute's setter must set this element's href content attribute's value to the given value.
dom_element_set_attribute_value(html_hyperlink_element_utils_hyperlink_element_utils_element(this), html_attribute_names_href, href)
## Ported from HTMLHyperlinkElementUtils.cpp:451 Web::HTML::HTMLHyperlinkElementUtils::update_href.
# https://html.spec.whatwg.org/multipage/links.html#update-href
pub func html_hyperlink_element_utils_update_href(this: HtmlHyperlinkElementUtils*):
# To update href, set the element's href content attribute's value to the element's url, serialized.
let url = this.m_url else trap("VERIFY: Optional::operator-> on an empty url")
dom_element_set_attribute_value(html_hyperlink_element_utils_hyperlink_element_utils_element(this), html_attribute_names_href, web_url.url_serialize(&url))
## Ported from HTMLHyperlinkElementUtils.cpp:458 Web::HTML::HTMLHyperlinkElementUtils::hyperlink_element_utils_extract_an_origin.
# https://html.spec.whatwg.org/multipage/links.html#api-for-a-and-area-elements:extract-an-origin
pub func html_hyperlink_element_utils_hyperlink_element_utils_extract_an_origin(this: HtmlHyperlinkElementUtils*) -> web_url.Origin?:
# 1. If this's url is null, then return null.
let url = this.m_url else return none
# 2. Return this's url's origin.
return web_url.url_origin(&url)
# mark: The including class =====================================================================
## Ported from Libraries/LibWeb/HTML/HTMLHyperlinkElementUtils.h:51 Web::HTML::HTMLHyperlinkElementUtils::hyperlink_element_utils_element (virtual).
pub func html_hyperlink_element_utils_hyperlink_element_utils_element_impl(this: HtmlHyperlinkElementUtils*) -> DomElement*:
trap("pure virtual: Web::HTML::HTMLHyperlinkElementUtils::hyperlink_element_utils_element")
## Ported from Libraries/LibWeb/HTML/HTMLHyperlinkElementUtils.h:52 Web::HTML::HTMLHyperlinkElementUtils::hyperlink_element_utils_element (virtual).
pub func html_hyperlink_element_utils_hyperlink_element_utils_element_const_impl(this: HtmlHyperlinkElementUtils*) -> DomElement*:
trap("pure virtual: Web::HTML::HTMLHyperlinkElementUtils::hyperlink_element_utils_element")