summaryrefslogtreecommitdiff
path: root/Userland/Applications/Browser/ConsoleWidget.cpp
blob: 1b69c858f5c5b09c1544db3ea82835f4974bca8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ConsoleWidget.h"
#include <AK/StringBuilder.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/TextBox.h>
#include <LibGfx/FontDatabase.h>
#include <LibJS/Interpreter.h>
#include <LibJS/MarkupGenerator.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/SyntaxHighlighter.h>
#include <LibWeb/Bindings/DOMExceptionWrapper.h>
#include <LibWeb/DOM/DocumentType.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/DOMTreeModel.h>
#include <LibWeb/HTML/HTMLBodyElement.h>

namespace Browser {

ConsoleWidget::ConsoleWidget()
{
    set_layout<GUI::VerticalBoxLayout>();
    set_fill_with_background_color(true);

    auto base_document = Web::DOM::Document::create();
    base_document->append_child(adopt_ref(*new Web::DOM::DocumentType(base_document)));
    auto html_element = base_document->create_element("html");
    base_document->append_child(html_element);
    auto head_element = base_document->create_element("head");
    html_element->append_child(head_element);
    auto body_element = base_document->create_element("body");
    html_element->append_child(body_element);
    m_output_container = body_element;

    m_output_view = add<Web::InProcessWebView>();
    m_output_view->set_document(base_document);

    auto& bottom_container = add<GUI::Widget>();
    bottom_container.set_layout<GUI::HorizontalBoxLayout>();
    bottom_container.set_fixed_height(22);

    m_input = bottom_container.add<GUI::TextBox>();
    m_input->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
    // FIXME: Syntax Highlighting breaks the cursor's position on non fixed-width fonts.
    m_input->set_font(Gfx::FontDatabase::default_fixed_width_font());
    m_input->set_history_enabled(true);

    m_input->on_return_pressed = [this] {
        auto js_source = m_input->text();

        // FIXME: An is_blank check to check if there is only whitespace would probably be preferable.
        if (js_source.is_empty())
            return;

        m_input->add_current_text_to_history();
        m_input->clear();

        print_source_line(js_source);

        if (on_js_input)
            on_js_input(js_source);

        // no interpreter being set means we are running in multi-process mode
        if (!m_interpreter)
            return;

        auto parser = JS::Parser(JS::Lexer(js_source));
        auto program = parser.parse_program();

        StringBuilder output_html;
        if (parser.has_errors()) {
            auto error = parser.errors()[0];
            auto hint = error.source_location_hint(js_source);
            if (!hint.is_empty())
                output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint)));
            m_interpreter->vm().throw_exception<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
        } else {
            m_interpreter->run(m_interpreter->global_object(), *program);
        }

        if (m_interpreter->exception()) {
            auto* exception = m_interpreter->exception();
            m_interpreter->vm().clear_exception();
            output_html.append("Uncaught exception: ");
            auto error = exception->value();
            if (error.is_object())
                output_html.append(JS::MarkupGenerator::html_from_error(error.as_object()));
            else
                output_html.append(JS::MarkupGenerator::html_from_value(error));
            print_html(output_html.string_view());
            return;
        }

        print_html(JS::MarkupGenerator::html_from_value(m_interpreter->vm().last_value()));
    };

    set_focus_proxy(m_input);

    auto& clear_button = bottom_container.add<GUI::Button>();
    clear_button.set_fixed_size(22, 22);
    clear_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"));
    clear_button.set_tooltip("Clear the console output");
    clear_button.on_click = [this](auto) {
        clear_output();
    };
}

ConsoleWidget::~ConsoleWidget()
{
}

void ConsoleWidget::handle_js_console_output(const String& method, const String& line)
{
    if (method == "html") {
        print_html(line);
    } else if (method == "clear") {
        clear_output();
    }
}

void ConsoleWidget::set_interpreter(WeakPtr<JS::Interpreter> interpreter)
{
    if (m_interpreter.ptr() == interpreter.ptr())
        return;

    m_interpreter = interpreter;
    m_console_client = make<BrowserConsoleClient>(interpreter->global_object().console(), *this);
    interpreter->global_object().console().set_client(*m_console_client.ptr());

    clear_output();
}

void ConsoleWidget::print_source_line(const StringView& source)
{
    StringBuilder html;
    html.append("<span class=\"repl-indicator\">");
    html.append("&gt; ");
    html.append("</span>");

    html.append(JS::MarkupGenerator::html_from_source(source));

    print_html(html.string_view());
}

void ConsoleWidget::print_html(const StringView& line)
{
    auto paragraph = m_output_container->document().create_element("p");
    paragraph->set_inner_html(line);

    m_output_container->append_child(paragraph);
    m_output_container->document().invalidate_layout();
    m_output_container->document().update_layout();

    m_output_view->scroll_to_bottom();
}

void ConsoleWidget::clear_output()
{
    m_output_container->remove_all_children();
    m_output_view->update();
}

}