forked from tsai/budibase
6 changed files with 131 additions and 3 deletions
@ -0,0 +1,46 @@ |
|||||
|
const { generateAssetCss, generateCss } = require("../../../utilities/builder/generateCss") |
||||
|
|
||||
|
describe("generate_css", () => { |
||||
|
it("Check how array styles are output", () => { |
||||
|
expect(generateCss({ margin: ["0", "10", "0", "15"] })).toBe("margin: 0 10 0 15;") |
||||
|
}) |
||||
|
|
||||
|
it("Check handling of an array with empty string values", () => { |
||||
|
expect(generateCss({ padding: ["", "", "", ""] })).toBe("") |
||||
|
}) |
||||
|
|
||||
|
it("Check handling of an empty array", () => { |
||||
|
expect(generateCss({ margin: [] })).toBe("") |
||||
|
}) |
||||
|
|
||||
|
it("Check handling of valid font property", () => { |
||||
|
expect(generateCss({ "font-size": "10px" })).toBe("font-size: 10px;") |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
|
||||
|
describe("generate_screen_css", () => { |
||||
|
const normalComponent = { _id: "123-456", _component: "@standard-components/header", _children: [], _styles: { normal: { "font-size": "16px" }, hover: {}, active: {}, selected: {} } } |
||||
|
|
||||
|
it("Test generation of normal css styles", () => { |
||||
|
expect(generateAssetCss([normalComponent])).toBe(".header-123-456 {\nfont-size: 16px;\n}") |
||||
|
}) |
||||
|
|
||||
|
const hoverComponent = { _id: "123-456", _component: "@standard-components/header", _children: [], _styles: { normal: {}, hover: {"font-size": "16px"}, active: {}, selected: {} } } |
||||
|
|
||||
|
it("Test generation of hover css styles", () => { |
||||
|
expect(generateAssetCss([hoverComponent])).toBe(".header-123-456:hover {\nfont-size: 16px;\n}") |
||||
|
}) |
||||
|
|
||||
|
const selectedComponent = { _id: "123-456", _component: "@standard-components/header", _children: [], _styles: { normal: {}, hover: {}, active: {}, selected: { "font-size": "16px" } } } |
||||
|
|
||||
|
it("Test generation of selection css styles", () => { |
||||
|
expect(generateAssetCss([selectedComponent])).toBe(".header-123-456::selection {\nfont-size: 16px;\n}") |
||||
|
}) |
||||
|
|
||||
|
const emptyComponent = { _id: "123-456", _component: "@standard-components/header", _children: [], _styles: { normal: {}, hover: {}, active: {}, selected: {} } } |
||||
|
|
||||
|
it("Testing handling of empty component styles", () => { |
||||
|
expect(generateAssetCss([emptyComponent])).toBe("") |
||||
|
}) |
||||
|
}) |
||||
@ -0,0 +1,43 @@ |
|||||
|
exports.generateAssetCss = component_arr => { |
||||
|
let styles = "" |
||||
|
for (const { _styles, _id, _children, _component } of component_arr) { |
||||
|
let [componentName] = _component.match(/[a-z]*$/) |
||||
|
Object.keys(_styles).forEach(selector => { |
||||
|
const cssString = exports.generateCss(_styles[selector]) |
||||
|
if (cssString) { |
||||
|
styles += exports.applyClass(_id, componentName, cssString, selector) |
||||
|
} |
||||
|
}) |
||||
|
if (_children && _children.length) { |
||||
|
styles += exports.generateAssetCss(_children) + "\n" |
||||
|
} |
||||
|
} |
||||
|
return styles.trim() |
||||
|
} |
||||
|
|
||||
|
exports.generateCss = style => { |
||||
|
let cssString = Object.entries(style).reduce((str, [key, value]) => { |
||||
|
if (typeof value === "string") { |
||||
|
if (value) { |
||||
|
return (str += `${key}: ${value};\n`) |
||||
|
} |
||||
|
} else if (Array.isArray(value)) { |
||||
|
if (value.length > 0 && !value.every(v => v === "")) { |
||||
|
return (str += `${key}: ${value.join(" ")};\n`) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return str |
||||
|
}, "") |
||||
|
|
||||
|
return (cssString || "").trim() |
||||
|
} |
||||
|
|
||||
|
exports.applyClass = (id, name = "element", styles, selector) => { |
||||
|
if (selector === "normal") { |
||||
|
return `.${name}-${id} {\n${styles}\n}` |
||||
|
} else { |
||||
|
let sel = selector === "selected" ? "::selection" : `:${selector}` |
||||
|
return `.${name}-${id}${sel} {\n${styles}\n}` |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue