diff options
-rwxr-xr-x | Applications/Browser/Makefile | 2 | ||||
-rw-r--r-- | Applications/Help/Makefile | 2 | ||||
-rw-r--r-- | Applications/IRCClient/Makefile | 2 | ||||
-rw-r--r-- | Base/home/anon/www/alert.html | 10 | ||||
-rw-r--r-- | Base/home/anon/www/welcome.html | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Document.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Document.h | 5 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/ElementFactory.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLScriptElement.cpp | 60 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLScriptElement.h | 41 | ||||
-rw-r--r-- | Libraries/LibWeb/Makefile | 1 |
11 files changed, 142 insertions, 3 deletions
diff --git a/Applications/Browser/Makefile b/Applications/Browser/Makefile index 4a57f47e68..962266dacc 100755 --- a/Applications/Browser/Makefile +++ b/Applications/Browser/Makefile @@ -4,7 +4,7 @@ OBJS = \ PROGRAM = Browser -LIB_DEPS = GUI Web Gfx IPC Protocol Core +LIB_DEPS = Web JS GUI Gfx IPC Protocol Core main.cpp: ../../Libraries/LibWeb/CSS/PropertyID.h ../../Libraries/LibWeb/CSS/PropertyID.h: diff --git a/Applications/Help/Makefile b/Applications/Help/Makefile index 8fac56daac..c1869dbbb0 100644 --- a/Applications/Help/Makefile +++ b/Applications/Help/Makefile @@ -7,6 +7,6 @@ OBJS = \ PROGRAM = Help -LIB_DEPS = GUI Web Gfx Markdown IPC Protocol Thread Pthread Core +LIB_DEPS = GUI Web JS Gfx Markdown IPC Protocol Thread Pthread Core include ../../Makefile.common diff --git a/Applications/IRCClient/Makefile b/Applications/IRCClient/Makefile index 1bc0b646fe..fc4a987ea6 100644 --- a/Applications/IRCClient/Makefile +++ b/Applications/IRCClient/Makefile @@ -11,6 +11,6 @@ OBJS = \ PROGRAM = IRCClient -LIB_DEPS = GUI Web Gfx Protocol IPC Thread Pthread Core +LIB_DEPS = Web JS GUI Gfx Protocol IPC Thread Pthread Core include ../../Makefile.common diff --git a/Base/home/anon/www/alert.html b/Base/home/anon/www/alert.html new file mode 100644 index 0000000000..fd96efbed4 --- /dev/null +++ b/Base/home/anon/www/alert.html @@ -0,0 +1,10 @@ +<!DOCTYPE html> +<html> + <head> + <script> + alert("Hello friends!"); + </script> + </head> + <body> + </body> +</html> diff --git a/Base/home/anon/www/welcome.html b/Base/home/anon/www/welcome.html index 728e3ba17b..6f7f3a3a78 100644 --- a/Base/home/anon/www/welcome.html +++ b/Base/home/anon/www/welcome.html @@ -23,6 +23,7 @@ h1 { <p>This is a very simple browser built on the LibWeb engine.</p> <p>Some small test pages:</p> <ul> + <li><a href="alert.html">alert() test</a></li> <li><a href="small.html">small</a></li> <li><a href="first-child.html">:first-child</a></li> <li><a href="last-child.html">:last-child</a></li> diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 6b2f11e81a..8e25cbf64d 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -28,6 +28,9 @@ #include <AK/StringBuilder.h> #include <LibCore/Timer.h> #include <LibGUI/Application.h> +#include <LibGUI/MessageBox.h> +#include <LibJS/GlobalObject.h> +#include <LibJS/Interpreter.h> #include <LibWeb/CSS/StyleResolver.h> #include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/DocumentType.h> @@ -332,4 +335,19 @@ Color Document::visited_link_color() const return frame()->html_view()->palette().visited_link(); } +JS::Interpreter& Document::interpreter() +{ + if (!m_interpreter) { + m_interpreter = make<JS::Interpreter>(); + + m_interpreter->global_object().put_native_function("alert", [](JS::Interpreter&, Vector<JS::Value> arguments) -> JS::Value { + if (arguments.size() < 1) + return JS::js_undefined(); + GUI::MessageBox::show(arguments[0].to_string(), "Alert", GUI::MessageBox::Type::Information); + return JS::js_undefined(); + }); + } + return *m_interpreter; +} + } diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h index aa7d3e2014..bdfe6e6cb9 100644 --- a/Libraries/LibWeb/DOM/Document.h +++ b/Libraries/LibWeb/DOM/Document.h @@ -33,6 +33,7 @@ #include <AK/URL.h> #include <AK/WeakPtr.h> #include <LibCore/Forward.h> +#include <LibJS/Forward.h> #include <LibWeb/CSS/StyleResolver.h> #include <LibWeb/CSS/StyleSheet.h> #include <LibWeb/DOM/ParentNode.h> @@ -120,6 +121,8 @@ public: const String& source() const { return m_source; } void set_source(const String& source) { m_source = source; } + JS::Interpreter& interpreter(); + private: virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override; @@ -139,6 +142,8 @@ private: RefPtr<Core::Timer> m_style_update_timer; String m_source; + + OwnPtr<JS::Interpreter> m_interpreter; }; template<> diff --git a/Libraries/LibWeb/DOM/ElementFactory.cpp b/Libraries/LibWeb/DOM/ElementFactory.cpp index b2c362fa88..3eef44c953 100644 --- a/Libraries/LibWeb/DOM/ElementFactory.cpp +++ b/Libraries/LibWeb/DOM/ElementFactory.cpp @@ -38,6 +38,7 @@ #include <LibWeb/DOM/HTMLImageElement.h> #include <LibWeb/DOM/HTMLInputElement.h> #include <LibWeb/DOM/HTMLLinkElement.h> +#include <LibWeb/DOM/HTMLScriptElement.h> #include <LibWeb/DOM/HTMLStyleElement.h> #include <LibWeb/DOM/HTMLTitleElement.h> @@ -82,6 +83,8 @@ NonnullRefPtr<Element> create_element(Document& document, const String& tag_name || lowercase_tag_name == "h6") { return adopt(*new HTMLHeadingElement(document, lowercase_tag_name)); } + if (lowercase_tag_name == "script") + return adopt(*new HTMLScriptElement(document, lowercase_tag_name)); return adopt(*new Element(document, lowercase_tag_name)); } diff --git a/Libraries/LibWeb/DOM/HTMLScriptElement.cpp b/Libraries/LibWeb/DOM/HTMLScriptElement.cpp new file mode 100644 index 0000000000..f92f2e2504 --- /dev/null +++ b/Libraries/LibWeb/DOM/HTMLScriptElement.cpp @@ -0,0 +1,60 @@ +/* + * 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> + +namespace Web { + +HTMLScriptElement::HTMLScriptElement(Document& document, const String& tag_name) + : HTMLElement(document, tag_name) +{ +} + +HTMLScriptElement::~HTMLScriptElement() +{ +} + +void HTMLScriptElement::inserted_into(Node& new_parent) +{ + HTMLElement::inserted_into(new_parent); + + StringBuilder builder; + for_each_child([&](auto& child) { + if (is<Text>(child)) + builder.append(to<Text>(child).text_content()); + }); + + auto source = builder.to_string(); + auto program = JS::Parser(JS::Lexer(source)).parse_program(); + document().interpreter().run(*program); +} + +} diff --git a/Libraries/LibWeb/DOM/HTMLScriptElement.h b/Libraries/LibWeb/DOM/HTMLScriptElement.h new file mode 100644 index 0000000000..90b3c8e784 --- /dev/null +++ b/Libraries/LibWeb/DOM/HTMLScriptElement.h @@ -0,0 +1,41 @@ +/* + * 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. + */ + +#pragma once + +#include <LibWeb/DOM/HTMLElement.h> + +namespace Web { + +class HTMLScriptElement : public HTMLElement { +public: + HTMLScriptElement(Document&, const String& tag_name); + virtual ~HTMLScriptElement() override; + + virtual void inserted_into(Node&) override; +}; + +} diff --git a/Libraries/LibWeb/Makefile b/Libraries/LibWeb/Makefile index 40d6899fbd..96acd12078 100644 --- a/Libraries/LibWeb/Makefile +++ b/Libraries/LibWeb/Makefile @@ -29,6 +29,7 @@ LIBWEB_OBJS = \ DOM/HTMLImageElement.o \ DOM/HTMLInputElement.o \ DOM/HTMLLinkElement.o \ + DOM/HTMLScriptElement.o \ DOM/HTMLStyleElement.o \ DOM/HTMLTitleElement.o \ DOM/Node.o \ |