2023-09-23 08:56:49 +12:00
|
|
|
|
/*
|
2024-01-14 16:42:41 +13:00
|
|
|
|
* Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
|
2024-12-04 01:59:59 +04:00
|
|
|
|
* Copyright (c) 2024, Pavel Shliak <shlyakpavel@gmail.com>
|
2023-09-23 08:56:49 +12:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/SVGScriptElementPrototype.h>
|
2024-12-04 01:59:59 +04:00
|
|
|
|
#include <LibWeb/Fetch/Fetching/Fetching.h>
|
|
|
|
|
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
|
2023-09-26 01:12:21 +13:00
|
|
|
|
#include <LibWeb/HTML/Scripting/ClassicScript.h>
|
|
|
|
|
#include <LibWeb/Namespace.h>
|
|
|
|
|
#include <LibWeb/SVG/AttributeNames.h>
|
2023-09-23 08:56:49 +12:00
|
|
|
|
#include <LibWeb/SVG/SVGScriptElement.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(SVGScriptElement);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2023-09-23 08:56:49 +12:00
|
|
|
|
SVGScriptElement::SVGScriptElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
|
|
|
|
: SVGElement(document, move(qualified_name))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SVGScriptElement::initialize(JS::Realm& realm)
|
|
|
|
|
{
|
|
|
|
|
Base::initialize(realm);
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGScriptElement);
|
2023-09-23 08:56:49 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 01:12:21 +13:00
|
|
|
|
void SVGScriptElement::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2024-07-16 13:49:28 +01:00
|
|
|
|
SVGURIReferenceMixin::visit_edges(visitor);
|
2023-09-26 01:12:21 +13:00
|
|
|
|
visitor.visit(m_script);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 12:48:57 +00:00
|
|
|
|
void SVGScriptElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
|
|
|
|
{
|
|
|
|
|
Base::attribute_changed(name, old_value, value, namespace_);
|
|
|
|
|
if (name == SVG::AttributeNames::href) {
|
|
|
|
|
process_the_script_element();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 12:37:38 +00:00
|
|
|
|
void SVGScriptElement::inserted()
|
|
|
|
|
{
|
|
|
|
|
Base::inserted();
|
|
|
|
|
if (m_parser_inserted)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
process_the_script_element();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SVGScriptElement::children_changed(ChildrenChangedMetadata const* metadata)
|
|
|
|
|
{
|
|
|
|
|
Base::children_changed(metadata);
|
|
|
|
|
if (m_parser_inserted)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
process_the_script_element();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 01:12:21 +13:00
|
|
|
|
// https://www.w3.org/TR/SVGMobile12/script.html#ScriptContentProcessing
|
|
|
|
|
void SVGScriptElement::process_the_script_element()
|
|
|
|
|
{
|
|
|
|
|
// 1. If the 'script' element's "already processed" flag is true or if the element is not in the
|
|
|
|
|
// document tree, then no action is performed and these steps are ended.
|
|
|
|
|
if (m_already_processed || !in_a_document_tree())
|
|
|
|
|
return;
|
|
|
|
|
|
2024-12-04 01:59:59 +04:00
|
|
|
|
IGNORE_USE_IN_ESCAPING_LAMBDA String script_content;
|
|
|
|
|
auto script_url = m_document->url();
|
2023-09-26 01:12:21 +13:00
|
|
|
|
|
2024-12-04 01:59:59 +04:00
|
|
|
|
// 2. If the 'script' element references external script content, then the external script content
|
|
|
|
|
// using the current value of the 'xlink:href' attribute is fetched. Further processing of the
|
|
|
|
|
// 'script' element is dependent on the external script content, and will block here until the
|
|
|
|
|
// resource has been fetched or is determined to be an invalid IRI reference.
|
2023-09-26 01:12:21 +13:00
|
|
|
|
if (has_attribute(SVG::AttributeNames::href) || has_attribute_ns(Namespace::XLink.to_string(), SVG::AttributeNames::href)) {
|
2024-12-04 01:59:59 +04:00
|
|
|
|
auto href_value = href()->base_val();
|
|
|
|
|
|
2025-01-23 19:40:57 +13:00
|
|
|
|
auto maybe_script_url = document().parse_url(href_value);
|
|
|
|
|
if (!maybe_script_url.has_value()) {
|
2024-12-04 01:59:59 +04:00
|
|
|
|
dbgln("Invalid script URL: {}", href_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-01-23 19:40:57 +13:00
|
|
|
|
script_url = maybe_script_url.release_value();
|
2024-12-04 01:59:59 +04:00
|
|
|
|
|
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
auto request = Fetch::Infrastructure::Request::create(vm);
|
|
|
|
|
request->set_url(script_url);
|
|
|
|
|
request->set_destination(Fetch::Infrastructure::Request::Destination::Script);
|
|
|
|
|
// FIXME: Use CORS state specified by the ‘crossorigin’ attribute.
|
|
|
|
|
request->set_mode(Fetch::Infrastructure::Request::Mode::NoCORS);
|
|
|
|
|
request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::SameOrigin);
|
|
|
|
|
request->set_client(&document().relevant_settings_object());
|
|
|
|
|
|
|
|
|
|
IGNORE_USE_IN_ESCAPING_LAMBDA bool fetch_done = false;
|
|
|
|
|
|
|
|
|
|
Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
|
|
|
|
|
fetch_algorithms_input.process_response = [this, &script_content, &fetch_done](GC::Ref<Fetch::Infrastructure::Response> response) {
|
|
|
|
|
if (response->is_network_error()) {
|
|
|
|
|
dbgln("Failed to fetch SVG external script.");
|
|
|
|
|
fetch_done = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto& realm = this->realm();
|
|
|
|
|
auto& global = document().realm().global_object();
|
|
|
|
|
|
|
|
|
|
auto on_data_read = GC::create_function(realm.heap(), [&script_content, &fetch_done](ByteBuffer data) {
|
|
|
|
|
auto content_or_error = String::from_utf8(data);
|
|
|
|
|
if (content_or_error.is_error()) {
|
|
|
|
|
dbgln("Failed to decode script content as UTF-8");
|
|
|
|
|
} else {
|
|
|
|
|
script_content = content_or_error.release_value();
|
|
|
|
|
}
|
|
|
|
|
fetch_done = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
auto on_error = GC::create_function(realm.heap(), [&fetch_done](JS::Value) {
|
|
|
|
|
dbgln("Error occurred while reading script data.");
|
|
|
|
|
fetch_done = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
VERIFY(response->body());
|
|
|
|
|
response->body()->fully_read(realm, on_data_read, on_error, GC::Ref { global });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto fetch_promise = Fetch::Fetching::fetch(realm(), request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input)));
|
|
|
|
|
|
|
|
|
|
// Block until the resource has been fetched or determined invalid
|
|
|
|
|
HTML::main_thread_event_loop().spin_until(GC::create_function(heap(), [&] { return fetch_done; }));
|
|
|
|
|
|
|
|
|
|
if (script_content.is_empty()) {
|
|
|
|
|
// Failed to fetch or decode
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Inline script content
|
|
|
|
|
script_content = child_text_content();
|
2025-02-26 12:37:38 +00:00
|
|
|
|
if (script_content.is_empty())
|
|
|
|
|
return;
|
2023-09-26 01:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. The 'script' element's "already processed" flag is set to true.
|
|
|
|
|
m_already_processed = true;
|
|
|
|
|
|
|
|
|
|
// 4. If the script content is inline, or if it is external and was fetched successfully, then the
|
|
|
|
|
// script is executed. Note that at this point, these steps may be re-entrant if the execution
|
|
|
|
|
// of the script results in further 'script' elements being inserted into the document.
|
2024-01-14 16:42:41 +13:00
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html
|
|
|
|
|
// Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for document.
|
|
|
|
|
if (!m_document->ready_to_run_scripts())
|
2024-11-15 04:01:23 +13:00
|
|
|
|
HTML::main_thread_event_loop().spin_until(GC::create_function(heap(), [&] { return m_document->ready_to_run_scripts(); }));
|
2024-01-14 16:42:41 +13:00
|
|
|
|
|
2024-12-04 01:59:59 +04:00
|
|
|
|
m_script = HTML::ClassicScript::create(script_url.basename(), script_content, realm(), m_document->base_url(), m_source_line_number);
|
|
|
|
|
|
|
|
|
|
// FIXME: Note that a load event is dispatched on a 'script' element once it has been processed,
|
|
|
|
|
// unless it referenced external script content with an invalid IRI reference and 'externalResourcesRequired' was set to 'true'.
|
2023-09-26 01:12:21 +13:00
|
|
|
|
|
|
|
|
|
(void)m_script->run();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-23 08:56:49 +12:00
|
|
|
|
}
|