@litejs/dom 26.3.0 → 26.4.1

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/css.js +36 -18
  2. package/dom.js +12 -2
  3. package/package.json +2 -2
package/css.js CHANGED
@@ -8,7 +8,7 @@ exports.CSSStyleSheet = CSSStyleSheet
8
8
 
9
9
  /* c8 ignore next */
10
10
  var URL = global.URL || require("url").URL
11
- , varRe = /var\((--[^,)]+)(?:,([^)]+))?\)/g
11
+ , varRe = /var\((--[^,)]+),?\s*([^)]*)\)/g
12
12
  , CSS = exports.CSS = {
13
13
  escape(sel) {
14
14
  return ("" + sel).replace(/[^a-zA-Z0-9_\u00A0-\uFFFF-]/g, "\\$&").replace(/^(-?)([0-9])/, "$1\\3$2 ")
@@ -19,10 +19,12 @@ var URL = global.URL || require("url").URL
19
19
  , vars = opts && opts.var ? {} : null
20
20
  , varFn = vars && function(_, n) { return vars[n] || _ }
21
21
  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)
26
28
  }
27
29
  return ""
28
30
  }
@@ -40,8 +42,8 @@ var URL = global.URL || require("url").URL
40
42
  }
41
43
 
42
44
  // 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++) {
45
47
  var idx = style._plugins[j][0]
46
48
  , pn = style._plugins[j][1]
47
49
  , k = style[idx]
@@ -49,7 +51,12 @@ var URL = global.URL || require("url").URL
49
51
  }
50
52
  }
51
53
 
52
- var text = clear(rule.cssText)
54
+ if (opts && opts.used && sel) {
55
+ sel = filterSelectors(sel, opts.used)
56
+ if (!sel) return ""
57
+ }
58
+
59
+ var text = clear(opts && style && style.__ ? sel + "{" + cssText(style, opts.prefix) + "}" : rule.cssText)
53
60
  if (!text || /\{\s*\}$/.test(text)) return ""
54
61
  if (vars) text = text.replace(varRe, (_, v, fb) => vars[v] || fb || _)
55
62
  if (opts && opts.color) text = text.replace(colorRe, colorFn)
@@ -58,6 +65,16 @@ var URL = global.URL || require("url").URL
58
65
  }).filter(Boolean).join("\n")
59
66
  }
60
67
  }
68
+ , filterSelectors = (text, used) => {
69
+ var classes = used.classes
70
+ , keep = used.keep
71
+ return require("./selector.js").selectorSplit(text).filter(sel => {
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
74
+ }
75
+ return true
76
+ }).join(",")
77
+ }
61
78
  , toUrl = (dir) => new URL((dir || ".").replace(/\/+$/, "") + "/", "file:///").href
62
79
  , read = (root, url, baseURI, enc = "utf8") => require("fs").readFileSync(
63
80
  new URL(url, new URL((baseURI || ".") + "/", new URL((root || ".").replace(/\/+$/, "") + "/", "file:///" + process.cwd() + "/"))).pathname.replace(/^\/(?=\w:)|[+#].*/, ""),
@@ -91,7 +108,7 @@ var URL = global.URL || require("url").URL
91
108
  q ? (q = str.indexOf("'") == -1 ? "'" : "\"", q + str.replace(q === "'" ? /\\(")/g : /\\(')/g, "$1")) + q :
92
109
  c ? "" :
93
110
  _.replace(/[\t\n]+/g, " ")
94
- .replace(/ +(?=[,;{}>~+\/])/g, "").replace(/([,;{}>~+\/]) +/g, "$1")
111
+ .replace(/ *([,;{}>~+\/]) */g, "$1")
95
112
  .replace(/;(?=})/g, "")
96
113
  .replace(/: +/g, ":")
97
114
  .replace(/([ :,])0\.([0-9])/g, "$1.$2")
@@ -114,17 +131,18 @@ var URL = global.URL || require("url").URL
114
131
  , toRgba = (rgbHex, alpha) => ("rgba(" + rgbHex.replace(/../g, x => parseInt(x, 16)+",") + alpha + ")").replace("0.", ".")
115
132
  , colorRe = /\b(rgb|hsl)a?\s*\(\s*(\d+)(?:deg)?[\s,]+(\d+)[\s,%]+(\d+)%?(?:[\s,\/]+(0?\.?\d+)(%?))?\s*\)/g
116
133
  , 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
+ , cssText = (style, prefix) => {
135
+ for (var out = [], name, value, i = style.length; i--; ) {
136
+ name = style[i]
137
+ value = style.__[i] || style[name]
138
+ out[i] = name + ":" + value
139
+ if (prefix && prefix[name]) out[i] = prefix[name].join(out[i] + ";") + out[i] + ";" + out[i]
140
+ }
141
+ return out.join(";")
142
+ }
117
143
  , styleHandler = {
118
144
  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] || ""
145
+ return prop === "cssText" ? cssText(style) : style[prop] || ""
128
146
  },
129
147
  set(style, prop, val) {
130
148
  if (prop === "cssText") {
package/dom.js CHANGED
@@ -222,11 +222,14 @@ var boolAttrs = {
222
222
  }
223
223
  }
224
224
  , Element = {
225
+ get children() {
226
+ return this.childNodes.filter(node => node.nodeType === 1)
227
+ },
225
228
  get firstElementChild() {
226
- return getElement(this.childNodes, 0, 1, 1)
229
+ return getElement(this.childNodes, 0, 1)
227
230
  },
228
231
  get lastElementChild() {
229
- return getElement(this.childNodes, this.childNodes.length - 1, -1, 1)
232
+ return getElement(this.childNodes, this.childNodes.length - 1, -1)
230
233
  },
231
234
  get nextElementSibling() {
232
235
  return getSibling(this, 1, 1)
@@ -414,6 +417,13 @@ extendNode(HTMLElement, Element, {
414
417
  get elements() {
415
418
  return this.tagName === "FORM" ? selector.find(this, "input,select,textarea,button") : undefined
416
419
  },
420
+ get labels() {
421
+ if (!selector.matches(this, "input,select,textarea,button")) return
422
+ var labels = this.id ? selector.find(this.ownerDocument, "label[for='" + this.id + "']") : []
423
+ , parent = selector.closest(this, "label")
424
+ if (parent && labels.indexOf(parent) < 0) labels.push(parent)
425
+ return labels
426
+ },
417
427
  get sheet() {
418
428
  return makeSheet(this)
419
429
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litejs/dom",
3
- "version": "26.3.0",
3
+ "version": "26.4.1",
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>",
@@ -29,6 +29,6 @@
29
29
  "url": "https://github.com/litejs/dom.git"
30
30
  },
31
31
  "devDependencies": {
32
- "@litejs/cli": "26.3.0"
32
+ "@litejs/cli": "26.4.0"
33
33
  }
34
34
  }