Skip to content

Commit ee23d71

Browse files
committed
css: Add prefixing support
1 parent 576663d commit ee23d71

3 files changed

Lines changed: 35 additions & 20 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ jobs:
1111
- run: npx c8 -r lcovonly -r text --check-coverage --100 npm test
1212
- run: node --import=@litejs/cli/test.js test/load.mjs
1313
- name: Browser tests
14-
run: |
15-
npx lj serve &
16-
google-chrome --headless --virtual-time-budget=3000 --enable-logging=stderr http://127.0.0.1:8080/test/index.html 2>&1 | sed -n '/INFO:CONSOLE/s,.*"\(.*\)".*,\1,p' | tee /tmp/browser-test.txt
17-
grep -q "# pass" /tmp/browser-test.txt && ! grep -q "FAIL" /tmp/browser-test.txt
14+
run: lj ui-test test/index.html
1815
- run: npx lj lint
1916
- run: ./test/readme.sh
2017
- run: npx tsc -p test/tsconfig.json --noEmit

css.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ var URL = global.URL || require("url").URL
1919
, vars = opts && opts.var ? {} : null
2020
, varFn = vars && function(_, n) { return vars[n] || _ }
2121
return Array.prototype.map.call(rules, rule => {
22-
if (vars && rule.selectorText === ":root") {
23-
if (rule.style) for (var i = 0; i < rule.style.length; i++) {
24-
var n = rule.style[i]
25-
if (n.slice(0, 2) === "--") vars[n] = rule.style[n].replace(varRe, varFn)
22+
var sel = rule.selectorText
23+
, style = rule.style
24+
if (vars && sel === ":root") {
25+
if (style) for (var i = 0; i < style.length; i++) {
26+
var n = style[i]
27+
if (n.slice(0, 2) === "--") vars[n] = style[n].replace(varRe, varFn)
2628
}
2729
return ""
2830
}
@@ -40,16 +42,16 @@ var URL = global.URL || require("url").URL
4042
}
4143

4244
// Handle plugins on style rules
43-
if (rule.style && rule.style._plugins) {
44-
for (var style = rule.style, j = 0; j < style._plugins.length; j++) {
45+
if (style && style._plugins) {
46+
for (var j = 0; j < style._plugins.length; j++) {
4547
var idx = style._plugins[j][0]
4648
, pn = style._plugins[j][1]
4749
, k = style[idx]
4850
style[k] = clear(plugins[pn](root, sheet.baseURI, style[k]))
4951
}
5052
}
5153

52-
var text = clear(rule.cssText)
54+
var text = clear(opts && style && style.__ ? sel + "{" + cssText(style, opts.prefix) + "}" : rule.cssText)
5355
if (!text || /\{\s*\}$/.test(text)) return ""
5456
if (vars) text = text.replace(varRe, (_, v, fb) => vars[v] || fb || _)
5557
if (opts && opts.color) text = text.replace(colorRe, colorFn)
@@ -114,17 +116,18 @@ var URL = global.URL || require("url").URL
114116
, toRgba = (rgbHex, alpha) => ("rgba(" + rgbHex.replace(/../g, x => parseInt(x, 16)+",") + alpha + ")").replace("0.", ".")
115117
, colorRe = /\b(rgb|hsl)a?\s*\(\s*(\d+)(?:deg)?[\s,]+(\d+)[\s,%]+(\d+)%?(?:[\s,\/]+(0?\.?\d+)(%?))?\s*\)/g
116118
, colorFn = (_, name, a, b, c, d, p) => (_ = toRgb[name](a, b, c), (p ? d/=100 : d) < 1 ? toRgba(_, d) : "#" + _.replace(/(\w)\1(\w)\2(\w)\3/, "$1$2$3"))
119+
, cssText = (style, prefix) => {
120+
for (var out = [], name, value, i = style.length; i--; ) {
121+
name = style[i]
122+
value = style.__[i] || style[name]
123+
out[i] = name + ":" + value
124+
if (prefix && prefix[name]) out[i] = prefix[name].join(out[i] + ";") + out[i] + ";" + out[i]
125+
}
126+
return out.join(";")
127+
}
117128
, styleHandler = {
118129
get(style, prop) {
119-
if (prop === "cssText") {
120-
for (var out = [], name, value, i = style.length; i--; ) {
121-
name = style[i]
122-
value = style.__[i] || style[name]
123-
out[i] = name + ":" + value
124-
}
125-
return out.join(";")
126-
}
127-
return style[prop] || ""
130+
return prop === "cssText" ? cssText(style) : style[prop] || ""
128131
},
129132
set(style, prop, val) {
130133
if (prop === "cssText") {

test/css.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ describe("css.js {0}", describe.env === "browser" ?
135135
assert.equal(minify(sheet, opts), expected).end()
136136
})
137137

138+
test("prefix {i} '{1}'", [
139+
[ { prefix: { "mask-image": ["-webkit-"] } },
140+
".a{color:red;mask-image:url('mask-image: .png');top:1}",
141+
".a{color:red;-webkit-mask-image:url('mask-image: .png');mask-image:url('mask-image: .png');top:1}" ],
142+
[ { prefix: { "transition": ["-webkit-", "-moz-"] } },
143+
".a{transition:all .3s}",
144+
".a{-webkit-transition:all .3s;-moz-transition:all .3s;transition:all .3s}" ],
145+
[ { prefix: { "mask-image": ["-webkit-"] } },
146+
".a{color:red}",
147+
".a{color:red}" ],
148+
], (opts, text, expected, assert) => {
149+
sheet.replaceSync(text)
150+
assert.equal(minify(sheet, opts), expected).end()
151+
})
152+
138153
test("url callback", assert => {
139154
var map = { "img/icon.png": "icon_abc.png", "bg.jpg": "bg_123.jpg" }
140155
sheet.replaceSync(".a{background:url(img/icon.png)}.b{background:url(bg.jpg)}.c{color:red}")

0 commit comments

Comments
 (0)