Blame view

lib/jsdom/living/helpers/style-rules.js 3.24 KB
858f2bdf5   Boyan Georgiev   fixes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  "use strict";
  const cssom = require("cssom");
  const defaultStyleSheet = require("../../browser/default-stylesheet");
  const { matchesDontThrow } = require("./selectors");
  
  const { forEach, indexOf } = Array.prototype;
  
  let parsedDefaultStyleSheet;
  
  // Properties for which getResolvedValue is implemented. This is less than
  // every supported property.
  // https://drafts.csswg.org/indexes/#properties
  exports.propertiesWithResolvedValueImplemented = {
    __proto__: null,
  
    // https://drafts.csswg.org/css2/visufx.html#visibility
    visibility: {
      inherited: true,
      initial: "visible",
      computedValue: "as-specified"
    }
  };
  
  exports.forEachMatchingSheetRuleOfElement = (element, handleRule) => {
    function handleSheet(sheet) {
      forEach.call(sheet.cssRules, rule => {
        if (rule.media) {
          if (indexOf.call(rule.media, "screen") !== -1) {
            forEach.call(rule.cssRules, innerRule => {
              if (matches(innerRule, element)) {
                handleRule(innerRule);
              }
            });
          }
        } else if (matches(rule, element)) {
          handleRule(rule);
        }
      });
    }
  
    if (!parsedDefaultStyleSheet) {
      parsedDefaultStyleSheet = cssom.parse(defaultStyleSheet);
    }
  
    handleSheet(parsedDefaultStyleSheet);
    forEach.call(element.ownerDocument.styleSheets, handleSheet);
  };
  
  function matches(rule, element) {
    return matchesDontThrow(element, rule.selectorText);
  }
  
  // Naive implementation of https://drafts.csswg.org/css-cascade-4/#cascading
  // based on the previous jsdom implementation of getComputedStyle.
  // Does not implement https://drafts.csswg.org/css-cascade-4/#cascade-specificity,
  // or rather specificity is only implemented by the order in which the matching
  // rules appear. The last rule is the most specific while the first rule is
  // the least specific.
  function getCascadedPropertyValue(element, property) {
    let value = "";
  
    exports.forEachMatchingSheetRuleOfElement(element, rule => {
      value = rule.style.getPropertyValue(property);
    });
  
    return value;
  }
  
  // https://drafts.csswg.org/css-cascade-4/#specified-value
  function getSpecifiedValue(element, property) {
    const cascade = getCascadedPropertyValue(element, property);
  
    if (cascade !== "") {
      return cascade;
    }
  
    // Defaulting
    const { initial, inherited } = exports.propertiesWithResolvedValueImplemented[property];
    if (inherited && element.parentElement !== null) {
      return getComputedValue(element.parentElement, property);
    }
  
    // root element without parent element or inherited property
    return initial;
  }
  
  // https://drafts.csswg.org/css-cascade-4/#computed-value
  function getComputedValue(element, property) {
    const { computedValue } = exports.propertiesWithResolvedValueImplemented[property];
    if (computedValue === "as-specified") {
      return getSpecifiedValue(element, property);
    }
  
    throw new TypeError(`Internal error: unrecognized computed value instruction '${computedValue}'`);
  }
  
  // https://drafts.csswg.org/cssom/#resolved-value
  // Only implements `visibility`
  exports.getResolvedValue = (element, property) => {
    // Determined for special case properties, none of which are implemented here.
    // So we skip to "any other property: The resolved value is the computed value."
    return getComputedValue(element, property);
  };