SVGScriptElement.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2023, 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. SVGScriptElement::SVGScriptElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : SVGElement(document, move(qualified_name))
  13. {
  14. }
  15. void SVGScriptElement::initialize(JS::Realm& realm)
  16. {
  17. Base::initialize(realm);
  18. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGScriptElementPrototype>(realm, "SVGScriptElement"));
  19. }
  20. void SVGScriptElement::visit_edges(Cell::Visitor& visitor)
  21. {
  22. Base::visit_edges(visitor);
  23. visitor.visit(m_script);
  24. }
  25. // https://www.w3.org/TR/SVGMobile12/script.html#ScriptContentProcessing
  26. void SVGScriptElement::process_the_script_element()
  27. {
  28. // 1. If the 'script' element's "already processed" flag is true or if the element is not in the
  29. // document tree, then no action is performed and these steps are ended.
  30. if (m_already_processed || !in_a_document_tree())
  31. return;
  32. auto inline_script = child_text_content();
  33. // FIXME: 2. If the 'script' element references external script content, then the external script content
  34. // using the current value of the 'xlink:href' attribute is fetched. Further processing of the
  35. // 'script' element is dependent on the external script content, and will block here until the
  36. // resource has been fetched or is determined to be an invalid IRI reference.
  37. if (has_attribute(SVG::AttributeNames::href) || has_attribute_ns(Namespace::XLink.to_string(), SVG::AttributeNames::href)) {
  38. dbgln("FIXME: Unsupported external fetch of SVGScriptElement!");
  39. return;
  40. }
  41. // 3. The 'script' element's "already processed" flag is set to true.
  42. m_already_processed = true;
  43. // 4. If the script content is inline, or if it is external and was fetched successfully, then the
  44. // script is executed. Note that at this point, these steps may be re-entrant if the execution
  45. // of the script results in further 'script' elements being inserted into the document.
  46. // FIXME: Support non-inline scripts.
  47. auto& settings_object = document().relevant_settings_object();
  48. auto base_url = document().base_url();
  49. m_script = HTML::ClassicScript::create(m_document->url().to_deprecated_string(), inline_script, settings_object, base_url, m_source_line_number);
  50. (void)m_script->run();
  51. }
  52. }