From 78c8c11e6bb7f1202bdf2c9c2081b53043300b79 Mon Sep 17 00:00:00 2001 From: Whalefreezer Date: Tue, 7 May 2019 01:56:33 +1200 Subject: [PATCH] add support for css @import rules (#11) --- src/snapshot.ts | 10 +++++++++- test/__snapshots__/integration.ts.snap | 11 +++++++++++ test/css/style-with-import.css | 1 + test/html/with-style-sheet-with-import.html | 16 ++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/css/style-with-import.css create mode 100644 test/html/with-style-sheet-with-import.html diff --git a/src/snapshot.ts b/src/snapshot.ts index 49642ed7..1be1b6a4 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -21,13 +21,21 @@ function getCssRulesString(s: CSSStyleSheet): string | null { try { const rules = s.rules || s.cssRules; return rules - ? Array.from(rules).reduce((prev, cur) => (prev += cur.cssText), '') + ? Array.from(rules).reduce((prev, cur) => prev + getCssRuleString(cur), '') : null; } catch (error) { return null; } } +function getCssRuleString(rule: CSSRule): string { + return isCSSImportRule(rule) ? getCssRulesString(rule.styleSheet) || '' : rule.cssText; +} + +function isCSSImportRule(rule: CSSRule): rule is CSSImportRule { + return 'styleSheet' in rule; +} + function extractOrigin(url: string): string { let origin; if (url.indexOf('//') > -1) { diff --git a/test/__snapshots__/integration.ts.snap b/test/__snapshots__/integration.ts.snap index 2c3d42b8..2ee77d42 100644 --- a/test/__snapshots__/integration.ts.snap +++ b/test/__snapshots__/integration.ts.snap @@ -205,3 +205,14 @@ exports[`[html file]: with-style-sheet.html 1`] = ` " `; + +exports[`[html file]: with-style-sheet-with-import.html 1`] = ` +" + + + + with style sheet with import + + +" +`; diff --git a/test/css/style-with-import.css b/test/css/style-with-import.css new file mode 100644 index 00000000..61058d7b --- /dev/null +++ b/test/css/style-with-import.css @@ -0,0 +1 @@ +@import "./style.css"; diff --git a/test/html/with-style-sheet-with-import.html b/test/html/with-style-sheet-with-import.html new file mode 100644 index 00000000..6b45f65b --- /dev/null +++ b/test/html/with-style-sheet-with-import.html @@ -0,0 +1,16 @@ + + + + + + + + with style sheet with import + + + + + + + +