Skip to content

Commit

Permalink
[DevTools] prevent StyleX plugin from throwing when inspecting CSS (#…
Browse files Browse the repository at this point in the history
…26364)

## Summary

An error might happen when we try to read the CSS rules, but the
stylesheet does not allow so (often happens on production).

Before:
<img width="713" alt="image"
src="https://user-images.githubusercontent.com/1001890/224376546-024f7a32-d314-4dd1-9333-7e47d96a2b7c.png">

After:

<img width="504" alt="image"
src="https://user-images.githubusercontent.com/1001890/224376426-964a33c4-0677-4a51-91c2-74074e4dde63.png">


## How did you test this change?

Built a fb version and tested locally (see above screenshot)

---------

Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
  • Loading branch information
mondaychen and unstubbable committed Mar 10, 2023
1 parent be353d2 commit a22bd99
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/react-devtools-shared/src/backend/StyleX/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ function crawlObjectProperties(
// Special case; this key is the name of the style's source/file/module.
sources.add(key);
} else {
resolvedStyles[key] = getPropertyValueForStyleName(value);
const propertyValue = getPropertyValueForStyleName(value);
if (propertyValue != null) {
resolvedStyles[key] = propertyValue;
}
}
} else {
const nestedStyle = {};
Expand All @@ -90,13 +93,19 @@ function getPropertyValueForStyleName(styleName: string): string | null {
const styleSheet = ((document.styleSheets[
styleSheetIndex
]: any): CSSStyleSheet);
// $FlowFixMe Flow doesn't konw about these properties
const rules = styleSheet.rules || styleSheet.cssRules;
// $FlowFixMe `rules` is mixed
let rules: CSSRuleList | null = null;
// this might throw if CORS rules are enforced https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interface
try {
rules = styleSheet.cssRules;
} catch (_e) {
continue;
}

for (let ruleIndex = 0; ruleIndex < rules.length; ruleIndex++) {
// $FlowFixMe `rules` is mixed
const rule = rules[ruleIndex];
// $FlowFixMe Flow doesn't konw about these properties
if (!(rules[ruleIndex] instanceof CSSStyleRule)) {
continue;
}
const rule = ((rules[ruleIndex]: any): CSSStyleRule);
const {cssText, selectorText, style} = rule;

if (selectorText != null) {
Expand Down

0 comments on commit a22bd99

Please sign in to comment.