-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hiccup-css): update conditional handling, add formatCond()
- Loading branch information
1 parent
428de3c
commit 57533c7
Showing
4 changed files
with
35 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,39 @@ | ||
import { CSSOpts, RuleFn } from "./api"; | ||
import { isString } from "@thi.ng/checks/is-string"; | ||
import { Conditional, CSSOpts, RuleFn } from "./api"; | ||
import { _css } from "./css"; | ||
import { indent } from "./utils"; | ||
|
||
export function conditional(type: string, cond: string, rules: any[]): RuleFn { | ||
export function conditional(type: string, cond: Conditional, rules: any[]): RuleFn { | ||
return (acc: string[], opts: CSSOpts) => { | ||
const space = indent(opts); | ||
acc.push(`${space}${type}(${cond})${opts.format.declStart}`); | ||
acc.push(`${space}${type} ${formatCond(cond)}${opts.format.declStart}`); | ||
opts.depth++; | ||
_css(acc, [], rules, opts); | ||
opts.depth--; | ||
acc.push(space + opts.format.declEnd); | ||
return acc; | ||
}; | ||
} | ||
|
||
function formatCond(cond: any) { | ||
if (isString(cond)) { | ||
return cond; | ||
} | ||
const acc = []; | ||
for (let c in cond) { | ||
if (cond.hasOwnProperty(c)) { | ||
let v = cond[c]; | ||
if (v === true) { | ||
v = c; | ||
} else if (v === false) { | ||
v = "not " + c; | ||
} else if (v === "only") { | ||
v += " " + c; | ||
} else { | ||
v = `(${c}:${v})`; | ||
} | ||
acc.push(v); | ||
} | ||
} | ||
return acc.join(" and "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { RuleFn } from "./api"; | ||
import { RuleFn, Conditional } from "./api"; | ||
import { conditional } from "./conditional"; | ||
|
||
export function at_media(cond, rules: any[]): RuleFn { | ||
export function at_media(cond: Conditional, rules: any[]): RuleFn { | ||
return conditional("@media", cond, rules); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { RuleFn } from "./api"; | ||
import { RuleFn, Conditional } from "./api"; | ||
import { conditional } from "./conditional"; | ||
|
||
export function at_supports(cond, rules: any[]): RuleFn { | ||
export function at_supports(cond: Conditional, rules: any[]): RuleFn { | ||
return conditional("@supports", cond, rules); | ||
} |