forked from tombonner/jurlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjurlp.min.js
27 lines (14 loc) · 12.9 KB
/
jurlp.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
jurlp.min.js
JQuery URL parser plugin for parsing, manipulating, filtering and monitoring URLs in href and src attributes within arbitrary elements (including document.location.href), as well as creating anchor elements from URLs found in HTML/text.
Authors:
Thomas James Bonner ([email protected]).
Yonas Sandbæk ([email protected]).
Version:
1.0.4
Copyright (C) 2012, Thomas James Bonner ([email protected]).
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function(w){var q=/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;var I={};var g="";var h=function(){var Q=L.apply(this);if(Q==""){if(this.get(0)==w(document).get(0)){Q=document.location.href}else{if(this.is("[href]")){Q=this.attr("href")}else{if(this.is("[src]")){Q=this.attr("src")}}}if(Q!=""){Q=D(Q);this.data("href",Q)}}};var n=function(){if(this.get(0)!=w(document).get(0)&&this.attr("href")==null&&this.attr("src")==null){if(this.html()!=null&&this.hasClass("jurlp-span")==false){var W=[];var V=false;var S="";var R=/((((mailto|spotify|skype)\:([a-zA-Z0-9\.\-\:@\?\=\%]*))|((ftp|git|irc|ircs|irc6|pop|rss|ssh|svn|udp|feed|imap|ldap|nntp|rtmp|sftp|snmp|xmpp|http|https|telnet|ventrilo|webcal|view\-source)\:[\/]{2})?(([a-zA-Z0-9\.\-]+)\:([a-zA-Z0-9\.&;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--80akhbyknj4f|xn--9t4b11yi5a|xn--deba0ad|xn--g6w251d|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--jxalpdlp|xn--kgbechtv|xn--zckzah|[a-zA-Z]{2}))(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\?\'\\\+&;%\$\=~_\-]+)?(#\w*)?)*))/i;var U=this.html();while(S=R.exec(U)){U=U.replace(S[0],"$"+W.length);W.push(S[0]);V=true}for(var T=0;T<W.length;T++){var X=U.indexOf("$"+T);var Q=U.substring(X-6,X-1);if(Q=="href="||Q==" src="||U.substring(X-1,X)==">"){U=U.replace("$"+T,W[T])}else{U=U.replace("$"+T,'<a href="[url]" class="jurlp-no-watch">[url]</a>'.replace(/\[url\]/g,W[T]))}}if(V!=false){this.addClass("jurlp-span");this.html(U);return this.find("a[href]").each(function(){var Y=L.apply(w(this));O.apply(w(this),[Y])})}}}return null};var J=function(Q,R){if(this.is("["+Q+"]")!=false){if(this.data("original-"+Q)==null){this.data("original-"+Q,this.attr(Q))}this.attr(Q,R)}};var P=function(Q){if(this.data("original-"+Q)!=null){this.attr(Q,this.data("original-"+Q));this.removeData("original-"+Q)}};var j=function(){this.removeData("href");P.apply(this,["href"]);P.apply(this,["src"]);this.removeData("jurlp-no-watch");this.removeData("is-watched");y.unwatch.apply(this)};var L=function(){return this.href||this.data("href")||this.attr("href")||this.attr("src")||""};var v=function(Q,R){O.apply(this,[p(L.apply(this),Q,R)])};var N=function(Q){v.apply(this,[Q[0],Q[1]])};var O=function(Q){if(typeof Q=="object"){Q=o(Q)}Q=D(Q);if(this.href!=null){this.href=Q;return}if(this.get(0)==w(document).get(0)){this.data("href",Q)}else{J.apply(this,["href",Q]);J.apply(this,["src",Q])}};var H=function(Q){if(Q==null){return{scheme:"",user:"",password:"",host:"",port:"",path:"",query:"",fragment:""}}var V={user:"",password:""};if(Q.substring(0,2)=="//"){Q="http:"+Q}if(Q!=""&&Q.indexOf("://")==-1){Q="http://"+Q}if(Q.indexOf("@")!=-1){var T=Q.match(q);if(T[4]){V.user=T[4]}if(T[5]){V.password=T[5]}}var X=document.createElement("a");X.href=Q;try{var Z=X.protocol}catch(U){if(U.number==-2146697202){var W="";if(V.user!=""){W+=V.user;if(V.password!=""){W+=":"+V.password}W+="@";X.href=Q.replace(W,"")}}}var Y=X.protocol;if(X.protocol.indexOf("//")==-1){Y+="//"}var R=X.pathname;if(R[0]!="/"){R="/"+R}var S=X.port+"";if((S=="21"&&Q.indexOf(":21")==-1)||(S=="80"&&Q.indexOf(":80")==-1)||(S=="443"&&Q.indexOf(":443")==-1)||S=="0"){S=""}return{scheme:Y,user:V.user,password:V.password,host:X.hostname,port:S,path:R,query:X.search,fragment:X.hash}};var o=function(Q){var R=Q.scheme;if(Q.user!=null&&Q.user!=""){R+=Q.user;if(Q.password!=null&&Q.password!=""){R+=":"+Q.password}R+="@"}return R+Q.host+(Q.port!=""?":"+Q.port:"")+Q.path+Q.query+Q.fragment};var D=function(Q){return e.parse(Q).toString()};var K=function(){return o(this)};var p=function(Q,R,T){var S=e.parse(Q);S[R]=T;return o(S)};var l=function(Q){if(typeof Q=="string"){return e.parse(Q)}return Q};var C=function(Q){return l(Q).fragment};var c=function(Q){var R=l(Q).query;if(R[0]=="?"){return R.slice(1)}return R};var z=function(R){var S={};var Q=u(L.apply(this));if(typeof R=="string"){if(R[0]=="?"){Q={};R=R.substring(1)}S=r(R)}else{S=R}if(w.isEmptyObject(S)==false){S=w.extend(Q,S)}else{Q={}}v.apply(this,["query",E.apply(S)])};var r=function(T){var Q={};if(T!=""){var S=T.split("&");for(var R=0;R<S.length;R++){var U=S[R].split("=");Q[U[0]]=U[1]}}return Q};var u=function(Q){return r(c(Q))};var E=function(){var Q="";for(var R in this){if(R!="toString"&&this[R]!=null){Q+="&"+R+"="+this[R]}}if(Q[0]=="&"){Q="?"+Q.slice(1)}return Q};var k=function(Q){var R=l(Q).path;if(R[0]=="/"){return R.slice(1)}return R};var F=function(R){var Q=k(R);if(Q!=""){return Q.split("/")}return[]};var M=function(V){var S="";var U=l(L.apply(this)).path.split("/");var R=V.split("/");var T=0;if(R[0]==""){U=[];T++}U.splice(0,1);for(var Q=R.length;T<Q;T++){if(R[T]==".."){if(U.length>0){U.splice(U.length-1,1)}}else{if(R[T]=="."){}else{U.push(R[T])}}}v.apply(this,["path",A.apply(U)])};var A=function(){if(this.length>0){return"/"+this.join("/")}return"/"};var d=function(Q){return l(Q).port};var s=function(Q){return l(Q).host};var x=function(Q){return l(Q).password};var G=function(Q){return l(Q).user};var a=function(Q){return l(Q).scheme};var t=function(Q,S,R){if(Q.data("is-watched")==true){return}if(I[g]){I[g].push([S,R])}};var f=function(R,Q){if(this.href!=null){R.apply(this,[Q]);return this}t(this,R,[Q]);return this.each(function(){R.apply(w(this),[Q])})};var i=function(S,R){if(this.href!=null){return S.apply(this,[R])}var Q=[];this.each(function(){Q.push(S.apply(w(this),[R]))});return Q};var b=function(R,S,Q){if(Q.length==0){return R.apply(this)}return S.apply(this,Q)};var m=function(Q){if(y[Q]!=null){return y[Q].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof Q=="object"||Q==null){return y.initialise.apply(this,Array.prototype.slice.call(arguments,1))}}return this};var e={toString:{http:function(){return o(this)},mailto:function(){this.password="";this.path="";this.port="";return o(this)},javascript:function(){return"javascript:"+this.javascript},generic:function(){return this.scheme+this.url}},parsers:{http:function(Q){return w.extend(H(Q),{toString:e.toString.http})},mailto:function(Q){return w.extend(H(Q.substring(7)),{scheme:"mailto:",toString:e.toString.mailto})},javascript:function(Q){return w.extend(H(document.location.href),{javascript:Q.substring(11),toString:e.toString.javascript})},generic:function(Q,R){if(R.substring(0,2)=="//"){return w.extend(H(R.substring(2)),{scheme:Q+"://",toString:e.toString.http})}return{scheme:Q+":",url:R,toString:e.toString.generic}}},parse:function(R){if(typeof R!="string"){return R}var S=R.indexOf(":");if(S!=-1){var Q=R.substring(0,S).toLowerCase();if(this.parsers[Q]!=null){return this.parsers[Q](R)}return this.parsers.generic(Q,R.substring(S+1))}return this.parsers.http(R)}};var B={getUrl:function(){return i.apply(this,[L,null])},setUrl:function(Q){O.apply(this,[Q])},parseUrl:function(){return i.apply(this,[function(){return e.parse(L.apply(this))},null])},getFragment:function(){return i.apply(this,[function(){return C(L.apply(this))},null])},setFragment:function(Q){if(Q[0]!="#"){Q="#"+Q}return f.apply(this,[N,["fragment",Q]])},getQuery:function(){return i.apply(this,[function(){return w.extend(u(L.apply(this)),{toString:E})},null])},setQuery:function(Q){return f.apply(this,[z,Q])},getPath:function(){return i.apply(this,[function(){return w.extend(F(L.apply(this)),{toString:A})},null])},setPath:function(Q){return f.apply(this,[M,Q])},getPort:function(){return i.apply(this,[function(){return d(L.apply(this))},null])},setPort:function(Q){return f.apply(this,[N,["port",Q]])},getHost:function(){return i.apply(this,[function(){return s(L.apply(this))},null])},setHost:function(Q){return f.apply(this,[N,["host",Q]])},getPassword:function(){return i.apply(this,[function(){return x(L.apply(this))},null])},setPassword:function(Q){return f.apply(this,[N,["password",Q]])},getUser:function(){return i.apply(this,[function(){return G(L.apply(this))},null])},setUser:function(Q){return f.apply(this,[N,["user",Q]])},getScheme:function(){return i.apply(this,[function(){return a(L.apply(this))},null])},setScheme:function(Q){return f.apply(this,[N,["scheme",Q]])},filters:{"=":function(R,Q){if(R==Q){return true}return false},"!=":function(R,Q){if(R!=Q){return true}return false},"<":function(R,Q){if(R<Q){return true}return false},">":function(R,Q){if(R>Q){return true}return false},"<=":function(R,Q){if(R<=Q){return true}return false},">=":function(R,Q){if(R>=Q){return true}return false},"*=":function(R,Q){if(R.indexOf(Q)!=-1){return true}return false},"^=":function(R,Q){if(R.length>=Q.length){if(R.substring(0,Q.length)==Q){return true}}return false},"$=":function(R,Q){if(R.length>=Q.length){if(R.substring(R.length-Q.length)==Q){return true}}return false},regex:function(R,Q){return R.match(Q)}}};var y={url:function(Q){return b.apply(this,[B.parseUrl,B.setUrl,arguments])},fragment:function(Q){return b.apply(this,[B.getFragment,B.setFragment,arguments])},query:function(Q){return b.apply(this,[B.getQuery,B.setQuery,arguments])},path:function(Q){return b.apply(this,[B.getPath,B.setPath,arguments])},port:function(Q){return b.apply(this,[B.getPort,B.setPort,arguments])},host:function(Q){return b.apply(this,[B.getHost,B.setHost,arguments])},password:function(Q){return b.apply(this,[B.getPassword,B.setPassword,arguments])},user:function(Q){return b.apply(this,[B.getUser,B.setUser,arguments])},scheme:function(Q){return b.apply(this,[B.getScheme,B.setScheme,arguments])},initialise:function(){var Q=this;var R=[];R=n.apply(w(this));if(R!=null){return f.apply(this.filter(function(){return w(this).get(0)!=w(Q).get(0)}).add(R),[h])}return f.apply(Q,[h])},restore:function(){return f.apply(this,[j])},"goto":function(){document.location.href=L.apply(this)},proxy:function(Q,T){var S=L.apply(this);var R={};O.apply(this,[o(l(Q))]);R[T]=S;B.setQuery.apply(this,[R])},watch:function(S){var Q=this.selector;if(I[g]==null){I[g]=[];w(document).bind("DOMNodeInserted",function R(X){if(I[Q]==null){w(document).unbind("DOMNodeInserted",R);return}var Y=w(X.target).filter(Q);if(Y.get(0)==null){Y=w(X.target).find(Q)}if(Y.length>0&&Y.is(".jurlp-no-watch")==false){var V=false;Y.data("is-watched",true);for(var W=0,T=I[Q].length;W<T;W++){var U=I[Q][W][0].apply(Y,I[Q][W][1]);if(U!=null&&U.length==0){V=true;break}}if(V==false&&typeof S=="function"){Y.each(function(){S(w(this),Q)})}}})}return this},unwatch:function(){I[this.selector]=null},filter:function(S,Q,T){var R=e.parse(L.apply(this));if(Q=="=="){Q="="}if((S=="url"||R[S]!=null)&&B.filters[Q]!=null){t(this,y.filter,[S,Q,T]);return this.filter(function(){var U=L.apply(w(this));var V="";if(S!="url"){V=e.parse(U)[S]}else{V=U}if(S=="port"){V=parseInt(V,10);T=parseInt(T,10)}return B.filters[Q].apply(w(this),[V,T])})}return this},"interface":function(){return y}};w.fn.jurlp=function(Q){if(this.selector.indexOf(".filter")==-1){g=this.selector}f.apply(this,[h]);return m.apply(this,arguments)};w.jurlp=function(Q){return{href:Q||document.location.href,url:y.url,scheme:y.scheme,user:y.user,password:y.password,host:y.host,port:y.port,path:y.path,query:y.query,fragment:y.fragment,proxy:y.proxy,"goto":y["goto"],watch:function(S){var R=this.host();return w('[href*="'+R+'"],[src*="'+R+'"]').jurlp("watch",S)},unwatch:function(){var R=this.host();return w('[href*="'+R+'"],[src*="'+R+'"]').jurlp("unwatch")}}}})(jQuery);