summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-06-23 16:38:44 +0200
committerAndreas Kling <kling@serenityos.org>2020-06-23 16:45:01 +0200
commit3a5af6ef6181c73fec7bd48c9f967b383de9d524 (patch)
treec34bace3900eb680a238d3323b1434cf1e5c8f9a
parentc33d17d363cc88bc6193f88c1b94eadc043fcaac (diff)
downloadserenity-3a5af6ef6181c73fec7bd48c9f967b383de9d524.zip
LibWeb: Remove hacky old ways of running <script> element contents
Now that we're using the new HTML parser, we don't have to do the weird "run the script when inserted into the document, uhh, or when the text content of the script element changes" dance. Instead, we just follow the spec, and scripts run the way they should.
-rw-r--r--Libraries/LibWeb/DOM/HTMLScriptElement.cpp62
-rw-r--r--Libraries/LibWeb/DOM/HTMLScriptElement.h3
-rw-r--r--Libraries/LibWeb/Parser/HTMLDocumentParser.cpp3
3 files changed, 3 insertions, 65 deletions
diff --git a/Libraries/LibWeb/DOM/HTMLScriptElement.cpp b/Libraries/LibWeb/DOM/HTMLScriptElement.cpp
index 1aa2968fe6..e7552211b3 100644
--- a/Libraries/LibWeb/DOM/HTMLScriptElement.cpp
+++ b/Libraries/LibWeb/DOM/HTMLScriptElement.cpp
@@ -53,68 +53,6 @@ void HTMLScriptElement::set_non_blocking(Badge<HTMLDocumentParser>, bool non_blo
m_non_blocking = non_blocking;
}
-void HTMLScriptElement::children_changed()
-{
- HTMLElement::children_changed();
-
- if (has_attribute(HTML::AttributeNames::src))
- return;
-
- StringBuilder builder;
- for_each_child([&](auto& child) {
- if (is<Text>(child))
- builder.append(to<Text>(child).text_content());
- });
- auto source = builder.to_string();
- if (source.is_empty())
- return;
-
- auto parser = JS::Parser(JS::Lexer(source));
- auto program = parser.parse_program();
- if (parser.has_errors()) {
- parser.print_errors();
- return;
- }
- document().interpreter().run(document().interpreter().global_object(), *program);
-}
-
-void HTMLScriptElement::inserted_into(Node& new_parent)
-{
- HTMLElement::inserted_into(new_parent);
-
- auto src = attribute(HTML::AttributeNames::src);
- if (src.is_null())
- return;
-
- URL src_url = document().complete_url(src);
- if (src_url.protocol() == "file" && document().url().protocol() != src_url.protocol()) {
- dbg() << "HTMLScriptElement: Forbidden to load " << src_url << " from " << document().url();
- return;
- }
-
- String source;
- ResourceLoader::the().load_sync(src_url, [&](auto& data, auto&) {
- if (data.is_null()) {
- dbg() << "HTMLScriptElement: Failed to load " << src;
- return;
- }
- source = String::copy(data);
- });
- if (source.is_empty()) {
- dbg() << "HTMLScriptElement: No source to parse :(";
- return;
- }
-
- dbg() << "Parsing and running script from " << src_url;
- auto parser = JS::Parser(JS::Lexer(source));
- auto program = parser.parse_program();
- if (parser.has_errors()) {
- parser.print_errors();
- return;
- }
- document().interpreter().run(document().interpreter().global_object(), *program);
-}
-
void HTMLScriptElement::execute_script()
{
auto parser = JS::Parser(JS::Lexer(m_script_source));
diff --git a/Libraries/LibWeb/DOM/HTMLScriptElement.h b/Libraries/LibWeb/DOM/HTMLScriptElement.h
index 4c11556f38..9d89ecd812 100644
--- a/Libraries/LibWeb/DOM/HTMLScriptElement.h
+++ b/Libraries/LibWeb/DOM/HTMLScriptElement.h
@@ -36,9 +36,6 @@ public:
HTMLScriptElement(Document&, const FlyString& tag_name);
virtual ~HTMLScriptElement() override;
- virtual void inserted_into(Node&) override;
- virtual void children_changed() override;
-
bool is_non_blocking() const { return m_non_blocking; }
bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; }
bool failed_to_load() const { return m_failed_to_load; }
diff --git a/Libraries/LibWeb/Parser/HTMLDocumentParser.cpp b/Libraries/LibWeb/Parser/HTMLDocumentParser.cpp
index f8fed2ca5c..1c316e0d41 100644
--- a/Libraries/LibWeb/Parser/HTMLDocumentParser.cpp
+++ b/Libraries/LibWeb/Parser/HTMLDocumentParser.cpp
@@ -1637,6 +1637,9 @@ void HTMLDocumentParser::handle_text(HTMLToken& token)
return;
}
if (token.is_end_tag() && token.tag_name() == HTML::TagNames::script) {
+ // Make sure the <script> element has up-to-date text content before preparing the script.
+ flush_character_insertions();
+
NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
m_stack_of_open_elements.pop();
m_insertion_mode = m_original_insertion_mode;