Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

7.1 KB
Raw Blame
use super::super::*;
use crate::native_bridge::document::CreateElementOptions;
use crate::webidl;

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "Document.createTextNode")]
struct DetachedDocumentCreateTextNodeArgs {
    #[webidl(index = 1, required)]
    data: String,
}

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "Document.createComment")]
struct DetachedDocumentCreateCommentArgs {
    #[webidl(index = 1, required)]
    data: String,
}

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "Document.createProcessingInstruction")]
struct DetachedDocumentCreateProcessingInstructionArgs {
    #[webidl(index = 1, required)]
    target: String,
    #[webidl(index = 2, required)]
    data: String,
}

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "Document.createCDATASection")]
struct DocumentCreateCdataSectionArgs {
    #[webidl(required)]
    data: String,
}

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "Document.createCDATASection")]
struct DetachedDocumentCreateCdataSectionArgs {
    #[webidl(index = 1, required)]
    data: String,
}

pub(in crate::native_bridge) fn bridge_detached_create_text_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Ok(document) = v8::Local::<v8::Object>::try_from(args.get(0)) else {
        rv.set_null();
        return;
    };
    let Some(parsed) = webidl::parse_args::<DetachedDocumentCreateTextNodeArgs>(scope, &args)
    else {
        return;
    };
    match build_detached_text_object(scope, document, &parsed.data) {
        Some(node) => rv.set(node.into()),
        None => rv.set_null(),
    }
}

pub(in crate::native_bridge) fn bridge_detached_create_comment_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Ok(document) = v8::Local::<v8::Object>::try_from(args.get(0)) else {
        rv.set_null();
        return;
    };
    let Some(parsed) = webidl::parse_args::<DetachedDocumentCreateCommentArgs>(scope, &args) else {
        return;
    };
    match build_detached_comment_object(scope, document, &parsed.data) {
        Some(node) => rv.set(node.into()),
        None => rv.set_null(),
    }
}

pub(in crate::native_bridge) fn bridge_detached_create_document_fragment_callback<'a>(
    scope: &mut v8::PinScope<'a, '_>,
    args: v8::FunctionCallbackArguments<'a>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Ok(document) = v8::Local::<v8::Object>::try_from(args.get(0)) else {
        rv.set_null();
        return;
    };
    match build_detached_document_fragment_object(scope, document) {
        Some(node) => rv.set(node.into()),
        None => rv.set_null(),
    }
}

pub(in crate::native_bridge) fn bridge_detached_create_processing_instruction_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Ok(document) = v8::Local::<v8::Object>::try_from(args.get(0)) else {
        rv.set_null();
        return;
    };
    let Some(parsed) =
        webidl::parse_args::<DetachedDocumentCreateProcessingInstructionArgs>(scope, &args)
    else {
        return;
    };
    if !is_valid_pi_target(&parsed.target) {
        throw_dom_exception(
            scope,
            "InvalidCharacterError",
            5,
            "String contains an invalid character",
        );
        return;
    }
    if parsed.data.contains("?>") {
        throw_dom_exception(
            scope,
            "InvalidCharacterError",
            5,
            "String contains an invalid character",
        );
        return;
    }
    match build_detached_processing_instruction_object(
        scope,
        document,
        &parsed.target,
        &parsed.data,
    ) {
        Some(node) => rv.set(node.into()),
        None => rv.set_null(),
    }
}

pub(in crate::native_bridge) fn bridge_detached_create_cdata_section_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Ok(document) = v8::Local::<v8::Object>::try_from(args.get(0)) else {
        rv.set_null();
        return;
    };
    let Some(parsed) = webidl::parse_args::<DetachedDocumentCreateCdataSectionArgs>(scope, &args)
    else {
        return;
    };
    if parsed.data.contains("]]>") {
        throw_dom_exception(
            scope,
            "InvalidCharacterError",
            5,
            "String contains an invalid character",
        );
        return;
    }
    match build_detached_cdata_section_object(scope, document, &parsed.data) {
        Some(node) => rv.set(node.into()),
        None => rv.set_null(),
    }
}

pub(in crate::native_bridge) fn bridge_create_cdata_section_not_supported_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    _rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(parsed) = webidl::parse_args::<DocumentCreateCdataSectionArgs>(scope, &args) else {
        return;
    };
    let _ = parsed.data;
    throw_dom_exception(
        scope,
        "NotSupportedError",
        9,
        "This operation is not supported for HTML documents",
    );
}

pub(in crate::native_bridge::document) fn create_detached_element_for_document<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    document: v8::Local<'s, v8::Object>,
    qualified_name: &str,
    namespace_uri: Option<String>,
    document_kind: &str,
    split_qualified_name: bool,
    options: CreateElementOptions,
) -> Option<v8::Local<'s, v8::Object>> {
    let mut namespace_uri = normalize_namespace(namespace_uri);
    let validation_error = if split_qualified_name {
        validate_qualified_element_name_and_namespace(namespace_uri.as_deref(), qualified_name)
            .err()
    } else if !validate_element_name(qualified_name) {
        Some((
            "InvalidCharacterError",
            5,
            "String contains an invalid character",
        ))
    } else {
        None
    };
    if let Some((name, code, message)) = validation_error {
        throw_dom_exception(scope, name, code, message);
        return None;
    }
    let (runtime_ptr, document_handle) =
        node_runtime_and_handle_from_object_or_detached(scope, document).ok()?;
    let options = options.flatten(scope, runtime_ptr, document_handle)?;
    // Conversion and DOM exceptions use the binding's realm. Allocation uses
    // the Document's realm so native ownership does not depend on the caller.
    let context = document.get_creation_context(scope)?;
    let scope = &mut v8::ContextScope::new(scope, context);
    if namespace_uri.is_none()
        && !split_qualified_name
        && document_kind == "xml"
        && detached_document_content_type_value(scope, document).as_deref()
            == Some("application/xhtml+xml")
    {
        namespace_uri = Some(XHTML_NS.to_owned());
    }
    build_detached_element_object(
        scope,
        document,
        qualified_name,
        namespace_uri,
        document_kind,
        split_qualified_name,
        options.is_name.as_deref(),
        options.registry_association,
    )
}