@litejs/dom 26.4.2 → 26.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +14 -3
  2. package/css.js +17 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -53,11 +53,22 @@ xhr.send();
53
53
  // Minify CSS
54
54
  import { CSS } from "@litejs/dom/css.js";
55
55
  const sheet = new CSSStyleSheet()
56
- sheet.replaceSync(".a { color: hsl(0 0% 100%) }")
57
- console.log(CSS.minify(sheet, { color: true }))
58
- // .a{color:#fff}
56
+ sheet.replaceSync(":root{--gap:10px} .a { color: hsl(0 0% 100%); top: calc(var(--gap) + 5px) }")
57
+ console.log(CSS.minify(sheet, { var: true, calc: true, color: true }))
58
+ // .a{color:#fff;top:15px}
59
59
  ```
60
60
 
61
+ ### CSS.minify options
62
+
63
+ - **calc** – resolve static `calc()` expressions, e.g. `calc(10px + 5px)` → `15px`
64
+ - **color** – shorten `rgb()`/`hsl()` to hex, e.g. `rgb(255,0,0)` → `#f00`
65
+ - **import** – inline `@import` rules, rewriting relative URLs
66
+ - **prefix** – add vendor prefixes, e.g. `{ "mask-image": ["-webkit-"] }`
67
+ - **prune** – remove unused selectors `{ keep: Set, keepRe: RegExp }`
68
+ - **root** – base directory for resolving `@import` paths
69
+ - **url** – URL rewrite callback `(path) => newPath`
70
+ - **var** – inline CSS custom properties from `:root`
71
+
61
72
  ## Contributing
62
73
 
63
74
  Follow [Coding Style Guide](https://github.com/litejs/litejs/wiki/Style-Guide),
package/css.js CHANGED
@@ -34,13 +34,17 @@ var URL = global.URL || require("url").URL
34
34
  href: rule.href,
35
35
  parentStyleSheet: sheet
36
36
  }, read(root, rule.href, sheet.baseURI))
37
- , urlFn = (m,q1,q2,u) => q1 ? m : "url('" + new URL(u, toUrl(imported.baseURI)).pathname.slice(1) + "')"
38
37
  if (sheet.baseURI !== imported.baseURI) {
39
- updateImportUrls(imported, urlFn)
38
+ updateImportUrls(imported, (m,q1,q2,u) => q1 ? m : "url('" + new URL(u, toUrl(imported.baseURI)).pathname.slice(1) + "')")
40
39
  }
41
40
  return CSS.minify(imported, opts)
42
41
  }
43
42
 
43
+ if (opts && opts.prune && sel) {
44
+ sel = filterSelectors(sel, opts.prune)
45
+ if (!sel) return ""
46
+ }
47
+
44
48
  // Handle plugins on style rules
45
49
  if (style && style._plugins) {
46
50
  for (var j = 0; j < style._plugins.length; j++) {
@@ -51,14 +55,10 @@ var URL = global.URL || require("url").URL
51
55
  }
52
56
  }
53
57
 
54
- if (opts && opts.used && sel) {
55
- sel = filterSelectors(sel, opts.used)
56
- if (!sel) return ""
57
- }
58
-
59
58
  var text = clear(opts && style && style.__ ? sel + "{" + cssText(style, opts.prefix) + "}" : rule.cssText)
60
59
  if (!text || /\{\s*\}$/.test(text)) return ""
61
60
  if (vars) text = text.replace(varRe, (_, v, fb) => vars[v] || fb || _)
61
+ if (opts && opts.calc) text = text.replace(calcRe, calcFn)
62
62
  if (opts && opts.color) text = text.replace(colorRe, colorFn)
63
63
  if (opts && opts.url) text = text.replace(urlRe, (m, q1, q2, u) => q1 ? m : "url(" + opts.url(u) + ")")
64
64
  return text
@@ -66,11 +66,11 @@ var URL = global.URL || require("url").URL
66
66
  }
67
67
  }
68
68
  , filterSelectors = (text, used) => {
69
- var classes = used.classes
70
- , keep = used.keep
69
+ var keep = used.keep
70
+ , keepRe = used.keepRe
71
71
  return require("./selector.js").selectorSplit(text).filter(sel => {
72
72
  for (var m, classRe = /\.([-\w]+)/g; (m = classRe.exec(sel)); ) {
73
- if (!(classes && classes.has(m[1]) || keep && keep.test(m[1]))) return false
73
+ if (!(keep && keep.has(m[1]) || keepRe && keepRe.test(m[1]))) return false
74
74
  }
75
75
  return true
76
76
  }).join(",")
@@ -129,6 +129,13 @@ var URL = global.URL || require("url").URL
129
129
  }
130
130
  }
131
131
  , toRgba = (rgbHex, alpha) => ("rgba(" + rgbHex.replace(/../g, x => parseInt(x, 16)+",") + alpha + ")").replace("0.", ".")
132
+ , calcRe = /calc\(\s*(-?[\d.]+)([a-z%]*)\s*([+\-*/])\s*(-?[\d.]+)([a-z%]*)\s*\)/g
133
+ , calcFn = (m, a, au, op, b, bu) => {
134
+ a -= 0
135
+ b -= 0
136
+ op = (op == "*" && !(au && bu)) ? (a *= b, au || bu) : (op == "/") ? (a /= b, !bu && b !== 0 && au) : au && au == bu && (op == "+" ? a += b : op === "-" ? a -= b : au -= 0, au)
137
+ return op ? (+a.toFixed(3) + op).replace(/^(-?)0\./, "$1.") : m
138
+ }
132
139
  , colorRe = /\b(rgb|hsl)a?\s*\(\s*(\d+)(?:deg)?[\s,]+(\d+)[\s,%]+(\d+)%?(?:[\s,\/]+(0?\.?\d+)(%?))?\s*\)/g
133
140
  , 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"))
134
141
  , cssText = (style, prefix) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litejs/dom",
3
- "version": "26.4.2",
3
+ "version": "26.4.3",
4
4
  "description": "A small DOM library for server-side testing, rendering, and handling of HTML files",
5
5
  "license": "MIT",
6
6
  "author": "Lauri Rooden <lauri@rooden.ee>",