/** * @file * Colorbox module load js. */ (function ($) { Drupal.behaviors.initColorboxLoad = { attach: function (context, settings) { if (!$.isFunction($.colorbox) || typeof settings.colorbox === 'undefined') { return; } if (settings.colorbox.mobiledetect && window.matchMedia) { // Disable Colorbox for small screens. var mq = window.matchMedia("(max-device-width: " + settings.colorbox.mobiledevicewidth + ")"); if (mq.matches) { return; } } $.urlParams = function (url) { var p = {}, e, a = /\+/g, // Regex for replacing addition symbol with a space. r = /([^&=]+)=?([^&]*)/g, d = function (s) { return decodeURIComponent(s.replace(a, ' ')); }, q = url.split('?'); while (e = r.exec(q[1])) { e[1] = d(e[1]); e[2] = d(e[2]); switch (e[2].toLowerCase()) { case 'true': case 'yes': e[2] = true; break; case 'false': case 'no': e[2] = false; break; } if (e[1] == 'width') { e[1] = 'innerWidth'; } if (e[1] == 'height') { e[1] = 'innerHeight'; } if (e[2]) { e[2] = Drupal.checkPlain(e[2]); } p[e[1]] = e[2]; } return p; }; $('.colorbox-load', context) .once('init-colorbox-load', function () { var href = $(this).attr('href'); if (!hrefIsSafe(href)) { return; } var params = $.urlParams(href); if (!params.hasOwnProperty('title')) { // If a title attribute is supplied, sanitize it. var title = $(this).attr('title'); if (title) { params.title = Drupal.checkPlain(title); } } $(this).colorbox($.extend({}, settings.colorbox, params)); }); } }; /** * Returns true if the passed-in href string is safe for colorbox_load. * * @param href * The href string to be tested. * * @return * Boolean true if the href is safe. */ function hrefIsSafe(href) { var normalizedUrl = Drupal.absoluteUrl(href); // Only local, non-file-system URLs are allowed. if (!Drupal.urlIsLocal(normalizedUrl)) { return false; } // Reject uploaded files from the public or private file system. if (normalizedUrl.indexOf(Drupal.settings.colorbox.file_public_path) !== -1 || normalizedUrl.match(/\/system\/files\//) || normalizedUrl.match(/[?|&]q=system\/files\//)) { return false; } // All checks passed. return true; } })(jQuery);