Skip to content

Truly independent web browser

LibWeb: Resolve legacy writing modes to their computed values

sideshowbarker369846580544
diff --git a/Libraries/LibWeb/CSS/Keywords.json b/Libraries/LibWeb/CSS/Keywords.json
index 28a01900da5..d1581f5354d 100644
--- a/Libraries/LibWeb/CSS/Keywords.json
+++ b/Libraries/LibWeb/CSS/Keywords.json
@@ -437,6 +437,8 @@
"lower-ukrainian-full",
"lower-ukrainian",
"lowercase",
+ "lr",
+ "lr-tb",
"ltr",
"luminance",
"luminosity",
@@ -613,6 +615,8 @@
"ridge",
"right",
"root",
+ "rl",
+ "rl-tb",
"rotate-left",
"rotate-right",
"round",
@@ -765,6 +769,8 @@
"tally-mark",
"tamil",
"telugu-alpha",
+ "tb",
+ "tb-rl",
"telugu",
"text",
"text-after-edge",
diff --git a/Libraries/LibWeb/CSS/Properties.json b/Libraries/LibWeb/CSS/Properties.json
index 6a50542059e..d128bb83968 100644
--- a/Libraries/LibWeb/CSS/Properties.json
+++ b/Libraries/LibWeb/CSS/Properties.json
@@ -6122,6 +6122,14 @@
"requires-computation": "never",
"valid-types": [
"writing-mode"
+ ],
+ "valid-identifiers": [
+ "lr",
+ "lr-tb",
+ "rl",
+ "rl-tb",
+ "tb",
+ "tb-rl"
]
},
"x": {
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
index 4119907b3e8..0aae24ccd43 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
@@ -1342,7 +1342,9 @@ impl RetainedState {
quirks_color: Option<&RetainedStyleValueData>,
) {
use crate::css::property_metadata::property_id as prop;
- use crate::css::style_compute::{initial_value_handle, keyword, writing_mode_and_direction_of};
+ use crate::css::style_compute::{
+ initial_value_handle, keyword, writing_mode_and_direction_of, writing_mode_from_specified_keyword,
+ };
decided.easy.clear();
decided.hard.clear();
@@ -1487,9 +1489,7 @@ impl RetainedState {
prop::WRITING_MODE => {
flow.0 = match flow_keyword {
Some(keyword::INITIAL) => initial_flow.0,
- Some(keyword) => {
- crate::css::style_compute::keyword_to_writing_mode(keyword).unwrap_or(inherited_flow.0)
- }
+ Some(keyword) => writing_mode_from_specified_keyword(keyword).unwrap_or(inherited_flow.0),
None => inherited_flow.0,
};
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
index 5d0f5c83f75..2702f3eb20b 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
@@ -369,7 +369,7 @@ impl RetainedState {
FfiEffectiveColorSchemeInput, FfiFontMetrics, FfiInputLineHeightMetrics, FfiLengthResolutionContext,
FfiStyleComputationEnvironment, LONGHAND_DRIVE_PHASE_COLOR_SCHEME, LONGHAND_DRIVE_PHASE_FONT,
LONGHAND_DRIVE_PHASE_LINE_HEIGHT, LONGHAND_DRIVE_PHASE_REMAINING, drive_property_computation,
- empty_longhand_driver_results, font_family_is_monospace,
+ empty_longhand_driver_results, font_family_is_monospace, writing_mode_from_specified_keyword,
};
use crate::css::table_group_builder::group_index;
use bridge::element_adjustment_fact as fact;
@@ -462,9 +462,6 @@ impl RetainedState {
snapshot,
}
});
- // The subject axis is the element's own writing mode: the keyword its winner declares,
- // which is its computed value, else the one it held, else its parent's; the initial
- // writing mode is horizontal.
let declared_writing_mode =
store
.winning_declaration(prop::WRITING_MODE)
@@ -472,7 +469,7 @@ impl RetainedState {
StyleValueData::Keyword {
keyword: crate::css::style_compute::keyword::INITIAL,
} => Some(crate::css::css_enums::writing_mode::HORIZONTAL_TB),
- StyleValueData::Keyword { keyword } => crate::css::style_compute::keyword_to_writing_mode(*keyword),
+ StyleValueData::Keyword { keyword } => writing_mode_from_specified_keyword(*keyword),
_ => None,
});
let subject_inline_axis_is_horizontal = match declared_writing_mode {
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index e06d56e95d6..ccee50eef8e 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -3203,8 +3203,7 @@ pub(crate) unsafe fn drive_property_computation(
let mut computed_math_depth: Option<i32> = None;
// The background-image list length, for the coordinated background properties.
let mut background_image_list_length: Option<usize> = None;
- // The computed writing-mode and direction, tracked for logical alias pairing;
- // both properties only take keywords, whose computed value is the specified one.
+ // Logical properties use the computed writing mode and direction to find their physical counterparts.
let mut computed_writing_mode = writer.get(prop::WRITING_MODE).and_then(|value| match value.data() {
StyleValueData::Keyword { keyword } => keyword_to_writing_mode(*keyword),
_ => None,
@@ -3442,6 +3441,13 @@ pub(crate) unsafe fn drive_property_computation(
// Whether the computed value depends on inherited information, so the specified
// value must be kept for re-resolution when an ancestor changes.
let value = value.expect("a longhand has a cascaded, inherited or initial value");
+ let value = if property_id == prop::WRITING_MODE
+ && let StyleValueData::Keyword { keyword } = value.data()
+ {
+ RetainedStyleValueData::shared_keyword(computed_writing_mode_keyword(*keyword))
+ } else {
+ value
+ };
let value_data = value.data();
let external_dependencies = external_dependencies.unwrap_or_else(|| external_value_dependencies(value_data));
@@ -6064,6 +6070,19 @@ fn keyword_from_style_value(value: &StyleValueData) -> u16 {
*keyword
}
+// https://drafts.csswg.org/css-writing-modes-4/#svg-writing-mode-css
+fn computed_writing_mode_keyword(keyword: u16) -> u16 {
+ match keyword {
+ keyword::LR | keyword::LR_TB | keyword::RL | keyword::RL_TB => keyword::HORIZONTAL_TB,
+ keyword::TB | keyword::TB_RL => keyword::VERTICAL_RL,
+ _ => keyword,
+ }
+}
+
+pub(crate) fn writing_mode_from_specified_keyword(keyword: u16) -> Option<u8> {
+ keyword_to_writing_mode(computed_writing_mode_keyword(keyword))
+}
+
/// The writing mode a computed `writing-mode` value holds.
pub(crate) fn writing_mode_of(value: &RetainedStyleValueData) -> u8 {
value
diff --git a/Tests/LibWeb/Text/expected/legacy-writing-mode.txt b/Tests/LibWeb/Text/expected/legacy-writing-mode.txt
new file mode 100644
index 00000000000..2c9dd16883c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/legacy-writing-mode.txt
@@ -0,0 +1,30 @@
+ltr lr horizontal-tb 7px
+ltr lr-tb horizontal-tb 7px
+ltr rl horizontal-tb 7px
+ltr rl-tb horizontal-tb 7px
+ltr tb vertical-rl 7px
+ltr tb-rl vertical-rl 7px
+rtl lr horizontal-tb 7px
+rtl lr-tb horizontal-tb 7px
+rtl rl horizontal-tb 7px
+rtl rl-tb horizontal-tb 7px
+rtl tb vertical-rl 7px
+rtl tb-rl vertical-rl 7px
+lr viewport units: 80px by 60px
+lr-tb viewport units: 80px by 60px
+rl viewport units: 80px by 60px
+rl-tb viewport units: 80px by 60px
+tb viewport units: 60px by 80px
+tb-rl viewport units: 60px by 80px
+ltr lr revert-layer: 7px
+ltr lr-tb revert-layer: 7px
+ltr rl revert-layer: 7px
+ltr rl-tb revert-layer: 7px
+ltr tb revert-layer: 7px
+ltr tb-rl revert-layer: 7px
+rtl lr revert-layer: 7px
+rtl lr-tb revert-layer: 7px
+rtl rl revert-layer: 7px
+rtl rl-tb revert-layer: 7px
+rtl tb revert-layer: 7px
+rtl tb-rl revert-layer: 7px
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-writing-modes/parsing/writing-mode-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-writing-modes/parsing/writing-mode-computed.txt
new file mode 100644
index 00000000000..3abf0fae962
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-writing-modes/parsing/writing-mode-computed.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass Property writing-mode value 'horizontal-tb'
+Pass Property writing-mode value 'vertical-rl'
+Pass Property writing-mode value 'vertical-lr'
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-writing-modes/writing-mode-parsing-svg1-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-writing-modes/writing-mode-parsing-svg1-001.txt
new file mode 100644
index 00000000000..377b89ef2f8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-writing-modes/writing-mode-parsing-svg1-001.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Pass
+Pass writing-mode: lr
+Pass writing-mode: lr-tb
+Pass writing-mode: rl
+Pass writing-mode: rl-tb
+Pass writing-mode: tb
+Pass writing-mode: tb-rl
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/legacy-writing-mode.html b/Tests/LibWeb/Text/input/legacy-writing-mode.html
new file mode 100644
index 00000000000..14e06bc3078
--- /dev/null
+++ b/Tests/LibWeb/Text/input/legacy-writing-mode.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<div id="parentBox"><div id="childBox"></div></div>
+<script>
+ test(() => {
+ for (const direction of ["ltr", "rtl"]) {
+ parentBox.dir = direction;
+ for (const value of ["lr", "lr-tb", "rl", "rl-tb", "tb", "tb-rl"]) {
+ parentBox.style.writingMode = value.startsWith("t") ? "horizontal-tb" : "vertical-rl";
+ childBox.style.writingMode = value;
+ childBox.style.marginInlineStart = "7px";
+ const style = getComputedStyle(childBox);
+ const edge = value.startsWith("t") ? (direction === "ltr" ? "Top" : "Bottom") : (direction === "ltr" ? "Left" : "Right");
+ println(`${direction} ${childBox.style.writingMode} ${style.writingMode} ${style["margin" + edge]}`);
+ }
+ }
+ childBox.style.width = "10vi";
+ childBox.style.height = "10vb";
+ for (const mode of ["lr", "lr-tb", "rl", "rl-tb", "tb", "tb-rl"]) {
+ parentBox.style.writingMode = mode.startsWith("t") ? "horizontal-tb" : "vertical-rl";
+ childBox.style.writingMode = mode;
+ const style = getComputedStyle(childBox);
+ println(mode + " viewport units: " + style.width + " by " + style.height);
+ }
+ childBox.style.removeProperty("margin-inline-start");
+ const sheet = document.head.appendChild(document.createElement("style"));
+ for (const direction of ["ltr", "rtl"]) {
+ parentBox.dir = direction;
+ for (const mode of ["lr", "lr-tb", "rl", "rl-tb", "tb", "tb-rl"]) {
+ const vertical = mode.startsWith("t");
+ parentBox.style.writingMode = vertical ? "horizontal-tb" : "vertical-rl";
+ childBox.style.writingMode = mode;
+ const edge = vertical ? (direction === "ltr" ? "top" : "bottom") : (direction === "ltr" ? "left" : "right");
+ sheet.textContent = `@layer base { #childBox { margin-inline-start: 7px; } }
+ @layer override { #childBox { margin-inline-start: 11px; margin-${edge}: revert-layer; } }`;
+ println(`${direction} ${mode} revert-layer: ${getComputedStyle(childBox).marginInlineStart}`);
+ }
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-writing-modes/parsing/writing-mode-computed.html b/Tests/LibWeb/Text/input/wpt-import/css/css-writing-modes/parsing/writing-mode-computed.html
new file mode 100644
index 00000000000..0eee926e323
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-writing-modes/parsing/writing-mode-computed.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>CSS Writing Modes: getComputedStyle().writingMode</title>
+<link rel="help" href="https://drafts.csswg.org/css-writing-modes-3/#propdef-writing-mode">
+<meta name="assert" content="writing-mode computed value is as specified.">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script src="../../../css/support/computed-testcommon.js"></script>
+</head>
+<body>
+<div id="target"></div>
+<script>
+test_computed_value("writing-mode", "horizontal-tb");
+test_computed_value("writing-mode", "vertical-rl");
+test_computed_value("writing-mode", "vertical-lr");
+</script>
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-writing-modes/writing-mode-parsing-svg1-001.html b/Tests/LibWeb/Text/input/wpt-import/css/css-writing-modes/writing-mode-parsing-svg1-001.html
new file mode 100644
index 00000000000..258ee9ec427
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-writing-modes/writing-mode-parsing-svg1-001.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<title>CSS Writing Modes: parsing SVG 1.1 writing-mode values</title>
+<link rel="author" title="Koji Ishii" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-writing-modes-3/#svg-writing-mode">
+<meta name="assert" content="This test asserts the parser and getComputedStyle works correctly for the SVG 1.1 writing-mode values.">
+<meta name="flags" content="dom svg may">
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div style="writing-mode: lr"
+ data-expected="horizontal-tb"></div>
+<div style="writing-mode: lr-tb"
+ data-expected="horizontal-tb"></div>
+<div style="writing-mode: rl"
+ data-expected="horizontal-tb"></div>
+<div style="writing-mode: rl-tb"
+ data-expected="horizontal-tb"></div>
+
+<div style="writing-mode: tb"
+ data-expected="vertical-rl"></div>
+<div style="writing-mode: tb-rl"
+ data-expected="vertical-rl"></div>
+
+<script>
+Array.prototype.forEach.call(document.querySelectorAll("[data-expected]"), function (element) {
+ test(function () {
+ var actual = getComputedStyle(element).writingMode;
+ assert_equals(actual, element.dataset.expected);
+ }, element.title || element.getAttribute("style"));
+});
+</script>