fix(parser): adapt text document integration after rebase

This commit is contained in:
ldm0
2026-10-22 22:20:44 +08:00
parent 6a3429aace
commit a7fb0edd76
5 changed files with 37 additions and 16 deletions
+14 -4
View File
@@ -493,8 +493,10 @@ impl HtmlParser {
/// Start a text document using the HTML tokenizer's plaintext state.
/// Subsequent chunks remain literal text inside the browser-owned `pre`.
pub fn start_text_document(&self, final_url: Url, content_type: &str) -> DocumentStream {
let stream =
DocumentStream::new_text_document(new_parser_stream_html_tree_sink_target(final_url));
let stream = DocumentStream::new_text_document(new_parser_stream_html_tree_sink_target(
final_url,
self.scripting_enabled,
));
stream.inner.initialize_text_document();
let mut host = stream.inner.take_parser_stream_dom_host();
let document_handle = host.document_handle();
@@ -2064,8 +2066,12 @@ mod tests {
fn text_document_stream_preserves_literal_markup_and_leading_newline() {
let payload =
"\u{feff}\n<b>Gülçek</b>&amp;<script>window.executed=1</script></pre>\u{feff}";
for content_type in ["text/plain", "application/json", "application/problem+json"] {
let stream = HtmlParser::SCRIPTING_ENABLED.start_text_document(
for (content_type, scripting_enabled) in
["text/plain", "application/json", "application/problem+json"]
.into_iter()
.flat_map(|mime| [false, true].map(|scripting| (mime, scripting)))
{
let stream = HtmlParser::with_scripting_enabled(scripting_enabled).start_text_document(
Url::parse("https://example.test/data").unwrap(),
content_type,
);
@@ -2076,6 +2082,10 @@ mod tests {
let pre = first_element_by_ns(&document, HTML_NS, "pre");
assert_eq!(document.text_content(pre).as_deref(), Some(payload));
assert_eq!(document.document().unwrap().content_type(), content_type);
assert_eq!(
document.document().unwrap().scripting_enabled(),
scripting_enabled
);
for tag in ["b", "script"] {
assert!(
document
@@ -1,16 +1,20 @@
use super::*;
#[tokio::test]
async fn webdriver_classic_text_children_preserve_parent_encoding_without_a_charset() {
async fn webdriver_classic_text_children_respect_encoding_inheritance_and_data_origins() {
const LITERAL: &str = "<meta charset=gbk>吴姓-姓氏渊源";
// An opaque data document cannot inherit the UTF-8 container's encoding.
// Its literal meta markup is text, so the default windows-1252 applies.
const DATA_LITERAL: &str = "<meta charset=gbk>å\u{90}´å§“ï¼\u{8d}å§“æ°\u{8f}渊æº\u{90}";
let cases = [
("ascii", "plain ASCII"),
("utf8", "吴姓-姓氏渊源"),
("literal", LITERAL),
("data", LITERAL),
("ascii", "plain ASCII", "UTF-8"),
("utf8", "吴姓-姓氏渊源", "UTF-8"),
("literal", LITERAL, "UTF-8"),
("data", DATA_LITERAL, "windows-1252"),
("data-utf8", LITERAL, "UTF-8"),
];
let mut fixture = axum::Router::new();
for (name, payload) in cases {
for (name, payload, _) in cases {
fixture = fixture.route(
&format!("/{name}"),
axum::routing::get(
@@ -22,9 +26,14 @@ async fn webdriver_classic_text_children_preserve_parent_encoding_without_a_char
"/parent/{kind}",
axum::routing::get(
|axum::extract::Path(kind): axum::extract::Path<String>| async move {
let target = if kind == "data" {
let target = if matches!(kind.as_str(), "data" | "data-utf8") {
let charset = if kind == "data-utf8" {
";charset=utf-8"
} else {
""
};
format!(
"data:text/plain;base64,{}",
"data:text/plain{charset};base64,{}",
base64::Engine::encode(&BASE64_STANDARD, LITERAL)
)
} else {
@@ -41,7 +50,7 @@ async fn webdriver_classic_text_children_preserve_parent_encoding_without_a_char
let app = build_router(test_state());
let session = classic_request_json(app.clone(), Method::POST, "/session").await;
let session_id = session["value"]["sessionId"].as_str().unwrap();
for (name, payload) in cases {
for (name, payload, character_set) in cases {
classic_request_json_with_body(
app.clone(),
Method::POST,
@@ -75,7 +84,7 @@ async fn webdriver_classic_text_children_preserve_parent_encoding_without_a_char
).await;
assert_eq!(
observed["value"],
json!([payload, "UTF-8", "text/plain"]),
json!([payload, character_set, "text/plain"]),
"{name}"
);
classic_request_json_with_body(
@@ -1328,7 +1328,8 @@ impl JsContextHost {
let mut parser = if is_xml_document {
DocumentParserSession::start_finite_live_xml_document(document_url, document_handle)
} else if content_type.is_some_and(moli_web_mime::is_text_document_mime) {
let mut parser_owner = ChildFrameLiveParserOwner::new(self, scope, document_handle);
let mut parser_owner =
ContextDocumentParserOwner::new_child(self, scope, document_handle);
DocumentParserSession::start_finite_live_text_document(
document_url,
document_handle,
@@ -2,6 +2,7 @@ mod renderer_owned;
use super::{JsContextHost, NavigationHistoryEntrySeed};
use crate::content_security_policy::ContentSecurityPolicy;
use crate::document_response_decoder::decode_document_response;
use crate::web_api_interfaces;
use crate::{
content_security_policy::content_security_policy_forces_opaque_origin,
@@ -54,7 +55,6 @@ use crate::{
};
use anyhow::Result;
use moli_crypto::sha256_hex;
use crate::document_response_decoder::decode_document_response;
use moli_fetch::Request;
use moli_storage_key::{MoliStorageKey, StoragePartitionRelation, site_for_url};
use moli_webapi_declare::{ObjectLiteralDeclaration, WebApiObject};
+1
View File
@@ -997,6 +997,7 @@ fn json_mime_group_includes_text_json_and_valid_suffixes() {
for mime in [
"text/plain",
"application/javascript",
"text/javascript",
"application/jsonp",
"text/jsonp",
"application/json+xml",