123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <AK/StringBuilder.h>
- #include <LibJS/Interpreter.h>
- #include <LibJS/Parser.h>
- #include <LibWeb/DOM/Document.h>
- #include <LibWeb/DOM/HTMLScriptElement.h>
- #include <LibWeb/DOM/Text.h>
- #include <LibWeb/Loader/ResourceLoader.h>
- namespace Web {
- HTMLScriptElement::HTMLScriptElement(Document& document, const FlyString& tag_name)
- : HTMLElement(document, tag_name)
- {
- }
- HTMLScriptElement::~HTMLScriptElement()
- {
- }
- void HTMLScriptElement::set_parser_document(Badge<HTMLDocumentParser>, Document& document)
- {
- m_parser_document = document.make_weak_ptr();
- }
- void HTMLScriptElement::set_non_blocking(Badge<HTMLDocumentParser>, bool non_blocking)
- {
- 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));
- auto program = parser.parse_program();
- if (parser.has_errors()) {
- parser.print_errors();
- return;
- }
- document().interpreter().run(document().interpreter().global_object(), *program);
- }
- void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
- {
- if (m_already_started)
- return;
- RefPtr<Document> parser_document = m_parser_document.ptr();
- m_parser_document = nullptr;
- if (parser_document && !has_attribute(HTML::AttributeNames::async)) {
- m_non_blocking = true;
- }
- auto source_text = child_text_content();
- if (!has_attribute(HTML::AttributeNames::src) && source_text.is_empty())
- return;
- if (!is_connected())
- return;
- // FIXME: Check the "type" and "language" attributes
- if (parser_document) {
- m_parser_document = parser_document->make_weak_ptr();
- m_non_blocking = false;
- }
- m_already_started = true;
- m_preparation_time_document = document().make_weak_ptr();
- if (parser_document && parser_document.ptr() != m_preparation_time_document.ptr()) {
- return;
- }
- // FIXME: Check if scripting is disabled, if so return
- // FIXME: Check the "nomodule" content attribute
- // FIXME: Check CSP
- // FIXME: Check "event" and "for" attributes
- // FIXME: Check "charset" attribute
- // FIXME: Check CORS
- // FIXME: Module script credentials mode
- // FIXME: Cryptographic nonce
- // FIXME: Check "integrity" attribute
- // FIXME: Check "referrerpolicy" attribute
- m_parser_inserted = !!m_parser_document;
- // FIXME: Check fetch options
- if (has_attribute(HTML::AttributeNames::src)) {
- auto src = attribute(HTML::AttributeNames::src);
- if (src.is_empty()) {
- // FIXME: Fire an "error" event at the element and return
- ASSERT_NOT_REACHED();
- }
- m_from_an_external_file = true;
- auto url = document().complete_url(src);
- if (!url.is_valid()) {
- // FIXME: Fire an "error" event at the element and return
- ASSERT_NOT_REACHED();
- }
- // FIXME: Check classic vs. module script type
- // FIXME: This load should be made asynchronous and the parser should spin an event loop etc.
- ResourceLoader::the().load_sync(url, [this, url](auto& data, auto&) {
- if (data.is_null()) {
- dbg() << "HTMLScriptElement: Failed to load " << url;
- return;
- }
- m_script_source = String::copy(data);
- script_became_ready();
- });
- } else {
- // FIXME: Check classic vs. module script type
- m_script_source = source_text;
- script_became_ready();
- }
- // FIXME: Check classic vs. module
- if (has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
- document().add_script_to_execute_when_parsing_has_finished({}, *this);
- }
- else if (has_attribute(HTML::AttributeNames::src) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
- document().set_pending_parsing_blocking_script({}, this);
- when_the_script_is_ready([this] {
- m_ready_to_be_parser_executed = true;
- });
- }
- else if (has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking) {
- ASSERT_NOT_REACHED();
- }
- else if (has_attribute(HTML::AttributeNames::src)) {
- m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
- }
- else {
- // Immediately execute the script block, even if other scripts are already executing.
- execute_script();
- }
- }
- void HTMLScriptElement::script_became_ready()
- {
- m_script_ready = true;
- if (!m_script_ready_callback)
- return;
- m_script_ready_callback();
- m_script_ready_callback = nullptr;
- }
- void HTMLScriptElement::when_the_script_is_ready(Function<void()> callback)
- {
- if (m_script_ready) {
- callback();
- return;
- }
- m_script_ready_callback = move(callback);
- }
- }
|