1/* 2 3Holder - 1.9 - client side image placeholders 4(c) 2012-2013 Ivan Malopinsky / http://imsky.co 5 6Provided under the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0 7Commercial use requires attribution. 8 9*/ 10 11var Holder = Holder || {}; 12(function (app, win) { 13 14var preempted = false, 15fallback = false, 16canvas = document.createElement('canvas'); 17 18//getElementsByClassName polyfill 19document.getElementsByClassName||(document.getElementsByClassName=function(e){var t=document,n,r,i,s=[];if(t.querySelectorAll)return t.querySelectorAll("."+e);if(t.evaluate){r=".//*[contains(concat(' ', @class, ' '), ' "+e+" ')]",n=t.evaluate(r,t,null,0,null);while(i=n.iterateNext())s.push(i)}else{n=t.getElementsByTagName("*"),r=new RegExp("(^|\\s)"+e+"(\\s|$)");for(i=0;i<n.length;i++)r.test(n[i].className)&&s.push(n[i])}return s}) 20 21//getComputedStyle polyfill 22window.getComputedStyle||(window.getComputedStyle=function(e,t){return this.el=e,this.getPropertyValue=function(t){var n=/(\-([a-z]){1})/g;return t=="float"&&(t="styleFloat"),n.test(t)&&(t=t.replace(n,function(){return arguments[2].toUpperCase()})),e.currentStyle[t]?e.currentStyle[t]:null},this}) 23 24//http://javascript.nwbox.com/ContentLoaded by Diego Perini with modifications 25function contentLoaded(n,t){var l="complete",s="readystatechange",u=!1,h=u,c=!0,i=n.document,a=i.documentElement,e=i.addEventListener?"addEventListener":"attachEvent",v=i.addEventListener?"removeEventListener":"detachEvent",f=i.addEventListener?"":"on",r=function(e){(e.type!=s||i.readyState==l)&&((e.type=="load"?n:i)[v](f+e.type,r,u),!h&&(h=!0)&&t.call(n,null))},o=function(){try{a.doScroll("left")}catch(n){setTimeout(o,50);return}r("poll")};if(i.readyState==l)t.call(n,"lazy");else{if(i.createEventObject&&a.doScroll){try{c=!n.frameElement}catch(y){}c&&o()}i[e](f+"DOMContentLoaded",r,u),i[e](f+s,r,u),n[e](f+"load",r,u)}}; 26 27//https://gist.github.com/991057 by Jed Schmidt with modifications 28function selector(a){ 29 a=a.match(/^(\W)?(.*)/);var b=document["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2]); 30 var ret=[]; b!=null&&(b.length?ret=b:b.length==0?ret=b:ret=[b]); return ret; 31} 32 33//shallow object property extend 34function extend(a,b){var c={};for(var d in a)c[d]=a[d];for(var e in b)c[e]=b[e];return c} 35 36//hasOwnProperty polyfill 37if (!Object.prototype.hasOwnProperty) 38 Object.prototype.hasOwnProperty = function(prop) { 39 var proto = this.__proto__ || this.constructor.prototype; 40 return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]); 41 } 42 43function text_size(width, height, template) { 44 var dimension_arr = [height, width].sort(); 45 var maxFactor = Math.round(dimension_arr[1] / 16), 46 minFactor = Math.round(dimension_arr[0] / 16); 47 var text_height = Math.max(template.size, maxFactor); 48 return { 49 height: text_height 50 } 51} 52 53function draw(ctx, dimensions, template, ratio) { 54 var ts = text_size(dimensions.width, dimensions.height, template); 55 var text_height = ts.height; 56 var width = dimensions.width * ratio, 57 height = dimensions.height * ratio; 58 var font = template.font ? template.font : "sans-serif"; 59 canvas.width = width; 60 canvas.height = height; 61 ctx.textAlign = "center"; 62 ctx.textBaseline = "middle"; 63 ctx.fillStyle = template.background; 64 ctx.fillRect(0, 0, width, height); 65 ctx.fillStyle = template.foreground; 66 ctx.font = "bold " + text_height + "px " + font; 67 var text = template.text ? template.text : (dimensions.width + "x" + dimensions.height); 68 if (ctx.measureText(text).width / width > 1) { 69 text_height = template.size / (ctx.measureText(text).width / width); 70 } 71 //Resetting font size if necessary 72 ctx.font = "bold " + (text_height * ratio) + "px " + font; 73 ctx.fillText(text, (width / 2), (height / 2), width); 74 return canvas.toDataURL("image/png"); 75} 76 77function render(mode, el, holder, src) { 78 var dimensions = holder.dimensions, 79 theme = holder.theme, 80 text = holder.text ? decodeURIComponent(holder.text) : holder.text; 81 var dimensions_caption = dimensions.width + "x" + dimensions.height; 82 83 theme = (text ? extend(theme, { 84 text: text 85 }) : theme); 86 theme = (holder.font ? extend(theme, { 87 font: holder.font 88 }) : theme); 89 90 if (mode == "image") { 91 el.setAttribute("data-src", src); 92 el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption); 93 94 if (fallback || !holder.auto) { 95 el.style.width = dimensions.width + "px"; 96 el.style.height = dimensions.height + "px"; 97 } 98 99 if (fallback) { 100 el.style.backgroundColor = theme.background; 101 102 } else { 103 el.setAttribute("src", draw(ctx, dimensions, theme, ratio)); 104 } 105 } else { 106 if (!fallback) { 107 el.style.backgroundImage = "url(" + draw(ctx, dimensions, theme, ratio) + ")"; 108 el.style.backgroundSize = dimensions.width + "px " + dimensions.height + "px"; 109 } 110 } 111}; 112 113function fluid(el, holder, src) { 114 var dimensions = holder.dimensions, 115 theme = holder.theme, 116 text = holder.text; 117 var dimensions_caption = dimensions.width + "x" + dimensions.height; 118 theme = (text ? extend(theme, { 119 text: text 120 }) : theme); 121 122 var fluid = document.createElement("div"); 123 124 if (el.fluidRef) { 125 fluid = el.fluidRef; 126 } 127 128 fluid.style.backgroundColor = theme.background; 129 fluid.style.color = theme.foreground; 130 fluid.className = el.className + " holderjs-fluid"; 131 fluid.style.width = holder.dimensions.width + (holder.dimensions.width.indexOf("%") > 0 ? "" : "px"); 132 fluid.style.height = holder.dimensions.height + (holder.dimensions.height.indexOf("%") > 0 ? "" : "px"); 133 fluid.id = el.id; 134 135 el.style.width = 0; 136 el.style.height = 0; 137 138 if (!el.fluidRef) { 139 140 if (theme.text) { 141 fluid.appendChild(document.createTextNode(theme.text)) 142 } else { 143 fluid.appendChild(document.createTextNode(dimensions_caption)) 144 fluid_images.push(fluid); 145 setTimeout(fluid_update, 0); 146 } 147 148 } 149 150 el.fluidRef = fluid; 151 el.parentNode.insertBefore(fluid, el.nextSibling) 152
153 if (window.jQuery) { 154 jQuery(function ($) { 155 $(el).on("load", function () { 156 el.style.width = fluid.style.width; 157 el.style.height = fluid.style.height; 158 $(el).show(); 159 $(fluid).remove(); 160 }); 161 }) 162 } 163} 164 165function fluid_update() { 166 for (i in fluid_images) { 167 if (!fluid_images.hasOwnProperty(i)) continue; 168 var el = fluid_images[i], 169 label = el.firstChild; 170 171 el.style.lineHeight = el.offsetHeight + "px"; 172 label.data = el.offsetWidth + "x" + el.offsetHeight; 173 } 174} 175 176function parse_flags(flags, options) { 177 178 var ret = { 179 theme: settings.themes.gray 180 }, render = false; 181 182 for (sl = flags.length, j = 0; j < sl; j++) { 183 var flag = flags[j]; 184 if (app.flags.dimensions.match(flag)) { 185 render = true; 186 ret.dimensions = app.flags.dimensions.output(flag); 187 } else if (app.flags.fluid.match(flag)) { 188 render = true; 189 ret.dimensions = app.flags.fluid.output(flag); 190 ret.fluid = true; 191 } else if (app.flags.colors.match(flag)) { 192 ret.theme = app.flags.colors.output(flag); 193 } else if (options.themes[flag]) { 194 //If a theme is specified, it will override custom colors 195 ret.theme = options.themes[flag]; 196 } else if (app.flags.text.match(flag)) { 197 ret.text = app.flags.text.output(flag); 198 } else if (app.flags.font.match(flag)) { 199 ret.font = app.flags.font.output(flag); 200 } else if (app.flags.auto.match(flag)) { 201 ret.auto = true; 202 } 203 } 204 205 return render ? ret : false;
vendor: 5,557 bytes, lines 206-444
206 207}; 208 209 210 211if (!canvas.getContext) { 212 fallback = true; 213} else { 214 if (canvas.toDataURL("image/png") 215 .indexOf("data:image/png") < 0) { 216 //Android doesn't support data URI 217 fallback = true; 218 } else { 219 var ctx = canvas.getContext("2d"); 220 } 221} 222 223var dpr = 1, bsr = 1; 224 225if(!fallback){ 226 dpr = window.devicePixelRatio || 1, 227 bsr = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1; 228} 229 230var ratio = dpr / bsr; 231 232var fluid_images = []; 233 234var settings = { 235 domain: "holder.js", 236 images: "img", 237 bgnodes: ".holderjs", 238 themes: { 239 "gray": { 240 background: "#eee", 241 foreground: "#aaa", 242 size: 12 243 }, 244 "social": { 245 background: "#3a5a97", 246 foreground: "#fff", 247 size: 12 248 }, 249 "industrial": { 250 background: "#434A52", 251 foreground: "#C2F200", 252 size: 12 253 } 254 }, 255 stylesheet: ".holderjs-fluid {font-size:16px;font-weight:bold;text-align:center;font-family:sans-serif;margin:0}" 256}; 257 258 259app.flags = { 260 dimensions: { 261 regex: /^(\d+)x(\d+)$/, 262 output: function (val) { 263 var exec = this.regex.exec(val); 264 return { 265 width: +exec[1], 266 height: +exec[2] 267 } 268 } 269 }, 270 fluid: { 271 regex: /^([0-9%]+)x([0-9%]+)$/, 272 output: function (val) { 273 var exec = this.regex.exec(val); 274 return { 275 width: exec[1], 276 height: exec[2] 277 } 278 } 279 }, 280 colors: { 281 regex: /#([0-9a-f]{3,})\:#([0-9a-f]{3,})/i, 282 output: function (val) { 283 var exec = this.regex.exec(val); 284 return { 285 size: settings.themes.gray.size, 286 foreground: "#" + exec[2], 287 background: "#" + exec[1] 288 } 289 } 290 }, 291 text: { 292 regex: /text\:(.*)\__.*/, 293 output: function (val) { 294 return this.regex.exec(val)[1]; 295 } 296 }, 297 font: { 298 regex: /font\:(.*)/, 299 output: function (val) { 300 return this.regex.exec(val)[1]; 301 } 302 }, 303 auto: { 304 regex: /^auto$/ 305 } 306} 307 308for (var flag in app.flags) { 309 if (!app.flags.hasOwnProperty(flag)) continue; 310 app.flags[flag].match = function (val) { 311 return val.match(this.regex) 312 } 313} 314 315app.add_theme = function (name, theme) { 316 name != null && theme != null && (settings.themes[name] = theme); 317 return app; 318}; 319 320app.add_image = function (src, el) { 321 var node = selector(el); 322 if (node.length) { 323 for (var i = 0, l = node.length; i < l; i++) { 324 var img = document.createElement("img") 325 img.setAttribute("data-src", src); 326 node[i].appendChild(img); 327 } 328 } 329 return app; 330}; 331 332app.run = function (o) { 333 var options = extend(settings, o), 334 images = [], imageNodes = [], bgnodes = []; 335 336 if(typeof(options.images) == "string"){ 337 imageNodes = selector(options.images); 338 } 339 else if (window.NodeList && options.images instanceof window.NodeList) { 340 imageNodes = options.images; 341 } else if (window.Node && options.images instanceof window.Node) { 342 imageNodes = [options.images]; 343 } 344 345 if(typeof(options.bgnodes) == "string"){ 346 bgnodes = selector(options.bgnodes); 347 } else if (window.NodeList && options.elements instanceof window.NodeList) { 348 bgnodes = options.bgnodes; 349 } else if (window.Node && options.bgnodes instanceof window.Node) { 350 bgnodes = [options.bgnodes]; 351 } 352 353 preempted = true; 354 355 for (i = 0, l = imageNodes.length; i < l; i++) images.push(imageNodes[i]); 356 357 var holdercss = document.getElementById("holderjs-style"); 358 if (!holdercss) { 359 holdercss = document.createElement("style"); 360 holdercss.setAttribute("id", "holderjs-style"); 361 holdercss.type = "text/css"; 362 document.getElementsByTagName("head")[0].appendChild(holdercss); 363 } 364 365 if (!options.nocss) { 366 if (holdercss.styleSheet) { 367 holdercss.styleSheet.cssText += options.stylesheet; 368 } else { 369 holdercss.appendChild(document.createTextNode(options.stylesheet)); 370 } 371 } 372 373 374 375 var cssregex = new RegExp(options.domain + "\/(.*?)\"?\\)"); 376 377 for (var l = bgnodes.length, i = 0; i < l; i++) { 378 var src = window.getComputedStyle(bgnodes[i], null) 379 .getPropertyValue("background-image"); 380 var flags = src.match(cssregex); 381 var bgsrc = bgnodes[i].getAttribute("data-background-src"); 382 383 if (flags) { 384 var holder = parse_flags(flags[1].split("/"), options); 385 if (holder) { 386 render("background", bgnodes[i], holder, src); 387 } 388 } 389 else if(bgsrc != null){ 390 var holder = parse_flags(bgsrc.substr(bgsrc.lastIndexOf(options.domain) + options.domain.length + 1) 391 .split("/"), options); 392 if(holder){ 393 render("background", bgnodes[i], holder, src); 394 } 395 } 396 } 397 398 for (l = images.length, i = 0; i < l; i++) { 399 400 var attr_src = attr_data_src = src = null; 401 402 try{ 403 attr_src = images[i].getAttribute("src"); 404 attr_datasrc = images[i].getAttribute("data-src"); 405 }catch(e){} 406 407 if (attr_datasrc == null && !! attr_src && attr_src.indexOf(options.domain) >= 0) { 408 src = attr_src; 409 } else if ( !! attr_datasrc && attr_datasrc.indexOf(options.domain) >= 0) { 410 src = attr_datasrc; 411 } 412 413 if (src) { 414 var holder = parse_flags(src.substr(src.lastIndexOf(options.domain) + options.domain.length + 1) 415 .split("/"), options); 416 if (holder) { 417 if (holder.fluid) { 418 fluid(images[i], holder, src); 419 } else { 420 render("image", images[i], holder, src); 421 } 422 } 423 } 424 } 425 return app; 426}; 427 428contentLoaded(win, function () { 429 if (window.addEventListener) { 430 window.addEventListener("resize", fluid_update, false); 431 window.addEventListener("orientationchange", fluid_update, false); 432 } else { 433 window.attachEvent("onresize", fluid_update) 434 } 435 preempted || app.run(); 436}); 437 438if (typeof define === "function" && define.amd) { 439 define("Holder", [], function () { 440 return app; 441 }); 442} 443 444})(Holder, window);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.