Skip to content

Commit 7959640

Browse files
committed
Implement toCss with URL
1 parent 9364e36 commit 7959640

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

tests/utils.test.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ import { serialize, checkIncompatibility, toCss } from "../utils";
44
describe("utils", () => {
55
it("serialize", () => {
66
expect(serialize(true && "1", true && "2", true && "3")).to.equal(
7-
"1 2 3"
7+
"1 2 3",
88
);
99

1010
expect(serialize(false && "1", true && "2", true && "3")).to.equal(
11-
"2 3"
11+
"2 3",
1212
);
1313

1414
expect(serialize(false && "1", true && "2", false && "3")).to.equal(
15-
"2"
15+
"2",
1616
);
1717
});
1818
it("checkIncompatibility", () => {
1919
expect(checkIncompatibility()).to.instanceOf(Array);
2020
expect(checkIncompatibility().length).to.equal(0);
2121
});
2222
it("toCss", () => {
23-
const styleSheet = toCss({
23+
const extract = (s) => Object.values(s.cssRules).map((v) => v.cssText);
24+
let styleSheet = toCss({
2425
":host": {
2526
width: 696,
2627
height: 100,
@@ -31,13 +32,11 @@ describe("utils", () => {
3132
lineHeight: 1.5,
3233
},
3334
});
34-
const cssText =
35-
"cssRules" in styleSheet
36-
? Object.values(styleSheet.cssRules).map((v) => v.cssText)
37-
: [];
38-
expect(cssText).to.eql([
35+
expect(extract(styleSheet)).to.eql([
3936
":host { width: 696px; height: 100px; flex: 1 1 0%; }",
4037
".root { font-size: 12px; line-height: 1.5; }",
4138
]);
39+
styleSheet = toCss("https://unpkg.com/open-props");
40+
expect(extract(styleSheet)).to.have.length.gt(1);
4241
});
4342
});

types/utils.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import * as CSS from "csstype";
2+
import { Sheet } from "./css.js";
3+
14
/**
25
* Filter the parameters and join in a string only those that are considered different from
36
* `"" | false | 0 | null | undefined`.
@@ -12,3 +15,8 @@ export function serialize(...args: any): string;
1215
* check Atomico's leveraged compatibility with the current browser
1316
*/
1417
export function checkIncompatibility(): string[];
18+
19+
export function toCss(obj: {
20+
[key: string]: CSS.Properties<string | number>;
21+
}): Sheet;
22+
export function toCss(obj: string): Sheet | undefined;

utils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,21 @@ const stringify = (obj) =>
7171

7272
/**
7373
* Create a Style from an object
74-
* @param {{[key:string]:import("csstype").Properties<string | number>}} obj
74+
* @param {{[key:string]:import("csstype").Properties<string | number>}|string} obj
75+
* @returns {import("./types/css.js").Sheet | undefined}
7576
*/
7677
export function toCss(obj) {
78+
if (typeof obj === "string") {
79+
if (obj.match(/^https?:\/\//)) {
80+
const request = new XMLHttpRequest();
81+
request.open("get", obj, false);
82+
request.send();
83+
if (request.status === 200)
84+
return css`
85+
${request.responseText}
86+
`;
87+
}
88+
}
7789
return css`
7890
${stringify(obj)}
7991
`;

0 commit comments

Comments
 (0)