Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #24 from gtanner/master

Added CSS Query Selector support
  • Loading branch information...
commit f48e5ce4ebaace468a566c8d7016e732080cf07a 2 parents 1f82986 + 62a312a
Laurent Hasson authored
View
1  .gitignore
@@ -0,0 +1 @@
+.DS_Store
View
2  js/alice-min.js
@@ -24,4 +24,4 @@
*//**
* @description
*
- */var alice=function(){"use strict";var a={id:"alice",name:"AliceJS",description:"A Lightweight Independent CSS Engine",version:"0.2",build:"20120204-1040",prefix:"",prefixJS:"",elems:null,format:{},helper:{},plugins:{},debug:!1,elements:function(a){var b=[],c;if(typeof a=="string")b.push(document.getElementById(a));else if(typeof a=="object"){if(a.length===undefined)b.push(a);else if(a.length===1)b.push(document.getElementById(a[0]));else if(a.length>0)for(c=0;c<a.length;c+=1)document.getElementById(a[c])?b.push(document.getElementById(a[c])):a[c].nodeType!==3&&b.push(a[c])}else if(typeof a=="function"&&a.length>0)for(c=0;c<a.length;c+=1)a[c].nodeType!==3&&b.push(a[c]);return b},randomize:function(a,b){var c,d,e=parseInt(a,10);return typeof b=="string"&&b.indexOf("%")>-1?c=parseInt(b,10)/100:c=parseFloat(b,10),d=e+e*(Math.random()*2*c-c),Math.floor(d)},duration:function(a){var b,c=function(a){return a},d=function(a){var b;return a.indexOf("ms")>-1?b=parseInt(a,10):a.indexOf("s")>-1?b=parseFloat(a,10)*1e3:b=parseInt(a,10),b},e=function(a){var b;return a.value&&(typeof a.value=="string"?b=d(a.value):b=c(a.value)),b};switch(typeof a){case"number":b=c(a);break;case"string":b=d(a);break;case"object":b=e(a);break;default:b=a}return b},coords:function(a){if(a===undefined||a===null)return{x:"50%",y:"50%"};if(typeof a=="string")switch(a){case"top-left":return{x:"0%",y:"0%"};case"top-center":return{x:"50%",y:"0%"};case"top-right":return{x:"100%",y:"0%"};case"middle-left":return{x:"0%",y:"50%"};case"middle-center":return{x:"50%",y:"50%"};case"middle-right":return{x:"100%",y:"50%"};case"bottom-left":return{x:"0%",y:"100%"};case"bottom-center":return{x:"50%",y:"100%"};case"bottom-right":return{x:"100%",y:"100%"};case"top":return{x:"50%",y:"0%"};case"left":return{x:"0%",y:"50%"};case"center":return{x:"50%",y:"50%"};case"right":return{x:"100%",y:"50%"};case"bottom":return{x:"50%",y:"100%"};case"NW":return{x:"0%",y:"0%"};case"N":return{x:"50%",y:"0%"};case"NE":return{x:"100%",y:"0%"};case"W":return{x:"0%",y:"50%"};case"E":return{x:"100%",y:"50%"};case"SW":return{x:"0%",y:"100%"};case"S":return{x:"50%",y:"100%"};case"SE":return{x:"100%",y:"100%"};default:return{x:"50%",y:"50%"}}return a},easing:function(a){switch(a){case"linear":return{p1:.25,p2:.25,p3:.75,p4:.75};case"ease":return{p1:.25,p2:.1,p3:.25,p4:1};case"ease-in":return{p1:.42,p2:0,p3:1,p4:1};case"ease-out":return{p1:0,p2:0,p3:.58,p4:1};case"ease-in-out":return{p1:.42,p2:0,p3:.58,p4:1};case"easeInQuad":return{p1:.55,p2:.085,p3:.68,p4:.53};case"easeInCubic":return{p1:.55,p2:.055,p3:.675,p4:.19};case"easeInQuart":return{p1:.895,p2:.03,p3:.685,p4:.22};case"easeInQuint":return{p1:.755,p2:.05,p3:.855,p4:.06};case"easeInSine":return{p1:.47,p2:0,p3:.745,p4:.715};case"easeInExpo":return{p1:.95,p2:.05,p3:.795,p4:.035};case"easeInCirc":return{p1:.6,p2:.04,p3:.98,p4:.335};case"easeInBack":return{p1:.6,p2:-0.28,p3:.735,p4:.045};case"easeOutQuad":return{p1:.25,p2:.46,p3:.45,p4:.94};case"easeOutCubic":return{p1:.215,p2:.61,p3:.355,p4:1};case"easeOutQuart":return{p1:.165,p2:.84,p3:.44,p4:1};case"easeOutQuint":return{p1:.23,p2:1,p3:.32,p4:1};case"easeOutSine":return{p1:.39,p2:.575,p3:.565,p4:1};case"easeOutExpo":return{p1:.19,p2:1,p3:.22,p4:1};case"easeOutCirc":return{p1:.075,p2:.82,p3:.165,p4:1};case"easeOutBack":return{p1:.175,p2:.885,p3:.32,p4:1.275};case"easeInOutQuad":return{p1:.455,p2:.03,p3:.515,p4:.955};case"easeInOutCubic":return{p1:.645,p2:.045,p3:.355,p4:1};case"easeInOutQuart":return{p1:.77,p2:0,p3:.175,p4:1};case"easeInOutQuint":return{p1:.86,p2:0,p3:.07,p4:1};case"easeInOutSine":return{p1:.445,p2:.05,p3:.55,p4:.95};case"easeInOutExpo":return{p1:1,p2:0,p3:0,p4:1};case"easeInOutCirc":return{p1:.785,p2:.135,p3:.15,p4:.86};case"easeInOutBack":return{p1:.68,p2:-0.55,p3:.265,p4:1.55};case"custom":return{p1:0,p2:.35,p3:.5,p4:1.3};case"random":return{p1:Math.random().toPrecision(3),p2:Math.random().toPrecision(3),p3:Math.random().toPrecision(3),p4:Math.random().toPrecision(3)};default:return{p1:.25,p2:.1,p3:.25,p4:1}}},flip:function(a,b,c){var d=b||1,e,f=function(a){return{start:0,end:a,axis:"Y"}},g=function(b){if(a==="left")return{start:0,end:-360*d,axis:"Y"};if(a==="right")return{start:0,end:360*d,axis:"Y"};if(a==="up")return{start:0,end:360*d,axis:"X"};if(a==="down")return{start:0,end:-360*d,axis:"X"}},h=function(a){var b;return a.value&&(typeof a.value=="string"?b=g(a.value):b=f(a.value)),b};switch(typeof a){case"number":e=f(a);break;case"string":e=g(a);break;case"object":e=h(a);break;default:e=null}return e},percentage:function(a){var b;return typeof a=="string"?a.indexOf("%")>-1||a.indexOf("°")>-1?b=parseInt(a,10)/100:a>=1||a<=-1?b=parseInt(a,10)/100:b=parseFloat(a,10):typeof a=="number"&&(a>=1||a<=-1?b=a/100:b=a),b},vendorPrefix:function(){var a=document.createElement("div");"webkitAnimation"in a.style?(this.prefix="-webkit-",this.prefixJS="webkit"):"MozAnimation"in a.style?(this.prefix="-moz-",this.prefixJS="Moz"):"MSAnimation"in a.style?(this.prefix="-ms-",this.prefixJS="MS"):"OAnimation"in a.style||"OTransform"in a.style?(this.prefix="-o-",this.prefixJS="O"):(this.prefix="",this.prefixJS=""),this.debug&&console.log("prefix="+this.prefix,"prefixJS="+this.prefixJS);return},docHeight:function(){var a=document;return Math.max(Math.max(a.body.scrollHeight,a.documentElement.scrollHeight),Math.max(a.body.offsetHeight,a.documentElement.offsetHeight),Math.max(a.body.clientHeight,a.documentElement.clientHeight))},pixel:function(a,b){if(typeof a=="number")return a%1===0?a:parseFloat(a,10)*b;if(a.indexOf("px")>-1)return parseInt(a,10);if(a.indexOf("%")>-1)return Math.round(parseInt(a,10)/100*b)},keyframeInsert:function(a){if(document.styleSheets&&document.styleSheets.length)try{document.styleSheets[0].insertRule(a,0)}catch(b){console.warn(b.message,a)}else{var c=document.createElement("style");c.innerHTML=a,document.head.appendChild(c)}return},keyframeDelete:function(a){var b=document.all?"rules":"cssRules",c;for(c=0;c<document.styleSheets[0][b].length;c+=1)if(document.styleSheets[0][b][c].name===a){document.styleSheets[0].deleteRule(c),this.debug&&console.log("Deleted keyframe: "+a);break}return},clearAnimation:function(a){this.style[this.prefixJS+"AnimationName"]="",this.style[this.prefixJS+"AnimationDelay"]="",this.style[this.prefixJS+"AnimationDuration"]="",this.style[this.prefixJS+"AnimationTimingFunction"]="",this.style[this.prefixJS+"AnimationIterationCount"]="",this.style[this.prefixJS+"AnimationDirection"]="",this.style[this.prefixJS+"AnimationPlayState"]="",alice.keyframeDelete(a.animationName);return},init:function(b){console.info("Initializing "+this.name+" ("+this.description+") "+this.version),this.vendorPrefix(),b&&b.elems&&(this.elems=this.elements(b.elems));if(typeof jWorkflow!="undefined"){console.log("jWorkflow: enabled");var c=b&&b.id?b.id:"",d=jWorkflow.order(),e={delay:function(a){return d.chill(a),e},log:function(a){return d.andThen(function(){console.log(a)}),e},custom:function(a){return d.andThen(a),e},start:function(){d.start(function(){console.info("workflow.start")})}};return Array.prototype.forEach.call(Object.keys(a.plugins),function(b){var f=a.plugins[b];e[b]=function(){var a=arguments;return d.andThen(function(){f.apply(document.getElementById(c),a)}),e}}),e}return console.log("jWorkflow: disabled"),a.plugins}};return a}();alice.format={duration:function(a){"use strict";var b=0,c=0,d=0;return b=alice.duration(a),d=b,a.randomness&&(c=alice.randomize(b,alice.percentage(a.randomness)),d=Math.abs(c)),d+"ms"},coords:function(a){"use strict";var b=alice.coords(a),c=b.x+" "+b.y;return c},easing:function(a){"use strict";var b=alice.easing(a),c="cubic-bezier("+b.p1+", "+b.p2+", "+b.p3+", "+b.p4+")";return c},oppositeNumber:function(a){return"use strict",-a}},alice.helper={duration:function(a,b,c){return a&&a.offset?(b?b=parseInt(b,10)+parseInt(a.offset,10):b=parseInt(alice.format.duration(c),10),b+="ms"):b=parseInt(alice.format.duration(c),10)+"ms",b},rotation:function(a,b){"use strict";var c=a;return b.randomness&&(c=alice.randomize(c,alice.percentage(b.randomness))),c}},alice.plugins.cheshire=function(a){"use strict",console.info("cheshire",a);var b=a.delay||"0ms",c=a.duration||"2000ms",d=a.timing||"ease",e=a.iteration||1,f=a.direction||"normal",g=a.playstate||"running",h=a.perspective||"1000",i=a.perspectiveOrigin||"center",j=a.backfaceVisibility||"visible",k=alice.percentage(a.overshoot)||0,l=85,m=a.rotate||0,n=a.turns||1,o=alice.flip(a.flip,n,k),p=a.fade&&a.fade!==""?a.fade:null,q=p&&p==="out"?1:0,r=p&&p==="out"?0:1,s=a.scale&&a.scale.from?alice.percentage(a.scale.from):1,t=a.scale&&a.scale.to?alice.percentage(a.scale.to):1,u=a.shadow||!1,v="",w="",x=1,y=0,z=a.posEnd||0,A=z+x*Math.floor(z*k),B={},C,D,E,F,G,H,I,J,K,L,M,N,O,P;alice.elems!==null?D=alice.elems:a.elems&&(D=alice.elements(a.elems));if(D&&D.length>0)for(F=0;F<D.length;F+=1){E=D[F],C=E.parentElement||E.parentNode,B.delay=alice.helper.duration(a.delay,B.delay,b),B.duration=alice.helper.duration(a.duration,B.duration,c),B.rotate=alice.helper.rotation(m,a),B.rotateStart=alice.percentage(B.rotate)*100,B.rotateOver=k*100,B.rotateEnd=0,G=alice.id+"-cheshire-"+(new Date).getTime()+"-"+Math.floor(Math.random()*1e6);if(a.move){N=a.move.direction||a.move;switch(N){case"left":v="Left",w="X",x=-1,O=window.innerWidth,y=a.move.start?alice.pixel(a.move.start,O):O,z=a.move.end?alice.pixel(a.move.end,O):0,A=x*Math.floor(y*k);break;case"right":v="Right",w="X",x=1,O=document.body.offsetWidth-E.clientWidth,y=a.move.start?alice.pixel(a.move.start,O):0,z=a.move.end?alice.pixel(a.move.end,O):O,A=z+x*Math.floor(z*k);break;case"up":v="Up",w="Y",x=-1,O=window.innerHeight,y=a.move.start?alice.pixel(a.move.start,O):O,z=a.move.end?alice.pixel(a.move.end,O):0,A=x*Math.floor(y*k);break;case"down":v="Down",w="Y",x=1,O=alice.docHeight()-C.clientHeight*3,y=a.move.start?alice.pixel(a.move.start,O):0,z=a.move.end?alice.pixel(a.move.end,O):O,A=z+x*Math.floor(z*k),alice.debug&&console.log(alice.docHeight(),window.innerHeight,window.pageYOffset,C.clientHeight)}}I="",I+=o?" rotate"+o.axis+"("+o.start+"deg)":" translate"+w+"("+y+"px)",I+=B.rotate&&parseInt(B.rotate,10)!==0?" rotate("+B.rotateStart+"deg)":"",I+=" scale("+s+")",J="",J+=o?" rotate"+o.axis+"("+Math.floor((1+k)*o.end)+"deg)":" translate"+w+"("+A+"px)",J+=B.rotate&&parseInt(B.rotate,10)!==0?" rotate("+B.rotateOver+"deg)":"",J+=t>1?" scale("+t+")":"",J+=" scale("+t+")",K="",K+=o?" rotate"+o.axis+"("+o.end+"deg)":" translate"+w+"("+z+"px)",v===""&&f==="alternate"?K+=" rotate("+alice.format.oppositeNumber(B.rotateStart)+"deg)":K+=B.rotate&&parseInt(B.rotate,10)!==0?" rotate("+B.rotateEnd+"deg)":"",K+=" scale("+t+")",u===!0&&t>1&&(P=Math.round(t*10),L=" 0px 0px 0px rgba(0, 0, 0, 1)",M=" "+P+"px "+P+"px "+P+"px rgba(0, 0, 0, 0.5)"),H="",H+="@"+alice.prefix+"keyframes "+G+" {\n",H+="\t0% {\n",H+="\t\t"+alice.prefix+"transform:"+I+";"+"\n",H+="\t\t"+alice.prefix+"transform-origin:"+alice.format.coords(i)+";"+"\n",H+=p?"\t\topacity: "+q+";"+"\n":"",H+=u===!0&&t>1?"\t\t"+alice.prefix+"box-shadow: "+L+";"+"\n":"",H+="\t}\n",k!==0&&(H+="\t"+l+"% {\n",H+="\t\t"+alice.prefix+"transform:"+J+";"+"\n",H+="\t\t"+alice.prefix+"transform-origin:"+alice.format.coords(i)+";"+"\n",H+="\t}\n"),H+="\t100% {\n",H+="\t\t"+alice.prefix+"transform:"+K+";"+"\n",H+="\t\t"+alice.prefix+"transform-origin:"+alice.format.coords(i)+";"+"\n",H+=p?"\t\topacity: "+r+";"+"\n":"",H+=u===!0&&t>1?"\t\t"+alice.prefix+"box-shadow: "+M+";"+"\n":"",H+="\t}\n",H+="}\n",alice.keyframeInsert(H),C.style[alice.prefixJS+"Perspective"]=h+"px",C.style[alice.prefixJS+"PerspectiveOrigin"]=alice.format.coords(i),E.style[alice.prefixJS+"BackfaceVisibility"]=j,E.style[alice.prefixJS+"AnimationName"]=G,E.style[alice.prefixJS+"AnimationDelay"]=B.delay,E.style[alice.prefixJS+"AnimationDuration"]=B.duration,E.style[alice.prefixJS+"AnimationTimingFunction"]=alice.format.easing(d),E.style[alice.prefixJS+"AnimationIterationCount"]=e,E.style[alice.prefixJS+"AnimationDirection"]=f,E.style[alice.prefixJS+"AnimationPlayState"]=g,E.style[alice.prefixJS+"Transform"]=K,E.style.opacity=p?r:"",E.style[alice.prefixJS+"BoxShadow"]=u===!0&&t>1?M:"","MozAnimation"in E.style?E.addEventListener("animationend",alice.clearAnimation,!1):E.addEventListener(alice.prefixJS+"AnimationEnd",alice.clearAnimation,!1),alice.debug&&(console.log(H),console.log(C.style),console.log(E.id,alice.prefixJS,E.style,E.style.cssText,E.style[alice.prefixJS+"AnimationDuration"],E.style[alice.prefixJS+"AnimationTimingFunction"]))}else console.warn("No elements!");return a},alice.plugins.bounce=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("bounce: ",arguments);var j={from:"100%",to:"125%"};b&&(typeof b=="object"?j=b:j.to=b);var k={elems:a,scale:j,shadow:c||!1,duration:d||"750ms",timing:e||"easeOutSine",delay:f||"0ms",iteration:g||"infinite",direction:h||"alternate",playstate:i};return alice.plugins.cheshire(k),k},alice.plugins.dance=function(a,b,c,d,e,f,g,h){"use strict",console.info("dance: ",arguments);var i={elems:a,rotate:b||45,duration:c||"750ms",timing:d||"easeInOutBack",delay:e||"0ms",iteration:f||"infinite",direction:g||"alternate",playstate:h};return alice.plugins.cheshire(i),i},alice.plugins.drain=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("drain: ",arguments);var j={scale:b==="in"?{from:"1%",to:"100%"}:{from:"100%",to:"1%"},elems:a,fade:b||"out",rotate:c||-2880,duration:d||"4500ms",timing:e||"ease-in-out",delay:f||"0ms",iteration:g||1,direction:h||"normal",playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.fade=function(a,b,c,d,e,f,g,h){"use strict",console.info("fade: ",arguments);var i={elems:a,fade:b||"in",duration:c||"4500ms",timing:d||"ease-in-out",delay:e||"0ms",iteration:f||1,direction:g||"normal",playstate:h};return alice.plugins.cheshire(i),i},alice.plugins.hinge=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("hinge: ",arguments);var j={perspectiveOrigin:"top-left",elems:a,rotate:b||25,overshoot:c||0,duration:d||"1000ms",timing:e||"linear",delay:f||"0ms",iteration:g||"infinite",direction:h||"alternate",playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.pageFlip=function(a,b,c,d,e,f,g,h,i,j){"use strict",console.info("pageFlip: ",arguments);var k="left";switch(b){case"right":k="right";break;case"up":k="top";break;case"down":k="bottom"}var l={perspectiveOrigin:k,elems:a,flip:b||"left",turns:c||1,overshoot:d||0,duration:e,timing:f,delay:g,iteration:h,direction:i,playstate:j};return alice.plugins.cheshire(l),l},alice.plugins.pendulum=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("pendulum: ",arguments);var j={perspectiveOrigin:"top",elems:a,rotate:b||45,overshoot:c||0,duration:d||"1000ms",timing:e||"ease-in-out",delay:f||"0ms",iteration:g||"infinite",direction:h||"alternate",playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.phantomZone=function(a,b,c,d,e,f,g,h,i,j){"use strict",console.info("phantomZone: ",arguments);var k={scale:b==="in"?{from:"1%",to:"100%"}:{from:"100%",to:"1%"},elems:a,fade:b||"out",rotate:c||-720,flip:d||"left",duration:e||"5000ms",timing:f,delay:g,iteration:h||1,direction:i||"normal",playstate:j};return alice.plugins.cheshire(k),k},alice.plugins.raceFlag=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("raceFlag: ",arguments);var j={flip:"down",elems:a,rotate:b||-720,perspectiveOrigin:c||"top-right",duration:d||"3000ms",timing:e,delay:f,iteration:g||1,direction:h||"normal",playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.slide=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("slide: ",arguments);var j={elems:a,move:b,overshoot:c,duration:d,timing:e,delay:f,iteration:g,direction:h,playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.spin=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("spin: ",arguments);var j={perspectiveOrigin:"center",direction:"normal",elems:a,flip:b||"left",turns:c||1,overshoot:d||0,duration:e,timing:f,delay:g,iteration:h,playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.toss=function(a,b,c,d,e,f,g,h,i,j){"use strict",console.info("toss: ",arguments);var k={rotate:b==="left"||b==="down"?720:-720,fade:"in",elems:a,move:b,overshoot:c,perspectiveOrigin:d,duration:e,timing:f,delay:g,iteration:h,direction:i,playstate:j};return alice.plugins.cheshire(k),k},alice.plugins.twirl=function(a,b,c,d,e,f,g,h){"use strict",console.info("twirl: ",arguments);var i={rotate:b==="left"?-135:135,elems:a,flip:b||"left",duration:c||"3000ms",timing:d,delay:e,iteration:f||1,direction:g||"normal",playstate:h};return alice.plugins.cheshire(i),i},alice.plugins.wobble=function(a,b,c,d,e,f,g,h){"use strict",console.info("wobble: ",arguments);var i={elems:a,rotate:b||5,perspectiveOrigin:c||"center",duration:d||"200ms",timing:e||"linear",delay:f||"0ms",iteration:g||"infinite",direction:"alternate",playstate:h};return alice.plugins.cheshire(i),i},alice.plugins.zoom=function(a,b,c,d,e,f,g,h,i,j){"use strict",console.info("zoom: ",arguments);var k={from:"100%",to:"125%"};b&&(typeof b=="object"?k=b:k.to=b);var l={elems:a,scale:k,shadow:c||!1,move:d||"none",duration:e,timing:f,delay:g,iteration:h||1,direction:i||"normal",playstate:j};return alice.plugins.cheshire(l),l};
+ */var alice=function(){"use strict";var a={id:"alice",name:"AliceJS",description:"A Lightweight Independent CSS Engine",version:"0.2",build:"20120204-1040",prefix:"",prefixJS:"",elems:null,format:{},helper:{},plugins:{},debug:!1,elements:function(a){var b=[],c=function(a,b){Array.prototype.forEach.apply(a,[b])},d=function(a){b.push(a)},e=function(a){if(typeof a!="string")return[];var b=document.getElementById(a);return b?[b]:document.querySelectorAll(a)};return typeof a=="string"?c(e(a),d):a.length===undefined?b.push(a):c(a,function(a){a.nodeType&&a.nodeType!==3?b.push(a):c(e(a),d)}),b},randomize:function(a,b){var c,d,e=parseInt(a,10);return typeof b=="string"&&b.indexOf("%")>-1?c=parseInt(b,10)/100:c=parseFloat(b,10),d=e+e*(Math.random()*2*c-c),Math.floor(d)},duration:function(a){var b,c=function(a){return a},d=function(a){var b;return a.indexOf("ms")>-1?b=parseInt(a,10):a.indexOf("s")>-1?b=parseFloat(a,10)*1e3:b=parseInt(a,10),b},e=function(a){var b;return a.value&&(typeof a.value=="string"?b=d(a.value):b=c(a.value)),b};switch(typeof a){case"number":b=c(a);break;case"string":b=d(a);break;case"object":b=e(a);break;default:b=a}return b},coords:function(a){if(a===undefined||a===null)return{x:"50%",y:"50%"};if(typeof a=="string")switch(a){case"top-left":return{x:"0%",y:"0%"};case"top-center":return{x:"50%",y:"0%"};case"top-right":return{x:"100%",y:"0%"};case"middle-left":return{x:"0%",y:"50%"};case"middle-center":return{x:"50%",y:"50%"};case"middle-right":return{x:"100%",y:"50%"};case"bottom-left":return{x:"0%",y:"100%"};case"bottom-center":return{x:"50%",y:"100%"};case"bottom-right":return{x:"100%",y:"100%"};case"top":return{x:"50%",y:"0%"};case"left":return{x:"0%",y:"50%"};case"center":return{x:"50%",y:"50%"};case"right":return{x:"100%",y:"50%"};case"bottom":return{x:"50%",y:"100%"};case"NW":return{x:"0%",y:"0%"};case"N":return{x:"50%",y:"0%"};case"NE":return{x:"100%",y:"0%"};case"W":return{x:"0%",y:"50%"};case"E":return{x:"100%",y:"50%"};case"SW":return{x:"0%",y:"100%"};case"S":return{x:"50%",y:"100%"};case"SE":return{x:"100%",y:"100%"};default:return{x:"50%",y:"50%"}}return a},easing:function(a){switch(a){case"linear":return{p1:.25,p2:.25,p3:.75,p4:.75};case"ease":return{p1:.25,p2:.1,p3:.25,p4:1};case"ease-in":return{p1:.42,p2:0,p3:1,p4:1};case"ease-out":return{p1:0,p2:0,p3:.58,p4:1};case"ease-in-out":return{p1:.42,p2:0,p3:.58,p4:1};case"easeInQuad":return{p1:.55,p2:.085,p3:.68,p4:.53};case"easeInCubic":return{p1:.55,p2:.055,p3:.675,p4:.19};case"easeInQuart":return{p1:.895,p2:.03,p3:.685,p4:.22};case"easeInQuint":return{p1:.755,p2:.05,p3:.855,p4:.06};case"easeInSine":return{p1:.47,p2:0,p3:.745,p4:.715};case"easeInExpo":return{p1:.95,p2:.05,p3:.795,p4:.035};case"easeInCirc":return{p1:.6,p2:.04,p3:.98,p4:.335};case"easeInBack":return{p1:.6,p2:-0.28,p3:.735,p4:.045};case"easeOutQuad":return{p1:.25,p2:.46,p3:.45,p4:.94};case"easeOutCubic":return{p1:.215,p2:.61,p3:.355,p4:1};case"easeOutQuart":return{p1:.165,p2:.84,p3:.44,p4:1};case"easeOutQuint":return{p1:.23,p2:1,p3:.32,p4:1};case"easeOutSine":return{p1:.39,p2:.575,p3:.565,p4:1};case"easeOutExpo":return{p1:.19,p2:1,p3:.22,p4:1};case"easeOutCirc":return{p1:.075,p2:.82,p3:.165,p4:1};case"easeOutBack":return{p1:.175,p2:.885,p3:.32,p4:1.275};case"easeInOutQuad":return{p1:.455,p2:.03,p3:.515,p4:.955};case"easeInOutCubic":return{p1:.645,p2:.045,p3:.355,p4:1};case"easeInOutQuart":return{p1:.77,p2:0,p3:.175,p4:1};case"easeInOutQuint":return{p1:.86,p2:0,p3:.07,p4:1};case"easeInOutSine":return{p1:.445,p2:.05,p3:.55,p4:.95};case"easeInOutExpo":return{p1:1,p2:0,p3:0,p4:1};case"easeInOutCirc":return{p1:.785,p2:.135,p3:.15,p4:.86};case"easeInOutBack":return{p1:.68,p2:-0.55,p3:.265,p4:1.55};case"custom":return{p1:0,p2:.35,p3:.5,p4:1.3};case"random":return{p1:Math.random().toPrecision(3),p2:Math.random().toPrecision(3),p3:Math.random().toPrecision(3),p4:Math.random().toPrecision(3)};default:return{p1:.25,p2:.1,p3:.25,p4:1}}},flip:function(a,b,c){var d=b||1,e,f=function(a){return{start:0,end:a,axis:"Y"}},g=function(b){if(a==="left")return{start:0,end:-360*d,axis:"Y"};if(a==="right")return{start:0,end:360*d,axis:"Y"};if(a==="up")return{start:0,end:360*d,axis:"X"};if(a==="down")return{start:0,end:-360*d,axis:"X"}},h=function(a){var b;return a.value&&(typeof a.value=="string"?b=g(a.value):b=f(a.value)),b};switch(typeof a){case"number":e=f(a);break;case"string":e=g(a);break;case"object":e=h(a);break;default:e=null}return e},percentage:function(a){var b;return typeof a=="string"?a.indexOf("%")>-1||a.indexOf("°")>-1?b=parseInt(a,10)/100:a>=1||a<=-1?b=parseInt(a,10)/100:b=parseFloat(a,10):typeof a=="number"&&(a>=1||a<=-1?b=a/100:b=a),b},vendorPrefix:function(){var a=document.createElement("div");"webkitAnimation"in a.style?(this.prefix="-webkit-",this.prefixJS="webkit"):"MozAnimation"in a.style?(this.prefix="-moz-",this.prefixJS="Moz"):"MSAnimation"in a.style?(this.prefix="-ms-",this.prefixJS="MS"):"OAnimation"in a.style||"OTransform"in a.style?(this.prefix="-o-",this.prefixJS="O"):(this.prefix="",this.prefixJS=""),this.debug&&console.log("prefix="+this.prefix,"prefixJS="+this.prefixJS);return},docHeight:function(){var a=document;return Math.max(Math.max(a.body.scrollHeight,a.documentElement.scrollHeight),Math.max(a.body.offsetHeight,a.documentElement.offsetHeight),Math.max(a.body.clientHeight,a.documentElement.clientHeight))},pixel:function(a,b){if(typeof a=="number")return a%1===0?a:parseFloat(a,10)*b;if(a.indexOf("px")>-1)return parseInt(a,10);if(a.indexOf("%")>-1)return Math.round(parseInt(a,10)/100*b)},keyframeInsert:function(a){if(document.styleSheets&&document.styleSheets.length)try{document.styleSheets[0].insertRule(a,0)}catch(b){console.warn(b.message,a)}else{var c=document.createElement("style");c.innerHTML=a,document.head.appendChild(c)}return},keyframeDelete:function(a){var b=document.all?"rules":"cssRules",c;for(c=0;c<document.styleSheets[0][b].length;c+=1)if(document.styleSheets[0][b][c].name===a){document.styleSheets[0].deleteRule(c),this.debug&&console.log("Deleted keyframe: "+a);break}return},clearAnimation:function(a){this.style[this.prefixJS+"AnimationName"]="",this.style[this.prefixJS+"AnimationDelay"]="",this.style[this.prefixJS+"AnimationDuration"]="",this.style[this.prefixJS+"AnimationTimingFunction"]="",this.style[this.prefixJS+"AnimationIterationCount"]="",this.style[this.prefixJS+"AnimationDirection"]="",this.style[this.prefixJS+"AnimationPlayState"]="",alice.keyframeDelete(a.animationName);return},init:function(b){console.info("Initializing "+this.name+" ("+this.description+") "+this.version),this.vendorPrefix(),b&&b.elems&&(this.elems=this.elements(b.elems));if(typeof jWorkflow!="undefined"){console.log("jWorkflow: enabled");var c=b&&b.id?b.id:"",d=jWorkflow.order(),e={delay:function(a){return d.chill(a),e},log:function(a){return d.andThen(function(){console.log(a)}),e},custom:function(a){return d.andThen(a),e},start:function(){d.start(function(){console.info("workflow.start")})}};return Array.prototype.forEach.call(Object.keys(a.plugins),function(b){var f=a.plugins[b];e[b]=function(){var a=arguments;return d.andThen(function(){f.apply(document.getElementById(c),a)}),e}}),e}return console.log("jWorkflow: disabled"),a.plugins}};return a}();alice.format={duration:function(a){"use strict";var b=0,c=0,d=0;return b=alice.duration(a),d=b,a.randomness&&(c=alice.randomize(b,alice.percentage(a.randomness)),d=Math.abs(c)),d+"ms"},coords:function(a){"use strict";var b=alice.coords(a),c=b.x+" "+b.y;return c},easing:function(a){"use strict";var b=alice.easing(a),c="cubic-bezier("+b.p1+", "+b.p2+", "+b.p3+", "+b.p4+")";return c},oppositeNumber:function(a){return"use strict",-a}},alice.helper={duration:function(a,b,c){return a&&a.offset?(b?b=parseInt(b,10)+parseInt(a.offset,10):b=parseInt(alice.format.duration(c),10),b+="ms"):b=parseInt(alice.format.duration(c),10)+"ms",b},rotation:function(a,b){"use strict";var c=a;return b.randomness&&(c=alice.randomize(c,alice.percentage(b.randomness))),c}},alice.plugins.cheshire=function(a){"use strict",console.info("cheshire",a);var b=a.delay||"0ms",c=a.duration||"2000ms",d=a.timing||"ease",e=a.iteration||1,f=a.direction||"normal",g=a.playstate||"running",h=a.perspective||"1000",i=a.perspectiveOrigin||"center",j=a.backfaceVisibility||"visible",k=alice.percentage(a.overshoot)||0,l=85,m=a.rotate||0,n=a.turns||1,o=alice.flip(a.flip,n,k),p=a.fade&&a.fade!==""?a.fade:null,q=p&&p==="out"?1:0,r=p&&p==="out"?0:1,s=a.scale&&a.scale.from?alice.percentage(a.scale.from):1,t=a.scale&&a.scale.to?alice.percentage(a.scale.to):1,u=a.shadow||!1,v="",w="",x=1,y=0,z=a.posEnd||0,A=z+x*Math.floor(z*k),B={},C,D,E,F,G,H,I,J,K,L,M,N,O,P;alice.elems!==null?D=alice.elems:a.elems&&(D=alice.elements(a.elems));if(D&&D.length>0)for(F=0;F<D.length;F+=1){E=D[F],C=E.parentElement||E.parentNode,B.delay=alice.helper.duration(a.delay,B.delay,b),B.duration=alice.helper.duration(a.duration,B.duration,c),B.rotate=alice.helper.rotation(m,a),B.rotateStart=alice.percentage(B.rotate)*100,B.rotateOver=k*100,B.rotateEnd=0,G=alice.id+"-cheshire-"+(new Date).getTime()+"-"+Math.floor(Math.random()*1e6);if(a.move){N=a.move.direction||a.move;switch(N){case"left":v="Left",w="X",x=-1,O=window.innerWidth,y=a.move.start?alice.pixel(a.move.start,O):O,z=a.move.end?alice.pixel(a.move.end,O):0,A=x*Math.floor(y*k);break;case"right":v="Right",w="X",x=1,O=document.body.offsetWidth-E.clientWidth,y=a.move.start?alice.pixel(a.move.start,O):0,z=a.move.end?alice.pixel(a.move.end,O):O,A=z+x*Math.floor(z*k);break;case"up":v="Up",w="Y",x=-1,O=window.innerHeight,y=a.move.start?alice.pixel(a.move.start,O):O,z=a.move.end?alice.pixel(a.move.end,O):0,A=x*Math.floor(y*k);break;case"down":v="Down",w="Y",x=1,O=alice.docHeight()-C.clientHeight*3,y=a.move.start?alice.pixel(a.move.start,O):0,z=a.move.end?alice.pixel(a.move.end,O):O,A=z+x*Math.floor(z*k),alice.debug&&console.log(alice.docHeight(),window.innerHeight,window.pageYOffset,C.clientHeight)}}I="",I+=o?" rotate"+o.axis+"("+o.start+"deg)":" translate"+w+"("+y+"px)",I+=B.rotate&&parseInt(B.rotate,10)!==0?" rotate("+B.rotateStart+"deg)":"",I+=" scale("+s+")",J="",J+=o?" rotate"+o.axis+"("+Math.floor((1+k)*o.end)+"deg)":" translate"+w+"("+A+"px)",J+=B.rotate&&parseInt(B.rotate,10)!==0?" rotate("+B.rotateOver+"deg)":"",J+=t>1?" scale("+t+")":"",J+=" scale("+t+")",K="",K+=o?" rotate"+o.axis+"("+o.end+"deg)":" translate"+w+"("+z+"px)",v===""&&f==="alternate"?K+=" rotate("+alice.format.oppositeNumber(B.rotateStart)+"deg)":K+=B.rotate&&parseInt(B.rotate,10)!==0?" rotate("+B.rotateEnd+"deg)":"",K+=" scale("+t+")",u===!0&&t>1&&(P=Math.round(t*10),L=" 0px 0px 0px rgba(0, 0, 0, 1)",M=" "+P+"px "+P+"px "+P+"px rgba(0, 0, 0, 0.5)"),H="",H+="@"+alice.prefix+"keyframes "+G+" {\n",H+=" 0% {\n",H+=" "+alice.prefix+"transform:"+I+";"+"\n",H+=" "+alice.prefix+"transform-origin:"+alice.format.coords(i)+";"+"\n",H+=p?" opacity: "+q+";"+"\n":"",H+=u===!0&&t>1?" "+alice.prefix+"box-shadow: "+L+";"+"\n":"",H+=" }\n",k!==0&&(H+=" "+l+"% {\n",H+=" "+alice.prefix+"transform:"+J+";"+"\n",H+=" "+alice.prefix+"transform-origin:"+alice.format.coords(i)+";"+"\n",H+=" }\n"),H+=" 100% {\n",H+=" "+alice.prefix+"transform:"+K+";"+"\n",H+=" "+alice.prefix+"transform-origin:"+alice.format.coords(i)+";"+"\n",H+=p?" opacity: "+r+";"+"\n":"",H+=u===!0&&t>1?" "+alice.prefix+"box-shadow: "+M+";"+"\n":"",H+=" }\n",H+="}\n",alice.keyframeInsert(H),C.style[alice.prefixJS+"Perspective"]=h+"px",C.style[alice.prefixJS+"PerspectiveOrigin"]=alice.format.coords(i),E.style[alice.prefixJS+"BackfaceVisibility"]=j,E.style[alice.prefixJS+"AnimationName"]=G,E.style[alice.prefixJS+"AnimationDelay"]=B.delay,E.style[alice.prefixJS+"AnimationDuration"]=B.duration,E.style[alice.prefixJS+"AnimationTimingFunction"]=alice.format.easing(d),E.style[alice.prefixJS+"AnimationIterationCount"]=e,E.style[alice.prefixJS+"AnimationDirection"]=f,E.style[alice.prefixJS+"AnimationPlayState"]=g,E.style[alice.prefixJS+"Transform"]=K,E.style.opacity=p?r:"",E.style[alice.prefixJS+"BoxShadow"]=u===!0&&t>1?M:"","MozAnimation"in E.style?E.addEventListener("animationend",alice.clearAnimation,!1):E.addEventListener(alice.prefixJS+"AnimationEnd",alice.clearAnimation,!1),alice.debug&&(console.log(H),console.log(C.style),console.log(E.id,alice.prefixJS,E.style,E.style.cssText,E.style[alice.prefixJS+"AnimationDuration"],E.style[alice.prefixJS+"AnimationTimingFunction"]))}else console.warn("No elements!");return a},alice.plugins.bounce=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("bounce: ",arguments);var j={from:"100%",to:"125%"};b&&(typeof b=="object"?j=b:j.to=b);var k={elems:a,scale:j,shadow:c||!1,duration:d||"750ms",timing:e||"easeOutSine",delay:f||"0ms",iteration:g||"infinite",direction:h||"alternate",playstate:i};return alice.plugins.cheshire(k),k},alice.plugins.dance=function(a,b,c,d,e,f,g,h){"use strict",console.info("dance: ",arguments);var i={elems:a,rotate:b||45,duration:c||"750ms",timing:d||"easeInOutBack",delay:e||"0ms",iteration:f||"infinite",direction:g||"alternate",playstate:h};return alice.plugins.cheshire(i),i},alice.plugins.drain=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("drain: ",arguments);var j={scale:b==="in"?{from:"1%",to:"100%"}:{from:"100%",to:"1%"},elems:a,fade:b||"out",rotate:c||-2880,duration:d||"4500ms",timing:e||"ease-in-out",delay:f||"0ms",iteration:g||1,direction:h||"normal",playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.fade=function(a,b,c,d,e,f,g,h){"use strict",console.info("fade: ",arguments);var i={elems:a,fade:b||"in",duration:c||"4500ms",timing:d||"ease-in-out",delay:e||"0ms",iteration:f||1,direction:g||"normal",playstate:h};return alice.plugins.cheshire(i),i},alice.plugins.hinge=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("hinge: ",arguments);var j={perspectiveOrigin:"top-left",elems:a,rotate:b||25,overshoot:c||0,duration:d||"1000ms",timing:e||"linear",delay:f||"0ms",iteration:g||"infinite",direction:h||"alternate",playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.pageFlip=function(a,b,c,d,e,f,g,h,i,j){"use strict",console.info("pageFlip: ",arguments);var k="left";switch(b){case"right":k="right";break;case"up":k="top";break;case"down":k="bottom"}var l={perspectiveOrigin:k,elems:a,flip:b||"left",turns:c||1,overshoot:d||0,duration:e,timing:f,delay:g,iteration:h,direction:i,playstate:j};return alice.plugins.cheshire(l),l},alice.plugins.pendulum=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("pendulum: ",arguments);var j={perspectiveOrigin:"top",elems:a,rotate:b||45,overshoot:c||0,duration:d||"1000ms",timing:e||"ease-in-out",delay:f||"0ms",iteration:g||"infinite",direction:h||"alternate",playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.phantomZone=function(a,b,c,d,e,f,g,h,i,j){"use strict",console.info("phantomZone: ",arguments);var k={scale:b==="in"?{from:"1%",to:"100%"}:{from:"100%",to:"1%"},elems:a,fade:b||"out",rotate:c||-720,flip:d||"left",duration:e||"5000ms",timing:f,delay:g,iteration:h||1,direction:i||"normal",playstate:j};return alice.plugins.cheshire(k),k},alice.plugins.raceFlag=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("raceFlag: ",arguments);var j={flip:"down",elems:a,rotate:b||-720,perspectiveOrigin:c||"top-right",duration:d||"3000ms",timing:e,delay:f,iteration:g||1,direction:h||"normal",playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.slide=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("slide: ",arguments);var j={elems:a,move:b,overshoot:c,duration:d,timing:e,delay:f,iteration:g,direction:h,playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.spin=function(a,b,c,d,e,f,g,h,i){"use strict",console.info("spin: ",arguments);var j={perspectiveOrigin:"center",direction:"normal",elems:a,flip:b||"left",turns:c||1,overshoot:d||0,duration:e,timing:f,delay:g,iteration:h,playstate:i};return alice.plugins.cheshire(j),j},alice.plugins.toss=function(a,b,c,d,e,f,g,h,i,j){"use strict",console.info("toss: ",arguments);var k={rotate:b==="left"||b==="down"?720:-720,fade:"in",elems:a,move:b,overshoot:c,perspectiveOrigin:d,duration:e,timing:f,delay:g,iteration:h,direction:i,playstate:j};return alice.plugins.cheshire(k),k},alice.plugins.twirl=function(a,b,c,d,e,f,g,h){"use strict",console.info("twirl: ",arguments);var i={rotate:b==="left"?-135:135,elems:a,flip:b||"left",duration:c||"3000ms",timing:d,delay:e,iteration:f||1,direction:g||"normal",playstate:h};return alice.plugins.cheshire(i),i},alice.plugins.wobble=function(a,b,c,d,e,f,g,h){"use strict",console.info("wobble: ",arguments);var i={elems:a,rotate:b||5,perspectiveOrigin:c||"center",duration:d||"200ms",timing:e||"linear",delay:f||"0ms",iteration:g||"infinite",direction:"alternate",playstate:h};return alice.plugins.cheshire(i),i},alice.plugins.zoom=function(a,b,c,d,e,f,g,h,i,j){"use strict",console.info("zoom: ",arguments);var k={from:"100%",to:"125%"};b&&(typeof b=="object"?k=b:k.to=b);var l={elems:a,scale:k,shadow:c||!1,move:d||"none",duration:e,timing:f,delay:g,iteration:h||1,direction:i||"normal",playstate:j};return alice.plugins.cheshire(l),l};
View
56 js/alice.js
@@ -55,48 +55,36 @@ var alice = (function () {
* Returns array of elements
*/
elements: function (params) {
- //console.info("elements", params, typeof params);
var elems = [],
- i;
+ each = function (arr, func) {
+ Array.prototype.forEach.apply(arr, [func]);
+ },
+ push = function (v) {
+ elems.push(v);
+ },
+ lookup = function (query) {
+ if (typeof query != 'string') return [];
+ var result = document.getElementById(query);
+ return result ? [result] : document.querySelectorAll(query);
+ };
if (typeof params === "string") {
- elems.push(document.getElementById(params)); // "myId1"
+ each(lookup(params), push);
}
- else if (typeof params === "object") {
- if (params.length === undefined) {
- elems.push(params); // myElem1
- }
- else if (params.length === 1) {
- elems.push(document.getElementById(params[0])); // ["myId2"]
- }
- else if (params.length > 0) {
- for (i = 0; i < params.length; i += 1) {
- if (document.getElementById(params[i])) {
- elems.push(document.getElementById(params[i])); // ["myId3-1", "myId3-2", "myId3-3"]
- }
- // Chrome NodeList
- else {
- // ignore Text
- if (params[i].nodeType !== 3) {
- elems.push(params[i]); // myElem4.childNodes
- }
- }
- }
- }
+ else if (params.length === undefined) {
+ elems.push(params); // myElem1
}
- // Safari NodeList
- else if (typeof params === "function") {
- if (params.length > 0) {
- for (i = 0; i < params.length; i += 1) {
- // ignore Text
- if (params[i].nodeType !== 3) {
- elems.push(params[i]); // myElem4.childNodes
- }
+ else {
+ each(params, function(param) {
+ if (param.nodeType && param.nodeType !== 3) {
+ elems.push(param);
}
- }
+ else {
+ each(lookup(param), push);
+ }
+ });
}
- //console.log(elems);
return elems;
},
View
56 js/src/alice.core.js
@@ -55,48 +55,36 @@ var alice = (function () {
* Returns array of elements
*/
elements: function (params) {
- //console.info("elements", params, typeof params);
var elems = [],
- i;
+ each = function (arr, func) {
+ Array.prototype.forEach.apply(arr, [func]);
+ },
+ push = function (v) {
+ elems.push(v);
+ },
+ lookup = function (query) {
+ if (typeof query != 'string') return [];
+ var result = document.getElementById(query);
+ return result ? [result] : document.querySelectorAll(query);
+ };
if (typeof params === "string") {
- elems.push(document.getElementById(params)); // "myId1"
+ each(lookup(params), push);
}
- else if (typeof params === "object") {
- if (params.length === undefined) {
- elems.push(params); // myElem1
- }
- else if (params.length === 1) {
- elems.push(document.getElementById(params[0])); // ["myId2"]
- }
- else if (params.length > 0) {
- for (i = 0; i < params.length; i += 1) {
- if (document.getElementById(params[i])) {
- elems.push(document.getElementById(params[i])); // ["myId3-1", "myId3-2", "myId3-3"]
- }
- // Chrome NodeList
- else {
- // ignore Text
- if (params[i].nodeType !== 3) {
- elems.push(params[i]); // myElem4.childNodes
- }
- }
- }
- }
+ else if (params.length === undefined) {
+ elems.push(params); // myElem1
}
- // Safari NodeList
- else if (typeof params === "function") {
- if (params.length > 0) {
- for (i = 0; i < params.length; i += 1) {
- // ignore Text
- if (params[i].nodeType !== 3) {
- elems.push(params[i]); // myElem4.childNodes
- }
+ else {
+ each(params, function(param) {
+ if (param.nodeType && param.nodeType !== 3) {
+ elems.push(param);
}
- }
+ else {
+ each(lookup(param), push);
+ }
+ });
}
- //console.log(elems);
return elems;
},
View
129 specs/alice.core.js
@@ -4,8 +4,7 @@
*
* Copyright 2011-2012 Research In Motion Limited.
*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
+ * Licensed under the Apache License, Version 2.0 (the "License") you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
@@ -321,3 +320,129 @@ describe('oppositeNumber', function () {
expect(alice.format.oppositeNumber(-0)).toEqual(0);
});
});
+
+describe("selecting elements", function () {
+ var sandbox;
+ beforeEach(function () {
+ sandbox = document.createElement("div");
+ sandbox.id = "sandbox";
+
+ document.body.appendChild(sandbox);
+ });
+
+ afterEach(function () {
+ document.body.removeChild(sandbox);
+ });
+
+ describe("by id", function () {
+ it("can find an element by passing in the id", function () {
+ var result = alice.elements('sandbox');
+ expect(result[0]).toBe(sandbox);
+ });
+
+ it("returns an empty array when the id doesn't exist", function () {
+ var result = alice.elements('bob');
+ expect(result).toEqual([]);
+ });
+ });
+
+ describe("by query selector", function () {
+ it("can find an elment by the id", function () {
+ var a = document.createElement("div");
+ a.id = "hippo";
+
+ sandbox.appendChild(a);
+ var result = alice.elements("#hippo");
+
+ expect(result).toEqual([a]);
+ });
+ });
+
+ describe("by element", function () {
+ it("returns the element passed in", function () {
+ var el = document.createElement('div');
+ var result = alice.elements(el);
+ expect(result[0]).toBe(el);
+ });
+ });
+
+ describe("when passing in an array", function () {
+ it("finds a collection of id's", function () {
+ var a = document.createElement("div");
+ b = document.createElement("div"),
+ c = document.createElement("div");
+
+ a.id = "a";
+ b.id = "b";
+ c.id = "c";
+
+ sandbox.appendChild(a);
+ sandbox.appendChild(b);
+ sandbox.appendChild(c);
+
+ var result = alice.elements(["a", "b", "c"]);
+
+ expect(result).toEqual([a, b, c]);
+ });
+
+ it("finds a collection of query selectors", function () {
+ var a = document.createElement("div");
+ b = document.createElement("div"),
+ c = document.createElement("div");
+
+ a.id = "a";
+ b.id = "b";
+ c.id = "c";
+
+ sandbox.appendChild(a);
+ sandbox.appendChild(b);
+ sandbox.appendChild(c);
+
+ var result = alice.elements(["#a", "div#b", "#c"]);
+
+ expect(result).toEqual([a, b, c]);
+ });
+
+ it("returns the element for a list containing a single id", function () {
+ var a = document.createElement("div");
+ a.id = "astroturf";
+ sandbox.appendChild(a);
+
+ var result = alice.elements(["astroturf"]);
+ expect(result).toEqual([a]);
+ });
+
+ it("returns the element for a list containing a single query selector", function () {
+ var a = document.createElement("div");
+ a.id = "scoobysnacks";
+ sandbox.appendChild(a);
+
+ var result = alice.elements(["#scoobysnacks"]);
+ expect(result).toEqual([a]);
+ });
+
+ it("filters out the text elements for a list of elements", function () {
+ var a = {nodeType: 1},
+ b = {nodeType: 3},
+ c = {nodeType: 1},
+ d = {nodeType: 3},
+ result = alice.elements([a, b, c, d]);
+
+ expect(result).toEqual([a,c]);
+ });
+
+ it("works with an actual list of elements", function () {
+ var a = document.createElement('div'),
+ b = document.createElement('div'),
+ c = document.createElement('div');
+
+ sandbox.appendChild(a);
+ sandbox.appendChild(b);
+ sandbox.appendChild(c);
+
+ var result = alice.elements(sandbox.querySelectorAll('div'));
+
+ expect(result).toEqual([a, b, c]);
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.