From def6e8184baae6c8f529515f67b2958aff74511b Mon Sep 17 00:00:00 2001 From: Blake West Date: Tue, 30 Sep 2014 18:55:48 -0700 Subject: [PATCH] Added little extension to do exact searching of text by default. Should probably add rough search functionality later. --- README.md | 2 +- dist/behave.js | 9 +++++++-- dist/behave.min.js | 2 +- src/behave.js | 9 +++++++-- test/test.js | 9 ++++++--- test/testRunner.html | 2 +- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7a50573..c4b4502 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ you can specify a "type" of DOM element to narrow the search by doing... Behave.find('email', 'field') // Finds els of type 'input', 'select', 'option', 'label', 'textarea', or 'form' Behave.find('Sign Up', 'clickable') // Finds els of type 'button', or 'a' Behave.find('danger', 'icon') // Finds els of type 'icon', 'div', or 'span' -Behave.find('Birthday', 'display') // Searches text of all elements. +Behave.find('Birthday', 'display') // Searches EXACT text of all elements. ``` Behave also finds and makes available each element on your page instantly! Let's say your page is... diff --git a/dist/behave.js b/dist/behave.js index cbdea2e..6a56986 100644 --- a/dist/behave.js +++ b/dist/behave.js @@ -52,8 +52,13 @@ if(typeof t!="number"&&null!=t){var o=u;for(t=Y.createCallback(t,e,3);o--&&t(n[o Y.prototype[n]=function(){return new nt(t.apply(this.__wrapped__,arguments),this.__chain__)}}),Y}var h,g=[],y=[],m=0,_=+new Date+"",b=75,d=40,w=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",j=/\b__p\+='';/g,k=/\b(__p\+=)''\+/g,x=/(__e\(.*?\)|\b__t\))\+'';/g,C=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,O=/\w*$/,I=/^function[ \n\r\t]+\w/,N=/<%=([\s\S]+?)%>/g,E=RegExp("^["+w+"]*0+(?=.$)"),S=/($^)/,R=/\bthis\b/,A=/['\n\r\t\u2028\u2029\\]/g,D="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),B="[object Arguments]",$="[object Array]",F="[object Boolean]",T="[object Date]",W="[object Function]",q="[object Number]",z="[object Object]",P="[object RegExp]",K="[object String]",L={}; L[W]=!1,L[B]=L[$]=L[F]=L[T]=L[q]=L[z]=L[P]=L[K]=!0;var M={leading:!1,maxWait:0,trailing:!1},U={configurable:!1,enumerable:!1,value:null,writable:!1},V={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},G={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},H=V[typeof window]&&window||this,J=V[typeof exports]&&exports&&!exports.nodeType&&exports,Q=V[typeof module]&&module&&!module.nodeType&&module,X=Q&&Q.exports===J&&J,Y=V[typeof global]&&global;!Y||Y.global!==Y&&Y.window!==Y||(H=Y); var Z=v();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(H._=Z, define(function(){return Z})):J&&Q?X?(Q.exports=Z)._=Z:J._=Z:H._=Z}).call(this); -window.Behave = {}; +// Little jQuery extension to have exact equals; +$.expr[':'].textEquals = function(a, i, m) { + var match = $(a).text().match("^" + m[3] + "$") + return match && match.length > 0; +} +window.Behave = {}; Behave.view = $(document.body); Behave.domTypes = { field: { @@ -88,7 +93,7 @@ Behave.find = function(identifier, type) { _.each(searchParams.attrOptions, function(attrOption) { switch (attrOption) { case 'contains': - var filter = ":contains(" + identifier + ")" + var filter = ":textEquals("+ identifier + ")" element = Behave.view.find(elType + filter); break; case 'class': diff --git a/dist/behave.min.js b/dist/behave.min.js index 43726b8..4b4e799 100644 --- a/dist/behave.min.js +++ b/dist/behave.min.js @@ -1 +1 @@ -(function(){function n(n,e,t){t=(t||0)-1;for(var r=n?n.length:0;++tr||"undefined"==typeof t)return 1;if(r>t||"undefined"==typeof r)return-1}return n.n-e.n}function o(n){var e=-1,r=n.length,u=n[0],o=n[0|r/2],i=n[r-1];if(u&&"object"==typeof u&&o&&"object"==typeof o&&i&&"object"==typeof i)return!1;for(u=f(),u["false"]=u["null"]=u["true"]=u.undefined=!1,o=f(),o.k=n,o.l=u,o.push=t;++et?0:t);++r3&&"function"==typeof i[f-2])var l=te(i[--f-1],i[f--],2);else f>2&&"function"==typeof i[f-1]&&(l=i[--f]);for(;++a=_&&f===n,v=u||h?a():s;if(h){var g=o(v);g?(f=e,v=g):(h=!1,v=u?v:(c(v),s))}for(;++if(v,y))&&((u||h)&&v.push(y),s.push(g))}return h?(c(v.k),p(v)):u&&c(v),s}function ae(n){return function(e,t,r){var u={};t=Y.createCallback(t,r,3),r=-1;var o=e?e.length:0;if("number"==typeof o)for(;++rt?Nt(0,o+t):t)||0,Pt(n)?i=-1o&&(o=a)}}else e=!e&&we(n)?r:Y.createCallback(e,t,3),Oe(n,function(n,t,r){t=e(n,t,r),t>u&&(u=t,o=n)});return o}function Ne(n,e){var t=-1,r=n?n.length:0;if("number"==typeof r)for(var u=Xe(r);++targuments.length;e=te(e,r,4);var o=-1,i=n.length;if("number"==typeof i)for(u&&(t=n[++o]);++oarguments.length;return e=te(e,r,4),Ee(n,function(n,r,o){t=u?(u=!1,n):e(t,n,r,o)}),t}function $e(n){var e=-1,t=n?n.length:0,r=Xe("number"==typeof t?t:0);return Oe(n,function(n){var t=He(++e);r[e]=r[t],r[t]=n}),r}function De(n,e,t){var r;e=Y.createCallback(e,t,3),t=-1;var u=n?n.length:0;if("number"==typeof u)for(;++t=_&&u===n;if(l){var c=o(a);c?(u=e,a=c):l=!1}for(;++ru(a,c)&&f.push(c);return l&&p(a),f}function Fe(n,e,t){var r=0,u=n?n.length:0;if("number"!=typeof e&&null!=e){var o=-1;for(e=Y.createCallback(e,t,3);++or?Nt(0,u+r):r||0}else if(r)return r=We(e,t),e[r]===t?r:-1;return n(e,t,r)}function ze(n,e,t){if("number"!=typeof e&&null!=e){var r=0,u=-1,o=n?n.length:0;for(e=Y.createCallback(e,t,3);++uu;)r=u+o>>>1,t(n[r])t?0:t);++e0?c=bt(u,t):(i&&st(i),t=p,i=c=p=v,t&&(s=dt(),a=n.apply(l,o)))}var o,i,a,f,l,c,p,s=0,h=!1,g=!0;if(!me(n))throw new it;if(e=Nt(0,e)||0,!0===t)var y=!0,g=!1;else _e(t)&&(y=t.leading,h="maxWait"in t&&(Nt(e,t.maxWait)||0),g="trailing"in t?t.trailing:g);return function(){if(o=arguments,f=dt(),l=this,p=g&&(c||!y),!1===h)var t=y&&!c;else{i||y||(s=f);var v=h-(f-s);v>0?i||(i=bt(r,v)):(i&&(i=st(i)),s=f,a=n.apply(l,o))}return c||e===h||(c=bt(u,e)),t&&(a=n.apply(l,o)),a}}function Ue(n){if(!me(n))throw new it;var e=Dt.call(arguments,1);return bt(function(){n.apply(v,e)},1)}function Qe(n){return n}function Ge(n,e){var t=n,r=!e||me(t);e||(t=ne,e=n,n=Y),Oe(ye(e),function(u){var o=n[u]=e[u];r&&(t.prototype[u]=function(){var e=this.__wrapped__,r=[e];return mt.apply(r,arguments),r=o.apply(n,r),e&&"object"==typeof e&&e===r?this:(r=new t(r),r.__chain__=this.__chain__,r)})})}function He(n,e,t){var r=null==n,u=null==e;return null==t&&("boolean"==typeof n&&u?(t=n,n=1):u||"boolean"!=typeof e||(t=e,u=!0)),r&&u&&(e=1),n=+n||0,u?(e=n,n=0):e=+e||0,r=$t(),t||n%1||e%1?Tt(n+r*(e-n+parseFloat("1e-"+((r+"").length-1))),e):n+ht(r*(e-n+1))}function Je(){return this.__wrapped__}t=t?Z.defaults(G.Object(),t,Z.pick(G,S)):G;var Xe=t.Array,Ye=t.Boolean,Ze=t.Date,nt=t.Function,et=t.Math,tt=t.Number,rt=t.Object,ut=t.RegExp,ot=t.String,it=t.TypeError,at=[],ft=rt.prototype,lt=t._,ct=ut("^"+ot(ft.valueOf).replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),pt=et.ceil,st=t.clearTimeout,ht=et.floor,vt=nt.prototype.toString,gt=ct.test(gt=rt.getPrototypeOf)&>,yt=ft.hasOwnProperty,dt=ct.test(dt=Ze.now)&&dt||function(){return+new Ze},mt=at.push,_t=t.setImmediate,bt=t.setTimeout,wt=at.splice,jt=ft.toString,kt=at.unshift,xt=function(){try{var n={},e=ct.test(e=rt.defineProperty)&&e,t=e(n,n,n)&&e}catch(r){}return t}(),Ct=ct.test(Ct=jt.bind)&&Ct,Bt=ct.test(Bt=rt.create)&&Bt,Ot=ct.test(Ot=Xe.isArray)&&Ot,Et=t.isFinite,At=t.isNaN,It=ct.test(It=rt.keys)&&It,Nt=et.max,Tt=et.min,St=t.parseInt,$t=et.random,Dt=at.slice,Rt=ct.test(t.attachEvent),Ft=Ct&&!/\n|true/.test(Ct+Rt),qt={};qt[D]=Xe,qt[R]=Ye,qt[F]=Ze,qt[q]=nt,qt[W]=rt,qt[z]=tt,qt[P]=ut,qt[V]=ot,ne.prototype=Y.prototype;var zt=Y.support={};zt.fastBind=Ct&&!Ft,zt.funcDecomp=!ct.test(t.a)&&N.test(h),zt.funcNames="string"==typeof nt.name,Y.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:E,variable:"",imports:{_:Y}},Bt||(le=function(n){if(_e(n)){l.prototype=n;var e=new l;l.prototype=null}return e||{}});var Wt=xt?function(n,e){M.value=e,xt(n,"__bindData__",M)}:l,Pt=Ot||function(n){return n&&"object"==typeof n&&"number"==typeof n.length&&jt.call(n)==D||!1},Vt=It?function(n){return _e(n)?It(n):[]}:J,Kt={"&":"&","<":"<",">":">",'"':""","'":"'"},Lt=de(Kt),Mt=ut("("+Vt(Lt).join("|")+")","g"),Ut=ut("["+Vt(Kt).join("")+"]","g"),Qt=ae(function(n,e,t){yt.call(n,t)?n[t]++:n[t]=1}),Gt=ae(function(n,e,t){(yt.call(n,t)?n[t]:n[t]=[]).push(e)}),Ht=ae(function(n,e,t){n[t]=e});Ft&&X&&"function"==typeof _t&&(Ue=function(n){if(!me(n))throw new it;return _t.apply(t,arguments)});var Jt=8==St(w+"08")?St:function(n,e){return St(we(n)?n.replace(A,""):n,e||0)};return Y.after=function(n,e){if(!me(e))throw new it;return function(){return 1>--n?e.apply(this,arguments):void 0}},Y.assign=H,Y.at=function(n){for(var e=arguments,t=-1,r=re(e,!0,!1,1),e=e[2]&&e[2][e[1]]===n?1:r.length,u=Xe(e);++t=_&&o(i?r[i]:g)}n:for(;++l(d?e(d,y):s(g,y))){for(i=u,(d||g).push(y);--i;)if(d=f[i],0>(d?e(d,y):s(r[i],y)))continue n;v.push(y)}}for(;u--;)(d=f[u])&&p(d);return c(f),c(g),v},Y.invert=de,Y.invoke=function(n,e){var t=Dt.call(arguments,2),r=-1,u="function"==typeof e,o=n?n.length:0,i=Xe("number"==typeof o?o:0);return Oe(n,function(n){i[++r]=(u?e:n[e]).apply(n,t)}),i},Y.keys=Vt,Y.map=Ae,Y.max=Ie,Y.memoize=function(n,e){function t(){var r=t.cache,u=e?e.apply(this,arguments):m+arguments[0];return yt.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!me(n))throw new it;return t.cache={},t},Y.merge=function(n){var e=arguments,t=2;if(!_e(n))return n;if("number"!=typeof e[2]&&(t=e.length),t>3&&"function"==typeof e[t-2])var r=te(e[--t-1],e[t--],2);else t>2&&"function"==typeof e[t-1]&&(r=e[--t]);for(var e=Dt.call(arguments,1,t),u=-1,o=a(),i=a();++ua&&(o=a)}}else e=!e&&we(n)?r:Y.createCallback(e,t,3),Oe(n,function(n,t,r){t=e(n,t,r),u>t&&(u=t,o=n)});return o},Y.omit=function(n,e,t){var r=pe(),u="function"==typeof e,o={};if(u)e=Y.createCallback(e,t,3);else var i=re(arguments,!0,!1,1);return b(n,function(n,t,a){(u?!e(n,t,a):0>r(i,t))&&(o[t]=n)}),o},Y.once=function(n){var e,t;if(!me(n))throw new it;return function(){return e?t:(e=!0,t=n.apply(this,arguments),n=null,t)}},Y.pairs=function(n){for(var e=-1,t=Vt(n),r=t.length,u=Xe(r);++et?Nt(0,r+t):Tt(t,r-1))+1);r--;)if(n[r]===e)return r;return-1},Y.mixin=Ge,Y.noConflict=function(){return t._=lt,this},Y.parseInt=Jt,Y.random=He,Y.reduce=Te,Y.reduceRight=Se,Y.result=function(n,e){if(n){var t=n[e];return me(t)?n[e]():t}},Y.runInContext=h,Y.size=function(n){var e=n?n.length:0;return"number"==typeof e?e:Vt(n).length},Y.some=De,Y.sortedIndex=We,Y.template=function(n,e,t){var r=Y.templateSettings;n||(n=""),t=Q({},t,r);var u,o=Q({},t.imports,r.imports),r=Vt(o),o=je(o),a=0,f=t.interpolate||I,l="__p+='",f=ut((t.escape||I).source+"|"+f.source+"|"+(f===E?C:I).source+"|"+(t.evaluate||I).source+"|$","g");n.replace(f,function(e,t,r,o,f,c){return r||(r=o),l+=n.slice(a,c).replace(T,i),t&&(l+="'+__e("+t+")+'"),f&&(u=!0,l+="';"+f+";__p+='"),r&&(l+="'+((__t=("+r+"))==null?'':__t)+'"),a=c+e.length,e}),l+="';\n",f=t=t.variable,f||(t="obj",l="with("+t+"){"+l+"}"),l=(u?l.replace(j,""):l).replace(k,"$1").replace(x,"$1;"),l="function("+t+"){"+(f?"":t+"||("+t+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+l+"return __p}";try{var c=nt(r,"return "+l).apply(v,o)}catch(p){throw p.source=l,p}return e?c(e):(c.source=l,c)},Y.unescape=function(n){return null==n?"":ot(n).replace(Mt,he)},Y.uniqueId=function(n){var e=++d;return ot(null==n?"":n)+e},Y.all=xe,Y.any=De,Y.detect=Be,Y.findWhere=Be,Y.foldl=Te,Y.foldr=Se,Y.include=ke,Y.inject=Te,y(Y,function(n,e){Y.prototype[e]||(Y.prototype[e]=function(){var e=[this.__wrapped__],t=this.__chain__;return mt.apply(e,arguments),e=n.apply(Y,e),t?new ne(e,t):e})}),Y.first=Fe,Y.last=function(n,e,t){var r=0,u=n?n.length:0;if("number"!=typeof e&&null!=e){var o=u;for(e=Y.createCallback(e,t,3);o--&&e(n[o],o,n);)r++}else if(r=e,null==r||t)return n?n[u-1]:v;return s(n,Nt(0,u-r))},Y.sample=function(n,e,t){var r=n?n.length:0;return"number"!=typeof r&&(n=je(n)),null==e||t?n?n[He(r-1)]:v:(n=$e(n),n.length=Tt(Nt(0,e),n.length),n)},Y.take=Fe,Y.head=Fe,y(Y,function(n,e){var t="sample"!==e;Y.prototype[e]||(Y.prototype[e]=function(e,r){var u=this.__chain__,o=n(this.__wrapped__,e,r);return u||null!=e&&(!r||t&&"function"==typeof e)?new ne(o,u):o})}),Y.VERSION="2.2.1",Y.prototype.chain=function(){return this.__chain__=!0,this},Y.prototype.toString=function(){return ot(this.__wrapped__)},Y.prototype.value=Je,Y.prototype.valueOf=Je,Oe(["join","pop","shift"],function(n){var e=at[n];Y.prototype[n]=function(){var n=this.__chain__,t=e.apply(this.__wrapped__,arguments);return n?new ne(t,n):t}}),Oe(["push","reverse","sort","unshift"],function(n){var e=at[n];Y.prototype[n]=function(){return e.apply(this.__wrapped__,arguments),this}}),Oe(["concat","slice","splice"],function(n){var e=at[n];Y.prototype[n]=function(){return new ne(e.apply(this.__wrapped__,arguments),this.__chain__)}}),Y}var v,g=[],y=[],d=0,m=+new Date+"",_=75,b=40,w=" \f \n\r\u2028\u2029 ᠎              ",j=/\b__p\+='';/g,k=/\b(__p\+=)''\+/g,x=/(__e\(.*?\)|\b__t\))\+'';/g,C=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,B=/\w*$/,O=/^function[ \n\r\t]+\w/,E=/<%=([\s\S]+?)%>/g,A=RegExp("^["+w+"]*0+(?=.$)"),I=/($^)/,N=/\bthis\b/,T=/['\n\r\t\u2028\u2029\\]/g,S="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),$="[object Arguments]",D="[object Array]",R="[object Boolean]",F="[object Date]",q="[object Function]",z="[object Number]",W="[object Object]",P="[object RegExp]",V="[object String]",K={};K[q]=!1,K[$]=K[D]=K[R]=K[F]=K[z]=K[W]=K[P]=K[V]=!0;var L={leading:!1,maxWait:0,trailing:!1},M={configurable:!1,enumerable:!1,value:null,writable:!1},U={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},Q={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},G=U[typeof window]&&window||this,H=U[typeof exports]&&exports&&!exports.nodeType&&exports,J=U[typeof module]&&module&&!module.nodeType&&module,X=J&&J.exports===H&&H,Y=U[typeof global]&&global;!Y||Y.global!==Y&&Y.window!==Y||(G=Y);var Z=h();"function"==typeof define&&"object"==typeof define.amd&&define.amd?(G._=Z,define(function(){return Z})):H&&J?X?(J.exports=Z)._=Z:H._=Z:G._=Z}).call(this),window.Behave={},Behave.view=$(document.body),Behave.domTypes={field:{elementTypes:["input","select","option","label","textarea","form"],attrOptions:["name","for","placeholder","contains","type","test-me"]},clickable:{elementTypes:["button","a"],attrOptions:["contains","href","test-me"]},icon:{elementTypes:["icon","div","span"],attrOptions:["type","class","test-me"]},display:{elementTypes:[""],attrOptions:["test-me","contains"]}},Behave.getAllElsAttrOptions=["name","for","placeholder","type","test-me"],Behave.find=function(n,e){var t="",r=e?{specificOption:Behave.domTypes[e]}:Behave.domTypes;return _.each(r,function(e){_.each(e.elementTypes,function(r){return t.length?!1:void _.each(e.attrOptions,function(e){switch(e){case"contains":var u=":contains("+n+")";t=Behave.view.find(r+u);break;case"class":t=findByClass(n,r,"glyphicon-");break;default:var u="["+e+"='"+n+"']";t=Behave.view.find(r+u)}return t.length?!1:void 0})})}),t&&t.is("label")&&(t=getClosestInput(t)),t.length||(t=Behave.view.find(n)),t},Behave.fill=function(n){var e=n instanceof jQuery?n:Behave.find(n,"field"),t=function(n){if(e.is("form")||"form"===e.attr("type")){if(!_.isObject(n))throw new Error("Must pass in a hash with signature of {element: value} when filling a whole form");return void _.each(n,function(n,e){Behave.fill(e).with(n)})}return"checkbox"===e.attr("type")?e.prop("checked",n):void e.val(n).trigger("input")},r={"with":t};return r},Behave.getAllEls=function(n,e){n=n||Behave.view,e=e||{};var t=n.children;t.length&&n.children().each(function(){e=Behave.getAllEls($(this),e)}),_.each(Behave.getAllElsAttrOptions,function(t){var r=cleanVal(n.attr(t));r&&(n.reload=function(){return Behave.find(r)}),r&&(e[r]=n)});var r=n.text();return r&&(e[cleanVal(r)]=n),e};var getClosestInput=function(n){var e=n.next();if(e.is("input"))return e;var t=e.find("input");return t.length?t:n},findByClass=function(n,e,t){return t=t||"",e=e||"",Behave.view.find(e+"."+t+n).first()},cleanVal=function(n){if(n){if(n=n.replace(" ",""),-1!==n.indexOf("-")){var e=n.split("-");e[1]=e[1][0].toUpperCase()+e[1].substring(1),e[2]&&(e[2]=e[2][0].toUpperCase()+e[2].substring(1)),n=e.join("")}return n}};window.find=Behave.find,window.fill=Behave.fill; \ No newline at end of file +(function(){function n(n,e,t){t=(t||0)-1;for(var r=n?n.length:0;++tr||"undefined"==typeof t)return 1;if(r>t||"undefined"==typeof r)return-1}return n.n-e.n}function o(n){var e=-1,r=n.length,u=n[0],o=n[0|r/2],i=n[r-1];if(u&&"object"==typeof u&&o&&"object"==typeof o&&i&&"object"==typeof i)return!1;for(u=f(),u["false"]=u["null"]=u["true"]=u.undefined=!1,o=f(),o.k=n,o.l=u,o.push=t;++et?0:t);++r3&&"function"==typeof i[f-2])var l=te(i[--f-1],i[f--],2);else f>2&&"function"==typeof i[f-1]&&(l=i[--f]);for(;++a=_&&f===n,v=u||h?a():s;if(h){var g=o(v);g?(f=e,v=g):(h=!1,v=u?v:(c(v),s))}for(;++if(v,y))&&((u||h)&&v.push(y),s.push(g))}return h?(c(v.k),p(v)):u&&c(v),s}function ae(n){return function(e,t,r){var u={};t=Y.createCallback(t,r,3),r=-1;var o=e?e.length:0;if("number"==typeof o)for(;++rt?Nt(0,o+t):t)||0,Pt(n)?i=-1o&&(o=a)}}else e=!e&&we(n)?r:Y.createCallback(e,t,3),Oe(n,function(n,t,r){t=e(n,t,r),t>u&&(u=t,o=n)});return o}function Ne(n,e){var t=-1,r=n?n.length:0;if("number"==typeof r)for(var u=Xe(r);++targuments.length;e=te(e,r,4);var o=-1,i=n.length;if("number"==typeof i)for(u&&(t=n[++o]);++oarguments.length;return e=te(e,r,4),Ee(n,function(n,r,o){t=u?(u=!1,n):e(t,n,r,o)}),t}function Se(n){var e=-1,t=n?n.length:0,r=Xe("number"==typeof t?t:0);return Oe(n,function(n){var t=He(++e);r[e]=r[t],r[t]=n}),r}function De(n,e,t){var r;e=Y.createCallback(e,t,3),t=-1;var u=n?n.length:0;if("number"==typeof u)for(;++t=_&&u===n;if(l){var c=o(a);c?(u=e,a=c):l=!1}for(;++ru(a,c)&&f.push(c);return l&&p(a),f}function Fe(n,e,t){var r=0,u=n?n.length:0;if("number"!=typeof e&&null!=e){var o=-1;for(e=Y.createCallback(e,t,3);++or?Nt(0,u+r):r||0}else if(r)return r=We(e,t),e[r]===t?r:-1;return n(e,t,r)}function ze(n,e,t){if("number"!=typeof e&&null!=e){var r=0,u=-1,o=n?n.length:0;for(e=Y.createCallback(e,t,3);++uu;)r=u+o>>>1,t(n[r])t?0:t);++e0?c=bt(u,t):(i&&st(i),t=p,i=c=p=v,t&&(s=mt(),a=n.apply(l,o)))}var o,i,a,f,l,c,p,s=0,h=!1,g=!0;if(!de(n))throw new it;if(e=Nt(0,e)||0,!0===t)var y=!0,g=!1;else _e(t)&&(y=t.leading,h="maxWait"in t&&(Nt(e,t.maxWait)||0),g="trailing"in t?t.trailing:g);return function(){if(o=arguments,f=mt(),l=this,p=g&&(c||!y),!1===h)var t=y&&!c;else{i||y||(s=f);var v=h-(f-s);v>0?i||(i=bt(r,v)):(i&&(i=st(i)),s=f,a=n.apply(l,o))}return c||e===h||(c=bt(u,e)),t&&(a=n.apply(l,o)),a}}function Ue(n){if(!de(n))throw new it;var e=Dt.call(arguments,1);return bt(function(){n.apply(v,e)},1)}function Qe(n){return n}function Ge(n,e){var t=n,r=!e||de(t);e||(t=ne,e=n,n=Y),Oe(ye(e),function(u){var o=n[u]=e[u];r&&(t.prototype[u]=function(){var e=this.__wrapped__,r=[e];return dt.apply(r,arguments),r=o.apply(n,r),e&&"object"==typeof e&&e===r?this:(r=new t(r),r.__chain__=this.__chain__,r)})})}function He(n,e,t){var r=null==n,u=null==e;return null==t&&("boolean"==typeof n&&u?(t=n,n=1):u||"boolean"!=typeof e||(t=e,u=!0)),r&&u&&(e=1),n=+n||0,u?(e=n,n=0):e=+e||0,r=St(),t||n%1||e%1?Tt(n+r*(e-n+parseFloat("1e-"+((r+"").length-1))),e):n+ht(r*(e-n+1))}function Je(){return this.__wrapped__}t=t?Z.defaults(G.Object(),t,Z.pick(G,$)):G;var Xe=t.Array,Ye=t.Boolean,Ze=t.Date,nt=t.Function,et=t.Math,tt=t.Number,rt=t.Object,ut=t.RegExp,ot=t.String,it=t.TypeError,at=[],ft=rt.prototype,lt=t._,ct=ut("^"+ot(ft.valueOf).replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),pt=et.ceil,st=t.clearTimeout,ht=et.floor,vt=nt.prototype.toString,gt=ct.test(gt=rt.getPrototypeOf)&>,yt=ft.hasOwnProperty,mt=ct.test(mt=Ze.now)&&mt||function(){return+new Ze},dt=at.push,_t=t.setImmediate,bt=t.setTimeout,wt=at.splice,jt=ft.toString,kt=at.unshift,xt=function(){try{var n={},e=ct.test(e=rt.defineProperty)&&e,t=e(n,n,n)&&e}catch(r){}return t}(),Ct=ct.test(Ct=jt.bind)&&Ct,Bt=ct.test(Bt=rt.create)&&Bt,Ot=ct.test(Ot=Xe.isArray)&&Ot,Et=t.isFinite,At=t.isNaN,It=ct.test(It=rt.keys)&&It,Nt=et.max,Tt=et.min,$t=t.parseInt,St=et.random,Dt=at.slice,Rt=ct.test(t.attachEvent),Ft=Ct&&!/\n|true/.test(Ct+Rt),qt={};qt[D]=Xe,qt[R]=Ye,qt[F]=Ze,qt[q]=nt,qt[W]=rt,qt[z]=tt,qt[P]=ut,qt[V]=ot,ne.prototype=Y.prototype;var zt=Y.support={};zt.fastBind=Ct&&!Ft,zt.funcDecomp=!ct.test(t.a)&&N.test(h),zt.funcNames="string"==typeof nt.name,Y.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:E,variable:"",imports:{_:Y}},Bt||(le=function(n){if(_e(n)){l.prototype=n;var e=new l;l.prototype=null}return e||{}});var Wt=xt?function(n,e){M.value=e,xt(n,"__bindData__",M)}:l,Pt=Ot||function(n){return n&&"object"==typeof n&&"number"==typeof n.length&&jt.call(n)==D||!1},Vt=It?function(n){return _e(n)?It(n):[]}:J,Kt={"&":"&","<":"<",">":">",'"':""","'":"'"},Lt=me(Kt),Mt=ut("("+Vt(Lt).join("|")+")","g"),Ut=ut("["+Vt(Kt).join("")+"]","g"),Qt=ae(function(n,e,t){yt.call(n,t)?n[t]++:n[t]=1}),Gt=ae(function(n,e,t){(yt.call(n,t)?n[t]:n[t]=[]).push(e)}),Ht=ae(function(n,e,t){n[t]=e});Ft&&X&&"function"==typeof _t&&(Ue=function(n){if(!de(n))throw new it;return _t.apply(t,arguments)});var Jt=8==$t(w+"08")?$t:function(n,e){return $t(we(n)?n.replace(A,""):n,e||0)};return Y.after=function(n,e){if(!de(e))throw new it;return function(){return 1>--n?e.apply(this,arguments):void 0}},Y.assign=H,Y.at=function(n){for(var e=arguments,t=-1,r=re(e,!0,!1,1),e=e[2]&&e[2][e[1]]===n?1:r.length,u=Xe(e);++t=_&&o(i?r[i]:g)}n:for(;++l(m?e(m,y):s(g,y))){for(i=u,(m||g).push(y);--i;)if(m=f[i],0>(m?e(m,y):s(r[i],y)))continue n;v.push(y)}}for(;u--;)(m=f[u])&&p(m);return c(f),c(g),v},Y.invert=me,Y.invoke=function(n,e){var t=Dt.call(arguments,2),r=-1,u="function"==typeof e,o=n?n.length:0,i=Xe("number"==typeof o?o:0);return Oe(n,function(n){i[++r]=(u?e:n[e]).apply(n,t)}),i},Y.keys=Vt,Y.map=Ae,Y.max=Ie,Y.memoize=function(n,e){function t(){var r=t.cache,u=e?e.apply(this,arguments):d+arguments[0];return yt.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!de(n))throw new it;return t.cache={},t},Y.merge=function(n){var e=arguments,t=2;if(!_e(n))return n;if("number"!=typeof e[2]&&(t=e.length),t>3&&"function"==typeof e[t-2])var r=te(e[--t-1],e[t--],2);else t>2&&"function"==typeof e[t-1]&&(r=e[--t]);for(var e=Dt.call(arguments,1,t),u=-1,o=a(),i=a();++ua&&(o=a)}}else e=!e&&we(n)?r:Y.createCallback(e,t,3),Oe(n,function(n,t,r){t=e(n,t,r),u>t&&(u=t,o=n)});return o},Y.omit=function(n,e,t){var r=pe(),u="function"==typeof e,o={};if(u)e=Y.createCallback(e,t,3);else var i=re(arguments,!0,!1,1);return b(n,function(n,t,a){(u?!e(n,t,a):0>r(i,t))&&(o[t]=n)}),o},Y.once=function(n){var e,t;if(!de(n))throw new it;return function(){return e?t:(e=!0,t=n.apply(this,arguments),n=null,t)}},Y.pairs=function(n){for(var e=-1,t=Vt(n),r=t.length,u=Xe(r);++et?Nt(0,r+t):Tt(t,r-1))+1);r--;)if(n[r]===e)return r;return-1},Y.mixin=Ge,Y.noConflict=function(){return t._=lt,this},Y.parseInt=Jt,Y.random=He,Y.reduce=Te,Y.reduceRight=$e,Y.result=function(n,e){if(n){var t=n[e];return de(t)?n[e]():t}},Y.runInContext=h,Y.size=function(n){var e=n?n.length:0;return"number"==typeof e?e:Vt(n).length},Y.some=De,Y.sortedIndex=We,Y.template=function(n,e,t){var r=Y.templateSettings;n||(n=""),t=Q({},t,r);var u,o=Q({},t.imports,r.imports),r=Vt(o),o=je(o),a=0,f=t.interpolate||I,l="__p+='",f=ut((t.escape||I).source+"|"+f.source+"|"+(f===E?C:I).source+"|"+(t.evaluate||I).source+"|$","g");n.replace(f,function(e,t,r,o,f,c){return r||(r=o),l+=n.slice(a,c).replace(T,i),t&&(l+="'+__e("+t+")+'"),f&&(u=!0,l+="';"+f+";__p+='"),r&&(l+="'+((__t=("+r+"))==null?'':__t)+'"),a=c+e.length,e}),l+="';\n",f=t=t.variable,f||(t="obj",l="with("+t+"){"+l+"}"),l=(u?l.replace(j,""):l).replace(k,"$1").replace(x,"$1;"),l="function("+t+"){"+(f?"":t+"||("+t+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+l+"return __p}";try{var c=nt(r,"return "+l).apply(v,o)}catch(p){throw p.source=l,p}return e?c(e):(c.source=l,c)},Y.unescape=function(n){return null==n?"":ot(n).replace(Mt,he)},Y.uniqueId=function(n){var e=++m;return ot(null==n?"":n)+e},Y.all=xe,Y.any=De,Y.detect=Be,Y.findWhere=Be,Y.foldl=Te,Y.foldr=$e,Y.include=ke,Y.inject=Te,y(Y,function(n,e){Y.prototype[e]||(Y.prototype[e]=function(){var e=[this.__wrapped__],t=this.__chain__;return dt.apply(e,arguments),e=n.apply(Y,e),t?new ne(e,t):e})}),Y.first=Fe,Y.last=function(n,e,t){var r=0,u=n?n.length:0;if("number"!=typeof e&&null!=e){var o=u;for(e=Y.createCallback(e,t,3);o--&&e(n[o],o,n);)r++}else if(r=e,null==r||t)return n?n[u-1]:v;return s(n,Nt(0,u-r))},Y.sample=function(n,e,t){var r=n?n.length:0;return"number"!=typeof r&&(n=je(n)),null==e||t?n?n[He(r-1)]:v:(n=Se(n),n.length=Tt(Nt(0,e),n.length),n)},Y.take=Fe,Y.head=Fe,y(Y,function(n,e){var t="sample"!==e;Y.prototype[e]||(Y.prototype[e]=function(e,r){var u=this.__chain__,o=n(this.__wrapped__,e,r);return u||null!=e&&(!r||t&&"function"==typeof e)?new ne(o,u):o})}),Y.VERSION="2.2.1",Y.prototype.chain=function(){return this.__chain__=!0,this},Y.prototype.toString=function(){return ot(this.__wrapped__)},Y.prototype.value=Je,Y.prototype.valueOf=Je,Oe(["join","pop","shift"],function(n){var e=at[n];Y.prototype[n]=function(){var n=this.__chain__,t=e.apply(this.__wrapped__,arguments);return n?new ne(t,n):t}}),Oe(["push","reverse","sort","unshift"],function(n){var e=at[n];Y.prototype[n]=function(){return e.apply(this.__wrapped__,arguments),this}}),Oe(["concat","slice","splice"],function(n){var e=at[n];Y.prototype[n]=function(){return new ne(e.apply(this.__wrapped__,arguments),this.__chain__)}}),Y}var v,g=[],y=[],m=0,d=+new Date+"",_=75,b=40,w=" \f \n\r\u2028\u2029 ᠎              ",j=/\b__p\+='';/g,k=/\b(__p\+=)''\+/g,x=/(__e\(.*?\)|\b__t\))\+'';/g,C=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,B=/\w*$/,O=/^function[ \n\r\t]+\w/,E=/<%=([\s\S]+?)%>/g,A=RegExp("^["+w+"]*0+(?=.$)"),I=/($^)/,N=/\bthis\b/,T=/['\n\r\t\u2028\u2029\\]/g,$="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),S="[object Arguments]",D="[object Array]",R="[object Boolean]",F="[object Date]",q="[object Function]",z="[object Number]",W="[object Object]",P="[object RegExp]",V="[object String]",K={};K[q]=!1,K[S]=K[D]=K[R]=K[F]=K[z]=K[W]=K[P]=K[V]=!0;var L={leading:!1,maxWait:0,trailing:!1},M={configurable:!1,enumerable:!1,value:null,writable:!1},U={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},Q={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},G=U[typeof window]&&window||this,H=U[typeof exports]&&exports&&!exports.nodeType&&exports,J=U[typeof module]&&module&&!module.nodeType&&module,X=J&&J.exports===H&&H,Y=U[typeof global]&&global;!Y||Y.global!==Y&&Y.window!==Y||(G=Y);var Z=h();"function"==typeof define&&"object"==typeof define.amd&&define.amd?(G._=Z,define(function(){return Z})):H&&J?X?(J.exports=Z)._=Z:H._=Z:G._=Z}).call(this),$.expr[":"].textEquals=function(n,e,t){var r=$(n).text().match("^"+t[3]+"$");return r&&r.length>0},window.Behave={},Behave.view=$(document.body),Behave.domTypes={field:{elementTypes:["input","select","option","label","textarea","form"],attrOptions:["name","for","placeholder","contains","type","test-me"]},clickable:{elementTypes:["button","a"],attrOptions:["contains","href","test-me"]},icon:{elementTypes:["icon","div","span"],attrOptions:["type","class","test-me"]},display:{elementTypes:[""],attrOptions:["test-me","contains"]}},Behave.getAllElsAttrOptions=["name","for","placeholder","type","test-me"],Behave.find=function(n,e){var t="",r=e?{specificOption:Behave.domTypes[e]}:Behave.domTypes;return _.each(r,function(e){_.each(e.elementTypes,function(r){return t.length?!1:void _.each(e.attrOptions,function(e){switch(e){case"contains":var u=":textEquals("+n+")";t=Behave.view.find(r+u);break;case"class":t=findByClass(n,r,"glyphicon-");break;default:var u="["+e+"='"+n+"']";t=Behave.view.find(r+u)}return t.length?!1:void 0})})}),t&&t.is("label")&&(t=getClosestInput(t)),t.length||(t=Behave.view.find(n)),t},Behave.fill=function(n){var e=n instanceof jQuery?n:Behave.find(n,"field"),t=function(n){if(e.is("form")||"form"===e.attr("type")){if(!_.isObject(n))throw new Error("Must pass in a hash with signature of {element: value} when filling a whole form");return void _.each(n,function(n,e){Behave.fill(e).with(n)})}return"checkbox"===e.attr("type")?e.prop("checked",n):void e.val(n).trigger("input")},r={"with":t};return r},Behave.getAllEls=function(n,e){n=n||Behave.view,e=e||{};var t=n.children;t.length&&n.children().each(function(){e=Behave.getAllEls($(this),e)}),_.each(Behave.getAllElsAttrOptions,function(t){var r=cleanVal(n.attr(t));r&&(n.reload=function(){return Behave.find(r)}),r&&(e[r]=n)});var r=n.text();return r&&(e[cleanVal(r)]=n),e};var getClosestInput=function(n){var e=n.next();if(e.is("input"))return e;var t=e.find("input");return t.length?t:n},findByClass=function(n,e,t){return t=t||"",e=e||"",Behave.view.find(e+"."+t+n).first()},cleanVal=function(n){if(n){if(n=n.replace(" ",""),-1!==n.indexOf("-")){var e=n.split("-");e[1]=e[1][0].toUpperCase()+e[1].substring(1),e[2]&&(e[2]=e[2][0].toUpperCase()+e[2].substring(1)),n=e.join("")}return n}};window.find=Behave.find,window.fill=Behave.fill; \ No newline at end of file diff --git a/src/behave.js b/src/behave.js index 03215ba..a97bbd7 100644 --- a/src/behave.js +++ b/src/behave.js @@ -1,5 +1,10 @@ -window.Behave = {}; +// Little jQuery extension to have exact equals; +$.expr[':'].textEquals = function(a, i, m) { + var match = $(a).text().match("^" + m[3] + "$") + return match && match.length > 0; +} +window.Behave = {}; Behave.view = $(document.body); Behave.domTypes = { field: { @@ -34,7 +39,7 @@ Behave.find = function(identifier, type) { _.each(searchParams.attrOptions, function(attrOption) { switch (attrOption) { case 'contains': - var filter = ":contains(" + identifier + ")" + var filter = ":textEquals("+ identifier + ")" element = Behave.view.find(elType + filter); break; case 'class': diff --git a/test/test.js b/test/test.js index 3684acd..113d478 100644 --- a/test/test.js +++ b/test/test.js @@ -53,9 +53,12 @@ bResult.is('button').should.eql(true); bResult.text().should.eql(jqResult.text()); }); - it("should default to doing a rough search", function() { - var bResult = Behave.find('Subdo', 'clickable'); - bResult.text().should.eql(jqResult.text()); + it("should default to doing an exact search", function() { + var bRoughResult = Behave.find('Subdo', 'clickable'); + var bExactResult = Behave.find('Subdomain', 'clickable'); + bRoughResult.is('button').should.eql(false); + bExactResult.is('button').should.eql(true); + bExactResult.text().should.eql(jqResult.text()); }); it("should find things based on href", function() { bResult = Behave.find("www.test.com", 'clickable'); diff --git a/test/testRunner.html b/test/testRunner.html index 2436843..6872d33 100644 --- a/test/testRunner.html +++ b/test/testRunner.html @@ -8,7 +8,7 @@
- +