Comparing version 2.0.1 to 2.1.0
@@ -7,2 +7,9 @@ # Change Log | ||
## 2.1.0 - 2017-10-20 | ||
### Added | ||
* Restore the second argument in `d.get()` and `d.getAll()` to define a context | ||
* Added a polyfill to `:scope` selector | ||
## 2.0.1 - 2017-08-04 | ||
@@ -9,0 +16,0 @@ |
64
d.js
@@ -16,7 +16,10 @@ 'use strict'; | ||
var d = { | ||
/* | ||
* Select the first element | ||
*/ | ||
get: function(query) { | ||
get: function(query, context) { | ||
if (context) { | ||
return context.querySelector(query); | ||
} | ||
if (typeof query === 'string') { | ||
@@ -46,3 +49,7 @@ return document.querySelector(query); | ||
*/ | ||
getAll: function(query) { | ||
getAll: function(query, context) { | ||
if (context) { | ||
return context.querySelectorAll(query); | ||
} | ||
if (query instanceof NodeList) { | ||
@@ -308,2 +315,53 @@ return query; | ||
// https://developer.mozilla.org/en-US/docs/Web/CSS/:scope | ||
// Polyfill https://github.com/lazd/scopedQuerySelectorShim | ||
try { | ||
div.querySelectorAll(':scope *'); | ||
} catch (e) { | ||
// Match usage of scope | ||
var scopeRegexp = /:scope/g; | ||
// Overrides | ||
['querySelector', 'querySelectorAll'].forEach(function(methodName) { | ||
var original = HTMLElement.prototype[methodName]; | ||
// Override the method | ||
HTMLElement.prototype[methodName] = function(query) { | ||
if (!query.match(scopeRegexp)) { | ||
return original.call(this, query); | ||
} | ||
var nodeList, removeId; | ||
// Temporary container | ||
if (!this.parentNode) { | ||
div.appendChild(this); | ||
} | ||
// Temporary id | ||
if (!this.id) { | ||
this.id = Math.random() | ||
.toString() | ||
.replace('0.', 'id_' + Date.now()); | ||
removeId = true; | ||
} | ||
nodeList = original.call( | ||
this.parentNode, | ||
query.replace(scopeRegexp, '#' + this.id) | ||
); | ||
if (removeId) { | ||
this.id = ''; | ||
} | ||
if (this.parentNode === div) { | ||
div.removeChild(this); | ||
} | ||
return nodeList; | ||
}; | ||
}); | ||
} | ||
/****************************** | ||
@@ -310,0 +368,0 @@ * Helpers functions |
{ | ||
"name": "d_js", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "DOM manipulation micro-library", | ||
@@ -5,0 +5,0 @@ "main": "d.js", |
@@ -25,5 +25,3 @@ # d.js | ||
//Handle events | ||
d.on('click', buttons, function () { | ||
alert('clicked'); | ||
}); | ||
d.on('click', buttons, () => alert('clicked')); | ||
``` | ||
@@ -53,5 +51,3 @@ | ||
```js | ||
d.get('.button').forEach(function (el) { | ||
el.classList.add('selected'); | ||
}); | ||
d.getAll('.button').forEach(el => el.classList.add('selected')); | ||
``` | ||
@@ -208,1 +204,2 @@ | ||
* `NodeList.prototype.forEach` | ||
* `:scope` selector |
Sorry, the diff of this file is not supported yet
31059
407
203