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.
This commit is contained in:
Andreas Kling 2020-06-23 16:38:44 +02:00
parent c33d17d363
commit 3a5af6ef61
3 changed files with 3 additions and 65 deletions

View file

@ -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));

View file

@ -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; }

View file

@ -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;