dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / form_associated_element_2.lucb
#==============================================================================================
#
# form_associated_element_2 - HTML::FormAssociatedElement: the constraint validation
# API and the FACE state
#
# DESCRIPTION:
# Port of LibWeb/HTML/FormAssociatedElement.cpp (lines 305-633) and the inline functions of
# FormAssociatedElement.h: the clear algorithm, formAction, the constraint validation API
# (willValidate, validationMessage, checkValidity, reportValidity, candidates for
# constraint validation, the suffering-from states), the form-associated custom element
# state set through ElementInternals, visit_edges and the inline virtuals.
#
#==============================================================================================
# mark: formAction ==============================================================================
## Ported from FormAssociatedElement.cpp:314 Web::HTML::FormAssociatedElement::form_action.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-formaction
pub func html_form_associated_element_form_action(this: HtmlFormAssociatedElement*) -> ak.String:
# The formAction IDL attribute must reflect the formaction content attribute, 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 html_element = html_form_associated_element_html_element(this)
let form_action_attribute = dom_element_attribute(html_element.dom_element(), html_attribute_names_formaction)
let form_action = form_action_attribute else return dom_document_url_string(html_element_document(html_element))
if ak.string_is_empty(&form_action):
return dom_document_url_string(html_element_document(html_element))
let document_base_url = dom_document_base_url(html_element_document(html_element))
if let maybe_url = web_url.url_complete_url(&document_base_url, ak.string_bytes_as_string_view(&form_action)):
return web_url.url_to_string(&maybe_url)
return ak.string_new()
## Ported from FormAssociatedElement.cpp:330 Web::HTML::FormAssociatedElement::set_form_action.
pub func html_form_associated_element_set_form_action(this: HtmlFormAssociatedElement*, value: ak.String):
let html_element = html_form_associated_element_html_element(this)
dom_element_set_attribute_value(html_element.dom_element(), html_attribute_names_formaction, value)
# mark: Constraint validation ===================================================================
## Ported from FormAssociatedElement.cpp:337 Web::HTML::FormAssociatedElement::report_validity.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-reportvalidity
pub func html_form_associated_element_report_validity(this: HtmlFormAssociatedElement*) -> bool:
# The reportValidity() method, when invoked, must run the report validity steps on this element.
return html_form_associated_element_report_validity_steps(this)
## Ported from FormAssociatedElement.cpp:344 Web::HTML::FormAssociatedElement::check_validity.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity
pub func html_form_associated_element_check_validity(this: HtmlFormAssociatedElement*) -> bool:
# The checkValidity() method, when invoked, must run the check validity steps on this element.
return html_form_associated_element_check_validity_steps(this)
## Ported from FormAssociatedElement.cpp:351 Web::HTML::FormAssociatedElement::will_validate.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-willvalidate
pub func html_form_associated_element_will_validate(this: HtmlFormAssociatedElement*) -> bool:
# The willValidate attribute's getter must return true, if this element is a candidate for constraint validation,
# and false otherwise (i.e., false if any conditions are barring it from constraint validation).
return html_form_associated_element_is_candidate_for_constraint_validation(this)
## Ported from FormAssociatedElement.cpp:359 Web::HTML::FormAssociatedElement::validation_message.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-validationmessage
pub func html_form_associated_element_validation_message(this: HtmlFormAssociatedElement*) -> ak.Utf16String:
# 1. If this element is not a candidate for constraint validation or if this element satisfies its constraints,
# then return the empty string.
if not html_form_associated_element_is_candidate_for_constraint_validation(this) or html_form_associated_element_satisfies_its_constraints(this):
return ak.utf16_string_new()
# 2. Return a suitably localized message that the user agent would show the user if this were the only form
# control with a validity constraint problem. If the user agent would not actually show a textual message in
# such a situation (e.g., it would show a graphical cue instead), then return a suitably localized message that
# expresses (one or more of) the validity constraint(s) that the control does not satisfy.
# If the element is a candidate for constraint validation and is suffering from a custom error, then
# the custom validity error message should be present in the return value.
if html_form_associated_element_suffering_from_a_custom_error(this):
return ak.utf16_string_from_utf8_string(&this.m_custom_validity_error_message)
# FIXME: Return more specific localized messages
return ak.utf16_string_from_str("Invalid form")
## Ported from FormAssociatedElement.cpp:382 Web::HTML::FormAssociatedElement::check_validity_steps.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#check-validity-steps
pub func html_form_associated_element_check_validity_steps(this: HtmlFormAssociatedElement*) -> bool:
# 1. If element is a candidate for constraint validation and does not satisfy its constraints
if html_form_associated_element_is_candidate_for_constraint_validation(this) and not html_form_associated_element_satisfies_its_constraints(this):
let element = html_form_associated_element_html_element(this)
# 1. Fire an event named invalid at element, with the cancelable attribute initialized to true
_ = dom_event_target_dispatch_event(element.dom_event_target(), dom_event_create(html_element_realm(element), html_event_names_invalid, DomEventInit(cancelable = true)))
# 2. Return false.
return false
return true
## Ported from FormAssociatedElement.cpp:396 Web::HTML::FormAssociatedElement::report_validity_steps.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#report-validity-steps
pub func html_form_associated_element_report_validity_steps(this: HtmlFormAssociatedElement*) -> bool:
# 1. If element is a candidate for constraint validation and does not satisfy its constraints, then:
if html_form_associated_element_is_candidate_for_constraint_validation(this) and not html_form_associated_element_satisfies_its_constraints(this):
let element = html_form_associated_element_html_element(this)
# 1. Let report be the result of firing an event named invalid at element, with the cancelable attribute initialized to true.
let report = dom_event_target_dispatch_event(element.dom_event_target(), dom_event_create(html_element_realm(element), html_event_names_invalid, DomEventInit(cancelable = true)))
# 2. If report is true, then report the problems with the constraints of this element to the user. When reporting the problem with the constraints to the user,
# the user agent may run the focusing steps for element, and may change the scrolling position of the document, or perform some other action that brings
# element to the user's attention. User agents may report more than one constraint violation, if element suffers from multiple problems at once.
# FIXME: Does this align with other browsers?
if report and dom_element_check_visibility(element.dom_element(), none):
html_run_focusing_steps(element.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(element.dom_element(), .dom_scroll_into_view_options(scroll_options))
# 3. Return false.
return false
# 2. Return true.
return true
## Ported from FormAssociatedElement.cpp:426 Web::HTML::FormAssociatedElement::is_candidate_for_constraint_validation.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#candidate-for-constraint-validation
pub func html_form_associated_element_is_candidate_for_constraint_validation(this: HtmlFormAssociatedElement*) -> bool:
# A submittable element is a candidate for constraint validation except when a condition has barred the element from constraint validation.
if not html_form_associated_element_is_submittable(this):
return false
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#enabling-and-disabling-form-controls%3A-the-disabled-attribute%3Abarred-from-constraint-validation
# If an element is disabled, it is barred from constraint validation.
if not html_form_associated_element_enabled(this):
return false
let html_element = html_form_associated_element_html_element(this)
# https://html.spec.whatwg.org/multipage/form-elements.html#the-datalist-element%3Abarred-from-constraint-validation
# If an element has a datalist element ancestor, it is barred from constraint validation.
if tree_node_first_ancestor_of_type(html_element.dom_node(), &html_data_list_element_class) != none:
return false
if is_html_input_element(html_element):
let input_element = as_html_input_element(html_element)
# https://html.spec.whatwg.org/multipage/input.html#hidden-state-(type%3Dhidden)%3Abarred-from-constraint-validation
# If an input element's type attribute is in the Hidden state, it is barred from constraint validation.
if html_input_element_type_state(input_element) == HtmlInputElementTypeAttributeState.hidden:
return false
# https://html.spec.whatwg.org/multipage/input.html#reset-button-state-(type%3Dreset)%3Abarred-from-constraint-validation
# When an input element's type attribute is in the Reset Button state, the rules in this section apply.
# The element is barred from constraint validation.
if html_input_element_type_state(input_element) == HtmlInputElementTypeAttributeState.reset_button:
return false
# https://html.spec.whatwg.org/multipage/input.html#button-state-(type%3Dbutton)%3Abarred-from-constraint-validation
# When an input element's type attribute is in the Button state, the rules in this section apply.
# The element is barred from constraint validation.
if html_input_element_type_state(input_element) == HtmlInputElementTypeAttributeState.button:
return false
# https://html.spec.whatwg.org/multipage/input.html#the-readonly-attribute%3Abarred-from-constraint-validation
# If the readonly attribute is specified on an input element, the element is barred from constraint validation.
if dom_element_has_attribute(input_element.dom_element(), html_attribute_names_readonly):
return false
if is_html_button_element(html_element):
let button_element = as_html_button_element(html_element)
# https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element%3Abarred-from-constraint-validation
# If the element is not a submit button, the element is barred from constraint validation.
if not html_form_associated_element_is_submit_button(&button_element.html_element().form_associated_element):
return false
if is_html_text_area_element(html_element):
# https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element%3Abarred-from-constraint-validation
# If the readonly attribute is specified on a textarea element, the element is barred from constraint validation.
if dom_element_has_attribute(html_element.dom_element(), html_attribute_names_readonly):
return false
# https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-core-concepts:barred-from-constraint-validation-2
# If the readonly attribute is specified on a form-associated custom element, the element is barred from constraint validation.
if html_element_is_form_associated_custom_element(html_element) and dom_element_has_attribute(html_element.dom_element(), html_attribute_names_readonly):
return false
return true
## Ported from FormAssociatedElement.cpp:495 Web::HTML::FormAssociatedElement::satisfies_its_constraints.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fv-valid
pub func html_form_associated_element_satisfies_its_constraints(this: HtmlFormAssociatedElement*) -> bool:
return not (html_form_associated_element_suffering_from_being_missing(this) or html_form_associated_element_suffering_from_a_type_mismatch(this) or html_form_associated_element_suffering_from_a_pattern_mismatch(this) or html_form_associated_element_suffering_from_being_too_long(this) or html_form_associated_element_suffering_from_being_too_short(this) or html_form_associated_element_suffering_from_an_underflow(this) or html_form_associated_element_suffering_from_an_overflow(this) or html_form_associated_element_suffering_from_a_step_mismatch(this) or html_form_associated_element_suffering_from_bad_input(this) or html_form_associated_element_suffering_from_a_custom_error(this))
## Ported from FormAssociatedElement.cpp:502 Web::HTML::FormAssociatedElement::novalidate_state.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fs-novalidate
pub func html_form_associated_element_novalidate_state(this: HtmlFormAssociatedElement*) -> bool:
# The no-validate state of an element is true if the element is a submit button ...
if not html_form_associated_element_is_submit_button(this):
return false
# ..., and the element's formnovalidate attribute is present, ...
let html_element = html_form_associated_element_html_element(this)
if dom_element_has_attribute(html_element.dom_element(), html_attribute_names_formnovalidate):
return true
# ... or if the element's form owner's novalidate attribute is present, ...
if let form = html_form_associated_element_form(this):
if dom_element_has_attribute(form.dom_element(), html_attribute_names_novalidate):
return true
# ... and false otherwise.
return false
# mark: Suffering from ==========================================================================
## Ported from FormAssociatedElement.cpp:523 Web::HTML::FormAssociatedElement::suffering_from_being_missing (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-being-missing
pub func html_form_associated_element_suffering_from_being_missing_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets valueMissing flag to true for a form-associated custom element.
return this.m_face_validity_flags.value_missing
## Ported from FormAssociatedElement.cpp:530 Web::HTML::FormAssociatedElement::suffering_from_a_type_mismatch (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-type-mismatch
pub func html_form_associated_element_suffering_from_a_type_mismatch_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets typeMismatch flag to true for a form-associated custom element.
return this.m_face_validity_flags.type_mismatch
## Ported from FormAssociatedElement.cpp:537 Web::HTML::FormAssociatedElement::suffering_from_a_pattern_mismatch (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-pattern-mismatch
pub func html_form_associated_element_suffering_from_a_pattern_mismatch_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets patternMismatch flag to true for a form-associated custom element.
return this.m_face_validity_flags.pattern_mismatch
## Ported from FormAssociatedElement.cpp:544 Web::HTML::FormAssociatedElement::suffering_from_being_too_long (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-being-too-long
pub func html_form_associated_element_suffering_from_being_too_long_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets tooLong flag to true for a form-associated custom element.
# FIXME: Implement this for non-FACEs.
return this.m_face_validity_flags.too_long
## Ported from FormAssociatedElement.cpp:552 Web::HTML::FormAssociatedElement::suffering_from_being_too_short (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-being-too-short
pub func html_form_associated_element_suffering_from_being_too_short_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets tooShort flag to true for a form-associated custom element.
# FIXME: Implement this for non-FACEs.
return this.m_face_validity_flags.too_short
## Ported from FormAssociatedElement.cpp:560 Web::HTML::FormAssociatedElement::suffering_from_an_underflow (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-an-overflow
pub func html_form_associated_element_suffering_from_an_underflow_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets rangeUnderflow flag to true for a form-associated custom element.
return this.m_face_validity_flags.range_underflow
## Ported from FormAssociatedElement.cpp:567 Web::HTML::FormAssociatedElement::suffering_from_an_overflow (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-an-overflow
pub func html_form_associated_element_suffering_from_an_overflow_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets rangeOverflow flag to true for a form-associated custom element.
return this.m_face_validity_flags.range_overflow
## Ported from FormAssociatedElement.cpp:574 Web::HTML::FormAssociatedElement::suffering_from_a_step_mismatch (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-step-mismatch
pub func html_form_associated_element_suffering_from_a_step_mismatch_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets stepMismatch flag to true for a form-associated custom element.
return this.m_face_validity_flags.step_mismatch
## Ported from FormAssociatedElement.cpp:581 Web::HTML::FormAssociatedElement::suffering_from_bad_input (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-bad-input
pub func html_form_associated_element_suffering_from_bad_input_impl(this: HtmlFormAssociatedElement*) -> bool:
# When the setValidity() method sets badInput flag to true for a form-associated custom element.
return this.m_face_validity_flags.bad_input
## Ported from FormAssociatedElement.cpp:588 Web::HTML::FormAssociatedElement::suffering_from_a_custom_error.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-custom-error
pub func html_form_associated_element_suffering_from_a_custom_error(this: HtmlFormAssociatedElement*) -> bool:
# When a control's custom validity error message (as set by the element's setCustomValidity() method or ElementInternals's setValidity() method) is not the empty
# string.
return not ak.string_is_empty(&this.m_custom_validity_error_message)
# mark: The form-associated custom element state ================================================
## Ported from FormAssociatedElement.h:150 Web::HTML::FormAssociatedElement::face_validity_flags.
pub func html_form_associated_element_face_validity_flags(this: HtmlFormAssociatedElement*) -> HtmlValidityStateFlags:
return this.m_face_validity_flags
## Ported from FormAssociatedElement.cpp:595 Web::HTML::FormAssociatedElement::set_face_validity_flags.
pub func html_form_associated_element_set_face_validity_flags(this: HtmlFormAssociatedElement*, value: HtmlValidityStateFlags):
this.m_face_validity_flags = value
## Ported from FormAssociatedElement.h:153 Web::HTML::FormAssociatedElement::face_validation_message.
pub func html_form_associated_element_face_validation_message(this: HtmlFormAssociatedElement*) -> ak.String:
return this.m_face_validation_message
## Ported from FormAssociatedElement.cpp:600 Web::HTML::FormAssociatedElement::set_face_validation_message.
pub func html_form_associated_element_set_face_validation_message(this: HtmlFormAssociatedElement*, value: ak.String):
this.m_face_validation_message = value
## Ported from FormAssociatedElement.cpp:605 Web::HTML::FormAssociatedElement::set_face_validation_anchor.
pub func html_form_associated_element_set_face_validation_anchor(this: HtmlFormAssociatedElement*, value: HtmlElement*?):
gc.weak_assign(&this.m_face_validation_anchor, value)
## Ported from FormAssociatedElement.h:158 Web::HTML::FormAssociatedElement::face_submission_value.
pub func html_form_associated_element_face_submission_value(this: HtmlFormAssociatedElement*) -> HtmlFormAssociatedElementFaceSubmissionValue:
return this.m_face_submission_value
## Ported from FormAssociatedElement.cpp:610 Web::HTML::FormAssociatedElement::set_face_submission_value.
pub func html_form_associated_element_set_face_submission_value(this: HtmlFormAssociatedElement*, value: HtmlFormAssociatedElementFaceSubmissionValue):
this.m_face_submission_value = html_face_submission_value_clone(value)
## Ported from FormAssociatedElement.h:161 Web::HTML::FormAssociatedElement::face_state.
pub func html_form_associated_element_face_state(this: HtmlFormAssociatedElement*) -> HtmlFormAssociatedElementFaceSubmissionValue:
return this.m_face_state
## Ported from FormAssociatedElement.cpp:615 Web::HTML::FormAssociatedElement::set_face_state.
pub func html_form_associated_element_set_face_state(this: HtmlFormAssociatedElement*, value: HtmlFormAssociatedElementFaceSubmissionValue):
this.m_face_state = html_face_submission_value_clone(value)
## Ported from FormAssociatedElement.h:164 Web::HTML::FormAssociatedElement::set_custom_validity_error_message.
pub func html_form_associated_element_set_custom_validity_error_message(this: HtmlFormAssociatedElement*, value: ak.String):
this.m_custom_validity_error_message = value
## The copy assignment of a FACESubmissionValue (C++ copies the variant, and so its list of entries):
## a list of entries is cloned so the element's list does not share the caller's storage.
func html_face_submission_value_clone(value: HtmlFormAssociatedElementFaceSubmissionValue) -> HtmlFormAssociatedElementFaceSubmissionValue:
match value:
.vector_xhr_form_data_entry(entries):
return HtmlFormAssociatedElementFaceSubmissionValue.vector_xhr_form_data_entry(ak.vector_clone(&entries))
_:
return value
## The File of a FACESubmissionValue holding one (the visitor's `[&](GC::Ref<FileAPI::File>)` arm).
func html_face_submission_value_file(value: HtmlFormAssociatedElementFaceSubmissionValue) -> FileApiFile*?:
return match value:
.file_api_file(file) => file
_ => none
## Ported from FormAssociatedElement.cpp:620 Web::HTML::FormAssociatedElement::visit_edges.
pub func html_form_associated_element_visit_edges(this: HtmlFormAssociatedElement*, visitor: gc.CellVisitor*):
if let file = html_face_submission_value_file(this.m_face_submission_value):
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)file) # FileAPI::File is opaque in phase 1
if let file = html_face_submission_value_file(this.m_face_state):
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)file) # FileAPI::File is opaque in phase 1
# mark: Inline virtuals of FormAssociatedElement.h ==============================================
## Ported from FormAssociatedElement.h:76 Web::HTML::FormAssociatedElement::is_autocapitalize_and_autocorrect_inheriting (virtual).
# https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
pub func html_form_associated_element_is_autocapitalize_and_autocorrect_inheriting_impl(this: HtmlFormAssociatedElement*) -> bool:
return false
## Ported from FormAssociatedElement.h:79 Web::HTML::FormAssociatedElement::is_button (virtual).
# https://html.spec.whatwg.org/multipage/forms.html#concept-button
pub func html_form_associated_element_is_button_impl(this: HtmlFormAssociatedElement*) -> bool:
return false
## Ported from FormAssociatedElement.h:82 Web::HTML::FormAssociatedElement::is_submit_button (virtual).
# https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
pub func html_form_associated_element_is_submit_button_impl(this: HtmlFormAssociatedElement*) -> bool:
return false
## Ported from FormAssociatedElement.h:111 Web::HTML::FormAssociatedElement::form_value (virtual).
pub func html_form_associated_element_form_value_impl(this: HtmlFormAssociatedElement*) -> ak.Utf16String:
return ak.utf16_string_new()
## Ported from FormAssociatedElement.h:112 Web::HTML::FormAssociatedElement::optional_value (virtual).
pub func html_form_associated_element_optional_value_impl(this: HtmlFormAssociatedElement*) -> ak.String?:
trap("VERIFY_NOT_REACHED")
## Ported from FormAssociatedElement.h:114 Web::HTML::FormAssociatedElement::form_associated_element_to_html_element (virtual).
pub func html_form_associated_element_form_associated_element_to_html_element_impl(this: HtmlFormAssociatedElement*) -> HtmlElement*:
trap("pure virtual: Web::HTML::FormAssociatedElement::form_associated_element_to_html_element")
## Ported from FormAssociatedElement.h:115 Web::HTML::FormAssociatedElement::form_associated_element_to_html_element.
pub func html_form_associated_element_form_associated_element_to_html_element_const(this: HtmlFormAssociatedElement*) -> HtmlElement*:
return html_form_associated_element_form_associated_element_to_html_element(this)
## Ported from FormAssociatedElement.h:144 Web::HTML::FormAssociatedElement::is_mutable (virtual).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#mutability
pub func html_form_associated_element_is_mutable_impl(this: HtmlFormAssociatedElement*) -> bool:
return true