Skip to content

Truly independent web browser

LibWeb: Wrap long lines in text documents, and follow dark mode

sideshowbarkerd3a9bd05d525
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cpp
index 3e9f8e2d21f..cb91cb41c52 100644
--- a/Libraries/LibWeb/DOM/DocumentLoading.cpp
+++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp
@@ -31,6 +31,7 @@
#include <LibWeb/HTML/HTMLHeadElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLLinkElement.h>
+#include <LibWeb/HTML/HTMLPreElement.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/HTML/NavigationParams.h>
#include <LibWeb/HTML/Parser/HTMLEncodingDetection.h>
@@ -306,9 +307,21 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_text_document(HTML::Navi
auto parser = HTML::HTMLParser::create_with_open_input_stream(document);
parser->tokenizer().update_insertion_point();
- parser->tokenizer().insert_input_at_insertion_point(u"<pre>\n"sv);
+ // NB: The meta element goes to the head, which step 5 below allows, so the text follows the user's color
+ // scheme.
+ parser->tokenizer().insert_input_at_insertion_point(u"<meta name=\"color-scheme\" content=\"light dark\"><pre>\n"sv);
parser->run();
+ // AD-HOC: The spec acts as if the tokenizer had emitted a bare "pre" start tag. We give the pre element a
+ // style that wraps long lines, so a line of text never runs off the side of the page. The
+ // Content-Security-Policy of the response must not block that style, so set it directly rather than
+ // through a style attribute.
+ if (auto* pre = document->body() ? document->body()->template first_child_of_type<HTML::HTMLPreElement>() : nullptr) {
+ auto style = CSS::CSSStyleProperties::create_element_inline_style({ *pre });
+ style->set_declarations_from_text(u"word-wrap: break-word; white-space: pre-wrap;"sv);
+ pre->set_inline_style(style);
+ }
+
parser->tokenizer().switch_to(HTML::HTMLTokenizer::State::PLAINTEXT);
auto decoder = TextCodec::decoder_for(encoding);
VERIFY(decoder.has_value());
@@ -322,7 +335,8 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_text_document(HTML::Navi
// 5. User agents may add content to the head element of document, e.g., linking to a style sheet, providing
// script, or giving the document a title.
- // NB: Leave the head empty so the document text consists only of the response body.
+ // NB: The head holds only the color-scheme meta element added above, so the document text consists only of
+ // the response body.
});
if (document->ready_to_run_scripts()) {
run_text_parser->function()();
diff --git a/Tests/LibWeb/Text/data/text-document-csp.txt b/Tests/LibWeb/Text/data/text-document-csp.txt
new file mode 100644
index 00000000000..da0875b775d
--- /dev/null
+++ b/Tests/LibWeb/Text/data/text-document-csp.txt
@@ -0,0 +1 @@
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
diff --git a/Tests/LibWeb/Text/data/text-document-csp.txt.headers b/Tests/LibWeb/Text/data/text-document-csp.txt.headers
new file mode 100644
index 00000000000..8110a2dd076
--- /dev/null
+++ b/Tests/LibWeb/Text/data/text-document-csp.txt.headers
@@ -0,0 +1,2 @@
+Content-Type: text/plain
+Content-Security-Policy: style-src 'none'
diff --git a/Tests/LibWeb/Text/expected/HTML/text-document-wraps-and-follows-color-scheme.txt b/Tests/LibWeb/Text/expected/HTML/text-document-wraps-and-follows-color-scheme.txt
new file mode 100644
index 00000000000..73e6f74cb58
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/text-document-wraps-and-follows-color-scheme.txt
@@ -0,0 +1,6 @@
+color-scheme meta: light dark
+head text: ""
+pre white-space: preserve wrap
+pre overflow-wrap: break-word
+text kept: true
+fits the frame: true
diff --git a/Tests/LibWeb/Text/expected/HTML/text-document-wraps-under-csp.txt b/Tests/LibWeb/Text/expected/HTML/text-document-wraps-under-csp.txt
new file mode 100644
index 00000000000..d9202294e20
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/text-document-wraps-under-csp.txt
@@ -0,0 +1,4 @@
+pre white-space: preserve wrap
+pre overflow-wrap: break-word
+pre style attribute: null
+fits the frame: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/non-html-documents.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/non-html-documents.txt
index 1c41bcc8023..5b4400075cf 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/non-html-documents.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/non-html-documents.txt
@@ -2,10 +2,9 @@ Harness status: OK
Found 5 tests
-4 Pass
-1 Fail
+5 Pass
Pass Text directive allowed in text/html
Pass Text directive blocked in text/css
Pass Text directive blocked in text/javascript
-Fail Text directive allowed in text/plain
+Pass Text directive allowed in text/plain
Pass Text directive blocked in application/xml
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/text-document-wraps-and-follows-color-scheme.html b/Tests/LibWeb/Text/input/HTML/text-document-wraps-and-follows-color-scheme.html
new file mode 100644
index 00000000000..6b5e0f45fd1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/text-document-wraps-and-follows-color-scheme.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<iframe id="frame"></iframe>
+<script>
+ asyncTest(done => {
+ const text = "x".repeat(400) + "\nsecond line\n";
+ frame.onload = () => {
+ const doc = frame.contentDocument;
+ const meta = doc.head.querySelector('meta[name="color-scheme"]');
+ println(`color-scheme meta: ${meta ? meta.content : "none"}`);
+ println(`head text: ${JSON.stringify(doc.head.textContent)}`);
+ const pre = doc.body.firstChild;
+ const style = doc.defaultView.getComputedStyle(pre);
+ println(`pre white-space: ${style.whiteSpaceCollapse} ${style.textWrapMode}`);
+ println(`pre overflow-wrap: ${style.overflowWrap}`);
+ println(`text kept: ${pre.textContent === text}`);
+ println(`fits the frame: ${pre.scrollWidth <= doc.documentElement.clientWidth}`);
+ done();
+ };
+ frame.src = URL.createObjectURL(new Blob([text], { type: "text/plain" }));
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/text-document-wraps-under-csp.html b/Tests/LibWeb/Text/input/HTML/text-document-wraps-under-csp.html
new file mode 100644
index 00000000000..e7fc7915382
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/text-document-wraps-under-csp.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<iframe id="frame" src="../../data/text-document-csp.txt"></iframe>
+<script>
+ asyncTest(done => {
+ frame.onload = () => {
+ const doc = frame.contentDocument;
+ const pre = doc.body.firstChild;
+ const style = doc.defaultView.getComputedStyle(pre);
+ println(`pre white-space: ${style.whiteSpaceCollapse} ${style.textWrapMode}`);
+ println(`pre overflow-wrap: ${style.overflowWrap}`);
+ println(`pre style attribute: ${pre.getAttribute("style")}`);
+ println(`fits the frame: ${pre.scrollWidth <= doc.documentElement.clientWidth}`);
+ done();
+ };
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/text-document-wraps-under-csp.html.headers b/Tests/LibWeb/Text/input/HTML/text-document-wraps-under-csp.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/text-document-wraps-under-csp.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html