Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/ByteBuffer.h>
28#include <LibCore/File.h>
29#include <LibGUI/AboutDialog.h>
30#include <LibGUI/Action.h>
31#include <LibGUI/Application.h>
32#include <LibGUI/Menu.h>
33#include <LibGUI/MenuBar.h>
34#include <LibGUI/Window.h>
35#include <LibWeb/CSS/StyleResolver.h>
36#include <LibWeb/DOM/Element.h>
37#include <LibWeb/Dump.h>
38#include <LibWeb/HtmlView.h>
39#include <LibWeb/Layout/LayoutBlock.h>
40#include <LibWeb/Layout/LayoutInline.h>
41#include <LibWeb/Layout/LayoutNode.h>
42#include <LibWeb/Parser/CSSParser.h>
43#include <LibWeb/Parser/HTMLParser.h>
44#include <stdio.h>
45
46int main(int argc, char** argv)
47{
48 GUI::Application app(argc, argv);
49
50 auto f = Core::File::construct();
51 bool success;
52 if (argc < 2) {
53 success = f->open(STDIN_FILENO, Core::IODevice::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescription::No);
54 } else {
55 f->set_filename(argv[1]);
56 success = f->open(Core::IODevice::OpenMode::ReadOnly);
57 }
58 if (!success) {
59 fprintf(stderr, "Error: %s\n", f->error_string());
60 return 1;
61 }
62
63 String html = String::copy(f->read_all());
64 auto document = Web::parse_html_document(html);
65
66 auto window = GUI::Window::construct();
67 auto& widget = window->set_main_widget<Web::HtmlView>();
68 widget.set_document(document);
69 if (!widget.document()->title().is_null())
70 window->set_title(String::format("%s - HTML", widget.document()->title().characters()));
71 else
72 window->set_title("HTML");
73 window->show();
74
75 auto menubar = make<GUI::MenuBar>();
76
77 auto& app_menu = menubar->add_menu("HTML");
78 app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
79 app.quit();
80 }));
81
82 auto& help_menu = menubar->add_menu("Help");
83 help_menu.add_action(GUI::Action::create("About", [&](auto&) {
84 GUI::AboutDialog::show("HTML", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-html.png"), window);
85 }));
86
87 app.set_menubar(move(menubar));
88
89 window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
90
91 return app.exec();
92}