1window.CopyTheCodeToClipboard = (function (window, document, navigator) { 2 var textArea, 3 copy; 4 5 function isOS() { 6 return navigator.userAgent.match(/ipad|iphone/i); 7 } 8 9 function createTextArea(text) { 10 textArea = document.createElement('textArea'); 11 textArea.value = text; 12 document.body.appendChild(textArea); 13 } 14 15 function selectText() { 16 var range, 17 selection; 18 19 if (isOS()) { 20 range = document.createRange(); 21 range.selectNodeContents(textArea); 22 selection = window.getSelection(); 23 selection.removeAllRanges(); 24 selection.addRange(range); 25 textArea.setSelectionRange(0, 999999); 26 } else { 27 textArea.select(); 28 } 29 } 30 31 function copyToClipboard() { 32 document.execCommand('copy'); 33 document.body.removeChild(textArea); 34 } 35 36 copy = function (text) { 37 createTextArea(text); 38 selectText(); 39 copyToClipboard(); 40 }; 41 42 return { 43 copy: copy 44 }; 45})(window, document, navigator); 46 47(function ($) { 48 49 CopyTheCode = { 50 51 selector: copyTheCode.settings.selector || copyTheCode.selector || 'pre', 52 button_position: copyTheCode.settings['button-position'] || 'inside', 53 54 /** 55 * Init 56 */ 57 init: function () { 58 this._bind(); 59 this._initialize(); 60 }, 61 62 /** 63 * Binds events 64 */ 65 _bind: function () { 66 $(document).on('click', '.copy-the-code-button', CopyTheCode.copyCode); 67 $(document).on('click', '.copy-the-code-shortcode', CopyTheCode.copyShortcode); 68 }, 69 70 /** 71 * Initialize the Button 72 */ 73 _initialize: function () { 74 if (!$(copyTheCode.selectors).length) { 75 return; 76 } 77 78 $(copyTheCode.selectors).each(function (index, el) { 79 var button_copy_text = el['button_copy_text'] || ''; 80 var button_position = el['button_position'] || ''; 81 var button_text = el['button_text'] || ''; 82 var button_title = el['button_title'] || ''; 83 var selector = el['selector'] || ''; 84 var style = el['style'] || ''; 85 86 $(selector).each(function (index, single_selector) { 87 88 var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style); 89 90 $(single_selector).addClass('copy-the-code-target'); 91 92 if ('cover' !== style && 'outside' === button_position) { 93 $(single_selector).wrap('<span data-button-text="' + button_text + '" data-button-position="' + button_position + '" data-button-copy-text="' + button_copy_text + '" data-style="' + style + '" data-button-title="' + button_title + '" data-selector="' + selector + '" class="copy-the-code-wrap copy-the-code-style-' + style + ' copy-the-code-outside-wrap"></span>'); 94 $(single_selector).parent().prepend('<div class="copy-the-code-outside">' + buttonMarkup + '</div>'); 95 } else { 96 $(single_selector).wrap('<span data-button-text="' + button_text + '" data-button-position="' + button_position + '" data-button-copy-text="' + button_copy_text + '" data-style="' + style + '" data-button-title="' + button_title + '" data-selector="' + selector + '" class="copy-the-code-wrap copy-the-code-style-' + style + ' copy-the-code-inside-wrap"></span>'); 97 $(single_selector).append(buttonMarkup); 98 } 99 100 switch (style) { 101 case 'svg-icon': 102 $(single_selector).find('.copy-the-code-button').html(copyTheCode.buttonSvg); 103 break; 104 case 'cover': 105 case 'button': 106 default: 107 $(single_selector).find('.copy-the-code-button').html(button_text); 108 break; 109 } 110 111 112 }); 113 }); 114 115 }, 116 117 /** 118 * Get Copy Button Markup 119 */ 120 _getButtonMarkup: function (button_title, button_text, style) { 121 if ('svg-icon' === style) { 122 button_text = copyTheCode.buttonSvg; 123 } 124 125 return '<button class="copy-the-code-button" data-style="' + style + '" title="' + button_title + '">' + button_text + '</button>'; 126 }, 127 128 format: function (html) { 129 var tab = '\t'; 130 var result = ''; 131 var indent = ''; 132 133 html.split(/>
133\s*</).forEach(function (element) { 134 if (element.match(/^\/\w/)) { 135 indent = indent.substring(tab.length); 136 } 137 138 result += indent + '<' + element + '>\r\n'; 139 140 if (element.match(/^<?\w[^>]*[^\/]$/) && !element.startsWith("input")) { 141 indent += tab; 142 } 143 }); 144 145 return result.substring(1, result.length - 3); 146 }, 147 148 /** 149 * Copy to Clipboard 150 */ 151 copyShortcode: function (event) { 152 event.preventDefault(); 153 154 var btn = $(this), 155 oldText = btn.text(), 156 target = btn.attr('data-target') || '', 157 copy_content_as = btn.attr('data-copy-as') || copyTheCode.copy_content_as, 158 button_copy_text = btn.attr('data-button-copy-text') || '', 159 content = btn.attr('data-content') || ''; 160 161 // Copy the secrate content. 162 if (content) { 163 CopyTheCodeToClipboard.copy(content); 164 // Copied! 165 btn.text(button_copy_text); 166 setTimeout(function () { 167 btn.text(oldText); 168 }, 1000); 169 return; 170 } 171 172 var source = $(target); 173 174 if (!source.length) { 175 btn.text('Not found!'); 176 setTimeout(function () { 177 btn.text(oldText); 178 }, 1000); 179 return; 180 } 181 182 var html = source.html(); 183 184 html = CopyTheCode.format(html); 185 186 if ('html' !== copy_content_as) { 187 188 // Convert the <br/> tags into new line. 189 var brRegex = /<br\s*[\/]?>/gi; 190 html = html.replace(brRegex, "\n"); 191 192 // Convert the <div> tags into new line. 193 var divRegex = /<div\s*[\/]?>/gi; 194 html = html.replace(divRegex, "\n"); 195 196 // Convert the <p> tags into new line. 197 var pRegex = /<p\s*[\/]?>/gi; 198 html = html.replace(pRegex, "\n"); 199 200 // Convert the <li> tags into new line. 201 var pRegex = /<li\s*[\/]?>/gi; 202 html = html.replace(pRegex, "\n"); 203 204 // Remove all tags. 205 html = html.replace(/(<([^>]+)>)/ig, ''); 206 } 207 208 if ('html' !== copy_content_as) { 209 html = html.replace(/[\t\n]+/gm, ' ').trim(); 210 } else { 211 var reWhiteSpace = new RegExp("/^\s+$/"); 212 html = html.replace(reWhiteSpace, ""); 213 } 214 215 var tempElement = $("<div id='temp-element'></div>"); 216 $("body").append(tempElement); 217 html = $.trim(html); 218 $('#temp-element').html(html); 219 var html = $('#temp-element').html(); 220 $('#temp-element').remove(); 221 222 var tempHTML = html; 223 224 // Copy the Code. 225 var tempPre = $("<textarea id='temp-pre'>"), 226 temp = $("<textarea>"); 227 228 // Append temporary elements to DOM. 229 $("body").append(temp); 230 $("body").append(tempPre); 231 232 // Set temporary HTML markup. 233 tempPre.html(tempHTML); 234 235 var content = tempPre.text(); 236 237 content = $.trim(content); 238 239 // Format the HTML markup. 240 temp.val(content).select(); 241 242 // Support for IOS devices too. 243 CopyTheCodeToClipboard.copy(content); 244 245 // Remove temporary elements. 246 temp.remove(); 247 tempPre.remove(); 248 249 // Copied! 250 btn.text(button_copy_text); 251 setTimeout(function () { 252 btn.text(oldText); 253 }, 1000); 254 }, 255 256 /** 257 * Copy to Clipboard 258 */ 259 copyCode: function (event) { 260 event.preventDefault(); 261 262 var btn = $(this), 263 oldText = btn.text(), 264 parent = btn.parents('.copy-the-code-wrap'), 265 selector = parent.attr('data-selector') || '', 266 button_text = parent.attr('data-button-text') || '', 267 button_position = parent.attr('data-button-position') || '', 268 button_copy_text = parent.attr('data-button-copy-text') || '', 269 button_title = parent.attr('data-button-title') || '', 270 style = parent.attr('data-style') || ''; 271 272 // Fix: nested selectors e.g. `.entry-content pre` 273 if (selector.indexOf(' ') >= 0) { 274 var source = btn.parents('.copy-the-code-wrap'); 275 } else { 276 var source = btn.parents('.copy-the-code-wrap').find(selector); 277 } 278
279 var html = source.html(); 280 281 if ('html' !== copyTheCode.copy_content_as) { 282 // Convert the <br/> tags into new line. 283 var brRegex = /<br\s*[\/]?>/gi; 284 html = html.replace(brRegex, "\n"); 285 286 // Convert the <div> tags into new line. 287 var divRegex = /<div\s*[\/]?>/gi; 288 html = html.replace(divRegex, "\n"); 289 290 // Convert the <p> tags into new line. 291 var pRegex = /<p\s*[\/]?>/gi; 292 html = html.replace(pRegex, "\n"); 293 294 // Convert the <li> tags into new line. 295 var pRegex = /<li\s*[\/]?>/gi; 296 html = html.replace(pRegex, "\n"); 297 298 // Remove all tags. 299 html = html.replace(/(<([^>]+)>)/ig, ''); 300 } 301 302 // Remove white spaces. 303 var reWhiteSpace = new RegExp("/^\s+$/"); 304 html = html.replace(reWhiteSpace, ""); 305 306 var tempElement = $("<div id='temp-element'></div>"); 307 $("body").append(tempElement); 308 html = $.trim(html); 309 $('#temp-element').html(html); 310 var html = $('#temp-element').html(); 311 $('#temp-element').remove(); 312 313 var tempHTML = html; 314 console.log(tempHTML); 315 316 var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style); 317 console.log(buttonMarkup); 318 319 // Remove the <copy> button. 320 tempHTML = tempHTML.replace(buttonMarkup, ''); 321 // console.log( buttonMarkup ); 322 // console.log( tempHTML ); 323 324 // Remove button text. 325 tempHTML = tempHTML.replace(button_text, ''); 326 327 // Copy the Code. 328 var tempPre = $("<textarea id='temp-pre'>"), 329 temp = $("<textarea>"); 330 331 // Append temporary elements to DOM. 332 $("body").append(temp); 333 $("body").append(tempPre); 334 335 // Set temporary HTML markup. 336 tempPre.html(tempHTML); 337 338 var content = tempPre.text(); 339 340 content = $.trim(content); 341 342 // Format the HTML markup. 343 temp.val(content).select(); 344 345 // Support for IOS devices too. 346 CopyTheCodeToClipboard.copy(content); 347 348 // Remove temporary elements. 349 temp.remove(); 350 tempPre.remove(); 351 352 // Copied! 353 btn.text(button_copy_text); 354 setTimeout(function () { 355 if ('svg-icon' === style) { 356 btn.html(copyTheCode.buttonSvg); 357 } else { 358 btn.text(oldText); 359 } 360 }, 1000); 361 } 362 }; 363 364 /** 365 * Initialization 366 */ 367 $(function () { 368 CopyTheCode.init(); 369 }); 370 371})(jQuery);
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.