SVGScriptElement.cpp 2.9 KB

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