Skip to content

Commit

Permalink
Implement toCss with URL
Browse files Browse the repository at this point in the history
  • Loading branch information
efoken committed Nov 16, 2023
1 parent 9364e36 commit 0e7ef3a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
17 changes: 8 additions & 9 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import { serialize, checkIncompatibility, toCss } from "../utils";
describe("utils", () => {
it("serialize", () => {
expect(serialize(true && "1", true && "2", true && "3")).to.equal(
"1 2 3"
"1 2 3",
);

expect(serialize(false && "1", true && "2", true && "3")).to.equal(
"2 3"
"2 3",
);

expect(serialize(false && "1", true && "2", false && "3")).to.equal(
"2"
"2",
);
});
it("checkIncompatibility", () => {
expect(checkIncompatibility()).to.instanceOf(Array);
expect(checkIncompatibility().length).to.equal(0);
});
it("toCss", () => {
const styleSheet = toCss({
const extract = (s) => Object.values(s.cssRules).map((v) => v.cssText);
let styleSheet = toCss({
":host": {
width: 696,
height: 100,
Expand All @@ -31,13 +32,11 @@ describe("utils", () => {
lineHeight: 1.5,
},
});
const cssText =
"cssRules" in styleSheet
? Object.values(styleSheet.cssRules).map((v) => v.cssText)
: [];
expect(cssText).to.eql([
expect(extract(styleSheet)).to.eql([
":host { width: 696px; height: 100px; flex: 1 1 0%; }",
".root { font-size: 12px; line-height: 1.5; }",
]);
styleSheet = toCss("https://unpkg.com/open-props");
expect(extract(styleSheet)).to.have.length.gt(1);
});
});
8 changes: 8 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as CSS from "csstype";
import { Sheet } from "./css.js";

/**
* Filter the parameters and join in a string only those that are considered different from
* `"" | false | 0 | null | undefined`.
Expand All @@ -12,3 +15,8 @@ export function serialize(...args: any): string;
* check Atomico's leveraged compatibility with the current browser
*/
export function checkIncompatibility(): string[];

export function toCss(obj: {
[key: string]: CSS.Properties<string | number>;
}): Sheet;
export function toCss(obj: string): Sheet;
14 changes: 13 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,21 @@ const stringify = (obj) =>

/**
* Create a Style from an object
* @param {{[key:string]:import("csstype").Properties<string | number>}} obj
* @param {{[key:string]:import("csstype").Properties<string | number>}|string} obj
* @returns {import("./types/css.js").Sheet}
*/
export function toCss(obj) {
if (typeof obj === "string") {
if (obj.match(/^https?:\/\//)) {
const request = new XMLHttpRequest();
request.open("get", obj, false);
request.send();
if (request.status === 200)
return css`
${request.responseText}
`;
}
}
return css`
${stringify(obj)}
`;
Expand Down

0 comments on commit 0e7ef3a

Please sign in to comment.