dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / element_factory_3.lucb
#==============================================================================================
#
# element_factory_3 - DOM::ElementFactory: creating an element
#
# DESCRIPTION:
# Port of LibWeb/DOM/ElementFactory.cpp (lines 610-800): creating an element internal
# and the DOM's "create an element" algorithm, with its custom element branches. The
# C++ `Interface` template parameter is a function pointer making the element.
#
#==============================================================================================
# mark: Element interfaces ======================================================================
## The `Interface` of create_element_internal: makes the element for a qualified name.
type DomElementInterface = func(DomDocument*, DomQualifiedName) -> DomElement*
## create_svg_element as an element interface.
func dom_create_svg_element_interface(document: DomDocument*, qualified_name: DomQualifiedName) -> DomElement*:
return dom_create_svg_element(document, qualified_name).dom_element()
## create_mathml_element as an element interface.
func dom_create_mathml_element_interface(document: DomDocument*, qualified_name: DomQualifiedName) -> DomElement*:
return dom_create_mathml_element(document, qualified_name).dom_element()
## `[](auto& document, auto qualified_name) { return document.realm().create<HTML::HTMLUnknownElement>(document, qualified_name); }`.
func dom_create_html_unknown_element_interface(document: DomDocument*, qualified_name: DomQualifiedName) -> DomElement*:
return realm_create_html_unknown_element(bind_platform_object_realm(document.bind_platform_object()), document, qualified_name).dom_element()
## `[](auto& document, auto qualified_name) { return document.realm().create<HTML::HTMLElement>(document, qualified_name); }`.
func dom_create_html_element_interface(document: DomDocument*, qualified_name: DomQualifiedName) -> DomElement*:
return realm_create_html_element(bind_platform_object_realm(document.bind_platform_object()), document, qualified_name).dom_element()
## `[](auto& document, auto qualified_name) { return document.realm().create<DOM::Element>(document, qualified_name); }`.
func dom_create_dom_element_interface(document: DomDocument*, qualified_name: DomQualifiedName) -> DomElement*:
return realm_create_dom_element(bind_platform_object_realm(document.bind_platform_object()), document, qualified_name)
# mark: Create an element =======================================================================
## Ported from ElementFactory.cpp:612 Web::DOM::create_element_internal.
# https://dom.spec.whatwg.org/#create-an-element-internal
func dom_create_element_internal(document: DomDocument*, interface_: DomElementInterface, local_name: ak.FlyString, namespace_: ak.FlyString?, prefix: ak.FlyString?, custom_element_state: DomCustomElementState, is_value: ak.String?, registry: HtmlCustomElementRegistry*?) -> DomElement*:
# 1. Let element be a new element that implements interface, with namespace set to namespace, namespace prefix set
# to prefix, local name set to localName, custom element registry set to registry, custom element state set to
# state, custom element definition set to null, is value set to is, and node document set to document.
var qualified_name: DomQualifiedName = ---
dom_qualified_name_construct(&qualified_name, local_name, prefix, namespace_)
let element = interface_(document, qualified_name)
dom_element_set_custom_element_registry(element, registry)
dom_element_set_custom_element_state(element, custom_element_state)
dom_element_set_is_value(element, is_value)
# NB: Document is set in the call to `interface()` above. Custom element definition is null by default.
# 2. Assert: element’s attribute list is empty.
assert(not dom_element_has_attributes(element))
# 3. Return element.
return element
## create_element's `synchronously_upgrade_custom_element` lambda: construct C and check the result.
func dom_create_element_synchronously_upgrade_custom_element(document: DomDocument*, constructor: IdlCallbackType*, local_name: ak.FlyString, prefix: ak.FlyString?, registry: HtmlCustomElementRegistry*?) -> DomElement*!:
let realm = bind_platform_object_realm(document.bind_platform_object())
# 1. Set result to the result of constructing C, with no arguments.
# NOTE: IDL does not currently convert the object for us, so we will have to do it here.
var no_arguments: ak.Vector[JsValue]
let constructed = try js_completion_release_value(idl_construct(constructor, ak.vector_span(&no_arguments)))
let maybe_result = js_value_as_if_html_element(constructed)
if maybe_result == none:
try vm_throw_completion(js_cell_vm(document.js_cell()), IdlSimpleExceptionType.type_error, .string_view(ak.sv("Custom element constructor must return an object that implements HTMLElement")))
let result = (maybe_result else trap("unreachable")).dom_element()
# FIXME: 2. Assert: result’s custom element state and custom element definition are initialized.
# 3. Assert: result’s namespace is the HTML namespace.
# Note: IDL enforces that result is an HTMLElement object, which all use the HTML namespace.
assert(dom_node_optional_fly_string_is(dom_element_namespace_uri(result), namespace_html))
# 4. If result’s attribute list is not empty, then throw a "NotSupportedError" DOMException.
if dom_element_has_attributes(result):
try idl_throw(.idl_dom_exception(idl_not_supported_error_create(realm, ak.utf16_string_from_str("Synchronously created custom element cannot have attributes"))))
# 5. If result has children, then throw a "NotSupportedError" DOMException.
if tree_node_has_children(result.dom_node()):
try idl_throw(.idl_dom_exception(idl_not_supported_error_create(realm, ak.utf16_string_from_str("Synchronously created custom element cannot have children"))))
# 6. If result’s parent is not null, then throw a "NotSupportedError" DOMException.
if tree_node_parent(result.dom_node()) != none:
try idl_throw(.idl_dom_exception(idl_not_supported_error_create(realm, ak.utf16_string_from_str("Synchronously created custom element cannot have a parent"))))
# 7. If result’s node document is not document, then throw a "NotSupportedError" DOMException.
if dom_node_document(result.dom_node()) != document:
try idl_throw(.idl_dom_exception(idl_not_supported_error_create(realm, ak.utf16_string_from_str("Synchronously created custom element must be in the same document that element creation was invoked in"))))
# 8. If result’s local name is not equal to localName, then throw a "NotSupportedError" DOMException.
let result_local_name = dom_element_local_name(result)
if not ak.fly_string_eq_fly_string(&result_local_name, &local_name):
try idl_throw(.idl_dom_exception(idl_not_supported_error_create(realm, ak.utf16_string_from_str("Synchronously created custom element must have the same local name that element creation was invoked with"))))
# 9. Set result’s namespace prefix to prefix.
dom_element_set_prefix(result, prefix)
# 10. Set result’s is value to null.
dom_element_set_is_value(result, none)
# 11. Set result’s custom element registry to registry.
dom_element_set_custom_element_registry(result, registry)
return result
## `as<HTML::WindowOrWorkerGlobalScopeMixin>(relevant_global_object(definition->constructor().callback))
## .report_an_exception(...)` with the exception the failed step left pending.
func dom_create_element_report_exception(definition: HtmlCustomElementDefinition*):
let global = html_relevant_global_object(html_custom_element_definition_constructor(definition).callback)
let window_or_worker = as_if_window_or_worker_global_scope_mixin((gc.Cell*)global) else trap("as<HTML::WindowOrWorkerGlobalScopeMixin> failed")
html_window_or_worker_global_scope_mixin_report_an_exception(window_or_worker, js_take_pending_exception_value())
## Ported from ElementFactory.cpp:632 Web::DOM::create_element.
# https://dom.spec.whatwg.org/#concept-create-element
pub func dom_create_element(document: DomDocument*, local_name: ak.FlyString, namespace_: ak.FlyString?, prefix: ak.FlyString? = none, is_value: ak.String? = none, synchronous_custom_elements_flag: bool = false, initial_registry: HtmlCustomElementRegistryOrDomDefault = HtmlCustomElementRegistryOrDomDefault.dom_default(DomDefault(empty_ = 0))) -> DomElement*!:
# 1. Let result be null.
var result: DomElement*? = none
# 2. If registry is "default", then set registry to the result of looking up a custom element registry given
# document.
let registry = match initial_registry:
.dom_default(_) => html_look_up_a_custom_element_registry(document.dom_node())
.html_custom_element_registry(pointer) => pointer
# 3. Let definition be the result of looking up a custom element definition given registry, namespace, localName,
# and is.
let definition = html_look_up_a_custom_element_definition(registry, namespace_, local_name, is_value)
# 4. If definition is non-null, and definition’s name is not equal to its local name (i.e., definition represents
# a customized built-in element):
var is_customized_built_in = false
if let found = definition:
let name = html_custom_element_definition_name(found)
let definition_local_name = html_custom_element_definition_local_name(found)
is_customized_built_in = not ak.string_equals(&name, &definition_local_name)
if is_customized_built_in:
let found = definition else trap("unreachable")
# 1. Let interface be the element interface for localName and the HTML namespace.
# 2. Set result to the result of creating an element internal given document, interface, localName, the HTML
# namespace, prefix, "undefined", is, and registry.
let created = dom_create_element_internal(document, dom_create_html_element, local_name, namespace_html, prefix, DomCustomElementState.undefined, is_value, registry)
result = created
# 3. If synchronousCustomElements is true, then run this step while catching any exceptions:
if synchronous_custom_elements_flag:
# 1. Upgrade result using definition.
# If this step threw an exception exception:
dom_element_upgrade_element(created, found) catch:
# 1. Report exception for definition’s constructor’s corresponding JavaScript object’s associated
# realm’s global object.
dom_create_element_report_exception(found)
# 2. Set result’s custom element state to "failed".
dom_element_set_custom_element_state(created, DomCustomElementState.failed)
recover ()
# 4. Otherwise, enqueue a custom element upgrade reaction given result and definition.
else:
dom_element_enqueue_a_custom_element_upgrade_reaction(created, found)
# 5. Otherwise, if definition is non-null, then:
elif let found = definition:
# 1. If synchronousCustomElements is true:
if synchronous_custom_elements_flag:
# 1. Let C be definition’s constructor.
let constructor = html_custom_element_definition_constructor(found)
# 2. Set the surrounding agent’s active custom element constructor map[C] to registry.
let active_custom_element_constructor_map = &html_relevant_similar_origin_window_agent((JsObject*)document).active_custom_element_constructor_map
_ = ak.hash_map_set(active_custom_element_constructor_map, (JsFunctionObject*)constructor.callback, registry)
# 3. Run these steps while catching any exceptions:
# If any of these steps threw an exception, then:
result = dom_create_element_synchronously_upgrade_custom_element(document, constructor, local_name, prefix, registry) catch:
# 1. Report exception for definition’s constructor’s corresponding JavaScript object’s associated
# realm’s global object.
dom_create_element_report_exception(found)
# 2. Set result to the result of creating an element internal given document, HTMLUnknownElement,
# localName, the HTML namespace, prefix, "failed", null, and registry.
recover dom_create_element_internal(document, dom_create_html_unknown_element_interface, local_name, namespace_html, prefix, DomCustomElementState.failed, none, registry)
# 4. Remove the surrounding agent’s active custom element constructor map[C].
# Note: Under normal circumstances it will already have been removed at this point.
_ = ak.hash_map_remove_k(active_custom_element_constructor_map, (JsFunctionObject*)constructor.callback)
# 2. Otherwise:
else:
# 1. Set result to the result of creating an element internal given document, HTMLElement, localName, the HTML
# namespace, prefix, "undefined", null, and registry.
let created = dom_create_element_internal(document, dom_create_html_element_interface, local_name, namespace_html, prefix, DomCustomElementState.undefined, none, registry)
result = created
# 2. Enqueue a custom element upgrade reaction given result and definition.
dom_element_enqueue_a_custom_element_upgrade_reaction(created, found)
# 6. Otherwise:
else:
# 1. Let interface be the element interface for localName and namespace.
# 2. Set result to the result of creating an element internal given document, interface, localName, namespace,
# prefix, "uncustomized", is, and registry.
if dom_node_optional_fly_string_is(namespace_, namespace_html):
let created = dom_create_element_internal(document, dom_create_html_element, local_name, namespace_, prefix, DomCustomElementState.uncustomized, is_value, registry)
result = created
# 3. If namespace is the HTML namespace, and either localName is a valid custom element name or is is
# non-null, then set result’s custom element state to "undefined".
if html_is_valid_custom_element_name(ak.fly_string_to_string_const(&local_name)) or is_value != none:
dom_element_set_custom_element_state(created, DomCustomElementState.undefined)
elif dom_node_optional_fly_string_is(namespace_, namespace_svg):
result = dom_create_element_internal(document, dom_create_svg_element_interface, local_name, namespace_, prefix, DomCustomElementState.uncustomized, is_value, registry)
elif dom_node_optional_fly_string_is(namespace_, namespace_math_ml):
result = dom_create_element_internal(document, dom_create_mathml_element_interface, local_name, namespace_, prefix, DomCustomElementState.uncustomized, is_value, registry)
else:
# https://dom.spec.whatwg.org/#concept-element-interface
# The element interface for any name and namespace is Element, unless stated otherwise.
result = dom_create_element_internal(document, dom_create_dom_element_interface, local_name, namespace_, prefix, DomCustomElementState.uncustomized, is_value, registry)
# 7. Return result.
return result else trap("create an element produces an element")