Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getComputedStyle() for font-family (#3614) #3615

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/jsdom/living/helpers/style-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ exports.propertiesWithResolvedValueImplemented = {
inherited: false,
initial: "invert",
computedValue: "computed-color"
},
// https://drafts.csswg.org/css-fonts-4/#font-family-prop
"font-family": {
inherited: true,
initial: "",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this needs to be set to something like "serif". Can you do that, and then update the test? https://jsbin.com/tulemoreka/edit?html,output

computedValue: "as-specified"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear correct. Per spec it's

list, each item a string and/or keywords

The appropriate test to add is something like

<div style="font-family: serif,sans-serif,     'Times New Roman',Arial"></div>

and then testing that the computed style is serif, sans-serif, "Times New Roman", Arial (with the right quotes and spacing).

Making that work correctly would involve introducing some new infrastructure, probably around here?

function getComputedValue(element, property) {
const { computedValue, initial } = exports.propertiesWithResolvedValueImplemented[property];
if (computedValue === "as-specified") {
return getSpecifiedValue(element, property);
} else if (computedValue === "computed-color") {
const specifiedValue = getSpecifiedValue(element, property);
// https://drafts.csswg.org/css-color-4/#resolving-other-colors
if (specifiedValue === "currentcolor") {
if (property === "color") {
if (element.parentElement !== null) {
return getComputedValue(element.parentElement, "color");
}
return initial;
}
return getComputedValue(element, "color");
}
return getComputedOrUsedColor(specifiedValue);
}
throw new TypeError(`Internal error: unrecognized computed value instruction '${computedValue}'`);
}

}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Computed fontFamily is inherited</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<style>
#parent {
pointer-events: none;
}
</style>

<body>
<div id="parent" style="font-family: Arial;">
<div id="child">Child</div>
</div>
</body>

<script>
"use strict";

test(() => {
const parent = window.document.querySelector("#parent");
assert_equals(getComputedStyle(parent).fontFamily, "Arial");
}, "Parent element returns the directly specified value for fontFamily");

test(() => {
const child = document.querySelector("#child");
assert_equals(getComputedStyle(child).fontFamily, "Arial");
}, "Child element inherits the fontFamily value from its parent");
</script>