mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:07:44 +00:00
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.
This commit is contained in:
parent
f94099f796
commit
9c9d3f0904
11 changed files with 142 additions and 3 deletions
|
@ -4,7 +4,7 @@ OBJS = \
|
||||||
|
|
||||||
PROGRAM = Browser
|
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
|
main.cpp: ../../Libraries/LibWeb/CSS/PropertyID.h
|
||||||
../../Libraries/LibWeb/CSS/PropertyID.h:
|
../../Libraries/LibWeb/CSS/PropertyID.h:
|
||||||
|
|
|
@ -7,6 +7,6 @@ OBJS = \
|
||||||
|
|
||||||
PROGRAM = Help
|
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
|
include ../../Makefile.common
|
||||||
|
|
|
@ -11,6 +11,6 @@ OBJS = \
|
||||||
|
|
||||||
PROGRAM = IRCClient
|
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
|
include ../../Makefile.common
|
||||||
|
|
10
Base/home/anon/www/alert.html
Normal file
10
Base/home/anon/www/alert.html
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script>
|
||||||
|
alert("Hello friends!");
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -23,6 +23,7 @@ h1 {
|
||||||
<p>This is a very simple browser built on the LibWeb engine.</p>
|
<p>This is a very simple browser built on the LibWeb engine.</p>
|
||||||
<p>Some small test pages:</p>
|
<p>Some small test pages:</p>
|
||||||
<ul>
|
<ul>
|
||||||
|
<li><a href="alert.html">alert() test</a></li>
|
||||||
<li><a href="small.html">small</a></li>
|
<li><a href="small.html">small</a></li>
|
||||||
<li><a href="first-child.html">:first-child</a></li>
|
<li><a href="first-child.html">:first-child</a></li>
|
||||||
<li><a href="last-child.html">:last-child</a></li>
|
<li><a href="last-child.html">:last-child</a></li>
|
||||||
|
|
|
@ -28,6 +28,9 @@
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibCore/Timer.h>
|
#include <LibCore/Timer.h>
|
||||||
#include <LibGUI/Application.h>
|
#include <LibGUI/Application.h>
|
||||||
|
#include <LibGUI/MessageBox.h>
|
||||||
|
#include <LibJS/GlobalObject.h>
|
||||||
|
#include <LibJS/Interpreter.h>
|
||||||
#include <LibWeb/CSS/StyleResolver.h>
|
#include <LibWeb/CSS/StyleResolver.h>
|
||||||
#include <LibWeb/DOM/Document.h>
|
#include <LibWeb/DOM/Document.h>
|
||||||
#include <LibWeb/DOM/DocumentType.h>
|
#include <LibWeb/DOM/DocumentType.h>
|
||||||
|
@ -332,4 +335,19 @@ Color Document::visited_link_color() const
|
||||||
return frame()->html_view()->palette().visited_link();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#include <AK/URL.h>
|
#include <AK/URL.h>
|
||||||
#include <AK/WeakPtr.h>
|
#include <AK/WeakPtr.h>
|
||||||
#include <LibCore/Forward.h>
|
#include <LibCore/Forward.h>
|
||||||
|
#include <LibJS/Forward.h>
|
||||||
#include <LibWeb/CSS/StyleResolver.h>
|
#include <LibWeb/CSS/StyleResolver.h>
|
||||||
#include <LibWeb/CSS/StyleSheet.h>
|
#include <LibWeb/CSS/StyleSheet.h>
|
||||||
#include <LibWeb/DOM/ParentNode.h>
|
#include <LibWeb/DOM/ParentNode.h>
|
||||||
|
@ -120,6 +121,8 @@ public:
|
||||||
const String& source() const { return m_source; }
|
const String& source() const { return m_source; }
|
||||||
void set_source(const String& source) { m_source = source; }
|
void set_source(const String& source) { m_source = source; }
|
||||||
|
|
||||||
|
JS::Interpreter& interpreter();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
|
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
|
||||||
|
|
||||||
|
@ -139,6 +142,8 @@ private:
|
||||||
RefPtr<Core::Timer> m_style_update_timer;
|
RefPtr<Core::Timer> m_style_update_timer;
|
||||||
|
|
||||||
String m_source;
|
String m_source;
|
||||||
|
|
||||||
|
OwnPtr<JS::Interpreter> m_interpreter;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
#include <LibWeb/DOM/HTMLImageElement.h>
|
#include <LibWeb/DOM/HTMLImageElement.h>
|
||||||
#include <LibWeb/DOM/HTMLInputElement.h>
|
#include <LibWeb/DOM/HTMLInputElement.h>
|
||||||
#include <LibWeb/DOM/HTMLLinkElement.h>
|
#include <LibWeb/DOM/HTMLLinkElement.h>
|
||||||
|
#include <LibWeb/DOM/HTMLScriptElement.h>
|
||||||
#include <LibWeb/DOM/HTMLStyleElement.h>
|
#include <LibWeb/DOM/HTMLStyleElement.h>
|
||||||
#include <LibWeb/DOM/HTMLTitleElement.h>
|
#include <LibWeb/DOM/HTMLTitleElement.h>
|
||||||
|
|
||||||
|
@ -82,6 +83,8 @@ NonnullRefPtr<Element> create_element(Document& document, const String& tag_name
|
||||||
|| lowercase_tag_name == "h6") {
|
|| lowercase_tag_name == "h6") {
|
||||||
return adopt(*new HTMLHeadingElement(document, lowercase_tag_name));
|
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));
|
return adopt(*new Element(document, lowercase_tag_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
60
Libraries/LibWeb/DOM/HTMLScriptElement.cpp
Normal file
60
Libraries/LibWeb/DOM/HTMLScriptElement.cpp
Normal file
|
@ -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
Libraries/LibWeb/DOM/HTMLScriptElement.h
Normal file
41
Libraries/LibWeb/DOM/HTMLScriptElement.h
Normal file
|
@ -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;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -29,6 +29,7 @@ LIBWEB_OBJS = \
|
||||||
DOM/HTMLImageElement.o \
|
DOM/HTMLImageElement.o \
|
||||||
DOM/HTMLInputElement.o \
|
DOM/HTMLInputElement.o \
|
||||||
DOM/HTMLLinkElement.o \
|
DOM/HTMLLinkElement.o \
|
||||||
|
DOM/HTMLScriptElement.o \
|
||||||
DOM/HTMLStyleElement.o \
|
DOM/HTMLStyleElement.o \
|
||||||
DOM/HTMLTitleElement.o \
|
DOM/HTMLTitleElement.o \
|
||||||
DOM/Node.o \
|
DOM/Node.o \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue