PageView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. #include <AK/LexicalPath.h>
  27. #include <AK/URL.h>
  28. #include <LibCore/File.h>
  29. #include <LibCore/MimeData.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/ScrollBar.h>
  33. #include <LibGUI/Window.h>
  34. #include <LibGemini/Document.h>
  35. #include <LibGfx/ImageDecoder.h>
  36. #include <LibJS/Runtime/Value.h>
  37. #include <LibMarkdown/Document.h>
  38. #include <LibWeb/DOM/Element.h>
  39. #include <LibWeb/DOM/ElementFactory.h>
  40. #include <LibWeb/DOM/HTMLAnchorElement.h>
  41. #include <LibWeb/DOM/HTMLImageElement.h>
  42. #include <LibWeb/DOM/MouseEvent.h>
  43. #include <LibWeb/DOM/Text.h>
  44. #include <LibWeb/Dump.h>
  45. #include <LibWeb/Frame.h>
  46. #include <LibWeb/PageView.h>
  47. #include <LibWeb/Layout/LayoutDocument.h>
  48. #include <LibWeb/Layout/LayoutNode.h>
  49. #include <LibWeb/Parser/HTMLDocumentParser.h>
  50. #include <LibWeb/Parser/HTMLParser.h>
  51. #include <LibWeb/RenderingContext.h>
  52. #include <LibWeb/ResourceLoader.h>
  53. #include <stdio.h>
  54. //#define SELECTION_DEBUG
  55. namespace Web {
  56. PageView::PageView()
  57. : m_main_frame(Web::Frame::create(*this))
  58. {
  59. main_frame().on_set_needs_display = [this](auto& content_rect) {
  60. if (content_rect.is_empty()) {
  61. update();
  62. return;
  63. }
  64. Gfx::Rect adjusted_rect = content_rect;
  65. adjusted_rect.set_location(to_widget_position(content_rect.location()));
  66. update(adjusted_rect);
  67. };
  68. set_should_hide_unnecessary_scrollbars(true);
  69. set_background_role(ColorRole::Base);
  70. }
  71. PageView::~PageView()
  72. {
  73. }
  74. void PageView::set_document(Document* new_document)
  75. {
  76. RefPtr<Document> old_document = document();
  77. if (new_document == old_document)
  78. return;
  79. if (old_document)
  80. old_document->on_layout_updated = nullptr;
  81. main_frame().set_document(new_document);
  82. if (on_set_document)
  83. on_set_document(new_document);
  84. if (new_document) {
  85. new_document->on_layout_updated = [this] {
  86. layout_and_sync_size();
  87. update();
  88. };
  89. }
  90. #ifdef HTML_DEBUG
  91. if (document != nullptr) {
  92. dbgprintf("\033[33;1mLayout tree before layout:\033[0m\n");
  93. ::dump_tree(*layout_root());
  94. }
  95. #endif
  96. layout_and_sync_size();
  97. update();
  98. }
  99. void PageView::layout_and_sync_size()
  100. {
  101. if (!document())
  102. return;
  103. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  104. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  105. main_frame().set_size(available_size());
  106. document()->layout();
  107. set_content_size(enclosing_int_rect(layout_root()->rect()).size());
  108. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  109. // since the scrollbars now take up some of the available space.
  110. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  111. main_frame().set_size(available_size());
  112. document()->layout();
  113. set_content_size(enclosing_int_rect(layout_root()->rect()).size());
  114. }
  115. main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  116. #ifdef HTML_DEBUG
  117. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  118. ::dump_tree(*layout_root());
  119. #endif
  120. }
  121. void PageView::resize_event(GUI::ResizeEvent& event)
  122. {
  123. GUI::ScrollableWidget::resize_event(event);
  124. layout_and_sync_size();
  125. }
  126. void PageView::paint_event(GUI::PaintEvent& event)
  127. {
  128. GUI::Frame::paint_event(event);
  129. GUI::Painter painter(*this);
  130. painter.add_clip_rect(widget_inner_rect());
  131. painter.add_clip_rect(event.rect());
  132. if (!layout_root()) {
  133. painter.fill_rect(event.rect(), palette().color(background_role()));
  134. return;
  135. }
  136. painter.fill_rect(event.rect(), document()->background_color(palette()));
  137. if (auto background_bitmap = document()->background_image()) {
  138. painter.draw_tiled_bitmap(event.rect(), *background_bitmap);
  139. }
  140. painter.translate(frame_thickness(), frame_thickness());
  141. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  142. RenderingContext context(painter, palette());
  143. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  144. context.set_viewport_rect(viewport_rect_in_content_coordinates());
  145. layout_root()->render(context);
  146. }
  147. void PageView::mousemove_event(GUI::MouseEvent& event)
  148. {
  149. if (!layout_root())
  150. return GUI::ScrollableWidget::mousemove_event(event);
  151. bool hovered_node_changed = false;
  152. bool is_hovering_link = false;
  153. bool was_hovering_link = document()->hovered_node() && document()->hovered_node()->is_link();
  154. auto result = layout_root()->hit_test(to_content_position(event.position()));
  155. const HTMLAnchorElement* hovered_link_element = nullptr;
  156. if (result.layout_node) {
  157. RefPtr<Node> node = result.layout_node->node();
  158. hovered_node_changed = node != document()->hovered_node();
  159. document()->set_hovered_node(node);
  160. if (node) {
  161. hovered_link_element = node->enclosing_link_element();
  162. if (hovered_link_element) {
  163. #ifdef HTML_DEBUG
  164. dbg() << "PageView: hovering over a link to " << hovered_link_element->href();
  165. #endif
  166. is_hovering_link = true;
  167. }
  168. auto offset = compute_mouse_event_offset(event.position(), *result.layout_node);
  169. node->dispatch_event(MouseEvent::create("mousemove", offset.x(), offset.y()));
  170. }
  171. if (m_in_mouse_selection) {
  172. layout_root()->selection().set_end({ result.layout_node, result.index_in_node });
  173. dump_selection("MouseMove");
  174. update();
  175. }
  176. }
  177. if (window())
  178. window()->set_override_cursor(is_hovering_link ? GUI::StandardCursor::Hand : GUI::StandardCursor::None);
  179. if (hovered_node_changed) {
  180. update();
  181. RefPtr<HTMLElement> hovered_html_element = document()->hovered_node() ? document()->hovered_node()->enclosing_html_element() : nullptr;
  182. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  183. auto screen_position = screen_relative_rect().location().translated(event.position());
  184. GUI::Application::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4));
  185. } else {
  186. GUI::Application::the().hide_tooltip();
  187. }
  188. }
  189. if (is_hovering_link != was_hovering_link) {
  190. if (on_link_hover) {
  191. on_link_hover(hovered_link_element ? document()->complete_url(hovered_link_element->href()).to_string() : String());
  192. }
  193. }
  194. event.accept();
  195. }
  196. void PageView::mousedown_event(GUI::MouseEvent& event)
  197. {
  198. if (!layout_root())
  199. return GUI::ScrollableWidget::mousemove_event(event);
  200. bool hovered_node_changed = false;
  201. auto result = layout_root()->hit_test(to_content_position(event.position()));
  202. if (result.layout_node) {
  203. RefPtr<Node> node = result.layout_node->node();
  204. hovered_node_changed = node != document()->hovered_node();
  205. document()->set_hovered_node(node);
  206. if (node) {
  207. auto offset = compute_mouse_event_offset(event.position(), *result.layout_node);
  208. node->dispatch_event(MouseEvent::create("mousedown", offset.x(), offset.y()));
  209. if (RefPtr<HTMLAnchorElement> link = node->enclosing_link_element()) {
  210. dbg() << "PageView: clicking on a link to " << link->href();
  211. if (event.button() == GUI::MouseButton::Left) {
  212. if (link->href().starts_with("javascript:")) {
  213. run_javascript_url(link->href());
  214. } else {
  215. if (on_link_click)
  216. on_link_click(link->href(), link->target(), event.modifiers());
  217. }
  218. } else if (event.button() == GUI::MouseButton::Right) {
  219. if (on_link_context_menu_request)
  220. on_link_context_menu_request(link->href(), event.position().translated(screen_relative_rect().location()));
  221. } else if (event.button() == GUI::MouseButton::Middle) {
  222. if (on_link_middle_click)
  223. on_link_middle_click(link->href());
  224. }
  225. } else {
  226. if (event.button() == GUI::MouseButton::Left) {
  227. if (layout_root())
  228. layout_root()->selection().set({ result.layout_node, result.index_in_node }, {});
  229. dump_selection("MouseDown");
  230. m_in_mouse_selection = true;
  231. }
  232. }
  233. }
  234. }
  235. if (hovered_node_changed)
  236. update();
  237. event.accept();
  238. }
  239. void PageView::mouseup_event(GUI::MouseEvent& event)
  240. {
  241. if (!layout_root())
  242. return GUI::ScrollableWidget::mouseup_event(event);
  243. auto result = layout_root()->hit_test(to_content_position(event.position()));
  244. if (result.layout_node) {
  245. if (RefPtr<Node> node = result.layout_node->node()) {
  246. auto offset = compute_mouse_event_offset(event.position(), *result.layout_node);
  247. node->dispatch_event(MouseEvent::create("mouseup", offset.x(), offset.y()));
  248. }
  249. }
  250. if (event.button() == GUI::MouseButton::Left) {
  251. dump_selection("MouseUp");
  252. m_in_mouse_selection = false;
  253. }
  254. }
  255. void PageView::keydown_event(GUI::KeyEvent& event)
  256. {
  257. if (event.modifiers() == 0) {
  258. switch (event.key()) {
  259. case Key_Home:
  260. vertical_scrollbar().set_value(0);
  261. break;
  262. case Key_End:
  263. vertical_scrollbar().set_value(vertical_scrollbar().max());
  264. break;
  265. case Key_Down:
  266. vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
  267. break;
  268. case Key_Up:
  269. vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
  270. break;
  271. case Key_Left:
  272. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
  273. break;
  274. case Key_Right:
  275. horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
  276. break;
  277. case Key_PageDown:
  278. vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
  279. break;
  280. case Key_PageUp:
  281. vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. event.accept();
  288. }
  289. void PageView::reload()
  290. {
  291. load(main_frame().document()->url());
  292. }
  293. static RefPtr<Document> create_markdown_document(const ByteBuffer& data, const URL& url)
  294. {
  295. auto markdown_document = Markdown::Document::parse(data);
  296. if (!markdown_document)
  297. return nullptr;
  298. return parse_html_document(markdown_document->render_to_html(), url);
  299. }
  300. static RefPtr<Document> create_text_document(const ByteBuffer& data, const URL& url)
  301. {
  302. auto document = adopt(*new Document(url));
  303. auto html_element = document->create_element("html");
  304. document->append_child(html_element);
  305. auto head_element = document->create_element("head");
  306. html_element->append_child(head_element);
  307. auto title_element = document->create_element("title");
  308. head_element->append_child(title_element);
  309. auto title_text = document->create_text_node(url.basename());
  310. title_element->append_child(title_text);
  311. auto body_element = document->create_element("body");
  312. html_element->append_child(body_element);
  313. auto pre_element = create_element(document, "pre");
  314. body_element->append_child(pre_element);
  315. pre_element->append_child(document->create_text_node(String::copy(data)));
  316. return document;
  317. }
  318. static RefPtr<Document> create_image_document(const ByteBuffer& data, const URL& url)
  319. {
  320. auto document = adopt(*new Document(url));
  321. auto image_decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  322. auto bitmap = image_decoder->bitmap();
  323. ASSERT(bitmap);
  324. auto html_element = create_element(document, "html");
  325. document->append_child(html_element);
  326. auto head_element = create_element(document, "head");
  327. html_element->append_child(head_element);
  328. auto title_element = create_element(document, "title");
  329. head_element->append_child(title_element);
  330. auto basename = LexicalPath(url.path()).basename();
  331. auto title_text = adopt(*new Text(document, String::format("%s [%dx%d]", basename.characters(), bitmap->width(), bitmap->height())));
  332. title_element->append_child(title_text);
  333. auto body_element = create_element(document, "body");
  334. html_element->append_child(body_element);
  335. auto image_element = create_element(document, "img");
  336. image_element->set_attribute("src", url.to_string());
  337. body_element->append_child(image_element);
  338. return document;
  339. }
  340. static RefPtr<Document> create_gemini_document(const ByteBuffer& data, const URL& url)
  341. {
  342. auto markdown_document = Gemini::Document::parse({ (const char*)data.data(), data.size() }, url);
  343. return parse_html_document(markdown_document->render_to_html(), url);
  344. }
  345. String encoding_from_content_type(const String& content_type)
  346. {
  347. auto offset = content_type.index_of("charset=");
  348. if (offset.has_value())
  349. return content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase();
  350. return "utf-8";
  351. }
  352. String mime_type_from_content_type(const String& content_type)
  353. {
  354. auto offset = content_type.index_of(";");
  355. if (offset.has_value())
  356. return content_type.substring(0, offset.value()).to_lowercase();
  357. return content_type;
  358. }
  359. static String guess_mime_type_based_on_filename(const URL& url)
  360. {
  361. if (url.path().ends_with(".png"))
  362. return "image/png";
  363. if (url.path().ends_with(".gif"))
  364. return "image/gif";
  365. if (url.path().ends_with(".md"))
  366. return "text/markdown";
  367. if (url.path().ends_with(".html") || url.path().ends_with(".htm"))
  368. return "text/html";
  369. return "text/plain";
  370. }
  371. RefPtr<Document> PageView::create_document_from_mime_type(const ByteBuffer& data, const URL& url, const String& mime_type, const String& encoding)
  372. {
  373. if (mime_type.starts_with("image/"))
  374. return create_image_document(data, url);
  375. if (mime_type == "text/plain")
  376. return create_text_document(data, url);
  377. if (mime_type == "text/markdown")
  378. return create_markdown_document(data, url);
  379. if (mime_type == "text/gemini")
  380. return create_gemini_document(data, url);
  381. if (mime_type == "text/html") {
  382. if (m_use_new_parser) {
  383. HTMLDocumentParser parser(data, encoding);
  384. parser.run(url);
  385. return parser.document();
  386. }
  387. return parse_html_document(data, url, encoding);
  388. }
  389. return nullptr;
  390. }
  391. void PageView::load(const URL& url)
  392. {
  393. dbg() << "PageView::load: " << url;
  394. if (!url.is_valid()) {
  395. load_error_page(url, "Invalid URL");
  396. return;
  397. }
  398. if (window())
  399. window()->set_override_cursor(GUI::StandardCursor::None);
  400. if (on_load_start)
  401. on_load_start(url);
  402. ResourceLoader::the().load(
  403. url,
  404. [this, url](auto data, auto& response_headers) {
  405. // FIXME: Also check HTTP status code before redirecting
  406. auto location = response_headers.get("Location");
  407. if (location.has_value()) {
  408. load(location.value());
  409. return;
  410. }
  411. if (data.is_null()) {
  412. load_error_page(url, "No data");
  413. return;
  414. }
  415. String encoding = "utf-8";
  416. String mime_type;
  417. auto content_type = response_headers.get("Content-Type");
  418. if (content_type.has_value()) {
  419. dbg() << "Content-Type header: _" << content_type.value() << "_";
  420. encoding = encoding_from_content_type(content_type.value());
  421. mime_type = mime_type_from_content_type(content_type.value());
  422. } else {
  423. dbg() << "No Content-Type header to go on! Guessing based on filename...";
  424. mime_type = guess_mime_type_based_on_filename(url);
  425. }
  426. dbg() << "I believe this content has MIME type '" << mime_type << "', encoding '" << encoding << "'";
  427. auto document = create_document_from_mime_type(data, url, mime_type, encoding);
  428. ASSERT(document);
  429. set_document(document);
  430. if (!url.fragment().is_empty())
  431. scroll_to_anchor(url.fragment());
  432. if (on_title_change)
  433. on_title_change(document->title());
  434. },
  435. [this, url](auto error) {
  436. load_error_page(url, error);
  437. });
  438. if (url.protocol() != "file" && url.protocol() != "about") {
  439. URL favicon_url;
  440. favicon_url.set_protocol(url.protocol());
  441. favicon_url.set_host(url.host());
  442. favicon_url.set_port(url.port());
  443. favicon_url.set_path("/favicon.ico");
  444. ResourceLoader::the().load(
  445. favicon_url,
  446. [this, favicon_url](auto data, auto&) {
  447. dbg() << "Favicon downloaded, " << data.size() << " bytes from " << favicon_url;
  448. auto decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  449. auto bitmap = decoder->bitmap();
  450. if (!bitmap) {
  451. dbg() << "Could not decode favicon " << favicon_url;
  452. return;
  453. }
  454. dbg() << "Decoded favicon, " << bitmap->size();
  455. if (on_favicon_change)
  456. on_favicon_change(*bitmap);
  457. });
  458. }
  459. this->scroll_to_top();
  460. }
  461. void PageView::load_error_page(const URL& failed_url, const String& error)
  462. {
  463. auto error_page_url = "file:///res/html/error.html";
  464. ResourceLoader::the().load(
  465. error_page_url,
  466. [this, failed_url, error](auto data, auto&) {
  467. ASSERT(!data.is_null());
  468. auto html = String::format(
  469. String::copy(data).characters(),
  470. escape_html_entities(failed_url.to_string()).characters(),
  471. escape_html_entities(error).characters());
  472. auto document = parse_html_document(html, failed_url);
  473. ASSERT(document);
  474. set_document(document);
  475. if (on_title_change)
  476. on_title_change(document->title());
  477. },
  478. [](auto error) {
  479. dbg() << "Failed to load error page: " << error;
  480. ASSERT_NOT_REACHED();
  481. });
  482. }
  483. const LayoutDocument* PageView::layout_root() const
  484. {
  485. return document() ? document()->layout_node() : nullptr;
  486. }
  487. LayoutDocument* PageView::layout_root()
  488. {
  489. if (!document())
  490. return nullptr;
  491. return const_cast<LayoutDocument*>(document()->layout_node());
  492. }
  493. void PageView::scroll_to_anchor(const StringView& name)
  494. {
  495. if (!document())
  496. return;
  497. const auto* element = document()->get_element_by_id(name);
  498. if (!element) {
  499. auto candidates = document()->get_elements_by_name(name);
  500. for (auto* candidate : candidates) {
  501. if (is<HTMLAnchorElement>(*candidate)) {
  502. element = to<HTMLAnchorElement>(candidate);
  503. break;
  504. }
  505. }
  506. }
  507. if (!element) {
  508. dbg() << "PageView::scroll_to_anchor(): Anchor not found: '" << name << "'";
  509. return;
  510. }
  511. if (!element->layout_node()) {
  512. dbg() << "PageView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'";
  513. return;
  514. }
  515. auto& layout_node = *element->layout_node();
  516. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)visible_content_rect().width(), (float)visible_content_rect().height() } };
  517. scroll_into_view(enclosing_int_rect(float_rect), true, true);
  518. window()->set_override_cursor(GUI::StandardCursor::None);
  519. }
  520. Document* PageView::document()
  521. {
  522. return main_frame().document();
  523. }
  524. const Document* PageView::document() const
  525. {
  526. return main_frame().document();
  527. }
  528. void PageView::dump_selection(const char* event_name)
  529. {
  530. UNUSED_PARAM(event_name);
  531. #ifdef SELECTION_DEBUG
  532. dbg() << event_name << " selection start: "
  533. << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
  534. << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
  535. #endif
  536. }
  537. void PageView::did_scroll()
  538. {
  539. main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  540. }
  541. Gfx::Point PageView::compute_mouse_event_offset(const Gfx::Point& event_position, const LayoutNode& layout_node) const
  542. {
  543. auto content_event_position = to_content_position(event_position);
  544. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  545. return {
  546. content_event_position.x() - static_cast<int>(top_left_of_layout_node.x()),
  547. content_event_position.y() - static_cast<int>(top_left_of_layout_node.y())
  548. };
  549. }
  550. void PageView::run_javascript_url(const String& url)
  551. {
  552. ASSERT(url.starts_with("javascript:"));
  553. if (!document())
  554. return;
  555. auto source = url.substring_view(11, url.length() - 11);
  556. dbg() << "running js from url: _" << source << "_";
  557. document()->run_javascript(source);
  558. }
  559. void PageView::drop_event(GUI::DropEvent& event)
  560. {
  561. if (event.mime_data().has_urls()) {
  562. if (on_url_drop) {
  563. on_url_drop(event.mime_data().urls().first());
  564. return;
  565. }
  566. }
  567. ScrollableWidget::drop_event(event);
  568. }
  569. }