From 238866fd26bf64ebda0a7056203e5428ca8e5dc5 Mon Sep 17 00:00:00 2001 From: Commander-lol <ljcapitanio@gmail.com> Date: Tue, 30 Jun 2015 05:19:48 +0100 Subject: [PATCH] Added optional selector to children and clear to delete children --- dist/tinydom.js | 22 ++++++++++++++++++---- dist/tinydom.min.js | 2 +- src/Batch.js | 17 +++++++++++++++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/dist/tinydom.js b/dist/tinydom.js index cef3b34..ffc9dce 100644 --- a/dist/tinydom.js +++ b/dist/tinydom.js @@ -26,7 +26,7 @@ TinyDOMFunction = function (selector) { if (selector === null || typeof (selector) === 'undefined') { this.length = 0; - } else if (typeof (selector) === 'string') { + } else if (selector.substring) { elements = document.querySelectorAll(selector); this.length = elements.length; @@ -34,11 +34,12 @@ TinyDOMFunction = function (selector) { e = elements.item(i); this[i] = e; } - } else if (selector.length) { + } else if (selector.push) { for (i = 0; i < selector.length; i++) { e = selector[i]; this[i] = e; } + this.length = selector.length; } else { this[0] = selector; this.length = 1; @@ -119,13 +120,19 @@ tinyDOM.fn = TinyDOMFunction.prototype = { } return tinyDOM(e); }, - children: function () { + children: function (selector) { var n = this[0].childNodes, a = [], i; for (i = 0; i < n.length; i++) { if (tinyDOM.isElement(n[i])) { - a.push(n[i]); + if (mu.exists(selector)) { + if(n[i].matches(selector)) { + a.push(n[i]); + } + } else { + a.push(n[i]); + } } } return tinyDOM(a); @@ -162,6 +169,13 @@ tinyDOM.fn = TinyDOMFunction.prototype = { } return this; }, + clear: function() { + this.each(function(i, e) { + while(e.firstChild) { + e.removeChild(e.firstChild); + } + }); + }, trigger: function (eventName, data, bubbles, cancelable) { bubbles = tinyDOM.exists(bubbles) ? bubbles : true; cancelable = tinyDOM.exists(cancelable) ? cancelable : true; diff --git a/dist/tinydom.min.js b/dist/tinydom.min.js index 881639f..720120d 100644 --- a/dist/tinydom.min.js +++ b/dist/tinydom.min.js @@ -1,2 +1,2 @@ /*! tinydom 2015-06-30 */ -!function(){"use strict";var a,b,c;Element&&!Element.prototype.matches&&(a=Element.prototype,a.matches=a.matchesSelector||a.mozMatchesSelector||a.msMatchesSelector||a.oMatchesSelector||a.webkitMatchesSelector),b=function(a){var c,d,e;if(!(this instanceof b))return new b(a);if(null===a||"undefined"==typeof a)this.length=0;else if("string"==typeof a)for(c=document.querySelectorAll(a),this.length=c.length,d=0;d<c.length;d++)e=c.item(d),this[d]=e;else if(a.length)for(d=0;d<a.length;d++)e=a[d],this[d]=e;else this[0]=a,this.length=1;return this.apiversion="1",this},c=function(a){return new b(a)},window.μ||(window.μ=c),window.mu||(window.mu=c),c.fn=b.prototype={each:function(a){for(var b=this.length;b--;)a(b,this[b],this);return this},on:function(a,b,c){return"string"==typeof b?this.each(function(d,e){e.addEventListener(a,function(a){var d=a.target,f=!1;do d&&d.matches(b)?(c.call(d,a),f=!0):(d=d.parentNode,d&&d.matches&&d!==e||(f=!0));while(f!==!0)})}):(c=b,this.each(function(b,d){d.addEventListener(a,c)})),this},first:function(){return"undefined"!=typeof this[0]?c(this[0]):null},parent:function(a){var b=this[0].parentNode,d=!0;if(c.exists(a)){for(;null!==b&&b!==document;){if(b.matches(a)){d=!1;break}b=b.parentNode}b=d?null:b}return c(b)},children:function(){var a,b=this[0].childNodes,d=[];for(a=0;a<b.length;a++)c.isElement(b[a])&&d.push(b[a]);return c(d)},data:function(a,b){return"undefined"!=typeof b?(this.each(function(c,d){d.setAttribute("data-"+a,b)}),this):this[0].getAttribute("data-"+a)},attr:function(a,b){return"undefined"!=typeof b?(this.each(function(c,d){d.setAttribute(a,b)}),this):this[0].getAttribute(a)},"class":function(a,b){return c.exists(b)?this.each(function(c,d){d.classList.toggle(a,b)}):this.each(function(b,c){c.classList.toggle(a)}),this},trigger:function(a,b,d,e){d=c.exists(d)?d:!0,e=c.exists(e)?e:!0;var f=new CustomEvent(a,b,d,e);return this.each(function(a,b){b.dispatchEvent(f)}),this}},c.ready=function(a){document.addEventListener("DOMContentLoaded",a)},c.isElement=function(a){var b=!1;try{b=a instanceof HTMLElement}catch(c){b=a.nodeType&&1===a.nodeType}return b},c.exists=function(a){return null!==a&&"undefined"!=typeof a},c.byID=function(a){return c(document.getElementById(a))},c.triggerOn=function(a,b,d,e,f){e=c.exists(e)?e:!0,f=c.exists(f)?f:!0,a.dispatchEvent(new CustomEvent(b,d,e,f))},c.json={keys:function(a){var b,c=[];for(b in a)a.hasOwnProperty(b)&&c.push(b);return c},is:function(a){try{return JSON.parse(a),null!==a&&"undefined"!=typeof a}catch(b){return!1}},merge:function(a,b){if(this.exists(a)&&this.exists(b)){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}return null}},c.ajax=function(a){var b,c,d=new XMLHttpRequest,e={method:"GET",url:"",async:!0,user:null,password:null,responseType:"text",data:null,headers:[],callbacks:{}},f=function(a){return function(b){a(b.currentTarget.response,b)}};if(this.merge(e,a),d.responseType=e.responseType,this.exists(e.callbacks))for(b in e.callbacks)e.callbacks.hasOwnProperty(b)&&d.addEventListener(b,f(e.callbacks[b]));for(d.open(e.method,e.url,e.async,e.user,e.password),c=0;c<e.headers.length;c++)d.setRequestHeader(e.headers[c].header,e.headers[c].value);return d.send(e.data),d}}(); +!function(){"use strict";var a,b,c;Element&&!Element.prototype.matches&&(a=Element.prototype,a.matches=a.matchesSelector||a.mozMatchesSelector||a.msMatchesSelector||a.oMatchesSelector||a.webkitMatchesSelector),b=function(a){var c,d,e;if(!(this instanceof b))return new b(a);if(null===a||"undefined"==typeof a)this.length=0;else if(a.substring)for(c=document.querySelectorAll(a),this.length=c.length,d=0;d<c.length;d++)e=c.item(d),this[d]=e;else if(a.push){for(d=0;d<a.length;d++)e=a[d],this[d]=e;this.length=a.length}else this[0]=a,this.length=1;return this.apiversion="1",this},c=function(a){return new b(a)},window.μ||(window.μ=c),window.mu||(window.mu=c),c.fn=b.prototype={each:function(a){for(var b=this.length;b--;)a(b,this[b],this);return this},on:function(a,b,c){return"string"==typeof b?this.each(function(d,e){e.addEventListener(a,function(a){var d=a.target,f=!1;do d&&d.matches(b)?(c.call(d,a),f=!0):(d=d.parentNode,d&&d.matches&&d!==e||(f=!0));while(f!==!0)})}):(c=b,this.each(function(b,d){d.addEventListener(a,c)})),this},first:function(){return"undefined"!=typeof this[0]?c(this[0]):null},parent:function(a){var b=this[0].parentNode,d=!0;if(c.exists(a)){for(;null!==b&&b!==document;){if(b.matches(a)){d=!1;break}b=b.parentNode}b=d?null:b}return c(b)},children:function(a){var b,d=this[0].childNodes,e=[];for(b=0;b<d.length;b++)c.isElement(d[b])&&(mu.exists(a)?d[b].matches(a)&&e.push(d[b]):e.push(d[b]));return c(e)},data:function(a,b){return"undefined"!=typeof b?(this.each(function(c,d){d.setAttribute("data-"+a,b)}),this):this[0].getAttribute("data-"+a)},attr:function(a,b){return"undefined"!=typeof b?(this.each(function(c,d){d.setAttribute(a,b)}),this):this[0].getAttribute(a)},"class":function(a,b){return c.exists(b)?this.each(function(c,d){d.classList.toggle(a,b)}):this.each(function(b,c){c.classList.toggle(a)}),this},clear:function(){this.each(function(a,b){for(;b.firstChild;)b.removeChild(b.firstChild)})},trigger:function(a,b,d,e){d=c.exists(d)?d:!0,e=c.exists(e)?e:!0;var f=new CustomEvent(a,b,d,e);return this.each(function(a,b){b.dispatchEvent(f)}),this}},c.ready=function(a){document.addEventListener("DOMContentLoaded",a)},c.isElement=function(a){var b=!1;try{b=a instanceof HTMLElement}catch(c){b=a.nodeType&&1===a.nodeType}return b},c.exists=function(a){return null!==a&&"undefined"!=typeof a},c.byID=function(a){return c(document.getElementById(a))},c.triggerOn=function(a,b,d,e,f){e=c.exists(e)?e:!0,f=c.exists(f)?f:!0,a.dispatchEvent(new CustomEvent(b,d,e,f))},c.json={keys:function(a){var b,c=[];for(b in a)a.hasOwnProperty(b)&&c.push(b);return c},is:function(a){try{return JSON.parse(a),null!==a&&"undefined"!=typeof a}catch(b){return!1}},merge:function(a,b){if(this.exists(a)&&this.exists(b)){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}return null}},c.ajax=function(a){var b,c,d=new XMLHttpRequest,e={method:"GET",url:"",async:!0,user:null,password:null,responseType:"text",data:null,headers:[],callbacks:{}},f=function(a){return function(b){a(b.currentTarget.response,b)}};if(this.merge(e,a),d.responseType=e.responseType,this.exists(e.callbacks))for(b in e.callbacks)e.callbacks.hasOwnProperty(b)&&d.addEventListener(b,f(e.callbacks[b]));for(d.open(e.method,e.url,e.async,e.user,e.password),c=0;c<e.headers.length;c++)d.setRequestHeader(e.headers[c].header,e.headers[c].value);return d.send(e.data),d}}(); diff --git a/src/Batch.js b/src/Batch.js index ee6a022..c44bd9e 100644 --- a/src/Batch.js +++ b/src/Batch.js @@ -56,13 +56,19 @@ tinyDOM.fn = TinyDOMFunction.prototype = { } return tinyDOM(e); }, - children: function () { + children: function (selector) { var n = this[0].childNodes, a = [], i; for (i = 0; i < n.length; i++) { if (tinyDOM.isElement(n[i])) { - a.push(n[i]); + if (mu.exists(selector)) { + if(n[i].matches(selector)) { + a.push(n[i]); + } + } else { + a.push(n[i]); + } } } return tinyDOM(a); @@ -99,6 +105,13 @@ tinyDOM.fn = TinyDOMFunction.prototype = { } return this; }, + clear: function() { + this.each(function(i, e) { + while(e.firstChild) { + e.removeChild(e.firstChild); + } + }); + }, trigger: function (eventName, data, bubbles, cancelable) { bubbles = tinyDOM.exists(bubbles) ? bubbles : true; cancelable = tinyDOM.exists(cancelable) ? cancelable : true; -- GitLab