Parcourir la source

LibWeb: Parse <script> elements and run any JavaScript found inside

This patch begins integrating LibJS into LibWeb. Document holds the
JS::Interpreter for now, and it is created on demand when you first
call Document::interpreter().

We also add a simple "alert()" function to the global object.
Andreas Kling il y a 5 ans
Parent
commit
9c9d3f0904

+ 1 - 1
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:

+ 1 - 1
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

+ 1 - 1
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

+ 10 - 0
Base/home/anon/www/alert.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+    <head>
+    <script>
+        alert("Hello friends!");
+    </script>
+    </head>
+    <body>
+    </body>
+</html>

+ 1 - 0
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>

+ 18 - 0
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;
+}
+
 }

+ 5 - 0
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<>

+ 3 - 0
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));
 }
 

+ 60 - 0
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);
+}
+
+}

+ 41 - 0
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;
+};
+
+}

+ 1 - 0
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 \