dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / html_form_element_2.lucb
#==============================================================================================
#
# html_form_element_2 - HTML::HTMLFormElement: constraint validation and the
# form's attributes
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLFormElement.cpp (lines 554-800): statically and interactively
# validating the constraints, checkValidity / reportValidity, the submittable elements,
# relList, method, action, the rel attribute's change steps, picking an encoding, and the
# name-value pairs and text/plain encodings.
#
#==============================================================================================
# mark: Constraint validation ===================================================================
## Ported from HTMLFormElement.cpp:555 Web::HTML::HTMLFormElement::statically_validate_constraints.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#statically-validate-the-constraints
pub func html_form_element_statically_validate_constraints(this: HtmlFormElement*) -> HtmlFormElementStaticValidationResult:
# 1. Let controls be a list of all the submittable elements whose form owner is form, in tree order.
var controls = html_form_element_get_submittable_elements(this)
# 2. Let invalid controls be an initially empty list of elements.
var invalid_controls: ak.Vector[DomElement*]
# 3. For each element field in controls, in tree order:
for element in ak.vector_span(&controls):
let field = as_if_form_associated_element(element) else trap("as<FormAssociatedElement> failed")
# 1. If field is not a candidate for constraint validation, then move on to the next element.
if not html_form_associated_element_is_candidate_for_constraint_validation(field):
continue
# 2. Otherwise, if field satisfies its constraints, then move on to the next element.
if html_form_associated_element_satisfies_its_constraints(field):
continue
# 3. Otherwise, add field to invalid controls.
ak.vector_append_t(&invalid_controls, html_form_associated_element_form_associated_element_to_html_element(field).dom_element())
# 4. If invalid controls is empty, then return a positive result.
if ak.vector_is_empty(&invalid_controls):
return HtmlFormElementStaticValidationResult(result = true, unhandled_invalid_controls = invalid_controls)
# 5. Let unhandled invalid controls be an initially empty list of elements.
var unhandled_invalid_controls: ak.Vector[DomElement*]
# 6. For each element field in invalid controls, if any, in tree order:
for field in ak.vector_span(&invalid_controls):
# 1. Let notCanceled be the result of firing an event named invalid at field, with the cancelable attribute
# initialized to true.
let not_canceled = dom_event_target_dispatch_event(field.dom_event_target(), dom_event_create(html_element_realm(this.html_element()), html_event_names_invalid, DomEventInit(cancelable = true)))
# 2. If notCanceled is true, then add field to unhandled invalid controls.
if not_canceled:
ak.vector_append_t(&unhandled_invalid_controls, field)
# 7. Return a negative result with the list of elements in the unhandled invalid controls list.
return HtmlFormElementStaticValidationResult(result = false, unhandled_invalid_controls = unhandled_invalid_controls)
## Ported from HTMLFormElement.cpp:593 Web::HTML::HTMLFormElement::interactively_validate_constraints.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#interactively-validate-the-constraints
pub func html_form_element_interactively_validate_constraints(this: HtmlFormElement*) -> bool:
# 1. Statically validate the constraints of form, and let unhandled invalid controls be the list of elements returned if the result was negative.
# 2. If the result was positive, then return that result.
let static_validation_result = html_form_element_statically_validate_constraints(this)
if static_validation_result.result:
return true
var unhandled_invalid_controls = static_validation_result.unhandled_invalid_controls
# 3. Report the problems with the constraints of at least one of the elements given in unhandled invalid controls to the user.
# - User agents may focus one of those elements in the process, by running the focusing steps for that element, and may change the
# scrolling position of the document, or perform some other action that brings the element to the user's attention.
# For elements that are form-associated custom elements, user agents should use their validation anchor instead, for the purposes of these actions.
# - User agents may report more than one constraint violation.
# - User agents may coalesce related constraint violation reports if appropriate (e.g. if multiple radio buttons in a group are marked as required, only one error need be reported).
# - If one of the controls is not being rendered (e.g. it has the hidden attribute set), then user agents may report a script error.
# FIXME: Does this align with other browsers?
var first_invalid_control: DomElement*? = none
for control in ak.vector_span(&unhandled_invalid_controls):
if dom_element_check_visibility(control, none):
first_invalid_control = control
break
if let control = first_invalid_control:
html_run_focusing_steps(control.dom_node())
var scroll_options = DomScrollIntoViewOptions()
dom_scroll_into_view_options_init_fields(&scroll_options)
scroll_options.block = BindScrollLogicalPosition.nearest
scroll_options.inline_ = BindScrollLogicalPosition.nearest
scroll_options.scroll_options.behavior = BindScrollBehavior.instant
_ = dom_element_scroll_into_view(control, .dom_scroll_into_view_options(scroll_options))
# 4. Return a negative result.
return false
## Ported from HTMLFormElement.cpp:628 Web::HTML::HTMLFormElement::check_validity.
# https://html.spec.whatwg.org/multipage/forms.html#dom-form-checkvalidity
pub func html_form_element_check_validity(this: HtmlFormElement*) -> bool!:
return html_form_element_statically_validate_constraints(this).result
## Ported from HTMLFormElement.cpp:634 Web::HTML::HTMLFormElement::report_validity.
# https://html.spec.whatwg.org/multipage/forms.html#dom-form-reportvalidity
pub func html_form_element_report_validity(this: HtmlFormElement*) -> bool!:
return html_form_element_interactively_validate_constraints(this)
## Ported from HTMLFormElement.cpp:640 Web::HTML::HTMLFormElement::get_submittable_elements.
# https://html.spec.whatwg.org/multipage/forms.html#category-submit
pub func html_form_element_get_submittable_elements(this: HtmlFormElement*) -> ak.Vector[DomElement*]:
var submittable_elements: ak.Vector[DomElement*]
for node in dom_node_subtree(tree_node_root(this.dom_node())):
if let form_associated_element = as_if_form_associated_element(node):
if html_form_associated_element_is_submittable(form_associated_element) and html_form_associated_element_form(form_associated_element) == this:
ak.vector_append_t(&submittable_elements, html_form_associated_element_form_associated_element_to_html_element(form_associated_element).dom_element())
return submittable_elements
# mark: relList, method and action ==============================================================
## Ported from HTMLFormElement.cpp:657 Web::HTML::HTMLFormElement::rel_list.
# https://html.spec.whatwg.org/multipage/forms.html#dom-form-rellist
pub func html_form_element_rel_list(this: HtmlFormElement*) -> DomTokenList*:
# The relList IDL attribute must reflect the rel content attribute.
if this.m_rel_list == none:
this.m_rel_list = dom_token_list_create(this.dom_element(), html_attribute_names_rel)
return this.m_rel_list else trap("unreachable")
## Ported from HTMLFormElement.cpp:666 Web::HTML::HTMLFormElement::set_method.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-method
pub func html_form_element_set_method(this: HtmlFormElement*, method: ak.String):
# The method and enctype IDL attributes must reflect the respective content attributes of the same name, limited to only known values.
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_method, method)
## Ported from HTMLFormElement.cpp:673 Web::HTML::HTMLFormElement::action.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-action
pub func html_form_element_action(this: HtmlFormElement*) -> ak.String:
# The action IDL attribute must reflect the content attribute of the same name, except that on getting, when the
# content attribute is missing or its value is the empty string, the element's node document's URL must be returned
# instead.
let document = dom_node_document(this.dom_node())
let form_action_attribute = dom_element_attribute(this.dom_element(), html_attribute_names_action)
let form_action = form_action_attribute else return dom_document_url_string(document)
if ak.string_is_empty(&form_action):
return dom_document_url_string(document)
let base_url = dom_document_base_url(document)
if let maybe_url = web_url.url_complete_url(&base_url, ak.string_bytes_as_string_view(&form_action)):
return web_url.url_to_string(&maybe_url)
return ak.string_new()
## Ported from HTMLFormElement.cpp:689 Web::HTML::HTMLFormElement::set_action.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-action
pub func html_form_element_set_action(this: HtmlFormElement*, value: ak.String):
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_action, value)
## Ported from HTMLFormElement.cpp:694 Web::HTML::HTMLFormElement::attribute_changed (override).
pub func html_form_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
let this = (HtmlFormElement*)base
html_element_attribute_changed_impl(base, name, old_value, value, namespace_)
if ak.fly_string_eq_fly_string(&name, &html_attribute_names_rel):
if let rel_list = this.m_rel_list:
let new_value = value else ak.string_new()
dom_token_list_associated_attribute_changed(rel_list, ak.string_bytes_as_string_view(&new_value))
## Ported from HTMLFormElement.cpp:705 Web::HTML::HTMLFormElement::pick_an_encoding.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#picking-an-encoding-for-the-form
pub func html_form_element_pick_an_encoding(this: HtmlFormElement*) -> ak.String!:
# 1. Let encoding be the document's character encoding.
let encoding = dom_document_encoding_or_default(dom_node_document(this.dom_node()))
# 2. If the form element has an accept-charset attribute, set encoding to the return value of running these substeps:
if let maybe_input = dom_element_attribute(this.dom_element(), html_attribute_names_accept_charset):
# 1. Let input be the value of the form element's accept-charset attribute.
let input = maybe_input
# 2. Let candidate encoding labels be the result of splitting input on ASCII whitespace.
let input_view = ak.string_bytes_as_string_view(&input)
let predicate = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
var candidate_encoding_labels = ak.string_view_split_view_if(&input_view, &predicate)
# 3. Let candidate encodings be an empty list of character encodings.
var candidate_encodings: ak.Vector[ak.StringView]
# 4. For each token in candidate encoding labels in turn (in the order in which they were found in input),
# get an encoding for the token and, if this does not result in failure, append the encoding to candidate
# encodings.
for token in ak.vector_span(&candidate_encoding_labels):
if let candidate_encoding = text_codec.get_standardized_encoding(token):
try ak.vector_try_append_t(&candidate_encodings, candidate_encoding)
# 5. If candidate encodings is empty, return UTF-8.
if ak.vector_is_empty(&candidate_encodings):
return ak.string_from_str("UTF-8")
# 6. Return the first encoding in candidate encodings.
return try ak.string_from_utf8(*ak.vector_first(&candidate_encodings))
# 3. Return the result of getting an output encoding from encoding.
return ak.string_from_utf8(text_codec.get_output_encoding(ak.string_bytes_as_string_view(&encoding))) else trap("MUST: String::from_utf8")
# mark: Encoding the entry list =================================================================
## Ported from HTMLFormElement.cpp:743 Web::HTML::convert_to_list_of_name_value_pairs.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#convert-to-a-list-of-name-value-pairs
func html_convert_to_list_of_name_value_pairs(entry_list: ak.Vector[XhrFormDataEntry]) -> ak.Vector[DomurlQueryParam]!:
# 1. Let list be an empty list of name-value pairs.
var list: ak.Vector[DomurlQueryParam]
# 2. For each entry of entry list:
for entry in ak.vector_span_const(&entry_list):
# 1. Let name be entry's name, with every occurrence of U+000D (CR) not followed by U+000A (LF), and every occurrence of U+000A (LF)
# not preceded by U+000D (CR), replaced by a string consisting of U+000D (CR) and U+000A (LF).
let name = try html_normalize_line_breaks(ak.string_bytes_as_string_view(&entry.name))
# 2. If entry's value is a File object, then let value be entry's value's name. Otherwise, let value be entry's value.
var value = ak.string_new()
match entry.value:
.file_api_file(file):
value = file_api_file_name(file)
.string(string):
value = string
# 3. Replace every occurrence of U+000D (CR) not followed by U+000A (LF), and every occurrence of
# U+000A (LF) not preceded by U+000D (CR), in value, by a string consisting of U+000D (CR) and U+000A (LF).
let normalized_value = try html_normalize_line_breaks(ak.string_bytes_as_string_view(&value))
# 4. Append to list a new name-value pair whose name is name and whose value is value.
try ak.vector_try_append_t(&list, DomurlQueryParam(name = name, value = normalized_value))
# 3. Return list.
return list
## Ported from HTMLFormElement.cpp:777 Web::HTML::plain_text_encode.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#text/plain-encoding-algorithm
func html_plain_text_encode(pairs: ak.Vector[DomurlQueryParam]) -> ak.String!:
# 1. Let result be the empty string.
var result = ak.string_builder_new()
# 2. For each pair in pairs:
for pair in ak.vector_span_const(&pairs):
# 1. Append pair's name to result.
try ak.string_builder_try_append_string_view(&result, ak.string_bytes_as_string_view(&pair.name))
# 2. Append a single U+003D EQUALS SIGN character (=) to result.
try ak.string_builder_try_append_u8(&result, '=')
# 3. Append pair's value to result.
try ak.string_builder_try_append_string_view(&result, ak.string_bytes_as_string_view(&pair.value))
# 4. Append a U+000D CARRIAGE RETURN (CR) U+000A LINE FEED (LF) character pair to result.
try ak.string_builder_try_append_string_view(&result, ak.sv("\r\n"))
# 3. Return result.
return try ak.string_builder_to_string(&result)