Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix #548: crashes calling `getComputedStyle` when @media rules are in…

… stylesheet.

See also #549.
  • Loading branch information...
commit a72b95d183fab6364f4f6f977cce242dbca20b78 1 parent c5ee555
@domenic domenic authored
Showing with 32 additions and 12 deletions.
  1. +21 −12 lib/jsdom/browser/index.js
  2. +11 −0 test/level2/style.js
View
33 lib/jsdom/browser/index.js
@@ -244,21 +244,30 @@ exports.createWindow = function(dom, options) {
getComputedStyle: function(node) {
var s = node.style,
cs = new CSSStyleDeclaration(),
- forEach = Array.prototype.forEach,
- selectors, matched;
+ forEach = Array.prototype.forEach;
+
+ function setPropertiesFromRule(rule) {
+ var selectors = rule.selectorText.split(/\s*,\s*/);
+ var matched = false;
+ selectors.forEach(function (selectorText) {
+ if (!matched && matchesDontThrow(node, selectorText)) {
+ matched = true;
+ forEach.call(rule.style, function (property) {
+ cs.setProperty(property, rule.style.getPropertyValue(property), rule.style.getPropertyPriority(property));
+ });
+ }
+ });
+ }
forEach.call(node.ownerDocument.styleSheets, function (sheet) {
- forEach.call(sheet.cssRules, function (ruleSet) {
- selectors = ruleSet.selectorText.split(/\s*,\s*/);
- matched = false;
- selectors.forEach(function (selectorText) {
- if (!matched && matchesDontThrow(node, selectorText)) {
- matched = true;
- forEach.call(ruleSet.style, function (property) {
- cs.setProperty(property, ruleSet.style.getPropertyValue(property), ruleSet.style.getPropertyPriority(property));
- });
+ forEach.call(sheet.cssRules, function (rule) {
+ if (rule.media) {
+ if (Array.prototype.indexOf.call(rule.media, 'screen') !== -1) {
+ forEach.call(rule.cssRules, setPropertiesFromRule);
}
- });
+ } else {
+ setPropertiesFromRule(rule);
+ }
});
});
View
11 test/level2/style.js
@@ -198,5 +198,16 @@ exports.tests = {
});
test.done();
});
+ },
+
+ getComputedStyleWithMediaRules: function(test) {
+ jsdom.env(
+ '<html><head><style>@media screen,handheld { .citation { color: blue; } } @media print { .citation { color: red; } }</style></head>' +
+ '<body><p class="citation">Hello</p></body></html>',
+ jsdom.level('2', 'html'), function(err, win) {
+ var style = win.getComputedStyle(win.document.querySelector('.citation'));
+ test.equal(style.color, 'blue', 'computed color of p is blue');
+ test.done();
+ });
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.