DocumentLoading.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Debug.h>
  9. #include <AK/LexicalPath.h>
  10. #include <LibGemini/Document.h>
  11. #include <LibGfx/ImageFormats/ImageDecoder.h>
  12. #include <LibMarkdown/Document.h>
  13. #include <LibTextCodec/Decoder.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOM/DocumentLoading.h>
  16. #include <LibWeb/HTML/HTMLHeadElement.h>
  17. #include <LibWeb/HTML/Navigable.h>
  18. #include <LibWeb/HTML/NavigationParams.h>
  19. #include <LibWeb/HTML/Parser/HTMLEncodingDetection.h>
  20. #include <LibWeb/HTML/Parser/HTMLParser.h>
  21. #include <LibWeb/Loader/GeneratedPagesLoader.h>
  22. #include <LibWeb/Namespace.h>
  23. #include <LibWeb/XML/XMLDocumentBuilder.h>
  24. namespace Web {
  25. static bool build_markdown_document(DOM::Document& document, ByteBuffer const& data)
  26. {
  27. auto markdown_document = Markdown::Document::parse(data);
  28. if (!markdown_document)
  29. return false;
  30. auto extra_head_contents = R"~~~(
  31. <style>
  32. .zoomable {
  33. cursor: zoom-in;
  34. max-width: 100%;
  35. }
  36. .zoomable.zoomed-in {
  37. cursor: zoom-out;
  38. max-width: none;
  39. }
  40. </style>
  41. <script>
  42. function imageClickEventListener(event) {
  43. let image = event.target;
  44. if (image.classList.contains("zoomable")) {
  45. image.classList.toggle("zoomed-in");
  46. }
  47. }
  48. function processImages() {
  49. let images = document.querySelectorAll("img");
  50. let windowWidth = window.innerWidth;
  51. images.forEach((image) => {
  52. if (image.naturalWidth > windowWidth) {
  53. image.classList.add("zoomable");
  54. } else {
  55. image.classList.remove("zoomable");
  56. image.classList.remove("zoomed-in");
  57. }
  58. image.addEventListener("click", imageClickEventListener);
  59. });
  60. }
  61. document.addEventListener("load", () => {
  62. processImages();
  63. });
  64. window.addEventListener("resize", () => {
  65. processImages();
  66. });
  67. </script>
  68. )~~~"sv;
  69. auto parser = HTML::HTMLParser::create(document, markdown_document->render_to_html(extra_head_contents), "utf-8");
  70. parser->run(document.url());
  71. return true;
  72. }
  73. static bool build_gemini_document(DOM::Document& document, ByteBuffer const& data)
  74. {
  75. StringView gemini_data { data };
  76. auto gemini_document = Gemini::Document::parse(gemini_data, document.url());
  77. ByteString html_data = gemini_document->render_to_html();
  78. dbgln_if(GEMINI_DEBUG, "Gemini data:\n\"\"\"{}\"\"\"", gemini_data);
  79. dbgln_if(GEMINI_DEBUG, "Converted to HTML:\n\"\"\"{}\"\"\"", html_data);
  80. auto parser = HTML::HTMLParser::create(document, html_data, "utf-8");
  81. parser->run(document.url());
  82. return true;
  83. }
  84. bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optional<String> content_encoding)
  85. {
  86. Optional<TextCodec::Decoder&> decoder;
  87. // The actual HTTP headers and other metadata, not the headers as mutated or implied by the algorithms given in this specification,
  88. // are the ones that must be used when determining the character encoding according to the rules given in the above specifications.
  89. if (content_encoding.has_value())
  90. decoder = TextCodec::decoder_for(*content_encoding);
  91. if (!decoder.has_value()) {
  92. auto encoding = HTML::run_encoding_sniffing_algorithm(document, data);
  93. decoder = TextCodec::decoder_for(encoding);
  94. }
  95. VERIFY(decoder.has_value());
  96. // Well-formed XML documents contain only properly encoded characters
  97. if (!decoder->validate(data))
  98. return false;
  99. auto source = decoder->to_utf8(data).release_value_but_fixme_should_propagate_errors();
  100. XML::Parser parser(source, { .resolve_external_resource = resolve_xml_resource });
  101. XMLDocumentBuilder builder { document };
  102. auto result = parser.parse_with_listener(builder);
  103. return !result.is_error() && !builder.has_error();
  104. }
  105. bool parse_document(DOM::Document& document, ByteBuffer const& data, [[maybe_unused]] Optional<String> content_encoding)
  106. {
  107. auto& mime_type = document.content_type();
  108. if (mime_type == "text/markdown")
  109. return build_markdown_document(document, data);
  110. if (mime_type == "text/gemini")
  111. return build_gemini_document(document, data);
  112. return false;
  113. }
  114. static bool is_supported_document_mime_type(StringView mime_type)
  115. {
  116. if (mime_type == "text/html")
  117. return true;
  118. if (mime_type.ends_with("+xml"sv) || mime_type.is_one_of("text/xml", "application/xml"))
  119. return true;
  120. if (mime_type.starts_with("image/"sv))
  121. return true;
  122. if (mime_type.starts_with("video/"sv))
  123. return true;
  124. if (mime_type.starts_with("audio/"sv))
  125. return true;
  126. if (mime_type == "text/plain" || mime_type == "application/json")
  127. return true;
  128. if (mime_type == "text/markdown")
  129. return true;
  130. if (mime_type == "text/gemini")
  131. return true;
  132. return false;
  133. }
  134. // https://html.spec.whatwg.org/multipage/document-lifecycle.html#navigate-html
  135. static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOM::Document>> load_html_document(HTML::NavigationParams& navigation_params)
  136. {
  137. // To load an HTML document, given navigation params navigationParams:
  138. // 1. Let document be the result of creating and initializing a Document object given "html", "text/html", and navigationParams.
  139. auto document = TRY(DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html"_string, navigation_params));
  140. // 2. If document's URL is about:blank, then populate with html/head/body given document.
  141. // FIXME: The additional check for a non-empty body fixes issues with loading javascript urls in iframes, which
  142. // default to an "about:blank" url. Is this a spec bug?
  143. if (document->url_string() == "about:blank"_string
  144. && navigation_params.response->body()->length().value_or(0) == 0) {
  145. TRY(document->populate_with_html_head_and_body());
  146. // Nothing else is added to the document, so mark it as loaded.
  147. HTML::HTMLParser::the_end(document);
  148. }
  149. // 3. Otherwise, create an HTML parser and associate it with the document.
  150. // Each task that the networking task source places on the task queue while fetching runs must then fill the
  151. // parser's input byte stream with the fetched bytes and cause the HTML parser to perform the appropriate
  152. // processing of the input stream.
  153. // The first task that the networking task source places on the task queue while fetching runs must process link
  154. // headers given document, navigationParams's response, and "media", after the task has been processed by the
  155. // HTML parser.
  156. // Before any script execution occurs, the user agent must wait for scripts may run for the newly-created
  157. // document to be true for document.
  158. // When no more bytes are available, the user agent must queue a global task on the networking task source given
  159. // document's relevant global object to have the parser to process the implied EOF character, which eventually
  160. // causes a load event to be fired.
  161. else {
  162. // FIXME: Parse as we receive the document data, instead of waiting for the whole document to be fetched first.
  163. auto process_body = [document, url = navigation_params.response->url().value()](ByteBuffer data) {
  164. auto parser = HTML::HTMLParser::create_with_uncertain_encoding(document, data);
  165. parser->run(url);
  166. };
  167. auto process_body_error = [](auto) {
  168. dbgln("FIXME: Load html page with an error if read of body failed.");
  169. };
  170. auto& realm = document->realm();
  171. TRY(navigation_params.response->body()->fully_read(realm, move(process_body), move(process_body_error), JS::NonnullGCPtr { realm.global_object() }));
  172. }
  173. // 4. Return document.
  174. return document;
  175. }
  176. // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-xml
  177. static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOM::Document>> load_xml_document(HTML::NavigationParams& navigation_params, MimeSniff::MimeType type)
  178. {
  179. // When faced with displaying an XML file inline, provided navigation params navigationParams and a string type, user agents
  180. // must follow the requirements defined in XML and Namespaces in XML, XML Media Types, DOM, and other relevant specifications
  181. // to create and initialize a Document object document, given "xml", type, and navigationParams, and return that Document.
  182. // They must also create a corresponding XML parser. [XML] [XMLNS] [RFC7303] [DOM]
  183. //
  184. // Note: At the time of writing, the XML specification community had not actually yet specified how XML and the DOM interact.
  185. //
  186. // The first task that the networking task source places on the task queue while fetching runs must process link headers
  187. // given document, navigationParams's response, and "media", after the task has been processed by the XML parser.
  188. //
  189. // The actual HTTP headers and other metadata, not the headers as mutated or implied by the algorithms given in this
  190. // specification, are the ones that must be used when determining the character encoding according to the rules given in the
  191. // above specifications. Once the character encoding is established, the document's character encoding must be set to that
  192. // character encoding.
  193. //
  194. // Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be
  195. // true for the newly-created Document.
  196. //
  197. // Once parsing is complete, the user agent must set document's during-loading navigation ID for WebDriver BiDi to null.
  198. //
  199. // Note: For HTML documents this is reset when parsing is complete, after firing the load event.
  200. //
  201. // Error messages from the parse process (e.g., XML namespace well-formedness errors) may be reported inline by mutating
  202. // the Document.
  203. // FIXME: Actually follow the spec! This is just the ad-hoc code we had before, modified somewhat.
  204. auto document = TRY(DOM::Document::create_and_initialize(DOM::Document::Type::XML, "application/xhtml+xml"_string, navigation_params));
  205. Optional<String> content_encoding;
  206. if (auto maybe_encoding = type.parameters().get("charset"sv); maybe_encoding.has_value())
  207. content_encoding = maybe_encoding.value();
  208. auto process_body = [document, url = navigation_params.response->url().value(), content_encoding = move(content_encoding)](ByteBuffer data) {
  209. Optional<TextCodec::Decoder&> decoder;
  210. // The actual HTTP headers and other metadata, not the headers as mutated or implied by the algorithms given in this specification,
  211. // are the ones that must be used when determining the character encoding according to the rules given in the above specifications.
  212. if (content_encoding.has_value())
  213. decoder = TextCodec::decoder_for(*content_encoding);
  214. if (!decoder.has_value()) {
  215. auto encoding = HTML::run_encoding_sniffing_algorithm(document, data);
  216. decoder = TextCodec::decoder_for(encoding);
  217. }
  218. VERIFY(decoder.has_value());
  219. // Well-formed XML documents contain only properly encoded characters
  220. if (!decoder->validate(data)) {
  221. // FIXME: Insert error message into the document.
  222. dbgln("XML Document contains improperly-encoded characters");
  223. return;
  224. }
  225. auto source = decoder->to_utf8(data);
  226. if (source.is_error()) {
  227. // FIXME: Insert error message into the document.
  228. dbgln("Failed to decode XML document: {}", source.error());
  229. return;
  230. }
  231. XML::Parser parser(source.value(), { .resolve_external_resource = resolve_xml_resource });
  232. XMLDocumentBuilder builder { document };
  233. auto result = parser.parse_with_listener(builder);
  234. if (result.is_error()) {
  235. // FIXME: Insert error message into the document.
  236. dbgln("Failed to parse XML document: {}", result.error());
  237. }
  238. };
  239. auto process_body_error = [](auto) {
  240. dbgln("FIXME: Load html page with an error if read of body failed.");
  241. };
  242. auto& realm = document->realm();
  243. TRY(navigation_params.response->body()->fully_read(realm, move(process_body), move(process_body_error), JS::NonnullGCPtr { realm.global_object() }));
  244. return document;
  245. }
  246. // https://html.spec.whatwg.org/multipage/document-lifecycle.html#navigate-text
  247. static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOM::Document>> load_text_document(HTML::NavigationParams& navigation_params, MimeSniff::MimeType type)
  248. {
  249. // To load a text document, given a navigation params navigationParams and a string type:
  250. // 1. Let document be the result of creating and initializing a Document object given "html", type, and navigationParams.
  251. auto document = TRY(DOM::Document::create_and_initialize(DOM::Document::Type::XML, type.essence(), navigation_params));
  252. // FIXME: 2. Set document's parser cannot change the mode flag to true.
  253. // 3. Set document's mode to "no-quirks".
  254. document->set_quirks_mode(DOM::QuirksMode::No);
  255. // 4. Create an HTML parser and associate it with the document. Act as if the tokenizer had emitted a start tag token
  256. // with the tag name "pre" followed by a single U+000A LINE FEED (LF) character, and switch the HTML parser's tokenizer
  257. // to the PLAINTEXT state. Each task that the networking task source places on the task queue while fetching runs must
  258. // then fill the parser's input byte stream with the fetched bytes and cause the HTML parser to perform the appropriate
  259. // processing of the input stream.
  260. // document's encoding must be set to the character encoding used to decode the document during parsing.
  261. // The first task that the networking task source places on the task queue while fetching runs must process link
  262. // headers given document, navigationParams's response, and "media", after the task has been processed by the HTML parser.
  263. // Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be
  264. // true for document.
  265. // When no more bytes are available, the user agent must queue a global task on the networking task source given
  266. // document's relevant global object to have the parser to process the implied EOF character, which eventually causes a
  267. // load event to be fired.
  268. // FIXME: Parse as we receive the document data, instead of waiting for the whole document to be fetched first.
  269. auto process_body = [document, url = navigation_params.response->url().value()](ByteBuffer data) {
  270. auto encoding = run_encoding_sniffing_algorithm(document, data);
  271. dbgln_if(HTML_PARSER_DEBUG, "The encoding sniffing algorithm returned encoding '{}'", encoding);
  272. auto parser = HTML::HTMLParser::create_for_scripting(document);
  273. parser->tokenizer().update_insertion_point();
  274. parser->tokenizer().insert_input_at_insertion_point("<pre>\n"sv);
  275. parser->run();
  276. parser->tokenizer().switch_to(HTML::HTMLTokenizer::State::PLAINTEXT);
  277. parser->tokenizer().insert_input_at_insertion_point(data);
  278. parser->tokenizer().insert_eof();
  279. parser->run(url);
  280. document->set_encoding(MUST(String::from_byte_string(encoding)));
  281. // 5. User agents may add content to the head element of document, e.g., linking to a style sheet, providing
  282. // script, or giving the document a title.
  283. auto title = MUST(String::from_byte_string(LexicalPath::basename(url.to_byte_string())));
  284. auto title_element = MUST(DOM::create_element(document, HTML::TagNames::title, Namespace::HTML));
  285. MUST(document->head()->append_child(title_element));
  286. auto title_text = document->heap().allocate<DOM::Text>(document->realm(), document, title);
  287. MUST(title_element->append_child(*title_text));
  288. };
  289. auto process_body_error = [](auto) {
  290. dbgln("FIXME: Load html page with an error if read of body failed.");
  291. };
  292. auto& realm = document->realm();
  293. TRY(navigation_params.response->body()->fully_read(realm, move(process_body), move(process_body_error), JS::NonnullGCPtr { realm.global_object() }));
  294. // 6. Return document.
  295. return document;
  296. }
  297. // https://html.spec.whatwg.org/multipage/document-lifecycle.html#navigate-media
  298. static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOM::Document>> load_media_document(HTML::NavigationParams& navigation_params, MimeSniff::MimeType type)
  299. {
  300. // To load a media document, given navigationParams and a string type:
  301. // 1. Let document be the result of creating and initializing a Document object given "html", type, and navigationParams.
  302. auto document = TRY(DOM::Document::create_and_initialize(DOM::Document::Type::XML, type.essence(), navigation_params));
  303. // 2. Set document's mode to "no-quirks".
  304. document->set_quirks_mode(DOM::QuirksMode::No);
  305. // 3. Populate with html/head/body given document.
  306. TRY(document->populate_with_html_head_and_body());
  307. // 4. Append an element host element for the media, as described below, to the body element.
  308. // 5. Set the appropriate attribute of the element host element, as described below, to the address of the image,
  309. // video, or audio resource.
  310. // 6. User agents may add content to the head element of document, or attributes to host element, e.g., to link
  311. // to a style sheet, to provide a script, to give the document a title, or to make the media autoplay.
  312. auto insert_title = [](auto& document, auto title) -> WebIDL::ExceptionOr<void> {
  313. auto title_element = TRY(DOM::create_element(document, HTML::TagNames::title, Namespace::HTML));
  314. TRY(document->head()->append_child(title_element));
  315. auto title_text = document->heap().template allocate<DOM::Text>(document->realm(), document, title);
  316. TRY(title_element->append_child(*title_text));
  317. return {};
  318. };
  319. auto url_string = document->url_string();
  320. if (type.is_image()) {
  321. auto img_element = TRY(DOM::create_element(document, HTML::TagNames::img, Namespace::HTML));
  322. TRY(img_element->set_attribute(HTML::AttributeNames::src, url_string));
  323. TRY(document->body()->append_child(img_element));
  324. TRY(insert_title(document, MUST(String::from_byte_string(LexicalPath::basename(url_string.to_byte_string())))));
  325. } else if (type.type() == "video"sv) {
  326. auto video_element = TRY(DOM::create_element(document, HTML::TagNames::video, Namespace::HTML));
  327. TRY(video_element->set_attribute(HTML::AttributeNames::src, url_string));
  328. TRY(video_element->set_attribute(HTML::AttributeNames::autoplay, String {}));
  329. TRY(video_element->set_attribute(HTML::AttributeNames::controls, String {}));
  330. TRY(document->body()->append_child(video_element));
  331. TRY(insert_title(document, MUST(String::from_byte_string(LexicalPath::basename(url_string.to_byte_string())))));
  332. } else if (type.type() == "audio"sv) {
  333. auto audio_element = TRY(DOM::create_element(document, HTML::TagNames::audio, Namespace::HTML));
  334. TRY(audio_element->set_attribute(HTML::AttributeNames::src, url_string));
  335. TRY(audio_element->set_attribute(HTML::AttributeNames::autoplay, String {}));
  336. TRY(audio_element->set_attribute(HTML::AttributeNames::controls, String {}));
  337. TRY(document->body()->append_child(audio_element));
  338. TRY(insert_title(document, MUST(String::from_byte_string(LexicalPath::basename(url_string.to_byte_string())))));
  339. } else {
  340. // FIXME: According to https://mimesniff.spec.whatwg.org/#audio-or-video-mime-type we might have to deal with
  341. // "application/ogg" and figure out whether it's audio or video.
  342. VERIFY_NOT_REACHED();
  343. }
  344. // FIXME: 7. Process link headers given document, navigationParams's response, and "media".
  345. // 8. Act as if the user agent had stopped parsing document.
  346. // FIXME: We should not need to force the media file to load before saying that parsing has completed!
  347. // However, if we don't, then we get stuck in HTMLParser::the_end() waiting for the media file to load, which
  348. // never happens.
  349. auto& realm = document->realm();
  350. TRY(navigation_params.response->body()->fully_read(
  351. realm,
  352. [document](auto) { HTML::HTMLParser::the_end(document); },
  353. [](auto) {},
  354. JS::NonnullGCPtr { realm.global_object() }));
  355. // 9. Return document.
  356. return document;
  357. // The element host element to create for the media is the element given in the table below in the second cell of
  358. // the row whose first cell describes the media. The appropriate attribute to set is the one given by the third cell
  359. // in that same row.
  360. // Type of media | Element for the media | Appropriate attribute
  361. // -------------------------------------------------------------
  362. // Image | img | src
  363. // Video | video | src
  364. // Audio | audio | src
  365. // Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to
  366. // be true for the Document.
  367. }
  368. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#loading-a-document
  369. JS::GCPtr<DOM::Document> load_document(HTML::NavigationParams navigation_params)
  370. {
  371. // To load a document given navigation params navigationParams, source snapshot params sourceSnapshotParams,
  372. // and origin initiatorOrigin, perform the following steps. They return a Document or null.
  373. // 1. Let type be the computed type of navigationParams's response.
  374. auto extracted_mime_type = navigation_params.response->header_list()->extract_mime_type().release_value_but_fixme_should_propagate_errors();
  375. if (!extracted_mime_type.has_value())
  376. return nullptr;
  377. auto type = extracted_mime_type.release_value();
  378. VERIFY(navigation_params.response->body());
  379. // 2. If the user agent has been configured to process resources of the given type using some mechanism other than
  380. // rendering the content in a navigable, then skip this step.
  381. // Otherwise, if the type is one of the following types:
  382. // -> an HTML MIME type
  383. if (type.is_html()) {
  384. // Return the result of loading an HTML document, given navigationParams.
  385. return load_html_document(navigation_params).release_value_but_fixme_should_propagate_errors();
  386. }
  387. // -> an XML MIME type that is not an explicitly supported XML MIME type
  388. // FIXME: that is not an explicitly supported XML MIME type
  389. if (type.is_xml()) {
  390. // Return the result of loading an XML document given navigationParams and type.
  391. return load_xml_document(navigation_params, type).release_value_but_fixme_should_propagate_errors();
  392. }
  393. // -> a JavaScript MIME type
  394. // -> a JSON MIME type that is not an explicitly supported JSON MIME type
  395. // -> "text/css"
  396. // -> "text/plain"
  397. // -> "text/vtt"
  398. if (type.is_javascript()
  399. || type.is_json()
  400. || type.essence() == "text/css"_string
  401. || type.essence() == "text/plain"_string
  402. || type.essence() == "text/vtt"_string) {
  403. // Return the result of loading a text document given navigationParams and type.
  404. return load_text_document(navigation_params, type).release_value_but_fixme_should_propagate_errors();
  405. }
  406. // -> "multipart/x-mixed-replace"
  407. if (type.essence() == "multipart/x-mixed-replace"_string) {
  408. // FIXME: Return the result of loading a multipart/x-mixed-replace document, given navigationParams,
  409. // sourceSnapshotParams, and initiatorOrigin.
  410. }
  411. // -> A supported image, video, or audio type
  412. if (type.is_image()
  413. || type.is_audio_or_video()) {
  414. // Return the result of loading a media document given navigationParams and type.
  415. return load_media_document(navigation_params, type).release_value_but_fixme_should_propagate_errors();
  416. }
  417. // -> "application/pdf"
  418. // -> "text/pdf"
  419. if (type.essence() == "application/pdf"_string
  420. || type.essence() == "text/pdf"_string) {
  421. // FIXME: If the user agent's PDF viewer supported is true, return the result of creating a document for inline
  422. // content that doesn't have a DOM given navigationParams's navigable.
  423. }
  424. // Otherwise, proceed onward.
  425. // FIXME: 3. If, given type, the new resource is to be handled by displaying some sort of inline content, e.g., a
  426. // native rendering of the content or an error message because the specified type is not supported, then
  427. // return the result of creating a document for inline content that doesn't have a DOM given navigationParams's
  428. // navigable, navigationParams's id, and navigationParams's navigation timing type.
  429. // FIXME: 4. Otherwise, the document's type is such that the resource will not affect navigationParams's navigable,
  430. // e.g., because the resource is to be handed to an external application or because it is an unknown type
  431. // that will be processed as a download. Hand-off to external software given navigationParams's response,
  432. // navigationParams's navigable, navigationParams's final sandboxing flag set, sourceSnapshotParams's has
  433. // transient activation, and initiatorOrigin.
  434. // FIXME: Start of old, ad-hoc code
  435. if (!is_supported_document_mime_type(type.essence()))
  436. return nullptr;
  437. auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html"_string, navigation_params).release_value_but_fixme_should_propagate_errors();
  438. document->set_content_type(type.essence());
  439. auto& realm = document->realm();
  440. if (navigation_params.response->body()) {
  441. Optional<String> content_encoding = type.parameters().get("charset"sv);
  442. auto process_body = [document, url = navigation_params.response->url().value(), encoding = move(content_encoding)](ByteBuffer bytes) {
  443. if (parse_document(*document, bytes, move(encoding)))
  444. return;
  445. document->remove_all_children(true);
  446. auto error_html = load_error_page(url).release_value_but_fixme_should_propagate_errors();
  447. auto parser = HTML::HTMLParser::create(document, error_html, "utf-8");
  448. document->set_url(AK::URL("about:error"));
  449. parser->run();
  450. };
  451. auto process_body_error = [](auto) {
  452. dbgln("FIXME: Load html page with an error if read of body failed.");
  453. };
  454. navigation_params.response->body()->fully_read(
  455. realm,
  456. move(process_body),
  457. move(process_body_error),
  458. JS::NonnullGCPtr { realm.global_object() })
  459. .release_value_but_fixme_should_propagate_errors();
  460. }
  461. return document;
  462. // FIXME: End of old, ad-hoc code
  463. // 5. Return null.
  464. return nullptr;
  465. }
  466. // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-ua-inline
  467. JS::GCPtr<DOM::Document> create_document_for_inline_content(JS::GCPtr<HTML::Navigable> navigable, Optional<String> navigation_id, StringView content_html)
  468. {
  469. auto& vm = navigable->vm();
  470. // 1. Let origin be a new opaque origin.
  471. HTML::Origin origin {};
  472. // 2. Let coop be a new cross-origin opener policy.
  473. auto coop = HTML::CrossOriginOpenerPolicy {};
  474. // 3. Let coopEnforcementResult be a new cross-origin opener policy enforcement result with
  475. // url: response's URL
  476. // origin: origin
  477. // cross-origin opener policy: coop
  478. HTML::CrossOriginOpenerPolicyEnforcementResult coop_enforcement_result {
  479. .url = AK::URL("about:error"), // AD-HOC
  480. .origin = origin,
  481. .cross_origin_opener_policy = coop
  482. };
  483. // 4. Let navigationParams be a new navigation params with
  484. // id: navigationId
  485. // navigable: navigable
  486. // request: null
  487. // response: a new response
  488. // origin: origin
  489. // fetch controller: null
  490. // commit early hints: null
  491. // COOP enforcement result: coopEnforcementResult
  492. // reserved environment: null
  493. // policy container: a new policy container
  494. // final sandboxing flag set: an empty set
  495. // cross-origin opener policy: coop
  496. // FIXME: navigation timing type: navTimingType
  497. // about base URL: null
  498. auto response = Fetch::Infrastructure::Response::create(vm);
  499. response->url_list().append(AK::URL("about:error")); // AD-HOC: https://github.com/whatwg/html/issues/9122
  500. HTML::NavigationParams navigation_params {
  501. .id = navigation_id,
  502. .navigable = navigable,
  503. .request = {},
  504. .response = *response,
  505. .fetch_controller = nullptr,
  506. .commit_early_hints = nullptr,
  507. .coop_enforcement_result = move(coop_enforcement_result),
  508. .reserved_environment = {},
  509. .origin = move(origin),
  510. .policy_container = HTML::PolicyContainer {},
  511. .final_sandboxing_flag_set = HTML::SandboxingFlagSet {},
  512. .cross_origin_opener_policy = move(coop),
  513. .about_base_url = {},
  514. };
  515. // 5. Let document be the result of creating and initializing a Document object given "html", "text/html", and navigationParams.
  516. auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html"_string, navigation_params).release_value_but_fixme_should_propagate_errors();
  517. // 6. Either associate document with a custom rendering that is not rendered using the normal Document rendering rules, or mutate document until it represents the content the
  518. // user agent wants to render.
  519. auto parser = HTML::HTMLParser::create(document, content_html, "utf-8");
  520. document->set_url(AK::URL("about:error"));
  521. parser->run();
  522. // 7. Return document.
  523. return document;
  524. }
  525. }