Permalink
Browse files

Version 0.3

Improved selector stickiness, added browser support to readme
  • Loading branch information...
1 parent df76308 commit cda85fc60f264e826af396956aff76eb4d8df563 @kylebarrow committed Jul 18, 2012
Showing with 62 additions and 37 deletions.
  1. +19 −2 README.md
  2. +1 −1 chibi-min.js
  3. +42 −34 chibi.js
View
@@ -1,4 +1,4 @@
-# Chibi v0.2
+# Chibi v0.3
#### A tiny JavaScript micro-framework
@@ -8,7 +8,7 @@ Because you understand we are all born with a finite number of keystrokes, you'v
### The sweet, juicy bits ###
* Chibi is really tiny: 5KB minified, 2KB gzipped, small enough to stick inline on single page web apps, saving an extra HTTP request.
-* Supports modern desktop and mobile browsers including Chrome, Internet Explorer, Firefox, Opera and Safari.
+* Supports modern desktop and mobile browsers including Chrome, Internet Explorer, Firefox, Opera and Safari (see Browser Support below).
* Supports creaky old browsers like IE7. Chibi may also support IE6 but this hasn't been tested and I really don't care.
* No animation cruft, instead use CSS transitions like a nice person.
* In modern browsers, Chibi typically executes DOM manipulation 20% to 50% faster than grown-up frameworks.
@@ -17,6 +17,23 @@ Because you understand we are all born with a finite number of keystrokes, you'v
* Chibi's polyfill for `document.querySelectorAll()` is limited to browser CSS support and is not as fast of some dedicated selector engines. This means no `input[type=text]` or `p:nth-child(even)` selectors with IE7. Fortunately modern browser don't need this polyfill.
* Ancient browsers that support neither `document.querySelectorAll()` nor `window.getComputedStyle` can bugger off.
+### Browser Support ####
+Chibi has been tested with and supports the following browsers:
+
+* Android Browser 2 or higher
+* Blackberry Browser 6 or higher
+* Chrome
+* Chrome Android
+* Internet Explorer 7 or higher
+* Internet Explorer Mobile 9 or higher
+* Firefox 3 or higher
+* Firefox Mobile
+* Opera 10 or higher
+* Opera Mobile 10 or higher
+* Safari 3 or higher
+* Safari Mobile 3 or higher
+* Symbian^3 Browser or higher
+
### Using Chibi
Chibi syntax is similar to that pioneered by jQuery: `$(selector).method()`. It intentionally uses the same `$` namespace as jQuery because micro-frameworks and grown-up frameworks should never mix.
View
@@ -1 +1 @@
-/* Chibi v0.2, (C) 2012 Kyle Barrow, GPLv3 Licensed */(function(){"use strict";function u(){r=!0;for(var e=0;e<t.length;e++)t[e]();t=[]}function a(){i=!0;r||u();for(var e=0;e<n.length;e++)n[e]();n=[]}function f(t){var n=e.length;while(n--)t(e[n])}function l(e,t){return e.currentStyle?e.currentStyle[t]:o.getComputedStyle?o.getComputedStyle(e,null).getPropertyValue(t):null}function c(e,t){return encodeURIComponent(e).replace(/%20/g,"+")+"="+encodeURIComponent(t).replace(/%20/g,"+")}function h(t){var n=[];if(t)if(t.nodeType&&t.nodeType===1)n=[t];else if(typeof t=="object"||typeof t.length=="number"&&typeof t.item=="function")n=t;else if(typeof t=="string"){s.querySelectorAll||(s.querySelectorAll=function(e){var t,n,r,i=[];t=s.createElement("STYLE");t.type="text/css";if(t.styleSheet){t.styleSheet.cssText=e+" {a:b}";if(s.getElementsByTagName("head").length===0){n=s.createElement("head");s.getElementsByTagName("html")[0].appendChild(n)}s.getElementsByTagName("head")[0].appendChild(t);r=s.getElementsByTagName("*");for(var o=0;o<r.length;o++)l(r[o],"a")==="b"?i.push(r[o]):0;s.getElementsByTagName("head")[0].removeChild(t)}return i});n=s.querySelectorAll(t)}e=[];for(var r=0;r<n.length;r++)e[r]=n[r]}function p(e,t,n){try{e.style[t]=n}catch(r){}}function d(e){e=e.reverse();e.length===1?e=e[0]:0;return e}function v(e){var t="",n;if(e.constructor===Object){for(n in e)if(e.hasOwnProperty(n))if(e[n].constructor===Array)for(var r=0;r<e[n].length;r++)t+="&"+c(n,e[n][r]);else t+="&"+c(n,e[n])}else f(function(e){if(e.nodeName==="FORM")for(var r=0;r<e.elements.length;r++){n=e.elements[r];if(!n.disabled)switch(n.type){case"button":case"image":case"file":case"submit":case"reset":break;case"select-one":n.length>0?t+="&"+c(n.name,n.value):0;break;case"select-multiple":for(var i=0;i<n.length;i++)n[i].selected?t+="&"+c(n.name,n[i].value):0;break;case"checkbox":case"radio":n.checked?t+="&"+c(n.name,n.value):0;break;default:t+="&"+c(n.name,n.value)}}});t.length>0?t=t.substring(1):0;return t}function m(s){h(s);return{ready:function(e){e&&(r?e():t.push(e))},loaded:function(e){e&&(i?e():n.push(e))},find:function(t){if(t){var n=[];switch(t){case"first":e.length>0?e=[e.shift()]:0;break;case"last":e.length>0?e=[e.pop()]:0;break;case"odd":for(var r=0;r<e.length;r+=2)n.push(e[r]);e=n;break;case"even":for(var r=1;r<e.length;r+=2)n.push(e[r]);e=n}}return e.length>0?e:!1},hide:function(){f(function(e){e.style.display="none"})},show:function(){f(function(e){e.style.display="";l(e,"display")==="none"?e.style.display="block":0})},toggle:function(){f(function(e){if(l(e,"display")==="none"){e.style.display="";l(e,"display")==="none"?e.style.display="block":0}else e.style.display="none"})},remove:function(){var t=e.length;f(function(e){e.parentNode.removeChild(e)})},css:function(e,t){var n=[];f(function(r){t?p(r,e,t):r.style[e]?n.push(r.style[e]):l(r,e)?n.push(l(r,e)):n.push(null)});if(n.length>0){n=n.reverse();n.length===1?n=n[0]:0;return n}},cls:function(e,t){var n=[],r,i;if(e){r=e.split(" ");t=t||"replace"}f(function(s){if(e)switch(t){case"add":s.className=s.className+" "+e;break;case"remove":for(var o=0;o<r.length;o++){i=new RegExp(r[o],"g");s.className=s.className.replace(i,"")}break;case"replace":s.className=e}else n.push(s.className)});if(n.length>0)return d(n)},html:function(e,t){var n=[],r,i;f(function(s){if(t){r=document.createElement("div");r.innerHTML=e;while(i=r.lastChild)t==="before"?s.parentNode.insertBefore(i,s):t==="after"&&s.parentNode.insertBefore(i,s.nextSibling)}else e?s.innerHTML=e:n.push(s.innerHTML)});if(n.length>0)return d(n)},attr:function(e,t){var n=[];f(function(r){if(e){e=e.toLowerCase();switch(e){case"style":t?r.style.cssText=t:r.style.cssText?n.push(r.style.cssText):n.push(null);break;case"class":t?r.className=t:r.className?n.push(r.className):n.push(null);break;default:t?r.setAttribute(e,t):r.getAttribute(e)?n.push(r.getAttribute(e)):n.push(null)}}});if(n.length>0)return d(n)},val:function(e){var t=[],n=[];typeof e!="undefined"&&typeof e!="object"&&(e=[e]);f(function(r){if(e)switch(r.nodeName){case"SELECT":for(var i=0;i<r.length;i++)for(var s=0;s<e.length;s++){r[i].selected="";if(r[i].value===e[s]){r[i].selected="selected";break}}break;case"INPUT":switch(r.type){case"checkbox":case"radio":r.checked="";for(var i=0;i<e.length;i++)if(r.value===e[i]){r.checked="checked";break}break;default:r.value=e[0]}break;case"TEXTAREA":case"BUTTON":r.value=e[0]}else switch(r.nodeName){case"SELECT":var o=n.length;n.push([]);for(var i=0;i<r.length;i++)r[i].selected?n[o].push(r[i].value):0;switch(n[o].length){case 0:n[o]=null;break;case 1:n[o]=n[o][0]}break;case"INPUT":switch(r.type){case"checkbox":r.checked?n.push(r.value):n.push(null);break;case"radio":var u=!1;for(var i=0;i<t.length;i++)if(t[i][0]===r.name){r.checked?n[t[i][1]]=r.value:0;u=!0}if(!u){t.push([r.name,n.length]);r.checked?n.push(r.value):n.push(null)}break;default:n.push(r.value)}break;case"TEXTAREA":case"BUTTON":n.push(r.value)}});if(n.length>0)return d(n)},ajax:function(t,n,r,i){var s,n=n||"GET",u=v(e),a="_ts="+ +(new Date);o.XMLHttpRequest?s=new XMLHttpRequest:o.ActiveXObject&&(s=new ActiveXObject("Microsoft.XMLHTTP"));if(s){n=n.toUpperCase();t=i?n==="POST"?t+"?"+a:t+"?"+u+"&"+a:n==="POST"?t:t+"?"+u;s.open(n,t,!0);n==="POST"?s.setRequestHeader("Content-Type","application/x-www-form-urlencoded"):0;s.send(u);s.onreadystatechange=function(){if(s.readyState===4&&s.status===200&&r)try{r(s.responseText)}catch(e){}}}}}}var e=[],t=[],n=[],r=!1,i=!1,s=document,o=window;if(s.addEventListener){s.addEventListener("DOMContentLoaded",u,!1);o.addEventListener("load",a,!1)}else if(s.attachEvent){s.attachEvent("onreadystatechange",u);o.attachEvent("onload",a)}else o.onload=a;o.$=m})();
+/* Chibi v0.3, Chibi v0.3, (C) 2012 Kyle Barrow, GPLv3 Licensed */(function(){"use strict";function o(){n=!0;for(var t=0;t<e.length;t++)e[t]();e=[]}function u(){r=!0;n||o();for(var e=0;e<t.length;e++)t[e]();t=[]}function a(e,t){var n=t.length;while(n--)e(t[n])}function f(e,t){return e.currentStyle?e.currentStyle[t]:s.getComputedStyle?s.getComputedStyle(e,null).getPropertyValue(t):null}function l(e,t){return encodeURIComponent(e).replace(/%20/g,"+")+"="+encodeURIComponent(t).replace(/%20/g,"+")}function c(e){var t=[],n=[];if(e)if(e.nodeType&&e.nodeType===1)t=[e];else if(typeof e=="object"||typeof e.length=="number"&&typeof e.item=="function")t=e;else if(typeof e=="string"){i.querySelectorAll||(i.querySelectorAll=function(e){var t,n,r,s=[];t=i.createElement("STYLE");t.type="text/css";if(t.styleSheet){t.styleSheet.cssText=e+" {a:b}";if(i.getElementsByTagName("head").length===0){n=i.createElement("head");i.getElementsByTagName("html")[0].appendChild(n)}i.getElementsByTagName("head")[0].appendChild(t);r=i.getElementsByTagName("*");for(var o=0;o<r.length;o++)f(r[o],"a")==="b"?s.push(r[o]):0;i.getElementsByTagName("head")[0].removeChild(t)}return s});t=i.querySelectorAll(e)}for(var r=0;r<t.length;r++)n[r]=t[r];return n}function h(e,t,n){try{e.style[t]=n}catch(r){}}function p(e){e=e.reverse();e.length===1?e=e[0]:0;return e}function d(e){var t="",n;if(e.constructor===Object){for(n in e)if(e.hasOwnProperty(n))if(e[n].constructor===Array)for(var r=0;r<e[n].length;r++)t+="&"+l(n,e[n][r]);else t+="&"+l(n,e[n])}else a(function(e){if(e.nodeName==="FORM")for(var r=0;r<e.elements.length;r++){n=e.elements[r];if(!n.disabled)switch(n.type){case"button":case"image":case"file":case"submit":case"reset":break;case"select-one":n.length>0?t+="&"+l(n.name,n.value):0;break;case"select-multiple":for(var i=0;i<n.length;i++)n[i].selected?t+="&"+l(n.name,n[i].value):0;break;case"checkbox":case"radio":n.checked?t+="&"+l(n.name,n.value):0;break;default:t+="&"+l(n.name,n.value)}}},e);t.length>0?t=t.substring(1):0;return t}function v(i){var o=c(i);return{ready:function(t){t&&(n?t():e.push(t))},loaded:function(e){e&&(r?e():t.push(e))},find:function(e){if(e){var t=[];switch(e){case"first":o.length>0?o=[o.shift()]:0;break;case"last":o.length>0?o=[o.pop()]:0;break;case"odd":for(var n=0;n<o.length;n+=2)t.push(o[n]);o=t;break;case"even":for(var n=1;n<o.length;n+=2)t.push(o[n]);o=t}}return o.length>0?o:!1},hide:function(){a(function(e){e.style.display="none"},o)},show:function(){a(function(e){e.style.display="";f(e,"display")==="none"?e.style.display="block":0},o)},toggle:function(){a(function(e){if(f(e,"display")==="none"){e.style.display="";f(e,"display")==="none"?e.style.display="block":0}else e.style.display="none"},o)},remove:function(){var e=o.length;a(function(e){try{e.parentNode.removeChild(e)}catch(t){}},o);o=[]},css:function(e,t){var n=[];a(function(r){t?h(r,e,t):r.style[e]?n.push(r.style[e]):f(r,e)?n.push(f(r,e)):n.push(null)},o);if(n.length>0){n=n.reverse();n.length===1?n=n[0]:0;return n}},cls:function(e,t){var n=[],r,i;if(e){r=e.split(" ");t=t||"replace"}a(function(s){if(e)switch(t){case"add":s.className=s.className+" "+e;break;case"remove":for(var o=0;o<r.length;o++){i=new RegExp(r[o],"g");s.className=s.className.replace(i,"")}break;case"replace":s.className=e}else n.push(s.className)},o);if(n.length>0)return p(n)},html:function(e,t){var n=[],r,i;a(function(s){if(t){r=document.createElement("div");r.innerHTML=e;while(i=r.lastChild)try{t==="before"?s.parentNode.insertBefore(i,s):t==="after"&&s.parentNode.insertBefore(i,s.nextSibling)}catch(o){break}}else e?s.innerHTML=e:n.push(s.innerHTML)},o);if(n.length>0)return p(n)},attr:function(e,t){var n=[];a(function(r){if(e){e=e.toLowerCase();switch(e){case"style":t?r.style.cssText=t:r.style.cssText?n.push(r.style.cssText):n.push(null);break;case"class":t?r.className=t:r.className?n.push(r.className):n.push(null);break;default:t?r.setAttribute(e,t):r.getAttribute(e)?n.push(r.getAttribute(e)):n.push(null)}}},o);if(n.length>0)return p(n)},val:function(e){var t=[],n=[];typeof e!="undefined"&&typeof e!="object"&&(e=[e]);a(function(r){if(e)switch(r.nodeName){case"SELECT":for(var i=0;i<r.length;i++)for(var s=0;s<e.length;s++){r[i].selected="";if(r[i].value===e[s]){r[i].selected="selected";break}}break;case"INPUT":switch(r.type){case"checkbox":case"radio":r.checked="";for(var i=0;i<e.length;i++)if(r.value===e[i]){r.checked="checked";break}break;default:r.value=e[0]}break;case"TEXTAREA":case"BUTTON":r.value=e[0]}else switch(r.nodeName){case"SELECT":var o=n.length;n.push([]);for(var i=0;i<r.length;i++)r[i].selected?n[o].push(r[i].value):0;switch(n[o].length){case 0:n[o]=null;break;case 1:n[o]=n[o][0]}break;case"INPUT":switch(r.type){case"checkbox":r.checked?n.push(r.value):n.push(null);break;case"radio":var u=!1;for(var i=0;i<t.length;i++)if(t[i][0]===r.name){r.checked?n[t[i][1]]=r.value:0;u=!0}if(!u){t.push([r.name,n.length]);r.checked?n.push(r.value):n.push(null)}break;default:n.push(r.value)}break;case"TEXTAREA":case"BUTTON":n.push(r.value)}},o);if(n.length>0)return p(n)},ajax:function(e,t,n,r){var i,t=t||"GET",u=d(o),a="_ts="+ +(new Date);s.XMLHttpRequest?i=new XMLHttpRequest:s.ActiveXObject&&(i=new ActiveXObject("Microsoft.XMLHTTP"));if(i){t=t.toUpperCase();e=r?t==="POST"?e+"?"+a:e+"?"+u+"&"+a:t==="POST"?e:e+"?"+u;i.open(t,e,!0);t==="POST"?i.setRequestHeader("Content-Type","application/x-www-form-urlencoded"):0;i.send(u);i.onreadystatechange=function(){if(i.readyState===4&&i.status===200&&n)try{n(i.responseText)}catch(e){}}}}}}var e=[],t=[],n=!1,r=!1,i=document,s=window;if(i.addEventListener){i.addEventListener("DOMContentLoaded",o,!1);s.addEventListener("load",u,!1)}else if(i.attachEvent){i.attachEvent("onreadystatechange",o);s.attachEvent("onload",u)}else s.onload=u;s.$=v})();
View
@@ -1,4 +1,4 @@
-/* Chibi v0.2, Copyright (C) 2012 Kyle Barrow
+/* Chibi v0.3, Copyright (C) 2012 Kyle Barrow
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -8,8 +8,7 @@ You should have received a copy of the GNU General Public License along with thi
(function() {
'use strict';
- var nodes = [],
- readyfn = [],
+ var readyfn = [],
loadedfn = [],
domready = false,
pageloaded = false,
@@ -61,7 +60,7 @@ You should have received a copy of the GNU General Public License along with thi
// Utility functions
// Loop through node array
- function nodeLoop(fn) {
+ function nodeLoop(fn,nodes) {
// Good idea to walk up the DOM
var i = nodes.length;
@@ -85,7 +84,7 @@ You should have received a copy of the GNU General Public License along with thi
// Get nodes
function getNodes(selector) {
- var nodelist = [];
+ var nodelist = [], nodes = [];
if (selector) {
@@ -140,12 +139,13 @@ You should have received a copy of the GNU General Public License along with thi
}
// Convert node list to array so find() results have full access to array methods
- nodes = []; // reset node array
// Array.prototype.slice.call not supported in IE < 9 and often slower than loop anyway
for (var i = 0; i < nodelist.length; i++) {
nodes[i] = nodelist[i];
}
+
+ return nodes;
}
// Set CSS, important to wrap in try to prevent error thown on unsupported property
@@ -167,20 +167,20 @@ You should have received a copy of the GNU General Public License along with thi
}
// Serialize form & JSON values
- function serializeData(node) {
+ function serializeData(nodes) {
var querystring = '',
subelm;
- if (node.constructor === Object) { // Serialize JSON data
- for (subelm in node) {
- if (node.hasOwnProperty(subelm)) {
- if (node[subelm].constructor === Array) {
- for (var i = 0; i < node[subelm].length; i++) {
- querystring += '&' + queryPair(subelm, node[subelm][i]);
+ if (nodes.constructor === Object) { // Serialize JSON data
+ for (subelm in nodes) {
+ if (nodes.hasOwnProperty(subelm)) {
+ if (nodes[subelm].constructor === Array) {
+ for (var i = 0; i < nodes[subelm].length; i++) {
+ querystring += '&' + queryPair(subelm, nodes[subelm][i]);
}
}
else {
- querystring += '&' + queryPair(subelm, node[subelm]);
+ querystring += '&' + queryPair(subelm, nodes[subelm]);
}
}
}
@@ -224,7 +224,7 @@ You should have received a copy of the GNU General Public License along with thi
}
}
}
- });
+ },nodes);
}
// Tidy up first &
@@ -236,7 +236,7 @@ You should have received a copy of the GNU General Public License along with thi
function chibi(selector) {
- getNodes(selector);
+ var nodes = getNodes(selector);
// Public functions
return {
@@ -288,7 +288,7 @@ You should have received a copy of the GNU General Public License along with thi
hide: function() {
nodeLoop(function(elm) {
elm.style.display = 'none';
- });
+ },nodes);
},
// Show node
show: function() {
@@ -297,7 +297,7 @@ You should have received a copy of the GNU General Public License along with thi
// For elements still hidden by style block
(computeStyle(elm, 'display') === 'none') ? elm.style.display = 'block' : 0;
- });
+ },nodes);
},
// Toggle node display
toggle: function() {
@@ -314,15 +314,22 @@ You should have received a copy of the GNU General Public License along with thi
elm.style.display = 'none';
}
- });
+ },nodes);
},
// Remove node
remove: function() {
var removed = nodes.length;
nodeLoop(function(elm) {
- elm.parentNode.removeChild(elm);
- });
+ // Catch error in unlikely case elm has been removed
+ try {
+ elm.parentNode.removeChild(elm);
+ }
+ catch (e) {}
+ },nodes);
+
+ // Clear nodes after remove
+ nodes = [];
},
// Get/Set CSS
css: function(property, value) {
@@ -331,7 +338,7 @@ You should have received a copy of the GNU General Public License along with thi
nodeLoop(function(elm) {
(value) ? setCss(elm, property, value) : (elm.style[property]) ? values.push(elm.style[property]) : (computeStyle(elm,property))? values.push(computeStyle(elm,property)) : values.push(null);
- });
+ },nodes);
// Get CSS property: return values
if (values.length > 0) {
@@ -376,7 +383,7 @@ You should have received a copy of the GNU General Public License along with thi
{
values.push(elm.className);
}
- });
+ },nodes);
if (values.length > 0) {
return returnValues(values);
@@ -397,20 +404,22 @@ You should have received a copy of the GNU General Public License along with thi
tmpnodes.innerHTML = value;
while ((tmpnode = tmpnodes.lastChild)) {
-
- if (location === 'before') {
- elm.parentNode.insertBefore(tmpnode, elm);
- }
- else if (location === 'after') {
- elm.parentNode.insertBefore(tmpnode, elm.nextSibling);
+ // Catch error in unlikely case elm has been removed
+ try {
+ if (location === 'before') {
+ elm.parentNode.insertBefore(tmpnode, elm);
+ }
+ else if (location === 'after') {
+ elm.parentNode.insertBefore(tmpnode, elm.nextSibling);
+ }
}
+ catch (e) {break;}
}
-
}
else {
(value) ? elm.innerHTML = value : values.push(elm.innerHTML);
}
- });
+ },nodes);
if (values.length > 0) {
return returnValues(values);
@@ -438,7 +447,7 @@ You should have received a copy of the GNU General Public License along with thi
(value) ? elm.setAttribute(name, value) : elm.getAttribute(name) ? values.push(elm.getAttribute(name)) : values.push(null);
}
}
- });
+ },nodes);
if (values.length > 0) {
return returnValues(values);
@@ -563,7 +572,7 @@ You should have received a copy of the GNU General Public License along with thi
}
- });
+ },nodes);
if (values.length > 0) {
return returnValues(values);
@@ -608,7 +617,6 @@ You should have received a copy of the GNU General Public License along with thi
};
}
}
-
};
}

0 comments on commit cda85fc

Please sign in to comment.