Skip to content

Truly independent web browser

LibWeb: Give a quirks-mode table the body’s color

sideshowbarker897b36e54d43
diff --git a/Libraries/LibWeb/CSS/Keywords.json b/Libraries/LibWeb/CSS/Keywords.json
index ea270a672dc..ad3be80e75a 100644
--- a/Libraries/LibWeb/CSS/Keywords.json
+++ b/Libraries/LibWeb/CSS/Keywords.json
@@ -6,6 +6,7 @@
"-libweb-center",
"-libweb-inherit-or-center",
"-libweb-left",
+ "-libweb-quirk-inherit",
"-libweb-right",
"-webkit-box",
"-webkit-fill-available",
diff --git a/Libraries/LibWeb/CSS/QuirksMode.css b/Libraries/LibWeb/CSS/QuirksMode.css
index 0d0e448208b..3b80a96f0b5 100644
--- a/Libraries/LibWeb/CSS/QuirksMode.css
+++ b/Libraries/LibWeb/CSS/QuirksMode.css
@@ -21,6 +21,8 @@ li :is(dir, menu, ol, ul) { list-style-position: outside; }
*/
table {
+ /* https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk */
+ color: -libweb-quirk-inherit;
font-weight: initial;
font-style: initial;
font-variant: initial;
diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
index fcceb0e049c..cc53c5c2f2e 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
@@ -3958,6 +3958,15 @@ pub(crate) fn context_allows_random_functions(context: &ParseContext) -> bool {
}
fn parse_color_property(context: &ParseContext, property: u16, values: &[ComponentValue]) -> ParseOutcome {
+ if context.is_ua_style_sheet && property == property_id::COLOR {
+ let mut tokens = values.iter().filter(|value| !value.is_whitespace());
+ if let Some(value) = tokens.next()
+ && tokens.next().is_none()
+ && let Some(value) = parse_specific_keyword(value, &[keyword::_LIBWEB_QUIRK_INHERIT])
+ {
+ return ParseOutcome::Parsed(shared_style_value(value));
+ }
+ }
if !(FIRST_SHORTHAND_PROPERTY_ID..=LAST_LONGHAND_PROPERTY_ID).contains(&property)
|| property_is_shorthand(property)
|| !property_accepted_value_types(property).contains(&VALUE_TYPE_COLOR)
@@ -6690,6 +6699,36 @@ mod tests {
parse_css_value(context, property, &values)
}
+ #[test]
+ fn quirk_inherit_is_only_a_ua_color_value() {
+ let mut ua = context();
+ ua.is_ua_style_sheet = true;
+ assert_eq!(
+ parsed_keyword(parse_with_context(&ua, property_id::COLOR, " -LIBWEB-QUIRK-INHERIT ")),
+ keyword::_LIBWEB_QUIRK_INHERIT
+ );
+ for property in [
+ property_id::COLOR,
+ property_id::BACKGROUND_COLOR,
+ property_id::BORDER_TOP_COLOR,
+ ] {
+ assert!(matches!(
+ parse(property, "-libweb-quirk-inherit"),
+ ParseOutcome::Invalid
+ ));
+ }
+ for (property, value) in [
+ (property_id::COLOR, "-libweb-quirk-inherit red"),
+ (property_id::BACKGROUND_COLOR, "-libweb-quirk-inherit"),
+ (property_id::BORDER_COLOR, "-libweb-quirk-inherit"),
+ ] {
+ assert!(matches!(
+ parse_with_context(&ua, property, value),
+ ParseOutcome::Invalid
+ ));
+ }
+ }
+
#[test]
fn parses_a_serialized_substituted_value() {
let mut parse_context = context();
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
index 3d00c95c539..f6d0e3f46e7 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
@@ -127,7 +127,37 @@ fn initial_drive_font_request(
Box::new(suspended_font.expect("an unanswered initial drive waits on a font"))
}
+fn is_quirk_inherit(property: u16, value: &RetainedStyleValueData) -> bool {
+ property == crate::css::property_metadata::property_id::COLOR
+ && matches!(
+ value.data(),
+ StyleValueData::Keyword {
+ keyword: crate::css::style_compute::keyword::_LIBWEB_QUIRK_INHERIT
+ }
+ )
+}
+
impl RetainedState {
+ pub(super) fn is_quirks_table(&self, node: StyleNodeID) -> bool {
+ self.document_style_computation_inputs.in_quirks_mode
+ && self.projection.adjustment_facts(node) & bridge::element_adjustment_fact::IS_TABLE != 0
+ }
+
+ // https://quirks.spec.whatwg.org/#the-document%27s-body-element
+ // The first HTML body child of the HTML document element supplies the color, wherever the table is.
+ pub(super) fn quirks_table_body(&self, node: StyleNodeID) -> Option<StyleNodeID> {
+ use bridge::element_adjustment_fact as fact;
+ let root = std::iter::successors(Some(node), |&node| self.projection.inheritance_parent(node))
+ .find(|&node| self.projection.adjustment_facts(node) & fact::IS_DOCUMENT_ELEMENT != 0)?;
+ if self.projection.adjustment_facts(root) & fact::IS_HTML_HTML_ELEMENT == 0 {
+ return None;
+ }
+ std::iter::successors(self.projection.first_element_child(root), |&child| {
+ self.projection.next_element_sibling(child)
+ })
+ .find(|&child| self.projection.adjustment_facts(child) & fact::IS_HTML_BODY_ELEMENT != 0)
+ }
+
/// The engine: the computed initial values, as an element with no declarations and no parent
/// computes them, with the groups they assemble into, each holding one reference, and the
/// color they resolve. Every table is seeded from this one, and an element whose
@@ -393,6 +423,7 @@ impl RetainedState {
target_pseudo_element: super::PseudoElementTarget,
root_font: Option<RootFontInputs>,
group_seeds: GroupSeeds<'_>,
+ quirks_color: Option<&RetainedStyleValueData>,
) -> Option<ComputedElement> {
use crate::css::property_metadata::{FIRST_LONGHAND_PROPERTY_ID, LONGHAND_WORD_COUNT, property_id as prop};
use crate::css::style_compute::{
@@ -517,7 +548,10 @@ impl RetainedState {
.declared_property(index)
.expect("the style engine answers every row");
let written = block.written(index);
- if !checks.is_plain() || declaration.operator() != CascadeOperator::Declared {
+ if !checks.is_plain()
+ || declaration.operator() != CascadeOperator::Declared
+ || is_quirk_inherit(property, written)
+ {
plain_dependencies = None;
break;
}
@@ -557,6 +591,7 @@ impl RetainedState {
winner.property,
winner.source,
winner.index,
+ quirks_color,
);
let rolls_back = classified.rolls_back();
if !rolls_back {
@@ -572,6 +607,7 @@ impl RetainedState {
&element_declarations,
custom_properties,
&defaulting,
+ quirks_color,
);
}
let ElementWinnerReads {
@@ -1291,6 +1327,7 @@ impl RetainedState {
/// decided before that was known.
#[cold]
#[inline(never)]
+ #[allow(clippy::too_many_arguments)]
fn decide_rolling_back<'a>(
&'a self,
decided: &mut DecidedWinners<'_, 'a>,
@@ -1302,6 +1339,7 @@ impl RetainedState {
)>; 3],
custom_properties: super::custom_properties::CustomPropertyStores<'_>,
defaulting: &WinnerDefaulting<'_>,
+ quirks_color: Option<&RetainedStyleValueData>,
) {
decided.easy.clear();
decided.hard.clear();
@@ -1329,8 +1367,14 @@ impl RetainedState {
source,
index,
} = winner;
- let mut classified =
- self.classify_winner(element_declarations, custom_properties, property, source, index);
+ let mut classified = self.classify_winner(
+ element_declarations,
+ custom_properties,
+ property,
+ source,
+ index,
+ quirks_color,
+ );
// A declaration that rolls back still decides where it does.
decided
.reads
@@ -1466,8 +1510,30 @@ impl RetainedState {
property: u16,
source: WinnerSource,
index: usize,
+ quirks_color: Option<&RetainedStyleValueData>,
) -> ClassifiedWinner<'a> {
let (declaration, written, checks) = self.declaration_of(element_declarations, source, index);
+ // https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk
+ // Resolve the UA marker before computation, including when a revert exposes it.
+ if is_quirk_inherit(property, written) {
+ let value = quirks_color
+ .unwrap_or_else(|| crate::css::style_compute::initial_value_handle(property))
+ .clone();
+ let computation = match crate::css::style_compute::value_computes_directly(property, value.data(), None) {
+ true => WinnerComputation::Direct(None),
+ false => WinnerComputation::Full,
+ };
+ let environment = environment_reads(Some(value.data()), None);
+ return ClassifiedWinner {
+ value: ResolvedWinnerValue::Substituted(value),
+ computation,
+ reads: WinnerReads {
+ element: true,
+ environment,
+ ..WinnerReads::default()
+ },
+ };
+ }
if checks.waits_on_substitution() {
use crate::css::style_value::StyleValueData;
let value = match written.data() {
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/declared_only.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/declared_only.rs
index 00057bd7db7..6a1639f87fd 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/declared_only.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/declared_only.rs
@@ -134,6 +134,7 @@ impl RetainedState {
super::PseudoElementTarget::of_kind(super::rules::ELEMENT_PSEUDO_KIND),
None,
initial.group_seeds(None),
+ None,
);
font_requests.extend(suspended_font);
let mut computed = driven.ok_or(())?;
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/sharing.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/sharing.rs
index 037859c3a6c..1c7ab435deb 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/sharing.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/sharing.rs
@@ -76,7 +76,8 @@ pub(super) fn key_of(
facts: &ElementFacts,
) -> Option<SharingKey> {
let id = facts.id();
- if (id != StyleAtomID::NONE && index.tests_id(id))
+ if state.is_quirks_table(node)
+ || (id != StyleAtomID::NONE && index.tests_id(id))
|| !state.element_declarations.custom_declarations(node).is_empty()
|| [
ElementDeclarationKind::InlineStyle,
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
index 3b526c875b1..ab374c83e19 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
@@ -289,6 +289,14 @@ pub(super) struct PassContext<'a> {
/// What of those moved from the ones the document element had before, which restyles every
/// element under it that reads it.
pub(super) root_font_moved: std::sync::OnceLock<RootFontReads>,
+ /// The body's color computed independently of the order in which the workers reach tables.
+ pub(super) quirks_body_style: std::sync::OnceLock<QuirksBodyStyle>,
+}
+
+pub(super) struct QuirksBodyStyle {
+ color: Option<RetainedStyleValueData>,
+ fonts: Vec<font_resolution::FontRequest>,
+ random_bases: Vec<RandomBaseRequest>,
}
impl<'a> PassContext<'a> {
@@ -320,11 +328,71 @@ impl<'a> PassContext<'a> {
held_root_font,
root_font: Default::default(),
root_font_moved: Default::default(),
+ quirks_body_style: Default::default(),
}
}
}
impl PassContext<'_> {
+ // https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk
+ // A body after the table can still be awaiting computation on another worker. Compute its inheritance chain
+ // privately, as an on-demand read does, and keep the result for this round. Font and random-value requests
+ // suspend the table until the next round, which computes the dependency against the resolved inputs.
+ fn quirks_table_color(&self, node: StyleNodeID) -> &QuirksBodyStyle {
+ self.quirks_body_style.get_or_init(|| {
+ let Some(body) = self.state.quirks_table_body(node) else {
+ return QuirksBodyStyle {
+ color: Some(
+ crate::css::style_compute::initial_value_handle(
+ crate::css::property_metadata::property_id::COLOR,
+ )
+ .clone(),
+ ),
+ fonts: Vec::new(),
+ random_bases: Vec::new(),
+ };
+ };
+ if self.records.computed(body)
+ && let Some(view) = self.records.assigned_record_view(body)
+ {
+ return QuirksBodyStyle {
+ color: Some(
+ view.longhands()
+ .effective_value(view.animated_overlay, crate::css::property_metadata::property_id::COLOR)
+ .clone(),
+ ),
+ fonts: Vec::new(),
+ random_bases: Vec::new(),
+ };
+ }
+ let mut chain: SmallVec<[StyleNodeID; 32]> =
+ std::iter::successors(Some(body), |&node| self.state.projection.inheritance_parent(node)).collect();
+ chain.reverse();
+ let mut cells = super::records::RecordCells::default();
+ cells.reserve(self.state.projection.element_slot_count());
+ let records =
+ Records::new(self.state, &cells, None).composing_over_equal_records(Record::holds_the_same_style_as);
+ let context = PassContext {
+ uses_caches: false,
+ ..PassContext::new(self.state, self.index, self.initial, &records, self.root_font())
+ };
+ let statistics = style_chain(&context, &chain);
+ let color = records
+ .assigned_record_view(body)
+ .filter(|_| records.computed(body))
+ .map(|view| {
+ view.longhands()
+ .effective_value(view.animated_overlay, crate::css::property_metadata::property_id::COLOR)
+ .clone()
+ });
+ QuirksBodyStyle {
+ color,
+ fonts: statistics.font_requests,
+ random_bases: statistics.random_requests,
+ }
+ })
+ }
+
/// The matched declarations cache of the pass.
fn matched_declarations(&self) -> &MatchedDeclarationsCache {
self.kept_matched_declarations.unwrap_or(&self.own_matched_declarations)
@@ -832,6 +900,7 @@ impl<'a> Scratch<'a> {
// What an element matches, its parent and its containers decide its style. Its matches
// are the ones it had where no entry it was rematched for answers otherwise.
let only_matches_moved = rematched
+ && !state.is_quirks_table(node)
&& moved & (PARENT_CHANGED | CONTAINERS_MOVED | RECOMPUTE_SELF) == 0
&& (moved & PARENT_RESTYLED == 0
|| state
@@ -1399,6 +1468,7 @@ impl<'a> Scratch<'a> {
target,
context.root_font(),
group_seeds,
+ None,
);
if let Some(request) = suspended_font {
self.statistics.font_requests.push(request);
@@ -1436,6 +1506,7 @@ impl<'a> Scratch<'a> {
target,
context.root_font(),
group_seeds,
+ None,
);
computed.reads.display_none_is_animated = ignoring_animations
.is_some_and(|ignoring_animations| !computed_display_is_none(&ignoring_animations));
@@ -1785,6 +1856,25 @@ impl<'a> Scratch<'a> {
// samples its animations over the record the pass installs.
let sampled_custom_properties = context.records.sampled_custom_properties(node).flatten();
+ let quirks_color = if state.is_quirks_table(node) {
+ let requested = self.requested();
+ let body = context.quirks_table_color(node);
+ self.statistics.font_requests.extend(body.fonts.iter().cloned());
+ self.statistics
+ .random_requests
+ .extend(body.random_bases.iter().map(|request| RandomBaseRequest {
+ node: request.node,
+ name: request.name.clone(),
+ element_shared: request.element_shared,
+ }));
+ if self.waits(node, requested) || body.color.is_none() {
+ return Err(Outcome::ComputeDeclined);
+ }
+ body.color.as_ref()
+ } else {
+ None
+ };
+
let group_seeds = context
.initial
.group_seeds(parent.and_then(|(parent, _)| context.records.get_shared(parent)));
@@ -1819,6 +1909,7 @@ impl<'a> Scratch<'a> {
super::PseudoElementTarget::of_kind(super::rules::ELEMENT_PSEUDO_KIND),
context.root_font(),
group_seeds,
+ quirks_color,
);
if let Some(request) = suspended_font {
self.statistics.font_requests.push(request);
@@ -1859,6 +1950,7 @@ impl<'a> Scratch<'a> {
super::PseudoElementTarget::of_kind(super::rules::ELEMENT_PSEUDO_KIND),
context.root_font(),
group_seeds,
+ quirks_color,
);
computed.reads.display_none_is_animated = ignoring_animations
.is_some_and(|ignoring_animations| !computed_display_is_none(&ignoring_animations));
@@ -2042,7 +2134,7 @@ impl<'a> Scratch<'a> {
node: StyleNodeID,
backed: Option<(u8, StyleNodeID)>,
) -> Option<MatchedKey<'c>> {
- if !context.uses_caches || backed.is_some() {
+ if !context.uses_caches || backed.is_some() || context.state.is_quirks_table(node) {
return None;
}
let state = context.state;
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-004-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-004-ref.html
new file mode 100644
index 00000000000..24b57be7d2f
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-004-ref.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<title>Quirks Mode Reference: The tables inherit color from body quirk - Table with body removed, light mode</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+html { color-scheme: light; }
+p { color: initial; }
+table { font: 200px/1 Ahem; color: initial; }
+</style>
+<p>Test passes if there is a square filled with initial color and <strong>no red</strong>.</p>
+<table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+</table>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-005-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-005-ref.html
new file mode 100644
index 00000000000..6902bb36dbe
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-005-ref.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<title>Quirks Mode Reference: The tables inherit color from body quirk - Table with body removed, light mode</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+html { color-scheme: dark; }
+p { color: initial; }
+table { font: 200px/1 Ahem; color: initial; }
+</style>
+<p>Test passes if there is a square filled with initial color and <strong>no red</strong>.</p>
+<table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+</table>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-001.html b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-001.html
new file mode 100644
index 00000000000..c5b5269c63e
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-001.html
@@ -0,0 +1,22 @@
+<!-- Quirks Mode -->
+<title>Quirks Mode: The tables inherit color from body quirk - Table inside the body</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk">
+<link rel="match" href="../../../expected/wpt-import/quirks/../css/reference/ref-filled-green-200px-square.html">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+body { color: green; height: min-content; margin: 0; }
+p { color: initial; }
+div { color: red; display: flow-root; margin: 0 8px; }
+table { font: 200px/1 Ahem; }
+</style>
+<body>
+ <div>
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+ </table>
+ </div>
+</body>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-002.html b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-002.html
new file mode 100644
index 00000000000..cb6246d3ae0
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-002.html
@@ -0,0 +1,25 @@
+<!-- Quirks Mode -->
+<title>Quirks Mode: The tables inherit color from body quirk - Table after the body</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk">
+<link rel="match" href="../../../expected/wpt-import/quirks/../css/reference/ref-filled-green-200px-square.html">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+body { color: green; height: min-content; margin: 0; }
+p { color: initial; }
+div { color: red; display: flow-root; margin: 0 8px; }
+table { font: 200px/1 Ahem; }
+</style>
+<body>
+ <div>
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+ </table>
+ </div>
+</body>
+<script>
+ document.body.after(document.querySelector("div"));
+</script>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-003.html b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-003.html
new file mode 100644
index 00000000000..b4aa204bba2
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-003.html
@@ -0,0 +1,25 @@
+<!-- Quirks Mode -->
+<title>Quirks Mode: The tables inherit color from body quirk - Table before the body</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk">
+<link rel="match" href="../../../expected/wpt-import/quirks/../css/reference/ref-filled-green-200px-square.html">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+body { color: green; height: min-content; margin: 0; }
+p { color: initial; }
+div { color: red; display: flow-root; margin: 0 8px; }
+table { font: 200px/1 Ahem; }
+</style>
+<body>
+ <div>
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+ </table>
+ </div>
+</body>
+<script>
+ document.body.before(document.querySelector("div"));
+</script>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-004.html b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-004.html
new file mode 100644
index 00000000000..ee3c494cce6
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-004.html
@@ -0,0 +1,29 @@
+<!-- Quirks Mode -->
+<title>Quirks Mode: The tables inherit color from body quirk - Table with body removed, light mode</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk">
+<link rel="match" href="../../../expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-004-ref.html">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+html { color-scheme: light; }
+body { color: red; height: min-content; margin: 0; }
+p { color: initial; }
+div { color: red; display: flow-root; margin: 0 8px; }
+table { font: 200px/1 Ahem; }
+</style>
+<body>
+ <div>
+ <p>Test passes if there is a square filled with initial color and <strong>no red</strong>.</p>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+ </table>
+ </div>
+</body>
+<script>
+ // For stability of the test, compute the color of the body.
+ getComputedStyle(document.body).color;
+
+ document.body.replaceWith(document.querySelector("div"));
+</script>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-005.html b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-005.html
new file mode 100644
index 00000000000..1f6fedba985
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-005.html
@@ -0,0 +1,29 @@
+<!-- Quirks Mode -->
+<title>Quirks Mode: The tables inherit color from body quirk - Table with body removed, dark mode</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk">
+<link rel="match" href="../../../expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-005-ref.html">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+html { color-scheme: dark; }
+body { color: red; height: min-content; margin: 0; }
+p { color: initial; }
+div { color: red; display: flow-root; margin: 0 8px; }
+table { font: 200px/1 Ahem; }
+</style>
+<body>
+ <div>
+ <p>Test passes if there is a square filled with initial color and <strong>no red</strong>.</p>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+ </table>
+ </div>
+</body>
+<script>
+ // For stability of the test, compute the color of the body.
+ getComputedStyle(document.body).color;
+
+ document.body.replaceWith(document.querySelector("div"));
+</script>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-006.html b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-006.html
new file mode 100644
index 00000000000..2fa59d0b854
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-006.html
@@ -0,0 +1,30 @@
+<!-- Quirks Mode -->
+<title>Quirks Mode: The tables inherit color from body quirk - Table without body, light mode</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk">
+<link rel="match" href="../../../expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-004-ref.html">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+html { color-scheme: light; }
+body { color: red; height: min-content; margin: 0; }
+p { color: initial; }
+div { color: red; display: flow-root; margin: 0 8px; }
+table { font: 200px/1 Ahem; }
+</style>
+<script>
+ // At this point, the <body> has not been created yet.
+ // And replacing the <html> prevents the creation of the <body>.
+ let root = document.documentElement;
+ let div = document.createElement("div");
+ div.innerHTML = `
+ <p>Test passes if there is a square filled with initial color and <strong>no red</strong>.</p>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+ </table>
+ `;
+ root.append(div);
+ document.documentElement.remove();
+ document.append(root.cloneNode(true));
+</script>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-007.html b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-007.html
new file mode 100644
index 00000000000..c969faeb5a5
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/quirks/tables-inherit-color-from-body-quirk-007.html
@@ -0,0 +1,30 @@
+<!-- Quirks Mode -->
+<title>Quirks Mode: The tables inherit color from body quirk - Table without body, dark mode</title>
+<link rel="author" title="Oriol Brufau" href="[email protected]">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk">
+<link rel="match" href="../../../expected/wpt-import/quirks/tables-inherit-color-from-body-quirk-005-ref.html">
+<link rel="stylesheet" type="text/css" href="../fonts/ahem.css">
+<style>
+html { color-scheme: dark; }
+body { color: red; height: min-content; margin: 0; }
+p { color: initial; }
+div { color: red; display: flow-root; margin: 0 8px; }
+table { font: 200px/1 Ahem; }
+</style>
+<script>
+ // At this point, the <body> has not been created yet.
+ // And replacing the <html> prevents the creation of the <body>.
+ let root = document.documentElement;
+ let div = document.createElement("div");
+ div.innerHTML = `
+ <p>Test passes if there is a square filled with initial color and <strong>no red</strong>.</p>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <td>X</td>
+ </tr>
+ </table>
+ `;
+ root.append(div);
+ document.documentElement.remove();
+ document.append(root.cloneNode(true));
+</script>
diff --git a/Tests/LibWeb/Text/expected/css/tables-inherit-color-from-body-quirk.txt b/Tests/LibWeb/Text/expected/css/tables-inherit-color-from-body-quirk.txt
new file mode 100644
index 00000000000..85cfeff8a9c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/tables-inherit-color-from-body-quirk.txt
@@ -0,0 +1,69 @@
+author keyword: false
+zoom 1 table: rgb(0, 128, 0)
+zoom 1 td: rgb(0, 128, 0)
+zoom 1 th: rgb(0, 128, 0)
+zoom 1 cache insert: rgb(128, 0, 128)
+zoom 1 cache recompute: rgb(0, 128, 0)
+zoom 1 body used color: rgb(255, 255, 255)
+zoom 1 border: rgb(0, 128, 0)
+zoom 1 blue: rgb(0, 0, 255)
+zoom 1 inherit: rgb(255, 0, 0)
+zoom 1 unset: rgb(255, 0, 0)
+zoom 1 initial: rgb(0, 0, 0)
+zoom 1 currentcolor: rgb(255, 0, 0)
+zoom 1 var(--missing): rgb(255, 0, 0)
+zoom 1 revert: rgb(0, 128, 0)
+zoom 1 revert-layer: rgb(128, 0, 128)
+zoom 1 invalid substituted keyword: rgb(255, 0, 0)
+zoom 1 display table: rgb(255, 0, 0)
+zoom 1 foreign table: rgb(255, 0, 0)
+zoom 1 animation: rgb(0, 0, 255)
+zoom 1 animation canceled: rgb(0, 128, 0)
+zoom 1 recomputed: rgb(128, 0, 128)
+zoom 1 inserted: rgb(128, 0, 128)
+zoom 1 before body: rgb(0, 128, 0)
+zoom 1 before body recomputed: rgb(128, 0, 128)
+zoom 1 after body: rgb(0, 128, 0)
+zoom 1 after body recomputed: rgb(128, 0, 128)
+zoom 1 removed body light: rgb(0, 0, 0)
+zoom 1 absent body light: rgb(0, 0, 0)
+zoom 1 removed body dark: rgb(255, 255, 255)
+zoom 1 absent body dark: rgb(255, 255, 255)
+zoom 1 standards: rgb(255, 0, 0)
+zoom 1 limited: rgb(255, 0, 0)
+zoom 1 first body: rgb(0, 0, 255)
+zoom 1 first body removed: rgb(0, 128, 0)
+zoom 2 table: rgb(0, 128, 0)
+zoom 2 td: rgb(0, 128, 0)
+zoom 2 th: rgb(0, 128, 0)
+zoom 2 cache insert: rgb(128, 0, 128)
+zoom 2 cache recompute: rgb(0, 128, 0)
+zoom 2 body used color: rgb(255, 255, 255)
+zoom 2 border: rgb(0, 128, 0)
+zoom 2 blue: rgb(0, 0, 255)
+zoom 2 inherit: rgb(255, 0, 0)
+zoom 2 unset: rgb(255, 0, 0)
+zoom 2 initial: rgb(0, 0, 0)
+zoom 2 currentcolor: rgb(255, 0, 0)
+zoom 2 var(--missing): rgb(255, 0, 0)
+zoom 2 revert: rgb(0, 128, 0)
+zoom 2 revert-layer: rgb(128, 0, 128)
+zoom 2 invalid substituted keyword: rgb(255, 0, 0)
+zoom 2 display table: rgb(255, 0, 0)
+zoom 2 foreign table: rgb(255, 0, 0)
+zoom 2 animation: rgb(0, 0, 255)
+zoom 2 animation canceled: rgb(0, 128, 0)
+zoom 2 recomputed: rgb(128, 0, 128)
+zoom 2 inserted: rgb(128, 0, 128)
+zoom 2 before body: rgb(0, 128, 0)
+zoom 2 before body recomputed: rgb(128, 0, 128)
+zoom 2 after body: rgb(0, 128, 0)
+zoom 2 after body recomputed: rgb(128, 0, 128)
+zoom 2 removed body light: rgb(0, 0, 0)
+zoom 2 absent body light: rgb(0, 0, 0)
+zoom 2 removed body dark: rgb(255, 255, 255)
+zoom 2 absent body dark: rgb(255, 255, 255)
+zoom 2 standards: rgb(255, 0, 0)
+zoom 2 limited: rgb(255, 0, 0)
+zoom 2 first body: rgb(0, 0, 255)
+zoom 2 first body removed: rgb(0, 128, 0)
diff --git a/Tests/LibWeb/Text/input/css/tables-inherit-color-from-body-quirk.html b/Tests/LibWeb/Text/input/css/tables-inherit-color-from-body-quirk.html
new file mode 100644
index 00000000000..7b3f4433c2a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/tables-inherit-color-from-body-quirk.html
@@ -0,0 +1,156 @@
+<!doctype html>
+<meta charset="utf-8">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk">
+<script src="../include.js"></script>
+<script>
+promiseTest(async () => {
+ const doctypes = {
+ quirks: "",
+ standards: "<!doctype html>",
+ limited: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
+ };
+ const frameFor = async (mode, zoom, setup = "", scheme = "light", withoutBody = false) => {
+ const frame = document.createElement("iframe");
+ const content = '<div id="wrapper" style="color: red"><table><tr><td>X</td><th>Y</th></tr></table></div>';
+ const style = `<style>
+ html { color-scheme: ${scheme}; color: orange; zoom: ${zoom}; }
+ body { color: green; }
+ @layer lower, higher;
+ @layer lower { .layered { color: purple; } }
+ @layer higher { .layered { color: revert-layer; } }
+ </style>`;
+ const script = withoutBody ? `
+ const root = document.documentElement;
+ const content = document.createElement("div");
+ content.innerHTML = ${JSON.stringify(content)};
+ root.append(content);
+ root.remove();
+ document.append(root.cloneNode(true));
+ ` : setup;
+ // NB: An iframe srcdoc document is never in quirks mode, so each document is written into an about:blank one.
+ document.body.append(frame);
+ const doc = frame.contentDocument;
+ doc.open();
+ doc.write(doctypes[mode] + style + (withoutBody ? "" : "<body>" + content)
+ + "<script>" + script + "<" + "/script>");
+ doc.close();
+ return frame;
+ };
+ const inspect = frame => {
+ const doc = frame.contentDocument;
+ const style = element => frame.contentWindow.getComputedStyle(element);
+ return { doc, style, table: doc.querySelector("table"), wrapper: doc.querySelector("#wrapper") };
+ };
+
+ println(`author keyword: ${CSS.supports("color", "-libweb-quirk-inherit")}`);
+ for (const zoom of [1, 2]) {
+ const frame = await frameFor("quirks", zoom);
+ const { doc, style, table, wrapper } = inspect(frame);
+ const print = (label, element = table) => println(`zoom ${zoom} ${label}: ${style(element).color}`);
+ print("table");
+ print("td", table.querySelector("td"));
+ print("th", table.querySelector("th"));
+
+ doc.body.style.color = "purple";
+ const cached = table.cloneNode(true);
+ wrapper.append(cached);
+ print("cache insert", cached);
+ doc.body.style.color = "green";
+ table.style.outlineWidth = "1px";
+ print("cache recompute");
+ table.style.removeProperty("outline-width");
+ cached.remove();
+
+ doc.body.style.color = "canvastext";
+ doc.body.style.colorScheme = "dark";
+ table.style.colorScheme = "light";
+ print("body used color");
+ doc.body.style.color = "green";
+ doc.body.style.colorScheme = "light";
+ table.style.removeProperty("color-scheme");
+ table.style.border = "1px solid currentcolor";
+ println(`zoom ${zoom} border: ${style(table).borderTopColor}`);
+ for (const value of ["blue", "inherit", "unset", "initial", "currentcolor", "var(--missing)", "revert"]) {
+ table.style.color = value;
+ print(value);
+ }
+ table.style.removeProperty("color");
+ table.className = "layered";
+ print("revert-layer");
+ table.className = "";
+ table.style.setProperty("--internal", "-libweb-quirk-inherit");
+ table.style.color = "var(--internal)";
+ print("invalid substituted keyword");
+ table.style.removeProperty("color");
+
+ const fake = doc.createElement("div");
+ fake.style.display = "table";
+ wrapper.append(fake);
+ print("display table", fake);
+ const foreign = doc.createElementNS("urn:test", "table");
+ wrapper.append(foreign);
+ print("foreign table", foreign);
+
+ const animation = table.animate({ color: ["blue", "blue"] }, { duration: 1000, fill: "both" });
+ animation.pause();
+ animation.currentTime = 500;
+ print("animation");
+ animation.cancel();
+ print("animation canceled");
+
+ doc.body.style.color = "purple";
+ table.style.outlineWidth = "2px";
+ print("recomputed");
+ const clone = table.cloneNode(true);
+ wrapper.append(clone);
+ print("inserted", clone);
+ frame.remove();
+
+ for (const position of ["before", "after"]) {
+ const moved = await frameFor("quirks", zoom,
+ `document.body.${position}(document.querySelector("#wrapper"));`);
+ const { doc, style, table } = inspect(moved);
+ println(`zoom ${zoom} ${position} body: ${style(table).color}`);
+ doc.body.style.color = "purple";
+ table.style.outlineWidth = "3px";
+ println(`zoom ${zoom} ${position} body recomputed: ${style(table).color}`);
+ moved.remove();
+ }
+
+ for (const scheme of ["light", "dark"]) {
+ const removed = await frameFor("quirks", zoom, "", scheme);
+ const { doc, style, table, wrapper } = inspect(removed);
+ style(doc.body).color;
+ doc.body.replaceWith(wrapper);
+ println(`zoom ${zoom} removed body ${scheme}: ${style(table).color}`);
+ removed.remove();
+ const absent = await frameFor("quirks", zoom, "", scheme, true);
+ const absentState = inspect(absent);
+ println(`zoom ${zoom} absent body ${scheme}: ${absentState.style(absentState.table).color}`);
+ absent.remove();
+ }
+
+ for (const mode of ["standards", "limited"]) {
+ const other = await frameFor(mode, zoom);
+ const { style, table } = inspect(other);
+ println(`zoom ${zoom} ${mode}: ${style(table).color}`);
+ other.remove();
+ }
+
+ const firstBody = await frameFor("quirks", zoom, `
+ const first = document.createElement("body");
+ first.style.color = "blue";
+ document.body.before(first);
+ const frameset = document.createElement("frameset");
+ frameset.style.color = "red";
+ first.before(frameset);
+ `);
+ const first = inspect(firstBody);
+ println(`zoom ${zoom} first body: ${first.style(first.table).color}`);
+ first.doc.querySelector("body").remove();
+ first.table.style.outlineWidth = "4px";
+ println(`zoom ${zoom} first body removed: ${first.style(first.table).color}`);
+ firstBody.remove();
+ }
+});
+</script>