Permalink
Browse files

bump to version 0.0.5

  • Loading branch information...
1 parent d04cd76 commit bd3d2802f34259bb725a51642c257575b2f438ca @davidmarkclements committed Oct 4, 2012
View
128 R.js
@@ -1,3 +1,85 @@
+/*! respondu - v0.0.52012-10-04 | David Mark Clements | BSD License */
+
+/*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas. Dual MIT/BSD license */
+/* With extra support for ie < 9, see https://github.com/benschwarz/matchMedia.js/tree/IE7-8 */
+window.matchMedia = window.matchMedia || (function(doc, undefined){
+
+ var docElem = doc.documentElement,
+ refNode = docElem.firstElementChild || docElem.firstChild,
+ // fakeBody required for <FF4 when executed in <head>
+ fakeBody = doc.createElement('body'),
+ div = doc.createElement('div');
+
+ div.id = 'mq-test-1';
+ div.style.cssText = "position:absolute;top:-100em";
+ fakeBody.style.background = "none";
+ fakeBody.appendChild(div);
+
+ var mqRun = function ( mq ) {
+ div.innerHTML = '&shy;<style media="' + mq + '"> #mq-test-1 { width: 42px; }</style>';
+ docElem.insertBefore( fakeBody, refNode );
+ bool = div.offsetWidth === 42;
+ docElem.removeChild( fakeBody );
+
+ return { matches: bool, media: mq };
+ },
+
+ getEmValue = function () {
+ var ret,
+ body = docElem.body,
+ fakeUsed = false;
+
+ div.style.cssText = "position:absolute;font-size:1em;width:1em";
+
+ if( !body ) {
+ body = fakeUsed = doc.createElement( "body" );
+ body.style.background = "none";
+ }
+
+ body.appendChild( div );
+
+ docElem.insertBefore( body, docElem.firstChild );
+
+ if( fakeUsed ) {
+ docElem.removeChild( body );
+ } else {
+ body.removeChild( div );
+ }
+
+ //also update eminpx before returning
+ ret = eminpx = parseFloat( div.offsetWidth );
+
+ return ret;
+ },
+
+ //cached container for 1em value, populated the first time it's needed
+ eminpx,
+
+ // verify that we have support for a simple media query
+ mqSupport = mqRun( '(min-width: 0px)' ).matches;
+
+ return function ( mq ) {
+ if( mqSupport ) {
+ return mqRun( mq );
+ } else {
+ var min = mq.match( /\(min\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/ ) && parseFloat( RegExp.$1 ) + ( RegExp.$2 || "" ),
+ max = mq.match( /\(max\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/ ) && parseFloat( RegExp.$1 ) + ( RegExp.$2 || "" ),
+ minnull = min === null,
+ maxnull = max === null,
+ currWidth = doc.body.offsetWidth,
+ em = 'em';
+
+ if( !!min ) { min = parseFloat( min ) * ( min.indexOf( em ) > -1 ? ( eminpx || getEmValue() ) : 1 ); }
+ if( !!max ) { max = parseFloat( max ) * ( max.indexOf( em ) > -1 ? ( eminpx || getEmValue() ) : 1 ); }
+
+ bool = ( !minnull || !maxnull ) && ( minnull || currWidth >= min ) && ( maxnull || currWidth <= max );
+
+ return { matches: bool, media: mq };
+ }
+ };
+
+}( document ));
+
;(function (_w) {
function iedoc() {
@@ -14,7 +96,7 @@
function i(){if(d){return}d=true;if(document.addEventListener&&!c.opera){document.addEventListener("DOMContentLoaded",g,false)}if(c.msie&&_w==top)(function(){if(e)return;try{document.documentElement.doScroll("left")}catch(a){setTimeout(arguments.callee,0);return}g()})();if(c.opera){document.addEventListener("DOMContentLoaded",function(){if(e)return;for(var a=0;a<document.styleSheets.length;a++)if(document.styleSheets[a].disabled){setTimeout(arguments.callee,0);return}g()},false)}if(c.safari){var a;(function(){if(e)return;if(document.readyState!="loaded"&&document.readyState!="complete"){setTimeout(arguments.callee,0);return}if(a===undefined){var b=document.getElementsByTagName("link");for(var c=0;c<b.length;c++){if(b[c].getAttribute("rel")=="stylesheet"){a++}}var d=document.getElementsByTagName("style");a+=d.length}if(document.styleSheets.length!=a){setTimeout(arguments.callee,0);return}g()})()}h(g)}function h(a){var b=_w.onload;if(typeof _w.onload!="function"){_w.onload=a}else{_w.onload=function(){if(b){b()}a()}}}function g(){if(!e){e=true;if(f){for(var a=0;a<f.length;a++){f[a].call(_w,[])}f=[]}}}var a={};var b=navigator.userAgent.toLowerCase();var c={version:(b.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)||[])[1],safari:/webkit/.test(b),opera:/opera/.test(b),msie:/msie/.test(b)&&!/opera/.test(b),mozilla:/mozilla/.test(b)&&!/(compatible|webkit)/.test(b)};var d=false;var e=false;var f=[];DOMReady=function(a,b){i();if(e){a.call(_w,[])}else{f.push(function(){return a.call(_w,[])})}};i()
if(!Array.prototype.reduce){Array.prototype.reduce = function(b){if(this===null||this===undefined)throw new TypeError("Object is null or undefined");var c=0,d=this.length>>0,e;if(typeof b!=="function")throw new TypeError("First argument is not callable");if(arguments.length<2){if(d===0)throw new TypeError("Array length is 0 and no second argument");e=this[0];c=1}else e=arguments[1];while(c<d){if(c in this)e=b.call(undefined,e,this[c],c,this);++c}return e}}
if(!String.prototype.trim) {String.prototype.trim = function () {return this.replace(/^\s+|\s+$/g,'');};}
- window.matchMedia=window.matchMedia||function(a,b){var c,d=a.documentElement,e=d.firstElementChild||d.firstChild,f=a.createElement("body"),g=a.createElement("div");g.id="mq-test-1";g.style.cssText="position:absolute;top:-100em";f.style.background="none";f.appendChild(g);return function(a){g.innerHTML='­<style media="'+a+'"> #mq-test-1 { width: 42px; }</style>';d.insertBefore(f,e);c=g.offsetWidth===42;d.removeChild(f);return{matches:c,media:a}}}(document)
+
var doc = document.implementation.createHTMLDocument ? document.implementation.createHTMLDocument('') : iedoc(),
_d = _w.document, mapTag;
@@ -24,6 +106,7 @@
_w.Respondu = function (implementation, opts, cb) {
if (!(this instanceof _w.Respondu)) {return new _w.Respondu(implementation, opts, cb);}
+ alert(Respondu.ltIE9);
var self = this,
defaults = {
escaper: "<!--", //specify alternative escape code
@@ -72,9 +155,17 @@
x = _h
.replace(/<\/?noscript(.+)?>?/g, '') //remove the noscript tags
.replace(/(<picture(.+)?>)/g,'$1<video>') //iOS Safari at least strips source tags unless they're wrapped in video tags
-
+
+ if (Respondu.ltIE9) {
+ x = x.replace(/<picture/g, '<div data-element="picture"').replace(/<source/g,'<p data-element="source"');
+ }
+
+
+ if (document.documentMode && document.documentMode < 9) x = x.replace(/<picture/g, '<div data-element="picture"').replace(/<source/g,'<p data-element="source"');
+
doc.body.innerHTML = x;
- console.log(doc.body.innerHTML);
+
+ //console.log(x, doc.body.innerHTML);
}
toProcess = self.context.nextSibling;
@@ -202,30 +293,48 @@
+
Respondu.plugin('picture', function (doc, done) {
if ('srcset' in document.createElement('picture')) { //if picture is implemented then just pass through
if (done) done();
return;
}
- var pictures = (doc.getElementsByTagName('picture')), pic, attrs, sources, src, i, c,
+ var pictures = doc.getElementsByTagName('picture'), pic, attrs, sources, src, i, c,
media, sourceImg, img, sW = window.screen.width, pixelRatio,
pr = window.devicePixelRatio || 1;//set devices pixel ratio;
-
+
+
+ if (!pictures.length) {
+ pictures = doc.getElementsByTagName('div');
+ c = pictures.length;
+ while (c--) {
+ if (pictures[c].attributes['data-element'].nodeValue !== 'picture') delete pictures[c];
+ }
+ }
+
for(i = 0; i < pictures.length; i++) {
pic = pictures[i];
attrs = pic.attributes;
sources = pic.getElementsByTagName('source');
+ if (!sources.length) {
+ sources = doc.getElementsByTagName('p');
+ c = sources.length;
+ while (c--) {
+ if (sources[c].attributes['data-element'].nodeValue !== 'source') delete sources[c];
+ }
+ }
+
sourceCandidates = [];
for(c = 0; c < sources.length; c++) {
srcel = sources[c];
media = srcel.getAttribute('media');
//!media means no media attribute, so will be used if no other source elements qualify
if (!media || matchMedia(media).matches) { sourceCandidates.push(srcel); }
- }
+ }
var x = sourceCandidates.length, closest = 0;
@@ -291,6 +400,11 @@ Respondu.plugin('picture', function (doc, done) {
done(); //finished.
});
+
+
+
+
+
Respondu.plugin('srcset', function (doc, done, utils) {
if ('srcset' in document.createElement('img')) { //if srcset is implemented then just pass through
if (done) done();
@@ -363,3 +477,5 @@ Respondu.plugin('srcset', function (doc, done, utils) {
done();
});
+
+Respondu.ltIE9 = /*@cc_on @if (@_jscript_version < 9) ! @end @*/false;
View
@@ -0,0 +1,2 @@
+/*! respondu - v0.0.52012-10-04 | David Mark Clements | BSD License */
+window.matchMedia=window.matchMedia||function(e,t){var n=e.documentElement,r=n.firstElementChild||n.firstChild,i=e.createElement("body"),s=e.createElement("div");s.id="mq-test-1",s.style.cssText="position:absolute;top:-100em",i.style.background="none",i.appendChild(s);var o=function(e){return s.innerHTML='&shy;<style media="'+e+'"> #mq-test-1 { width: 42px; }</style>',n.insertBefore(i,r),bool=s.offsetWidth===42,n.removeChild(i),{matches:bool,media:e}},u=function(){var t,r=n.body,i=!1;return s.style.cssText="position:absolute;font-size:1em;width:1em",r||(r=i=e.createElement("body"),r.style.background="none"),r.appendChild(s),n.insertBefore(r,n.firstChild),i?n.removeChild(r):r.removeChild(s),t=a=parseFloat(s.offsetWidth),t},a,f=o("(min-width: 0px)").matches;return function(t){if(f)return o(t);var n=t.match(/\(min\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/)&&parseFloat(RegExp.$1)+(RegExp.$2||""),r=t.match(/\(max\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/)&&parseFloat(RegExp.$1)+(RegExp.$2||""),i=n===null,s=r===null,l=e.body.offsetWidth,c="em";return!n||(n=parseFloat(n)*(n.indexOf(c)>-1?a||u():1)),!r||(r=parseFloat(r)*(r.indexOf(c)>-1?a||u():1)),bool=(!i||!s)&&(i||l>=n)&&(s||l<=r),{matches:bool,media:t}}}(document),function(e){function t(){var e,t=new ActiveXObject("htmlfile");return t.appendChild(t.createElement("html")),e=t.getElementsByTagName("html")[0],e.appendChild(t.createElement("head")),e.appendChild(t.createElement("body")),t.open(),t.close(),t}function r(){if(f)return;f=!0,document.addEventListener&&!a.opera&&document.addEventListener("DOMContentLoaded",s,!1),a.msie&&e==top&&function(){if(l)return;try{document.documentElement.doScroll("left")}catch(e){setTimeout(arguments.callee,0);return}s()}(),a.opera&&document.addEventListener("DOMContentLoaded",function(){if(l)return;for(var e=0;e<document.styleSheets.length;e++)if(document.styleSheets[e].disabled){setTimeout(arguments.callee,0);return}s()},!1);if(a.safari){var t;(function(){if(l)return;if(document.readyState!="loaded"&&document.readyState!="complete"){setTimeout(arguments.callee,0);return}if(t===undefined){var e=document.getElementsByTagName("link");for(var n=0;n<e.length;n++)e[n].getAttribute("rel")=="stylesheet"&&t++;var r=document.getElementsByTagName("style");t+=r.length}if(document.styleSheets.length!=t){setTimeout(arguments.callee,0);return}s()})()}i(s)}function i(t){var n=e.onload;typeof e.onload!="function"?e.onload=t:e.onload=function(){n&&n(),t()}}function s(){if(!l){l=!0;if(c){for(var t=0;t<c.length;t++)c[t].call(e,[]);c=[]}}}function m(e,t,n){var r,i;for(i=0;i<e.length;i++)r=e[i],t(r);n&&n()}function g(e,t){function n(){t(e.shift(),function(){g(e,t)})}if(!e)return;if(!(e instanceof Array)){var r=[];try{e=r.slice.call(e),n()}catch(i){m(e,function(e){r.push(e)},function(){e=r,n()})}return}n()}var n,o={},u=navigator.userAgent.toLowerCase(),a={version:(u.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)||[])[1],safari:/webkit/.test(u),opera:/opera/.test(u),msie:/msie/.test(u)&&!/opera/.test(u),mozilla:/mozilla/.test(u)&&!/(compatible|webkit)/.test(u)},f=!1,l=!1,c=[];n=function(t,n){r(),l?t.call(e,[]):c.push(function(){return t.call(e,[])})},r(),Array.prototype.reduce||(Array.prototype.reduce=function(e){if(this===null||this===undefined)throw new TypeError("Object is null or undefined");var t=0,n=this.length>>0,r;if(typeof e!="function")throw new TypeError("First argument is not callable");if(arguments.length<2){if(n===0)throw new TypeError("Array length is 0 and no second argument");r=this[0],t=1}else r=arguments[1];while(t<n)t in this&&(r=e.call(undefined,r,this[t],t,this)),++t;return r}),String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")});var h=document.implementation.createHTMLDocument?document.implementation.createHTMLDocument(""):t(),p=e.document,d;e.Respondu=function(t,r,i){if(!(this instanceof e.Respondu))return new e.Respondu(t,r,i);alert(Respondu.ltIE9);var s=this,o={escaper:"<!--",hires:!0,hiresSuffix:"@2x",className:"responsive",typical:"small",breakpoints:{small:320,medium:640,large:1280,xlarge:Infinity}},u=p.getElementsByTagName("*");this.context=u[u.length-1],typeof t!="string"&&(r=t,t=null),typeof r=="function"&&(i=r,r=null),this.cb=i,r=r||o,r.breakpoints.typical=r.breakpoints[r.typical],delete r.breakpoints[r.typical],this.opts=r,this.opts.breakpoints=r.breakpoints||o.breakpoints,d="css",n(function(){function r(e){var t;t=e.replace(/<\/?noscript(.+)?>?/g,"").replace(/(<picture(.+)?>)/g,"$1<video>"),Respondu.ltIE9&&(t=t.replace(/<picture/g,'<div data-element="picture"').replace(/<source/g,'<p data-element="source"')),document.documentMode&&document.documentMode<9&&(t=t.replace(/<picture/g,'<div data-element="picture"').replace(/<source/g,'<p data-element="source"')),h.body.innerHTML=t}function i(){if(t){if(!s.plugins[t]){console.error("Respondu: "+t+" implementation not found"),s.implement(h,!0);return}s.plugins[t](h,function(e){s.opts.breakpoints=e||s.opts.breakpoints,s.implement(h,!!e)},v);return}s.implement(h,!0)}var e=p.getElementsByTagName("body")[0],n;n=s.context.nextSibling;while(n.nodeType!==8)n=n.nextSibling;r(n.nodeValue),i()}),document.write(r.escaper)};var v=Respondu.prototype.utils={each:m,next:g};Respondu.prototype.plugins={},Respondu.plugin=function(e,t){Respondu.prototype.plugins[e]=t},Respondu.module=function(e,t){Respondu.prototype[e]=t},Respondu.prototype.implement=function(t,n){function o(e){var n="",r,s;if(e<=i.breakpoints.typical)return t;delete i.breakpoints.typical;for(r in i.breakpoints)if(i.breakpoints.hasOwnProperty(r)&&e<=i.breakpoints[r]){n=r+".";break}return m(t.images,function(e){if(i.className&&(!e.className||!e.className.match(i.className)))return;e.setAttribute("src",e.getAttribute("src").replace(/(.+)\.(.+)$/,"$1."+n+"$2"))}),t}var r=this.cb,i=this.opts,s=p.getElementsByTagName("body")[0];r?r(n?o(e.screen.width):t,function(){this.context.outerHTML=t.body.innerHTML}):this.context.outerHTML=n?o(e.screen.width).body.innerHTML:t.body.innerHTML}}(window),Respondu.plugin("picture",function(e,t){if("srcset"in document.createElement("picture")){t&&t();return}var n=e.getElementsByTagName("picture"),r,i,s,o,u,a,f,l,c,h=window.screen.width,p,d=window.devicePixelRatio||1;if(!n.length){n=e.getElementsByTagName("div"),a=n.length;while(a--)n[a].attributes["data-element"].nodeValue!=="picture"&&delete n[a]}for(u=0;u<n.length;u++){r=n[u],i=r.attributes,s=r.getElementsByTagName("source");if(!s.length){s=e.getElementsByTagName("p"),a=s.length;while(a--)s[a].attributes["data-element"].nodeValue!=="source"&&delete s[a]}sourceCandidates=[];for(a=0;a<s.length;a++)srcel=s[a],f=srcel.getAttribute("media"),(!f||matchMedia(f).matches)&&sourceCandidates.push(srcel);var v=sourceCandidates.length,m=0;if(v>1)while(v--){f=sourceCandidates[v].getAttribute("media");if(!f&&sourceCandidates.length>1){delete sourceCandidates[v];break}f=f.match(/\((min|max)-width:([0-9]+)(px|em)\)/),Math.abs(f[2]-h)<=Math.abs(m-h)&&(m=f[2],l=sourceCandidates[v])}else l=sourceCandidates[0];var g,y,b,w;if(l){g=l.getAttribute("srcset").split(","),y=g.length;while(y--)b=g[y].replace(/^\s\s*/,"").replace(/\s\s*$/,"").split(" "),b[1]===d+"x"&&(w=b[0]),b[1]||(w=b[0]);c=e.createElement("img"),c.setAttribute("src",w);for(a=0;a<i.length;a++)c.setAttribute(i[a].nodeName,i[a].nodeValue);r.parentNode.replaceChild(c,r)}}t()}),Respondu.plugin("srcset",function(e,t,n){function a(e,t){var n=e!=="x"?"[0-9]+"+e:"[1-9]+(.[1-9]+)?"+e,r=e!=="x"?0:1,i=t.match(new RegExp(n));return+(i?i[0].replace(e,""):"")||r}function f(e){var t=/(\S+)/,n=[];return function o(){var u=e.match(t)[0],f,l,c={};e=e.replace(t,""),l=e.indexOf(","),l>-1?(f=e.substr(0,l).trim(),e=e.substr(l+1,e.length-1).trim()):(f=e.substr(0,e.length).trim(),e=""),i(s,function(e){c[e]=f&&a(e,f)}),(c.w&&c.w<=r.width||c.h&&c.h<=r.height)&&c.x===r.ratio&&(c.url=u,n.push(c)),e&&o()}([]),n}if("srcset"in document.createElement("img")){t&&t();return}var r={width:document.documentElement.clientWidth,height:document.documentElement.clientHeight,ratio:window.devicePixelRatio||1},i=n.each,s=["w","h","x"],o=[],u;i(e.images,function(e){var t=e.getAttribute("srcset"),n=t&&f(t);if(!n||!n.length)return;u=n.reduce(function(e,t){return e.w&&t.w?e.w>t.w?e:t:e.h&&t.h?e.h>t.h?e:t:t.w?t:e.w?e:t.h?t:e.h?e:t}),e.src=u.url}),t()});Respondu.ltIE9 = /*@cc_on @if (@_jscript_version < 9) ! @end @*/false;
Oops, something went wrong. Retry.

0 comments on commit bd3d280

Please sign in to comment.