Skip to content

Commit 0dedfc0

Browse files
Fix some bad cascade computation in getComputedStyle()
Closes #3182.
1 parent 8021a56 commit 0dedfc0

3 files changed

Lines changed: 70 additions & 1 deletion

File tree

lib/jsdom/living/helpers/style-rules.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ function getCascadedPropertyValue(element, property) {
6060
let value = "";
6161

6262
exports.forEachMatchingSheetRuleOfElement(element, rule => {
63-
value = rule.style.getPropertyValue(property);
63+
const propertyValue = rule.style.getPropertyValue(property);
64+
// getPropertyValue returns "" if the property is not found
65+
if (propertyValue !== "") {
66+
value = propertyValue;
67+
}
6468
});
6569

6670
const inlineValue = element.style.getPropertyValue(property);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>Computed visibility in nominal case for inline styles</title>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
7+
<!-- Test for https://github.com/jsdom/jsdom/issues/3182 -->
8+
9+
<body>
10+
<div id="hiddenDiv" style="visibility: hidden;">Hello, Ronron!</div>
11+
</body>
12+
13+
<script>
14+
"use strict";
15+
16+
17+
test(() => {
18+
const element = document.querySelector("body");
19+
assert_equals(getComputedStyle(element).visibility, "visible");
20+
}, "Sanity check: getComputedStyle returns apparent visibility");
21+
22+
test(() => {
23+
const element = document.querySelector("#hiddenDiv");
24+
assert_equals(getComputedStyle(element).visibility, "hidden");
25+
}, "Real check: getComputedStyle returns hidden visibility for inline styles");
26+
27+
</script>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>Computed visibility in nominal case</title>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
7+
<!-- Test for https://github.com/jsdom/jsdom/issues/3182 -->
8+
9+
<style>
10+
#hiddenDiv {
11+
visibility: hidden;
12+
}
13+
14+
/* This rule is important because add rule on the div */
15+
* {
16+
margin-bottom: 10px;
17+
}
18+
</style>
19+
20+
<body>
21+
<div id="hiddenDiv">Hello, Ronron!</div>
22+
</body>
23+
24+
<script>
25+
"use strict";
26+
27+
28+
test(() => {
29+
const element = document.querySelector("body");
30+
assert_equals(getComputedStyle(element).visibility, "visible");
31+
}, "Sanity check: getComputedStyle returns apparent visibility");
32+
33+
test(() => {
34+
const element = document.querySelector("#hiddenDiv");
35+
assert_equals(getComputedStyle(element).visibility, "hidden");
36+
}, "Real check: getComputedStyle returns hidden visibility");
37+
38+
</script>

0 commit comments

Comments
 (0)