SVGScriptElement.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/SVGScriptElementPrototype.h>
  7. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  8. #include <LibWeb/Namespace.h>
  9. #include <LibWeb/SVG/AttributeNames.h>
  10. #include <LibWeb/SVG/SVGScriptElement.h>
  11. namespace Web::SVG {
  12. JS_DEFINE_ALLOCATOR(SVGScriptElement);
  13. SVGScriptElement::SVGScriptElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : SVGElement(document, move(qualified_name))
  15. {
  16. }
  17. void SVGScriptElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGScriptElement);
  21. }
  22. void SVGScriptElement::visit_edges(Cell::Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. SVGURIReferenceMixin::visit_edges(visitor);
  26. visitor.visit(m_script);
  27. }
  28. // https://www.w3.org/TR/SVGMobile12/script.html#ScriptContentProcessing
  29. void SVGScriptElement::process_the_script_element()
  30. {
  31. // 1. If the 'script' element's "already processed" flag is true or if the element is not in the
  32. // document tree, then no action is performed and these steps are ended.
  33. if (m_already_processed || !in_a_document_tree())
  34. return;
  35. auto inline_script = child_text_content();
  36. // FIXME: 2. If the 'script' element references external script content, then the external script content
  37. // using the current value of the 'xlink:href' attribute is fetched. Further processing of the
  38. // 'script' element is dependent on the external script content, and will block here until the
  39. // resource has been fetched or is determined to be an invalid IRI reference.
  40. if (has_attribute(SVG::AttributeNames::href) || has_attribute_ns(Namespace::XLink.to_string(), SVG::AttributeNames::href)) {
  41. dbgln("FIXME: Unsupported external fetch of SVGScriptElement!");
  42. return;
  43. }
  44. // 3. The 'script' element's "already processed" flag is set to true.
  45. m_already_processed = true;
  46. // 4. If the script content is inline, or if it is external and was fetched successfully, then the
  47. // script is executed. Note that at this point, these steps may be re-entrant if the execution
  48. // of the script results in further 'script' elements being inserted into the document.
  49. // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html
  50. // Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for document.
  51. if (!m_document->ready_to_run_scripts())
  52. HTML::main_thread_event_loop().spin_until(JS::create_heap_function(heap(), [&] { return m_document->ready_to_run_scripts(); }));
  53. // FIXME: Support non-inline scripts.
  54. auto& settings_object = document().relevant_settings_object();
  55. auto base_url = document().base_url();
  56. m_script = HTML::ClassicScript::create(m_document->url().to_byte_string(), inline_script, settings_object, base_url, m_source_line_number);
  57. (void)m_script->run();
  58. }
  59. }