@litejs/dom 25.1.0 → 25.5.0

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.
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  LiteJS DOM – [![Coverage][1]][2] [![Size][3]][4] [![Buy Me A Tea][5]][6]
11
11
  ==========
12
12
 
13
- Dependency-free DOM library for handling HTML and CSS files on server-side.
13
+ Dependency-free DOM library for handling HTML, XML and CSS files on server-side.
14
14
 
15
15
 
16
16
  ```javascript
package/css.js CHANGED
@@ -1,12 +1,38 @@
1
1
 
2
2
  /*! litejs.com/MIT-LICENSE.txt */
3
3
 
4
+ "use strict"
5
+
4
6
  exports.selectorSplit = selectorSplit
5
7
  exports.CSSStyleDeclaration = CSSStyleDeclaration
6
8
  exports.CSSStyleSheet = CSSStyleSheet
7
9
 
8
10
  var fs = require("fs")
9
11
  , path = require("path")
12
+ , read = (sheet, url, enc = "utf8") => fs.readFileSync(path.resolve(sheet.min.root || "", sheet.baseURI, url).split(/[+#]/)[0], enc)
13
+ , plugins = exports.plugins = {
14
+ "data-uri": function(sheet, v) {
15
+ var { DOMParser } = require("./dom.js")
16
+ return v.replace(urlRe, function(_, q1, q2, url) {
17
+ if (q1) return _
18
+ var frag = url.split("#").pop()
19
+ , ext = url.split(/[?#]/)[0].split(".").pop()
20
+ , enc = ext === "svg" ? "utf8" : "base64"
21
+ url = read(sheet, url, enc)
22
+ if (ext === "svg") {
23
+ url = new DOMParser().parseFromString(url, "application/xml")
24
+ if (frag && (frag = url.getElementById(frag))) {
25
+ frag.removeAttribute("id")
26
+ url.documentElement.childNodes = frag.tagName === "g" ? frag.childNodes : [ frag ]
27
+ }
28
+ url = url.toString(true).replace(/#/g, "%23")
29
+ enc = ""
30
+ ext += "+xml"
31
+ }
32
+ return "url('data:image/" + ext + ";" + enc + "," + url + "')"
33
+ })
34
+ }
35
+ }
10
36
  , urlRe = /(["']).*?\1|url\((['"]?)(?!\/|data:|https?:)(.*?)\2\)/g
11
37
  , clearFn = (_, q, str, c) =>
12
38
  q ? (q = str.indexOf("'") == -1 ? "'" : "\"", q + str.replace(q === "'" ? /\\(")/g : /\\(')/g, "$1")) + q :
@@ -19,7 +45,6 @@ var fs = require("fs")
19
45
  , clear = s => s
20
46
  .replace(/("|')((?:\\\1|[^\1])*?)\1|\s*(\/)\*(?:[^*]|\*(?!\/))*\*\/\s*|(?:[^"'\/]|\/(?!\*))+/g, clearFn)
21
47
  .replace(/(["']).*?\1|url\(("|')([^'"()\s]+)\2\)/g, (m,q1,q2,u) => q1 ? m : "url(" + u + ")")
22
- , read = (sheet, url, enc = "utf8") => fs.readFileSync(path.resolve(sheet.min.root || "", sheet.baseURI, url), enc)
23
48
  , toRgb = {
24
49
  rgb(r, g, b) {
25
50
  var f = n => ((n | 0) + 256).toString(16).slice(1)
@@ -63,7 +88,7 @@ var fs = require("fs")
63
88
  , lastIdx = {}
64
89
  for (; (m = re.exec(val)); ) {
65
90
  if (m[4]) {
66
- if (min && len) style[k = style[len - 1]] = clear(transformValue(m[4].trim(), style[k]))
91
+ if (min && len && plugins[m[4] = m[4].trim()]) style[k = style[len - 1]] = clear(plugins[m[4]](sheet, style[k]))
67
92
  continue
68
93
  } else {
69
94
  k = m[1]
@@ -78,24 +103,7 @@ var fs = require("fs")
78
103
  if (!style[prop]) style[style.length++] = prop
79
104
  style[prop] = style[prop === "cssFloat" ? "float" : prop.replace(/[A-Z]/g, "-$&").toLowerCase()] = clear(val)
80
105
  }
81
- function transformValue(cmd, v) {
82
- var { DOMParser } = require("./dom.js")
83
- if (cmd === "data-uri") {
84
- return v.replace(urlRe, function(_, q1, q2, url) {
85
- if (q1) return _
86
- var ext = url.split(".").pop()
87
- , enc = ext === "svg" ? "utf8" : "base64"
88
- url = read(sheet, url, enc)
89
- if (ext === "svg") {
90
- enc = ""
91
- ext += "+xml"
92
- url = new DOMParser().parseFromString(url, "application/xml").toString(true).replace(/#/g, "%23")
93
- }
94
- return "url('data:image/" + ext + ";" + enc + "," + url + "')"
95
- })
96
- }
97
- return v
98
- }
106
+ return true
99
107
  }
100
108
  }
101
109
  , ruleTypes = {
package/dom.js CHANGED
@@ -1,6 +1,8 @@
1
1
 
2
2
  /*! litejs.com/MIT-LICENSE.txt */
3
3
 
4
+ "use strict"
5
+
4
6
  var boolAttrs = {
5
7
  async:1, autoplay:1, loop:1, checked:1, defer:1, disabled:1, muted:1, multiple:1, nomodule:1, playsinline:1, readonly:1, required:1, selected:1
6
8
  }
@@ -44,9 +46,12 @@ var boolAttrs = {
44
46
  },
45
47
  set textContent(text) {
46
48
  if (this.nodeType === 3 || this.nodeType === 8) this.data = text
47
- else replaceChildren.call(this, this.ownerDocument.createTextNode(
48
- rawTextEscape[this.tagName] ? text.replace(rawTextEscape[this.tagName], "<\\") : text
49
- ))
49
+ else {
50
+ removeChilds(this)
51
+ this.appendChild(this.ownerDocument.createTextNode(
52
+ rawTextEscape[this.tagName] ? text.replace(rawTextEscape[this.tagName], "<\\") : text
53
+ ))
54
+ }
50
55
  },
51
56
  get firstChild() {
52
57
  return this.childNodes && this.childNodes[0] || null
@@ -96,7 +101,8 @@ var boolAttrs = {
96
101
  )
97
102
  }
98
103
  }
99
- replaceChildren.call(node, frag)
104
+ removeChilds(node)
105
+ node.appendChild(frag)
100
106
 
101
107
  function setAttr(_, name, value, q, qvalue) {
102
108
  child.setAttribute(name, (q ? qvalue : value || "").replace(unescRe, unescFn))
@@ -119,6 +125,21 @@ var boolAttrs = {
119
125
  set style(value) {
120
126
  this.style.cssText = value
121
127
  },
128
+ appendChild(el) {
129
+ return this.insertBefore(el)
130
+ },
131
+ cloneNode(deep) {
132
+ var node = this
133
+ , clone = new node.constructor(node.tagName || node.data)
134
+ clone.ownerDocument = node.ownerDocument
135
+
136
+ mergeAttributes(node, clone)
137
+
138
+ if (deep && node.hasChildNodes()) {
139
+ node.childNodes.forEach(child => clone.appendChild(child.cloneNode(deep)))
140
+ }
141
+ return clone
142
+ },
122
143
  contains(el) {
123
144
  for (; el; el = el.parentNode) if (el === this) return true
124
145
  return false
@@ -126,12 +147,6 @@ var boolAttrs = {
126
147
  hasChildNodes() {
127
148
  return !!this.firstChild
128
149
  },
129
- getElementById(id) {
130
- return selector.find(this, "#" + id, 1)
131
- },
132
- appendChild(el) {
133
- return this.insertBefore(el)
134
- },
135
150
  insertBefore(el, ref) {
136
151
  var node = this
137
152
  , childs = node.childNodes
@@ -164,18 +179,6 @@ var boolAttrs = {
164
179
  this.insertBefore(el, ref)
165
180
  return this.removeChild(ref)
166
181
  },
167
- cloneNode(deep) {
168
- var node = this
169
- , clone = new node.constructor(node.tagName || node.data)
170
- clone.ownerDocument = node.ownerDocument
171
-
172
- mergeAttributes(node, clone)
173
-
174
- if (deep && node.hasChildNodes()) {
175
- node.childNodes.forEach(child => clone.appendChild(child.cloneNode(deep)))
176
- }
177
- return clone
178
- },
179
182
  querySelector(sel) {
180
183
  return selector.find(this, sel, 1)
181
184
  },
@@ -202,7 +205,10 @@ var boolAttrs = {
202
205
  get previousElementSibling() {
203
206
  return getSibling(this, -1, 1)
204
207
  },
205
- replaceChildren: replaceChildren,
208
+ replaceChildren() {
209
+ removeChilds(this)
210
+ for (var i = 0, l = arguments.length; i < l; ) this.insertBefore(arguments[i++])
211
+ },
206
212
  hasAttribute(name) {
207
213
  return this.attributes.getNamedItem(name) != null
208
214
  },
@@ -235,18 +241,18 @@ var boolAttrs = {
235
241
  "&sect;": "§", "&sup2;": "²", "&sup3;": "³", "&yen;": "¥"
236
242
  }
237
243
 
238
- Object.keys(boolAttrs).forEach(addGetter, { isBool: true, readonly: "readOnly" })
239
- numAttrs.split(" ").forEach(addGetter, { isNum: true })
240
- strAttrs.split(" ").forEach(addGetter, { "for": "htmlFor", "class": "className" })
244
+ Object.keys(boolAttrs).forEach(key => addGetter(key, { isBool: true, readonly: "readOnly" }))
245
+ numAttrs.split(" ").forEach(key => addGetter(key, { isNum: true }))
246
+ strAttrs.split(" ").forEach(key => addGetter(key, { "for": "htmlFor", "class": "className" }))
241
247
 
242
- function addGetter(key) {
248
+ function addGetter(key, opts) {
243
249
  var attr = key.toLowerCase()
244
- Object.defineProperty(Element, this[key] || key, {
250
+ Object.defineProperty(Element, opts[key] || key, {
245
251
  configurable: true,
246
252
  enumerable: true,
247
253
  get: (
248
- this.isBool ? function() { return this.hasAttribute(attr) } :
249
- this.isNum ? function() { return +this.getAttribute(attr) || 0 } :
254
+ opts.isBool ? function() { return this.hasAttribute(attr) } :
255
+ opts.isNum ? function() { return +this.getAttribute(attr) || 0 } :
250
256
  function() { return this.getAttribute(attr) || "" }
251
257
  ),
252
258
  set(value) {
@@ -263,7 +269,7 @@ function addGetter(key) {
263
269
 
264
270
  function Attr(node, name, value) {
265
271
  this.ownerElement = node
266
- this.name = name.toLowerCase()
272
+ this.name = name
267
273
  this.value = "" + value
268
274
  }
269
275
 
@@ -295,7 +301,7 @@ NamedNodeMap.prototype = {
295
301
  },
296
302
  setNamedItem(attr) {
297
303
  var oldAttr = this.getNamedItem(attr.name)
298
- this[attr.name] = attr
304
+ this[attr.name.toLowerCase()] = attr
299
305
  return oldAttr
300
306
  },
301
307
  toString(minify) {
@@ -315,6 +321,7 @@ NamedNodeMap.prototype = {
315
321
  if (!quotedAttrRe.test(value)) return name + "=" + value
316
322
  if (value.split("\"").length > value.split("'").length) return name + "='" + value.replace(/'/g, "&#39;") + "'"
317
323
  }
324
+ name = loName
318
325
  }
319
326
  return name + "=\"" + value.replace(/"/g, "&quot;") + "\""
320
327
  }).filter(Boolean).join(" ")
@@ -339,16 +346,22 @@ function HTMLElement(tag) {
339
346
  }
340
347
 
341
348
  extendNode(HTMLElement, Element, {
349
+ localName: null,
350
+ namespaceURI: "http://www.w3.org/1999/xhtml",
342
351
  nodeType: 1,
343
- matches(sel) {
344
- return selector.matches(this, sel)
352
+ tagName: null,
353
+ blur() {
354
+ this.ownerDocument.activeElement = null
345
355
  },
346
356
  closest(sel) {
347
357
  return selector.closest(this, sel)
348
358
  },
349
- namespaceURI: "http://www.w3.org/1999/xhtml",
350
- localName: null,
351
- tagName: null,
359
+ focus() {
360
+ this.ownerDocument.activeElement = this
361
+ },
362
+ matches(sel) {
363
+ return selector.matches(this, sel)
364
+ },
352
365
  toString(minify) {
353
366
  var attrs = this.attributes.toString(minify)
354
367
  , isXml = this.ownerDocument.contentType === "application/xml"
@@ -436,7 +449,10 @@ extendNode(Document, Element, {
436
449
  createTextNode: own(Text),
437
450
  createComment: own(Comment),
438
451
  createDocumentType: own(DocumentType), //Should be document.implementation.createDocumentType(name, publicId, systemId)
439
- createDocumentFragment: own(DocumentFragment)
452
+ createDocumentFragment: own(DocumentFragment),
453
+ getElementById(id) {
454
+ return selector.find(this, "#" + id, 1)
455
+ }
440
456
  })
441
457
 
442
458
  function DOMParser() {}
@@ -470,9 +486,9 @@ function extendNode(obj, extras) {
470
486
  obj.prototype.constructor = obj
471
487
  }
472
488
 
473
- function replaceChildren() {
474
- for (var arr = this.childNodes, i = 0, l = arr.length; i < l; ) arr[i++].parentNode = null
475
- for (i = arr.length = 0, l = arguments.length; i < l; ) this.insertBefore(arguments[i++])
489
+ function removeChilds(node) {
490
+ node.childNodes.forEach(child => child.parentNode = null)
491
+ node.childNodes.length = 0
476
492
  }
477
493
 
478
494
  function getElement(childs, index, step, type) {
package/net.js CHANGED
@@ -1,6 +1,8 @@
1
1
 
2
2
  /*! litejs.com/MIT-LICENSE.txt */
3
3
 
4
+ "use strict"
5
+
4
6
  var DOM = require(".")
5
7
  , URL = require("url").URL
6
8
  , parser = new DOM.DOMParser()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litejs/dom",
3
- "version": "25.1.0",
3
+ "version": "25.5.0",
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>",
package/selector.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
-
3
2
  /*! litejs.com/MIT-LICENSE.txt */
4
3
 
4
+ "use strict"
5
5
 
6
6
  !function(exports) {
7
7
  var selectorCache = {
package/interactive.js DELETED
@@ -1,18 +0,0 @@
1
-
2
-
3
- /*! litejs.com/MIT-LICENSE.txt */
4
-
5
-
6
- var DOM = module.exports = require(".")
7
- , HTMLElementExtra = {
8
- focus() {
9
- this.ownerDocument.activeElement = this
10
- },
11
- blur() {
12
- this.ownerDocument.activeElement = null
13
- }
14
- }
15
-
16
- Object.assign(DOM.HTMLElement.prototype, HTMLElementExtra)
17
-
18
-