Skip to content

Truly independent web browser

LibWeb: Keep iframe srcdoc documents out of quirks mode

Andreas Klinga9765331d26b
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index 8fec30ce627..94980866113 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -68,6 +68,7 @@
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/SVG/SVGDecodedImageData.h>
#include <LibWeb/SVG/SVGScriptElement.h>
+#include <LibWebCommon/HTML/BrowsingContext.h>
#include <LibWebCommon/Infra/CharacterTypes.h>
#include <LibWebCommon/Infra/Strings.h>
@@ -176,6 +177,7 @@ extern "C" void ladybird_html_parser_visit_node(void*, size_t);
extern "C" size_t ladybird_html_parser_document_node(void*);
extern "C" size_t ladybird_html_parser_document_html_element(void*);
extern "C" void ladybird_html_parser_set_document_quirks_mode(void*, RustFfiHtmlQuirksMode);
+extern "C" bool ladybird_html_parser_document_is_iframe_srcdoc_document(void*);
extern "C" size_t ladybird_html_parser_create_document_type(void*, u16 const*, size_t, u16 const*, size_t, u16 const*, size_t);
extern "C" size_t ladybird_html_parser_create_comment(void*, u16 const*, size_t);
extern "C" void ladybird_html_parser_insert_text(size_t, size_t, u8 const*, size_t);
@@ -2836,6 +2838,15 @@ extern "C" void ladybird_html_parser_set_document_quirks_mode(void* parser, Rust
document.set_quirks_mode(quirks_mode_from_html_parser_ffi(mode));
}
+extern "C" bool ladybird_html_parser_document_is_iframe_srcdoc_document(void* parser)
+{
+ // NB: Only an iframe's srcdoc resource is loaded from about:srcdoc, so a document with that URL is the iframe
+ // srcdoc document made from it.
+ // FIXME: The document stays an iframe srcdoc document for good, but document.open() gives it the URL of the
+ // document that opened it, after which it is no longer recognized here.
+ return url_matches_about_srcdoc(parser_from_html_parser_ffi(parser).document().url());
+}
+
extern "C" size_t ladybird_html_parser_create_document_type(void* parser, u16 const* name_ptr, size_t name_len, u16 const* public_id_ptr, size_t public_id_len, u16 const* system_id_ptr, size_t system_id_len)
{
auto& html_parser = parser_from_html_parser_ffi(parser);
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
index 061285a5702..b5082fc626f 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
@@ -105,6 +105,7 @@ unsafe extern "C" {
fn ladybird_html_parser_document_node(parser: *mut c_void) -> usize;
fn ladybird_html_parser_document_html_element(parser: *mut c_void) -> usize;
fn ladybird_html_parser_set_document_quirks_mode(parser: *mut c_void, mode: RustFfiHtmlQuirksMode);
+ fn ladybird_html_parser_document_is_iframe_srcdoc_document(parser: *mut c_void) -> bool;
fn ladybird_html_parser_log_parse_error(parser: *mut c_void, message_ptr: *const u8, message_len: usize);
fn ladybird_html_parser_stop_parsing(parser: *mut c_void);
fn ladybird_html_parser_parse_errors_enabled() -> bool;
@@ -785,15 +786,25 @@ impl TreeBuilder {
let document_type = self.create_document_type(name, public_id, system_id);
let document = self.document_node();
self.append_child(document, document_type);
- self.set_document_quirks_mode(self.which_quirks_mode(&token));
+
+ // Then, if the document is not an iframe srcdoc document, and the parser cannot change the mode flag is
+ // false, set the Document to quirks or limited-quirks mode where the DOCTYPE token calls for it.
+ if !self.document_is_iframe_srcdoc_document() {
+ self.set_document_quirks_mode(self.which_quirks_mode(&token));
+ }
self.insertion_mode = InsertionMode::BeforeHtml;
return;
}
// -> Anything else
- // FIXME: Expose whether the document is an iframe srcdoc document so this parse error can be conditional.
- self.parse_error("anything else in initial insertion mode");
- self.set_document_quirks_mode(RustFfiHtmlQuirksMode::Yes);
+ // If the document is not an iframe srcdoc document, then this is a parse error; if the parser cannot change the
+ // mode flag is false, set the Document to quirks mode.
+ if !self.document_is_iframe_srcdoc_document() {
+ self.parse_error("anything else in initial insertion mode");
+ self.set_document_quirks_mode(RustFfiHtmlQuirksMode::Yes);
+ }
+
+ // In any case, switch the insertion mode to "before html", then reprocess the token.
self.insertion_mode = InsertionMode::BeforeHtml;
self.process_using_the_rules_for(InsertionMode::BeforeHtml, token);
}
@@ -3862,6 +3873,10 @@ impl TreeBuilder {
unsafe { ladybird_html_parser_set_document_quirks_mode(self.host, mode) }
}
+ fn document_is_iframe_srcdoc_document(&self) -> bool {
+ unsafe { ladybird_html_parser_document_is_iframe_srcdoc_document(self.host) }
+ }
+
// https://html.spec.whatwg.org/multipage/parsing.html#insert-a-comment
fn insert_comment(&mut self, data: &str, insertion_location: Option<AdjustedInsertionLocation>) {
// 1. Let data be the data given in the comment token being processed.
diff --git a/Tests/LibWeb/Text/expected/HTML/iframe-srcdoc-document-is-never-in-quirks-mode.txt b/Tests/LibWeb/Text/expected/HTML/iframe-srcdoc-document-is-never-in-quirks-mode.txt
new file mode 100644
index 00000000000..56df2b39ff2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/iframe-srcdoc-document-is-never-in-quirks-mode.txt
@@ -0,0 +1,3 @@
+no doctype: CSS1Compat
+quirks doctype: CSS1Compat
+DOMParser: BackCompat
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-partial-legacy-font.html b/Tests/LibWeb/Text/input/Editing/rich-paste-partial-legacy-font.html
index c6d1c9dfa5f..663f432445f 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-partial-legacy-font.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-partial-legacy-font.html
@@ -32,7 +32,7 @@
<div><font color="#ff00">Red text and </font><font color="#3399ff">blue text!</font><br></div>
<div><br></div>
<ul><li>A simple list<br></li><li>With two items<br></li></ul><div><br></div>
-<div><img src="../../../Screenshot/data/smiley.png" data-sceditor-emoticon=":)" alt=":)" title=":)"><br></div><div><br></div><div><span style="color: rgb(255, 0, 0);">Red text and</span>Just type <strong>:</strong>) and it will be converted into <img src="../../../Screenshot/data/smiley.png" data-sceditor-emoticon=":)" alt=":)" title=":)"> as you type.</div>
+<div><img src="../../../Screenshot/data/smiley.png" data-sceditor-emoticon=":)" alt=":)" title=":)"><br></div><div><br></div><div><span style="color: rgba(255, 255, 0, 0);">Red text and</span>Just type <strong>:</strong>) and it will be converted into <img src="../../../Screenshot/data/smiley.png" data-sceditor-emoticon=":)" alt=":)" title=":)"> as you type.</div>
`;
function path(node) {
diff --git a/Tests/LibWeb/Text/input/HTML/iframe-srcdoc-document-is-never-in-quirks-mode.html b/Tests/LibWeb/Text/input/HTML/iframe-srcdoc-document-is-never-in-quirks-mode.html
new file mode 100644
index 00000000000..373f488d058
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/iframe-srcdoc-document-is-never-in-quirks-mode.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const srcdocs = {
+ "no doctype": "<p>x</p>",
+ "quirks doctype": '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><p>x</p>',
+ };
+ for (const [name, srcdoc] of Object.entries(srcdocs)) {
+ const iframe = document.createElement("iframe");
+ iframe.srcdoc = srcdoc;
+ const loaded = new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
+ document.body.appendChild(iframe);
+ await loaded;
+
+ // An iframe srcdoc document stays in no-quirks mode whatever its DOCTYPE says, or if it has none.
+ println(`${name}: ${iframe.contentDocument.compatMode}`);
+ }
+
+ // A document parsed from the same markup is not an iframe srcdoc document.
+ println(`DOMParser: ${new DOMParser().parseFromString("<p>x</p>", "text/html").compatMode}`);
+ done();
+ });
+</script>