1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:38:11 +00:00

LibWeb: Add support for inline SVG element scripts

This commit is contained in:
Shannon Booth 2023-09-26 01:12:21 +13:00 committed by Sam Atkins
parent c4efc0a5aa
commit 4821d284c6
6 changed files with 74 additions and 10 deletions

View file

@ -1 +1 @@
Name = SVGScriptElement Hello from SVGScriptElement!

View file

@ -1,7 +1,8 @@
<script src="../include.js"></script> <script src="../include.js"></script>
<svg xmlns="http://www.w3.org/2000/svg"><script id="svg-script-element"></script></svg> <svg xmlns="http://www.w3.org/2000/svg">
<script> <script id="svg-script-element">
test(() => { test(() => {
println(`Name = ${document.getElementById('svg-script-element').constructor.name}`); println(`Hello from ${document.getElementById('svg-script-element').constructor.name}!`);
}); });
</script> </script>
</svg>

View file

@ -618,6 +618,7 @@ class SVGPathElement;
class SVGPolygonElement; class SVGPolygonElement;
class SVGPolylineElement; class SVGPolylineElement;
class SVGRectElement; class SVGRectElement;
class SVGScriptElement;
class SVGSVGElement; class SVGSVGElement;
class SVGTitleElement; class SVGTitleElement;
} }

View file

@ -39,6 +39,7 @@
#include <LibWeb/Infra/Strings.h> #include <LibWeb/Infra/Strings.h>
#include <LibWeb/MathML/TagNames.h> #include <LibWeb/MathML/TagNames.h>
#include <LibWeb/Namespace.h> #include <LibWeb/Namespace.h>
#include <LibWeb/SVG/SVGScriptElement.h>
#include <LibWeb/SVG/TagNames.h> #include <LibWeb/SVG/TagNames.h>
namespace Web::HTML { namespace Web::HTML {
@ -3525,6 +3526,9 @@ void HTMLParser::process_using_the_rules_for_foreign_content(HTMLToken& token)
// -> If the token's tag name is "script", and the new current node is in the SVG namespace // -> If the token's tag name is "script", and the new current node is in the SVG namespace
if (token.tag_name() == SVG::TagNames::script && current_node().namespace_uri() == Namespace::SVG) { if (token.tag_name() == SVG::TagNames::script && current_node().namespace_uri() == Namespace::SVG) {
auto& script_element = verify_cast<SVG::SVGScriptElement>(current_node());
script_element.set_source_line_number({}, token.start_position().line + 1); // FIXME: This +1 is incorrect for script tags whose script does not start on a new line
// Acknowledge the token's self-closing flag, and then act as described in the steps for a "script" end tag below. // Acknowledge the token's self-closing flag, and then act as described in the steps for a "script" end tag below.
token.acknowledge_self_closing_flag_if_set(); token.acknowledge_self_closing_flag_if_set();
goto ScriptEndTag; goto ScriptEndTag;
@ -3544,7 +3548,7 @@ void HTMLParser::process_using_the_rules_for_foreign_content(HTMLToken& token)
if (token.is_end_tag() && current_node().namespace_uri() == Namespace::SVG && current_node().tag_name() == SVG::TagNames::script) { if (token.is_end_tag() && current_node().namespace_uri() == Namespace::SVG && current_node().tag_name() == SVG::TagNames::script) {
ScriptEndTag: ScriptEndTag:
// Pop the current node off the stack of open elements. // Pop the current node off the stack of open elements.
(void)m_stack_of_open_elements.pop(); auto& script_element = verify_cast<SVG::SVGScriptElement>(*m_stack_of_open_elements.pop());
// Let the old insertion point have the same value as the current insertion point. // Let the old insertion point have the same value as the current insertion point.
m_tokenizer.store_insertion_point(); m_tokenizer.store_insertion_point();
// Let the insertion point be just before the next input character. // Let the insertion point be just before the next input character.
@ -3554,8 +3558,12 @@ void HTMLParser::process_using_the_rules_for_foreign_content(HTMLToken& token)
// Set the parser pause flag to true. // Set the parser pause flag to true.
m_parser_pause_flag = true; m_parser_pause_flag = true;
// FIXME: If the active speculative HTML parser is null and the user agent supports SVG, then Process the SVG script element according to the SVG rules. [SVG] // Non-standard: Make sure the <script> element has up-to-date text content before processing the script.
dbgln("FIXME: Missing 'Process the SVG script element according to the SVG rules."); flush_character_insertions();
// If the active speculative HTML parser is null and the user agent supports SVG, then Process the SVG script element according to the SVG rules. [SVG]
// FIXME: If the active speculative HTML parser is null
script_element.process_the_script_element();
// Decrement the parser's script nesting level by one. // Decrement the parser's script nesting level by one.
decrement_script_nesting_level(); decrement_script_nesting_level();

View file

@ -4,6 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/SVGScriptElement.h> #include <LibWeb/SVG/SVGScriptElement.h>
namespace Web::SVG { namespace Web::SVG {
@ -19,4 +22,43 @@ void SVGScriptElement::initialize(JS::Realm& realm)
set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGScriptElementPrototype>(realm, "SVGScriptElement")); set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGScriptElementPrototype>(realm, "SVGScriptElement"));
} }
void SVGScriptElement::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_script);
}
// 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;
auto inline_script = child_text_content();
// FIXME: 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.
if (has_attribute(SVG::AttributeNames::href) || has_attribute_ns(Namespace::XLink.to_string(), SVG::AttributeNames::href)) {
dbgln("FIXME: Unsupported external fetch of SVGScriptElement!");
return;
}
// 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.
// FIXME: Support non-inline scripts.
auto& settings_object = document().relevant_settings_object();
auto base_url = document().base_url();
m_script = HTML::ClassicScript::create(m_document->url().to_deprecated_string(), inline_script, settings_object, base_url, m_source_line_number);
(void)m_script->run();
}
} }

View file

@ -15,6 +15,10 @@ class SVGScriptElement : public SVGElement {
WEB_PLATFORM_OBJECT(SVGScriptElement, SVGElement); WEB_PLATFORM_OBJECT(SVGScriptElement, SVGElement);
public: public:
void process_the_script_element();
void set_source_line_number(Badge<HTML::HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; }
protected: protected:
SVGScriptElement(DOM::Document&, DOM::QualifiedName); SVGScriptElement(DOM::Document&, DOM::QualifiedName);
@ -22,6 +26,14 @@ protected:
private: private:
virtual bool is_svg_script_element() const final { return true; } virtual bool is_svg_script_element() const final { return true; }
virtual void visit_edges(Cell::Visitor&) override;
bool m_already_processed { false };
JS::GCPtr<HTML::ClassicScript> m_script;
size_t m_source_line_number { 1 };
}; };
} }