Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / dom_implementation.lucb

#==============================================================================================
#
#   dom_implementation - DOM::DOMImplementation: createDocument, createHTMLDocument, createDocumentType
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/DOMImplementation.cpp (lines 1-155) and the inline functions of
#       DOMImplementation.h.
#
#==============================================================================================

## Ported from DOMImplementation.cpp:24 Web::DOM::DOMImplementation::create.
pub func dom_implementation_create(document: DomDocument*) -> DomImplementation*:
    let realm = bind_platform_object_realm(document.bind_platform_object())
    return realm_create_dom_implementation(realm, document)

## Ported from DOMImplementation.cpp:30 Web::DOM::DOMImplementation::DOMImplementation.
pub func dom_implementation_construct(this: DomImplementation*, document: DomDocument*):
    bind_platform_object_construct_realm(this.bind_platform_object(), bind_platform_object_realm(document.bind_platform_object()))
    this.m_document = document

## Ported from DOMImplementation.cpp:38 Web::DOM::DOMImplementation::initialize (override).
pub func dom_implementation_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.dom_implementation)
    # Base::initialize(realm): PlatformObject inherits JS::Object's.
    js_cell_initialize_impl(base, realm)

## Ported from DOMImplementation.cpp:44 Web::DOM::DOMImplementation::visit_edges (override).
pub func dom_implementation_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (DomImplementation*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_document)

## Ported from DOMImplementation.cpp:51 Web::DOM::DOMImplementation::create_document.
# https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
pub func dom_implementation_create_document(this: DomImplementation*, namespace_: ak.FlyString?, qualified_name: ak.String, doctype: DomDocumentType*?) -> DomXmlDocument*!:
    # 1. Let document be a new XMLDocument
    let xml_document = dom_xml_document_create(bind_platform_object_realm(this.bind_platform_object()), web_url.about_blank())
    let document = xml_document.dom_document()
    dom_document_set_ready_for_post_load_tasks(document, true)

    # 2. Let element be null.
    var element: DomElement*? = none

    # 3. If qualifiedName is not the empty string, then set element to the result of running the internal createElementNS steps, given document, namespace, qualifiedName, and an empty dictionary.
    if not ak.string_is_empty(&qualified_name):
        element = try dom_document_create_element_ns(document, namespace_, qualified_name, .dom_element_creation_options(DomElementCreationOptions()))

    # 4. If doctype is non-null, append doctype to document.
    if let doctype_node = doctype:
        _ = try dom_node_append_child(document.dom_node(), doctype_node.dom_node())

    # 5. If element is non-null, append element to document.
    if let element_node = element:
        _ = try dom_node_append_child(document.dom_node(), element_node.dom_node())

    # 6. document’s origin is this’s associated document’s origin.
    dom_document_set_origin(document, dom_document_origin(dom_implementation_document(this)))

    # 7. document’s content type is determined by namespace:
    if dom_node_optional_fly_string_is(namespace_, namespace_html):
        # -> HTML namespace
        dom_document_set_content_type(document, ak.string_from_str("application/xhtml+xml"))
    elif dom_node_optional_fly_string_is(namespace_, namespace_svg):
        # -> SVG namespace
        dom_document_set_content_type(document, ak.string_from_str("image/svg+xml"))
    else:
        # -> Any other namespace
        dom_document_set_content_type(document, ak.string_from_str("application/xml"))

    # 8. Return document.
    return xml_document

