| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 14:52:44 +03:00
										 |  |  | #include <AK/LexicalPath.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | #include <AK/URL.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/File.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-10 21:03:37 +02:00
										 |  |  | #include <LibCore/MimeData.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Application.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Painter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/ScrollBar.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Window.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:58:33 +01:00
										 |  |  | #include <LibGfx/ImageDecoder.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-04 22:12:37 +02:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/DOM/Element.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/ElementFactory.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/HTMLAnchorElement.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/HTMLImageElement.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-21 18:17:18 +01:00
										 |  |  | #include <LibWeb/DOM/MouseEvent.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/DOM/Text.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Dump.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-07 14:40:38 +02:00
										 |  |  | #include <LibWeb/Frame/EventHandler.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-07 10:06:22 +02:00
										 |  |  | #include <LibWeb/Frame/Frame.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/Layout/LayoutDocument.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/LayoutNode.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | #include <LibWeb/Loader/ResourceLoader.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 19:07:38 +02:00
										 |  |  | #include <LibWeb/PageView.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-18 21:35:44 +02:00
										 |  |  | #include <LibWeb/Painting/PaintContext.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-21 21:37:07 +02:00
										 |  |  | #include <LibWeb/Parser/HTMLDocumentParser.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 08:28:38 +01:00
										 |  |  | //#define SELECTION_DEBUG
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | PageView::PageView() | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     : m_page(make<Page>(*this)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     set_should_hide_unnecessary_scrollbars(true); | 
					
						
							| 
									
										
										
										
											2020-01-05 00:09:35 +03:00
										 |  |  |     set_background_role(ColorRole::Base); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | PageView::~PageView() | 
					
						
							| 
									
										
										
										
											2019-10-04 15:50:04 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 18:02:08 +02:00
										 |  |  | void PageView::page_did_layout() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_content_size(layout_root()->size().to_int_size()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | void PageView::page_did_change_title(const String& title) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_title_change) | 
					
						
							|  |  |  |         on_title_change(title); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_set_document_in_main_frame(Document* document) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_set_document) | 
					
						
							|  |  |  |         on_set_document(document); | 
					
						
							|  |  |  |     layout_and_sync_size(); | 
					
						
							|  |  |  |     scroll_to_top(); | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_start_loading(const URL& url) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_load_start) | 
					
						
							|  |  |  |         on_load_start(url); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_change_selection() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_request_cursor_change(GUI::StandardCursor cursor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (window()) | 
					
						
							|  |  |  |         window()->set_override_cursor(cursor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 14:21:58 -06:00
										 |  |  | void PageView::page_did_request_context_menu(const Gfx::IntPoint& content_position) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_context_menu_request) | 
					
						
							|  |  |  |         on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  | void PageView::page_did_request_link_context_menu(const Gfx::IntPoint& content_position, const String& href, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_link_context_menu_request) | 
					
						
							|  |  |  |         on_link_context_menu_request(href, screen_relative_rect().location().translated(to_widget_position(content_position))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_click_link(const String& href, const String& target, unsigned modifiers) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_link_click) | 
					
						
							|  |  |  |         on_link_click(href, target, modifiers); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_middle_click_link(const String& href, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_link_middle_click) | 
					
						
							|  |  |  |         on_link_middle_click(href); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  | void PageView::page_did_enter_tooltip_area(const Gfx::IntPoint& content_position, const String& title) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     GUI::Application::the().show_tooltip(title, screen_relative_rect().location().translated(to_widget_position(content_position))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_leave_tooltip_area() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GUI::Application::the().hide_tooltip(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_hover_link(const URL& url) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_link_hover) | 
					
						
							|  |  |  |         on_link_hover(url.to_string()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_unhover_link() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PageView::page_did_request_scroll_to_anchor(const String& fragment) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     scroll_to_anchor(fragment); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  | void PageView::page_did_invalidate(const Gfx::IntRect&) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 21:35:31 +02:00
										 |  |  | void PageView::page_did_change_favicon(const Gfx::Bitmap& bitmap) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_favicon_change) | 
					
						
							|  |  |  |         on_favicon_change(bitmap); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::layout_and_sync_size() | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     if (!document()) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     bool had_vertical_scrollbar = vertical_scrollbar().is_visible(); | 
					
						
							|  |  |  |     bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().set_size(available_size()); | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     document()->layout(); | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |     set_content_size(layout_root()->size().to_int_size()); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
 | 
					
						
							|  |  |  |     //       since the scrollbars now take up some of the available space.
 | 
					
						
							|  |  |  |     if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |         page().main_frame().set_size(available_size()); | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |         document()->layout(); | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |         set_content_size(layout_root()->size().to_int_size()); | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().set_viewport_rect(viewport_rect_in_content_coordinates()); | 
					
						
							| 
									
										
										
										
											2019-12-18 20:54:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #ifdef HTML_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-10-03 10:25:00 +02:00
										 |  |  |     dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n"); | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     ::dump_tree(*layout_root()); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::resize_event(GUI::ResizeEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::ScrollableWidget::resize_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     layout_and_sync_size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::paint_event(GUI::PaintEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::Frame::paint_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::Painter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     painter.add_clip_rect(widget_inner_rect()); | 
					
						
							|  |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     if (!layout_root()) { | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |         painter.fill_rect(event.rect(), palette().color(background_role())); | 
					
						
							| 
									
										
										
										
											2019-10-04 21:05:52 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-05 00:09:35 +03:00
										 |  |  |     painter.fill_rect(event.rect(), document()->background_color(palette())); | 
					
						
							| 
									
										
										
										
											2019-10-05 21:51:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     if (auto background_bitmap = document()->background_image()) { | 
					
						
							| 
									
										
										
										
											2019-10-19 11:49:46 +02:00
										 |  |  |         painter.draw_tiled_bitmap(event.rect(), *background_bitmap); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     painter.translate(frame_thickness(), frame_thickness()); | 
					
						
							|  |  |  |     painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 21:35:44 +02:00
										 |  |  |     PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() }); | 
					
						
							| 
									
										
										
										
											2019-10-12 15:02:53 +02:00
										 |  |  |     context.set_should_show_line_box_borders(m_should_show_line_box_borders); | 
					
						
							| 
									
										
										
										
											2020-05-04 22:35:29 +02:00
										 |  |  |     context.set_viewport_rect(viewport_rect_in_content_coordinates()); | 
					
						
							| 
									
										
										
										
											2020-06-18 18:57:35 +02:00
										 |  |  |     layout_root()->paint_all_phases(context); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::mousemove_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers()); | 
					
						
							| 
									
										
										
										
											2020-06-07 14:40:38 +02:00
										 |  |  |     GUI::ScrollableWidget::mousemove_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::mousedown_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers()); | 
					
						
							| 
									
										
										
										
											2020-06-07 14:40:38 +02:00
										 |  |  |     GUI::ScrollableWidget::mousedown_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::mouseup_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers()); | 
					
						
							| 
									
										
										
										
											2020-06-07 14:40:38 +02:00
										 |  |  |     GUI::ScrollableWidget::mouseup_event(event); | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::keydown_event(GUI::KeyEvent& event) | 
					
						
							| 
									
										
										
										
											2019-10-17 21:36:22 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (event.modifiers() == 0) { | 
					
						
							|  |  |  |         switch (event.key()) { | 
					
						
							|  |  |  |         case Key_Home: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(0); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_End: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().max()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Down: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Up: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Left: | 
					
						
							|  |  |  |             horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Right: | 
					
						
							|  |  |  |             horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_PageDown: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_PageUp: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height()); | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2020-05-27 00:29:57 +03:00
										 |  |  |         default: | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2019-10-17 21:36:22 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     event.accept(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::reload() | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     load(page().main_frame().document()->url()); | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 21:37:07 +02:00
										 |  |  | void PageView::load_html(const StringView& html, const URL& url) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     HTMLDocumentParser parser(html, "utf-8"); | 
					
						
							|  |  |  |     parser.run(url); | 
					
						
							|  |  |  |     set_document(&parser.document()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | bool PageView::load(const URL& url) | 
					
						
							| 
									
										
										
										
											2020-05-16 15:38:13 +04:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  |     if (window()) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         window()->set_override_cursor(GUI::StandardCursor::None); | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     return page().main_frame().loader().load(url); | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | const LayoutDocument* PageView::layout_root() const | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return document() ? document()->layout_node() : nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | LayoutDocument* PageView::layout_root() | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!document()) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     return const_cast<LayoutDocument*>(document()->layout_node()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::scroll_to_anchor(const StringView& name) | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     if (!document()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-28 09:12:13 +01:00
										 |  |  |     const auto* element = document()->get_element_by_id(name); | 
					
						
							| 
									
										
										
										
											2019-10-21 12:04:17 +02:00
										 |  |  |     if (!element) { | 
					
						
							|  |  |  |         auto candidates = document()->get_elements_by_name(name); | 
					
						
							|  |  |  |         for (auto* candidate : candidates) { | 
					
						
							|  |  |  |             if (is<HTMLAnchorElement>(*candidate)) { | 
					
						
							|  |  |  |                 element = to<HTMLAnchorElement>(candidate); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2019-10-21 12:01:30 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-21 12:04:17 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!element) { | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  |         dbg() << "PageView::scroll_to_anchor(): Anchor not found: '" << name << "'"; | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!element->layout_node()) { | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  |         dbg() << "PageView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'"; | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto& layout_node = *element->layout_node(); | 
					
						
							| 
									
										
										
										
											2020-02-06 14:33:05 +01:00
										 |  |  |     Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)visible_content_rect().width(), (float)visible_content_rect().height() } }; | 
					
						
							| 
									
										
										
										
											2020-06-25 15:31:56 +02:00
										 |  |  |     if (is<LayoutBox>(layout_node)) { | 
					
						
							|  |  |  |         auto& layout_box = to<LayoutBox>(layout_node); | 
					
						
							|  |  |  |         auto padding_box = layout_box.box_model().padding_box(layout_box); | 
					
						
							|  |  |  |         float_rect.move_by(-padding_box.left, -padding_box.top); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-18 16:25:38 +01:00
										 |  |  |     scroll_into_view(enclosing_int_rect(float_rect), true, true); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     window()->set_override_cursor(GUI::StandardCursor::None); | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | void PageView::load_empty_document() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().set_document(nullptr); | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | Document* PageView::document() | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     return page().main_frame().document(); | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | const Document* PageView::document() const | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     return page().main_frame().document(); | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | void PageView::set_document(Document* document) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().set_document(document); | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::did_scroll() | 
					
						
							| 
									
										
										
										
											2019-12-18 20:54:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().set_viewport_rect(viewport_rect_in_content_coordinates()); | 
					
						
							|  |  |  |     page().main_frame().did_scroll({}); | 
					
						
							| 
									
										
										
										
											2019-12-18 20:54:23 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 18:21:22 +02:00
										 |  |  | void PageView::drop_event(GUI::DropEvent& event) | 
					
						
							| 
									
										
										
										
											2020-05-10 21:03:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (event.mime_data().has_urls()) { | 
					
						
							|  |  |  |         if (on_url_drop) { | 
					
						
							|  |  |  |             on_url_drop(event.mime_data().urls().first()); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ScrollableWidget::drop_event(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |