HtmlView.cpp 22 KB

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