add support for css @import rules (#11)
This commit is contained in:
@@ -21,13 +21,21 @@ function getCssRulesString(s: CSSStyleSheet): string | null {
|
|||||||
try {
|
try {
|
||||||
const rules = s.rules || s.cssRules;
|
const rules = s.rules || s.cssRules;
|
||||||
return rules
|
return rules
|
||||||
? Array.from(rules).reduce((prev, cur) => (prev += cur.cssText), '')
|
? Array.from(rules).reduce((prev, cur) => prev + getCssRuleString(cur), '')
|
||||||
: null;
|
: null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return null;
|
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 {
|
function extractOrigin(url: string): string {
|
||||||
let origin;
|
let origin;
|
||||||
if (url.indexOf('//') > -1) {
|
if (url.indexOf('//') > -1) {
|
||||||
|
|||||||
@@ -205,3 +205,14 @@ exports[`[html file]: with-style-sheet.html 1`] = `
|
|||||||
</head><body>
|
</head><body>
|
||||||
</body></html>"
|
</body></html>"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`[html file]: with-style-sheet-with-import.html 1`] = `
|
||||||
|
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
|
||||||
|
<meta charset=\\"UTF-8\\" />
|
||||||
|
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
|
||||||
|
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\" />
|
||||||
|
<title>with style sheet with import</title>
|
||||||
|
<style>body { margin: 0px; background: url('http://localhost:3030/a.jpg'); }p { color: red; background: url('http://localhost:3030/css/b.jpg'); }body > p { color: yellow; }</style>
|
||||||
|
</head><body>
|
||||||
|
</body></html>"
|
||||||
|
`;
|
||||||
|
|||||||
1
test/css/style-with-import.css
vendored
Normal file
1
test/css/style-with-import.css
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
@import "./style.css";
|
||||||
16
test/html/with-style-sheet-with-import.html
Normal file
16
test/html/with-style-sheet-with-import.html
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>with style sheet with import</title>
|
||||||
|
<link rel="stylesheet" href="/css/style-with-import.css">
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user