## Ported from DOMImplementation.cpp:93 Web::DOM::DOMImplementation::create_html_document.
# https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
pub func dom_implementation_create_html_document(this: DomImplementation*, title: ak.Utf16String?) -> DomDocument*:
    let realm = bind_platform_object_realm(this.bind_platform_object())

    # 1. Let doc be a new document that is an HTML document.
    let html_document = html_document_create(realm, web_url.about_blank()).dom_document()

    # 2. Set doc’s content type to "text/html".
    dom_document_set_content_type(html_document, ak.string_from_str("text/html"))
    dom_document_set_document_type(html_document, DocumentDomDocumentType.html)
    dom_document_set_ready_for_post_load_tasks(html_document, true)

    # 3. Append a new doctype, with "html" as its name and with its node document set to doc, to doc.
    let doctype = realm_create_dom_document_type(realm, html_document)
    dom_document_type_set_name(doctype, ak.string_from_str("html"))
    _ = dom_node_append_child(html_document.dom_node(), doctype.dom_node()) else trap("MUST: append_child")

    # 4. Append the result of creating an element given doc, "html", and the HTML namespace, to doc.
    let html_element = dom_create_element(html_document, html_tag_names_html, namespace_html) else trap("FIXME: should propagate errors")
    _ = dom_node_append_child(html_document.dom_node(), html_element.dom_node()) else trap("MUST: append_child")

    # 5. Append the result of creating an element given doc, "head", and the HTML namespace, to the html element created earlier.
    let head_element = dom_create_element(html_document, html_tag_names_head, namespace_html) else trap("FIXME: should propagate errors")
    _ = dom_node_append_child(html_element.dom_node(), head_element.dom_node()) else trap("MUST: append_child")

    # 6. If title is given:
    if let title_value = title:
        # 1. Append the result of creating an element given doc, "title", and the HTML namespace, to the head element created earlier.
        let title_element = dom_create_element(html_document, html_tag_names_title, namespace_html) else trap("FIXME: should propagate errors")
        _ = dom_node_append_child(head_element.dom_node(), title_element.dom_node()) else trap("MUST: append_child")

        # 2. Append a new Text node, with its data set to title (which could be the empty string) and its node document set to doc, to the title element created earlier.
        let text_node = realm_create_dom_text_utf16_string(realm, html_document, title_value)
        _ = dom_node_append_child(title_element.dom_node(), text_node.dom_node()) else trap("MUST: append_child")

    # 7. Append the result of creating an element given doc, "body", and the HTML namespace, to the html element created earlier.
    let body_element = dom_create_element(html_document, html_tag_names_body, namespace_html) else trap("FIXME: should propagate errors")
    _ = dom_node_append_child(html_element.dom_node(), body_element.dom_node()) else trap("MUST: append_child")

    # 8. doc’s origin is this’s associated document’s origin.
    dom_document_set_origin(html_document, dom_document_origin(dom_implementation_document(this)))

    # 9. Return doc.
    return html_document

## Ported from DOMImplementation.cpp:140 Web::DOM::DOMImplementation::create_document_type.
# https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
pub func dom_implementation_create_document_type(this: DomImplementation*, name: ak.String, public_id: ak.String, system_id: ak.String) -> DomDocumentType*!:
    # 1. If name is not a valid doctype name, then throw an "InvalidCharacterError" DOMException.
    if not dom_is_valid_doctype_name(name):
        try idl_throw(.idl_dom_exception(idl_invalid_character_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Invalid doctype name"))))

    # 2. Return a new doctype, with name as its name, publicId as its public ID, and systemId as its system ID, and with its node document set to the associated document of this.
    let document_type = dom_document_type_create(dom_implementation_document(this))
    dom_document_type_set_name(document_type, name)
    dom_document_type_set_public_id(document_type, public_id)
    dom_document_type_set_system_id(document_type, system_id)
    return document_type

# mark: DOMImplementation.h =====================================================================

## Ported from DOMImplementation.h:28 Web::DOM::DOMImplementation::has_feature.
# https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature
pub func dom_implementation_has_feature(this: DomImplementation*) -> bool:
    # The hasFeature() method steps are to return true.
    return true

## Ported from DOMImplementation.h:40 Web::DOM::DOMImplementation::document.
pub func dom_implementation_document(this: DomImplementation*) -> DomDocument*:
    return this.m_document

## Ported from DOMImplementation.h:41 Web::DOM::DOMImplementation::document.
pub func dom_implementation_document_const(this: DomImplementation*) -> DomDocument*:
    return this.m_document