Skip to content

Truly independent web browser

LibWeb: Expose FontFace.width as an alias for stretch

sideshowbarker6ea0a31a15d1
diff --git a/Libraries/LibWeb/CSS/FontFace.cpp b/Libraries/LibWeb/CSS/FontFace.cpp
index e163eb810a6..b9a21b62abb 100644
--- a/Libraries/LibWeb/CSS/FontFace.cpp
+++ b/Libraries/LibWeb/CSS/FontFace.cpp
@@ -267,7 +267,7 @@ GC::Ref<FontFace> FontFaceState::create_for_constructor(JS::Object& relevant_glo
try_set_descriptor(DescriptorID::FontFamily, family);
try_set_descriptor(DescriptorID::FontStyle, descriptors.style);
try_set_descriptor(DescriptorID::FontWeight, descriptors.weight);
- try_set_descriptor(DescriptorID::FontWidth, descriptors.stretch);
+ try_set_descriptor(DescriptorID::FontWidth, descriptors.width.value_or(descriptors.stretch));
try_set_descriptor(DescriptorID::UnicodeRange, descriptors.unicode_range);
try_set_descriptor(DescriptorID::FontFeatureSettings, descriptors.feature_settings);
try_set_descriptor(DescriptorID::FontVariationSettings, descriptors.variation_settings);
diff --git a/Libraries/LibWeb/CSS/FontFace.idl b/Libraries/LibWeb/CSS/FontFace.idl
index 819841c7ab0..46d475a7bdf 100644
--- a/Libraries/LibWeb/CSS/FontFace.idl
+++ b/Libraries/LibWeb/CSS/FontFace.idl
@@ -1,6 +1,7 @@
dictionary FontFaceDescriptors {
CSSOMString style = "normal";
CSSOMString weight = "normal";
+ CSSOMString width;
CSSOMString stretch = "normal";
CSSOMString unicodeRange = "U+0-10FFFF";
CSSOMString featureSettings = "normal";
@@ -22,6 +23,7 @@ interface FontFace {
attribute CSSOMString family;
attribute CSSOMString style;
attribute CSSOMString weight;
+ [ImplementedAs=stretch] attribute CSSOMString width;
attribute CSSOMString stretch;
attribute CSSOMString unicodeRange;
attribute CSSOMString featureSettings;
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-font-loading/fontface-width.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-font-loading/fontface-width.txt
new file mode 100644
index 00000000000..9493b976ffc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-font-loading/fontface-width.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 5 tests
+
+5 Pass
+Pass FontFace width and stretch have the same default value
+Pass The width constructor descriptor initializes both aliases
+Pass The width constructor descriptor takes precedence over stretch
+Pass Setting either width alias updates the other alias
+Pass Setting width to an invalid value throws and preserves its value
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-font-loading/fontface-width.html b/Tests/LibWeb/Text/input/wpt-import/css/css-font-loading/fontface-width.html
new file mode 100644
index 00000000000..512d488c42f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-font-loading/fontface-width.html
@@ -0,0 +1,49 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>CSS Font Loading: FontFace width and stretch aliases</title>
+<link rel="help" href="https://drafts.csswg.org/css-font-loading/#fontface-interface">
+<link rel="help" href="https://drafts.csswg.org/css-fonts-4/#font-width-prop">
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script>
+const source = "url(/fonts/Ahem.ttf)";
+
+test(() => {
+ const face = new FontFace("width-default", source);
+ assert_equals(face.width, "normal");
+ assert_equals(face.stretch, "normal");
+}, "FontFace width and stretch have the same default value");
+
+test(() => {
+ const face = new FontFace("width-constructor", source, {width: "expanded"});
+ assert_equals(face.width, "expanded");
+ assert_equals(face.stretch, "expanded");
+}, "The width constructor descriptor initializes both aliases");
+
+test(() => {
+ const face = new FontFace("width-precedence", source, {
+ stretch: "condensed",
+ width: "expanded",
+ });
+ assert_equals(face.width, "expanded");
+ assert_equals(face.stretch, "expanded");
+}, "The width constructor descriptor takes precedence over stretch");
+
+test(() => {
+ const face = new FontFace("width-setter", source);
+ face.width = "condensed";
+ assert_equals(face.width, "condensed");
+ assert_equals(face.stretch, "condensed");
+
+ face.stretch = "125%";
+ assert_equals(face.width, "125%");
+ assert_equals(face.stretch, "125%");
+}, "Setting either width alias updates the other alias");
+
+test(() => {
+ const face = new FontFace("width-invalid", source, {width: "expanded"});
+ assert_throws_dom("SyntaxError", () => { face.width = "invalid"; });
+ assert_equals(face.width, "expanded");
+ assert_equals(face.stretch, "expanded");
+}, "Setting width to an invalid value throws and preserves its value");
+</script>