| @@ -1,451 +1,525 @@ | ||
| 1 | 1 | window.CopyTheCodeToClipboard = (function (window, document, navigator) { |
| 2 | - var textArea, | |
| 3 | - copy; | |
| 2 | + var textArea, | |
| 3 | + copy; | |
| 4 | 4 | |
| 5 | - function isOS() { | |
| 6 | - return navigator.userAgent.match(/ipad|iphone/i); | |
| 7 | - } | |
| 5 | + function isOS() { | |
| 6 | + return navigator.userAgent.match( /ipad|iphone/i ); | |
| 7 | + } | |
| 8 | 8 | |
| 9 | - function createTextArea(text) { | |
| 10 | - textArea = document.createElement('textArea'); | |
| 11 | - textArea.value = text; | |
| 12 | - document.body.appendChild(textArea); | |
| 13 | - } | |
| 9 | + function createTextArea(text) { | |
| 10 | + textArea = document.createElement( 'textArea' ); | |
| 11 | + textArea.value = text; | |
| 12 | + document.body.appendChild( textArea ); | |
| 13 | + } | |
| 14 | 14 | |
| 15 | - function selectText() { | |
| 16 | - var range, | |
| 17 | - selection; | |
| 15 | + function selectText() { | |
| 16 | + var range, | |
| 17 | + selection; | |
| 18 | 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 | - } | |
| 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 | 30 | |
| 31 | - function copyToClipboard() { | |
| 32 | - document.execCommand('copy'); | |
| 33 | - document.body.removeChild(textArea); | |
| 31 | + function copyToClipboard() { | |
| 32 | + document.execCommand( 'copy' ); | |
| 33 | + document.body.removeChild( textArea ); | |
| 34 | 34 | |
| 35 | - // Redirect to page. | |
| 36 | - if( copyTheCode.redirect_url ) { | |
| 37 | - window.location.href = copyTheCode.redirect_url; | |
| 38 | - } | |
| 39 | - } | |
| 35 | + // Redirect to page. | |
| 36 | + if ( copyTheCode.redirect_url ) { | |
| 37 | + window.location.href = copyTheCode.redirect_url; | |
| 38 | + } | |
| 39 | + } | |
| 40 | 40 | |
| 41 | - copy = function (text) { | |
| 42 | - createTextArea(text); | |
| 43 | - selectText(); | |
| 44 | - copyToClipboard(); | |
| 45 | - }; | |
| 41 | + copy = function (text) { | |
| 42 | + createTextArea( text ); | |
| 43 | + selectText(); | |
| 44 | + copyToClipboard(); | |
| 45 | + }; | |
| 46 | 46 | |
| 47 | - return { | |
| 48 | - copy: copy | |
| 49 | - }; | |
| 50 | -})(window, document, navigator); | |
| 47 | + return { | |
| 48 | + copy: copy | |
| 49 | + }; | |
| 50 | +})( window, document, navigator ); | |
| 51 | 51 | |
| 52 | 52 | (function ($) { |
| 53 | 53 | |
| 54 | - CopyTheCode = { | |
| 54 | + CopyTheCode = { | |
| 55 | 55 | |
| 56 | - selector: copyTheCode.settings.selector || copyTheCode.selector || 'pre', | |
| 57 | - button_position: copyTheCode.settings['button-position'] || 'inside', | |
| 56 | + selector: copyTheCode.settings.selector || copyTheCode.selector || 'pre', | |
| 57 | + button_position: copyTheCode.settings['button-position'] || 'inside', | |
| 58 | 58 | |
| 59 | - /** | |
| 60 | - * Init | |
| 61 | - */ | |
| 62 | - init: function () { | |
| 63 | - this._bind(); | |
| 64 | - this._initialize(); | |
| 65 | - }, | |
| 59 | + /** | |
| 60 | + * Init | |
| 61 | + */ | |
| 62 | + init: function () { | |
| 63 | + this._bind(); | |
| 64 | + this._initialize(); | |
| 65 | + }, | |
| 66 | 66 | |
| 67 | - /** | |
| 68 | - * Binds events | |
| 69 | - */ | |
| 70 | - _bind: function () { | |
| 71 | - $(document).on('click', '.copy-the-code-button', CopyTheCode.copyCode); | |
| 72 | - $(document).on('click', '.copy-the-code-shortcode', CopyTheCode.copyShortcode); | |
| 73 | - }, | |
| 67 | + /** | |
| 68 | + * Binds events | |
| 69 | + */ | |
| 70 | + _bind: function () { | |
| 71 | + $( document ).on( 'click', '.copy-the-code-button', CopyTheCode.copyCode ); | |
| 72 | + $( document ).on( 'click', '.copy-the-code-shortcode', CopyTheCode.copyShortcode ); | |
| 73 | + }, | |
| 74 | 74 | |
| 75 | - /** | |
| 76 | - * Initialize the Button | |
| 77 | - */ | |
| 78 | - _initialize: function () { | |
| 79 | - if (!$(copyTheCode.selectors).length) { | |
| 80 | - return; | |
| 81 | - } | |
| 75 | + /** | |
| 76 | + * Initialize the Button | |
| 77 | + */ | |
| 78 | + _initialize: function () { | |
| 79 | + if ( ! $( copyTheCode.selectors ).length) { | |
| 80 | + return; | |
| 81 | + } | |
| 82 | 82 | |
| 83 | - $(copyTheCode.selectors).each(function (index, el) { | |
| 84 | - var button_copy_text = el['button_copy_text'] || ''; | |
| 85 | - var button_position = el['button_position'] || ''; | |
| 86 | - var button_text = el['button_text'] || ''; | |
| 87 | - var button_title = el['button_title'] || ''; | |
| 88 | - var selector = el['selector'] || ''; | |
| 89 | - var style = el['style'] || ''; | |
| 90 | - var copy_format = el['copy_format'] || ''; | |
| 83 | + $( copyTheCode.selectors ).each( | |
| 84 | + function (index, el) { | |
| 85 | + var button_copy_text = el['button_copy_text'] || ''; | |
| 86 | + var button_position = el['button_position'] || ''; | |
| 87 | + var button_text = el['button_text'] || ''; | |
| 88 | + var button_title = el['button_title'] || ''; | |
| 89 | + var selector = el['selector'] || ''; | |
| 90 | + var style = el['style'] || ''; | |
| 91 | + var copy_format = el['copy_format'] || ''; | |
| 91 | 92 | |
| 92 | - $(selector).each(function (index, single_selector) { | |
| 93 | + $( selector ).each( | |
| 94 | + function (index, single_selector) { | |
| 93 | 95 | |
| 94 | - var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style); | |
| 96 | + var buttonMarkup = CopyTheCode._getButtonMarkup( button_title, button_text, style ); | |
| 95 | 97 | |
| 96 | - $(single_selector).addClass('copy-the-code-target'); | |
| 98 | + $( single_selector ).addClass( 'copy-the-code-target' ); | |
| 97 | 99 | |
| 98 | - if ('cover' !== style && 'outside' === button_position) { | |
| 99 | - $(single_selector).wrap('<span data-copy-format="' + copy_format + '" 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>'); | |
| 100 | - $(single_selector).parent().prepend('<div class="copy-the-code-outside">' + buttonMarkup + '</div>'); | |
| 101 | - } else { | |
| 102 | - $(single_selector).wrap('<span data-copy-format="' + copy_format + '" 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>'); | |
| 103 | - $(single_selector).append(buttonMarkup); | |
| 104 | - } | |
| 100 | + if ('cover' !== style && 'outside' === button_position) { | |
| 101 | + $( single_selector ).wrap( '<span data-copy-format="' + copy_format + '" 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>' ); | |
| 102 | + $( single_selector ).parent().prepend( '<div class="copy-the-code-outside">' + buttonMarkup + '</div>' ); | |
| 103 | + } else { | |
| 104 | + $( single_selector ).wrap( '<span data-copy-format="' + copy_format + '" 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>' ); | |
| 105 | + $( single_selector ).append( buttonMarkup ); | |
| 106 | + } | |
| 105 | 107 | |
| 106 | - switch (style) { | |
| 107 | - case 'svg-icon': | |
| 108 | - $(single_selector).find('.copy-the-code-button').html(copyTheCode.buttonSvg); | |
| 109 | - break; | |
| 110 | - case 'cover': | |
| 111 | - case 'button': | |
| 112 | - default: | |
| 113 | - $(single_selector).find('.copy-the-code-button').html(button_text); | |
| 114 | - break; | |
| 115 | - } | |
| 108 | + switch (style) { | |
| 109 | + case 'svg-icon': | |
| 110 | + $( single_selector ).find( '.copy-the-code-button' ).html( copyTheCode.buttonSvg ); | |
| 111 | + break; | |
| 112 | + case 'cover': | |
| 113 | + case 'button': | |
| 114 | + default: | |
| 115 | + $( single_selector ).find( '.copy-the-code-button' ).html( button_text ); | |
| 116 | + break; | |
| 117 | + } | |
| 116 | 118 | |
| 119 | + } | |
| 120 | + ); | |
| 121 | + } | |
| 122 | + ); | |
| 117 | 123 | |
| 118 | - }); | |
| 119 | - }); | |
| 124 | + }, | |
| 120 | 125 | |
| 121 | - }, | |
| 126 | + /** | |
| 127 | + * Get Copy Button Markup | |
| 128 | + */ | |
| 129 | + _getButtonMarkup: function (button_title, button_text, style) { | |
| 130 | + if ('svg-icon' === style) { | |
| 131 | + button_text = copyTheCode.buttonSvg; | |
| 132 | + } | |
| 122 | 133 | |
| 123 | - /** | |
| 124 | - * Get Copy Button Markup | |
| 125 | - */ | |
| 126 | - _getButtonMarkup: function (button_title, button_text, style) { | |
| 127 | - if ('svg-icon' === style) { | |
| 128 | - button_text = copyTheCode.buttonSvg; | |
| 129 | - } | |
| 134 | + return '<button class="copy-the-code-button" data-style="' + style + '" title="' + button_title + '">' + button_text + '</button>'; | |
| 135 | + }, | |
| 130 | 136 | |
| 131 | - return '<button class="copy-the-code-button" data-style="' + style + '" title="' + button_title + '">' + button_text + '</button>'; | |
| 132 | - }, | |
| 137 | + format: function (html) { | |
| 138 | + var tab = '\t'; | |
| 139 | + var result = ''; | |
| 140 | + var indent = ''; | |
| 133 | 141 | |
| 134 | - format: function (html) { | |
| 135 | - var tab = '\t'; | |
| 136 | - var result = ''; | |
| 137 | - var indent = ''; | |
| 142 | + html.split( />\s*</ ).forEach( | |
| 143 | + function (element) { | |
| 144 | + if (element.match( /^\/\w/ )) { | |
| 145 | + indent = indent.substring( tab.length ); | |
| 146 | + } | |
| 138 | 147 | |
| 139 | - html.split(/>\s*</).forEach(function (element) { | |
| 140 | - if (element.match(/^\/\w/)) { | |
| 141 | - indent = indent.substring(tab.length); | |
| 142 | - } | |
| 148 | + result += indent + '<' + element + '>\r\n'; | |
| 143 | 149 | |
| 144 | - result += indent + '<' + element + '>\r\n'; | |
| 150 | + if (element.match( /^<?\w[^>]*[^\/]$/ ) && ! element.startsWith( "input" )) { | |
| 151 | + indent += tab; | |
| 152 | + } | |
| 153 | + } | |
| 154 | + ); | |
| 145 | 155 | |
| 146 | - if (element.match(/^<?\w[^>]*[^\/]$/) && !element.startsWith("input")) { | |
| 147 | - indent += tab; | |
| 148 | - } | |
| 149 | - }); | |
| 156 | + return result.substring( 1, result.length - 3 ); | |
| 157 | + }, | |
| 150 | 158 | |
| 151 | - return result.substring(1, result.length - 3); | |
| 152 | - }, | |
| 159 | + /** | |
| 160 | + * Copy to Clipboard | |
| 161 | + */ | |
| 162 | + copyShortcode: function (event) { | |
| 163 | + event.preventDefault(); | |
| 153 | 164 | |
| 154 | - /** | |
| 155 | - * Copy to Clipboard | |
| 156 | - */ | |
| 157 | - copyShortcode: function (event) { | |
| 158 | - event.preventDefault(); | |
| 165 | + var btn = $( this ), | |
| 166 | + oldText = btn.html(), | |
| 167 | + target = btn.attr( 'data-target' ) || '', | |
| 168 | + copy_content_as = btn.attr( 'data-copy-as' ) || copyTheCode.copy_content_as, | |
| 169 | + button_copy_text = btn.attr( 'data-button-copy-text' ) || '', | |
| 170 | + content = btn.attr( 'data-content' ) || '', | |
| 171 | + link = btn.attr( 'data-link' ) || ''; | |
| 159 | 172 | |
| 160 | - var btn = $(this), | |
| 161 | - oldText = btn.html(), | |
| 162 | - target = btn.attr('data-target') || '', | |
| 163 | - copy_content_as = btn.attr('data-copy-as') || copyTheCode.copy_content_as, | |
| 164 | - button_copy_text = btn.attr('data-button-copy-text') || '', | |
| 165 | - content = btn.attr('data-content') || '', | |
| 166 | - link = btn.attr( 'data-link' ) || ''; | |
| 173 | + // Copy the secrate content. | |
| 174 | + if (content) { | |
| 175 | + CopyTheCodeToClipboard.copy( content ); | |
| 176 | + // Copied! | |
| 177 | + btn.text( button_copy_text ); | |
| 178 | + setTimeout( | |
| 179 | + function () { | |
| 180 | + btn.html( oldText ); | |
| 167 | 181 | |
| 168 | - // Copy the secrate content. | |
| 169 | - if (content) { | |
| 170 | - CopyTheCodeToClipboard.copy(content); | |
| 171 | - // Copied! | |
| 172 | - btn.text(button_copy_text); | |
| 173 | - setTimeout(function () { | |
| 174 | - btn.html(oldText); | |
| 182 | + if ( link ) { | |
| 183 | + window.open( link, '_blank' ).focus() | |
| 184 | + } | |
| 185 | + }, | |
| 186 | + 1000 | |
| 187 | + ); | |
| 188 | + return; | |
| 189 | + } | |
| 175 | 190 | |
| 176 | - if( link ) { | |
| 177 | - window.open(link, '_blank').focus() | |
| 178 | - } | |
| 179 | - }, 1000); | |
| 180 | - return; | |
| 181 | - } | |
| 191 | + var source = $( target ); | |
| 182 | 192 | |
| 183 | - var source = $(target); | |
| 193 | + if ( ! source.length) { | |
| 194 | + btn.text( 'Not found!' ); | |
| 195 | + setTimeout( | |
| 196 | + function () { | |
| 197 | + btn.text( oldText ); | |
| 198 | + }, | |
| 199 | + 1000 | |
| 200 | + ); | |
| 201 | + return; | |
| 202 | + } | |
| 184 | 203 | |
| 185 | - if (!source.length) { | |
| 186 | - btn.text('Not found!'); | |
| 187 | - setTimeout(function () { | |
| 188 | - btn.text(oldText); | |
| 189 | - }, 1000); | |
| 190 | - return; | |
| 191 | - } | |
| 204 | + var html = source.html(); | |
| 192 | 205 | |
| 193 | - var html = source.html(); | |
| 206 | + html = CopyTheCode.format( html ); | |
| 194 | 207 | |
| 195 | - html = CopyTheCode.format(html); | |
| 208 | + if ('html' !== copy_content_as) { | |
| 196 | 209 | |
| 197 | - if ('html' !== copy_content_as) { | |
| 210 | + // Convert the <br/> tags into new line. | |
| 211 | + var brRegex = /<br\s*[\/]?>/gi; | |
| 212 | + html = html.replace( brRegex, "\n" ); | |
| 198 | 213 | |
| 199 | - // Convert the <br/> tags into new line. | |
| 200 | - var brRegex = /<br\s*[\/]?>/gi; | |
| 201 | - html = html.replace(brRegex, "\n"); | |
| 214 | + // Convert the <div> tags into new line. | |
| 215 | + var divRegex = /<div\s*[\/]?>/gi; | |
| 216 | + html = html.replace( divRegex, "\n" ); | |
| 202 | 217 | |
| 203 | - // Convert the <div> tags into new line. | |
| 204 | - var divRegex = /<div\s*[\/]?>/gi; | |
| 205 | - html = html.replace(divRegex, "\n"); | |
| 218 | + // Convert the <p> tags into new line. | |
| 219 | + var pRegex = /<p\s*[\/]?>/gi; | |
| 220 | + html = html.replace( pRegex, "\n" ); | |
| 206 | 221 | |
| 207 | - // Convert the <p> tags into new line. | |
| 208 | - var pRegex = /<p\s*[\/]?>/gi; | |
| 209 | - html = html.replace(pRegex, "\n"); | |
| 222 | + // Convert the <li> tags into new line. | |
| 223 | + var pRegex = /<li\s*[\/]?>/gi; | |
| 224 | + html = html.replace( pRegex, "\n" ); | |
| 210 | 225 | |
| 211 | - // Convert the <li> tags into new line. | |
| 212 | - var pRegex = /<li\s*[\/]?>/gi; | |
| 213 | - html = html.replace(pRegex, "\n"); | |
| 226 | + // Remove all tags. | |
| 227 | + html = html.replace( /(<([^>]+)>)/ig, '' ); | |
| 228 | + } | |
| 214 | 229 | |
| 215 | - // Remove all tags. | |
| 216 | - html = html.replace(/(<([^>]+)>)/ig, ''); | |
| 217 | - } | |
| 230 | + if ('html' !== copy_content_as) { | |
| 231 | + html = html.replace( /[\t\n]+/gm, ' ' ).trim(); | |
| 232 | + } else { | |
| 233 | + var reWhiteSpace = new RegExp( "/^\s+$/" ); | |
| 234 | + html = html.replace( reWhiteSpace, "" ); | |
| 235 | + } | |
| 218 | 236 | |
| 219 | - if ('html' !== copy_content_as) { | |
| 220 | - html = html.replace(/[\t\n]+/gm, ' ').trim(); | |
| 221 | - } else { | |
| 222 | - var reWhiteSpace = new RegExp("/^\s+$/"); | |
| 223 | - html = html.replace(reWhiteSpace, ""); | |
| 224 | - } | |
| 237 | + var tempElement = $( "<div id='temp-element'></div>" ); | |
| 238 | + $( "body" ).append( tempElement ); | |
| 239 | + html = $.trim( html ); | |
| 240 | + $( '#temp-element' ).html( html ); | |
| 241 | + var html = $( '#temp-element' ).html(); | |
| 242 | + $( '#temp-element' ).remove(); | |
| 225 | 243 | |
| 226 | - var tempElement = $("<div id='temp-element'></div>"); | |
| 227 | - $("body").append(tempElement); | |
| 228 | - html = $.trim(html); | |
| 229 | - $('#temp-element').html(html); | |
| 230 | - var html = $('#temp-element').html(); | |
| 231 | - $('#temp-element').remove(); | |
| 244 | + var tempHTML = html; | |
| 232 | 245 | |
| 233 | - var tempHTML = html; | |
| 246 | + // Copy the Code. | |
| 247 | + var tempPre = $( "<textarea id='temp-pre'>" ), | |
| 248 | + temp = $( "<textarea>" ); | |
| 234 | 249 | |
| 235 | - // Copy the Code. | |
| 236 | - var tempPre = $("<textarea id='temp-pre'>"), | |
| 237 | - temp = $("<textarea>"); | |
| 250 | + // Append temporary elements to DOM. | |
| 251 | + $( "body" ).append( temp ); | |
| 252 | + $( "body" ).append( tempPre ); | |
| 238 | 253 | |
| 239 | - // Append temporary elements to DOM. | |
| 240 | - $("body").append(temp); | |
| 241 | - $("body").append(tempPre); | |
| 254 | + // Set temporary HTML markup. | |
| 255 | + tempPre.html( tempHTML ); | |
| 242 | 256 | |
| 243 | - // Set temporary HTML markup. | |
| 244 | - tempPre.html(tempHTML); | |
| 257 | + var content = tempPre.text(); | |
| 245 | 258 | |
| 246 | - var content = tempPre.text(); | |
| 259 | + content = content.trim(); | |
| 247 | 260 | |
| 248 | - content = content.trim(); | |
| 261 | + if ( copyTheCode.trim_lines ) { | |
| 262 | + content = content.replace( /^(?=\n)$|\s*$|\n\n+/gm,"" ); | |
| 263 | + if ( content ) { | |
| 264 | + content = content.split( '\n' ); | |
| 265 | + content = content.map( | |
| 266 | + item => { | |
| 267 | + return item.trim(); | |
| 268 | + } | |
| 269 | + ); | |
| 270 | + content = content.join( '\n' ); | |
| 271 | + } | |
| 272 | + } | |
| 249 | 273 | |
| 250 | - if( copyTheCode.trim_lines ) { | |
| 251 | - content = content.replace(/^(?=\n)$|\s*$|\n\n+/gm,""); | |
| 252 | - if( content ) { | |
| 253 | - content = content.split('\n'); | |
| 254 | - content = content.map( item => { | |
| 255 | - return item.trim(); | |
| 256 | - } ); | |
| 257 | - content = content.join('\n'); | |
| 258 | - } | |
| 259 | - } | |
| 274 | + // Format the HTML markup. | |
| 275 | + temp.val( content ).select(); | |
| 260 | 276 | |
| 261 | - // Format the HTML markup. | |
| 262 | - temp.val(content).select(); | |
| 277 | + // Support for IOS devices too. | |
| 278 | + CopyTheCodeToClipboard.copy( content.trim() ); | |
| 263 | 279 | |
| 264 | - // Support for IOS devices too. | |
| 265 | - CopyTheCodeToClipboard.copy(content.trim()); | |
| 280 | + // Remove temporary elements. | |
| 281 | + temp.remove(); | |
| 282 | + tempPre.remove(); | |
| 266 | 283 | |
| 267 | - // Remove temporary elements. | |
| 268 | - temp.remove(); | |
| 269 | - tempPre.remove(); | |
| 284 | + // Copied! | |
| 285 | + btn.text( button_copy_text ); | |
| 286 | + setTimeout( | |
| 287 | + function () { | |
| 288 | + btn.text( oldText ); | |
| 289 | + }, | |
| 290 | + 1000 | |
| 291 | + ); | |
| 292 | + }, | |
| 270 | 293 | |
| 271 | - // Copied! | |
| 272 | - btn.text(button_copy_text); | |
| 273 | - setTimeout(function () { | |
| 274 | - btn.text(oldText); | |
| 275 | - }, 1000); | |
| 276 | - }, | |
| 294 | + /** | |
| 295 | + * Copy to Clipboard | |
| 296 | + */ | |
| 297 | + copyCode: function (event) { | |
| 298 | + event.preventDefault(); | |
| 277 | 299 | |
| 278 | - /** | |
| 279 | - * Copy to Clipboard | |
| 280 | - */ | |
| 281 | - copyCode: function (event) { | |
| 282 | - event.preventDefault(); | |
| 300 | + var btn = $( this ), | |
| 301 | + oldText = btn.text(), | |
| 302 | + parent = btn.parents( '.copy-the-code-wrap' ), | |
| 303 | + selector = parent.attr( 'data-selector' ) || '', | |
| 304 | + button_text = parent.attr( 'data-button-text' ) || '', | |
| 305 | + button_position = parent.attr( 'data-button-position' ) || '', | |
| 306 | + button_copy_text = parent.attr( 'data-button-copy-text' ) || '', | |
| 307 | + button_title = parent.attr( 'data-button-title' ) || '', | |
| 308 | + style = parent.attr( 'data-style' ) || '' | |
| 309 | + copy_format = parent.attr( 'data-copy-format' ) || ''; | |
| 283 | 310 | |
| 284 | - var btn = $(this), | |
| 285 | - oldText = btn.text(), | |
| 286 | - parent = btn.parents('.copy-the-code-wrap'), | |
| 287 | - selector = parent.attr('data-selector') || '', | |
| 288 | - button_text = parent.attr('data-button-text') || '', | |
| 289 | - button_position = parent.attr('data-button-position') || '', | |
| 290 | - button_copy_text = parent.attr('data-button-copy-text') || '', | |
| 291 | - button_title = parent.attr('data-button-title') || '', | |
| 292 | - style = parent.attr('data-style') || '' | |
| 293 | - copy_format = parent.attr('data-copy-format') || ''; | |
| 311 | + // Fix: nested selectors e.g. `.entry-content pre` | |
| 312 | + if (selector.indexOf( ' ' ) >= 0) { | |
| 313 | + var source = btn.parents( '.copy-the-code-wrap' ); | |
| 314 | + } else { | |
| 315 | + var source = btn.parents( '.copy-the-code-wrap' ).find( selector ); | |
| 316 | + } | |
| 294 | 317 | |
| 295 | - // Fix: nested selectors e.g. `.entry-content pre` | |
| 296 | - if (selector.indexOf(' ') >= 0) { | |
| 297 | - var source = btn.parents('.copy-the-code-wrap'); | |
| 298 | - } else { | |
| 299 | - var source = btn.parents('.copy-the-code-wrap').find(selector); | |
| 300 | - } | |
| 318 | + if ( 'google-docs' === copy_format ) { | |
| 319 | + // Hide button temporary. | |
| 320 | + if ( 'inside' === button_position ) { | |
| 321 | + $( '.copy-the-code-button' ).hide(); | |
| 322 | + } | |
| 301 | 323 | |
| 302 | - if ( 'google-docs' === copy_format ) { | |
| 303 | - // Hide button temporary. | |
| 304 | - if ( 'inside' === button_position ) { | |
| 305 | - $( '.copy-the-code-button' ).hide(); | |
| 306 | - } | |
| 324 | + var range = document.createRange(); | |
| 325 | + range.selectNode( source[0] ); | |
| 326 | + var selection = window.getSelection(); | |
| 327 | + selection.removeAllRanges(); | |
| 328 | + selection.addRange( range ); | |
| 329 | + | |
| 330 | + // Get selected text content | |
| 331 | + var selectedText = selection.toString() || source[0].textContent || ''; | |
| 332 | + | |
| 333 | + // Use modern Clipboard API (iOS 26+ compatible) | |
| 334 | + if ( navigator.clipboard && navigator.clipboard.writeText ) { | |
| 335 | + navigator.clipboard.writeText( selectedText ).then( | |
| 336 | + function() { | |
| 337 | + // Clear selection. | |
| 338 | + if (window.getSelection) { | |
| 339 | + if (window.getSelection().empty) { // Chrome | |
| 340 | + window.getSelection().empty(); | |
| 341 | + } else if (window.getSelection().removeAllRanges) { // Firefox | |
| 342 | + window.getSelection().removeAllRanges(); | |
| 343 | + } | |
| 344 | + } else if (document.selection) { // IE? | |
| 345 | + document.selection.empty(); | |
| 346 | + } | |
| 307 | 347 | |
| 308 | - var range = document.createRange(); | |
| 309 | - range.selectNode(source[0]); | |
| 310 | - window.getSelection().addRange(range); | |
| 311 | - document.execCommand('copy'); | |
| 348 | + // Show button again. | |
| 349 | + if ( 'inside' === button_position ) { | |
| 350 | + $( '.copy-the-code-button' ).show(); | |
| 351 | + } | |
| 352 | + } | |
| 353 | + ).catch( | |
| 354 | + function() { | |
| 355 | + // Fallback to legacy method if Clipboard API fails | |
| 356 | + document.execCommand( 'copy' ); | |
| 357 | + | |
| 358 | + // Clear selection. | |
| 359 | + if (window.getSelection) { | |
| 360 | + if (window.getSelection().empty) { // Chrome | |
| 361 | + window.getSelection().empty(); | |
| 362 | + } else if (window.getSelection().removeAllRanges) { // Firefox | |
| 363 | + window.getSelection().removeAllRanges(); | |
| 364 | + } | |
| 365 | + } else if (document.selection) { // IE? | |
| 366 | + document.selection.empty(); | |
| 367 | + } | |
| 312 | 368 | |
| 313 | - // Clear selection. | |
| 314 | - if (window.getSelection) { | |
| 315 | - if (window.getSelection().empty) { // Chrome | |
| 316 | - window.getSelection().empty(); | |
| 317 | - } else if (window.getSelection().removeAllRanges) { // Firefox | |
| 318 | - window.getSelection().removeAllRanges(); | |
| 319 | - } | |
| 320 | - } else if (document.selection) { // IE? | |
| 321 | - document.selection.empty(); | |
| 322 | - } | |
| 369 | + // Show button again. | |
| 370 | + if ( 'inside' === button_position ) { | |
| 371 | + $( '.copy-the-code-button' ).show(); | |
| 372 | + } | |
| 373 | + } | |
| 374 | + ); | |
| 375 | + } else { | |
| 376 | + // Fallback for older browsers | |
| 377 | + document.execCommand( 'copy' ); | |
| 323 | 378 | |
| 324 | - // Show button again. | |
| 325 | - if ( 'inside' === button_position ) { | |
| 326 | - $( '.copy-the-code-button' ).show(); | |
| 327 | - } | |
| 379 | + // Clear selection. | |
| 380 | + if (window.getSelection) { | |
| 381 | + if (window.getSelection().empty) { // Chrome | |
| 382 | + window.getSelection().empty(); | |
| 383 | + } else if (window.getSelection().removeAllRanges) { // Firefox | |
| 384 | + window.getSelection().removeAllRanges(); | |
| 385 | + } | |
| 386 | + } else if (document.selection) { // IE? | |
| 387 | + document.selection.empty(); | |
| 388 | + } | |
| 328 | 389 | |
| 329 | - } else { | |
| 390 | + // Show button again. | |
| 391 | + if ( 'inside' === button_position ) { | |
| 392 | + $( '.copy-the-code-button' ).show(); | |
| 393 | + } | |
| 394 | + } | |
| 330 | 395 | |
| 331 | - var html = source.html(); | |
| 396 | + } else { | |
| 332 | 397 | |
| 333 | - var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style); | |
| 398 | + var html = source.html(); | |
| 334 | 399 | |
| 335 | - // Remove the <copy> button. | |
| 336 | - html = html.replace(buttonMarkup, ''); | |
| 400 | + var buttonMarkup = CopyTheCode._getButtonMarkup( button_title, button_text, style ); | |
| 337 | 401 | |
| 338 | - // Emojis. | |
| 339 | - let allEmojis = CopyTheCode._getImages( html ); | |
| 402 | + // Remove the <copy> button. | |
| 403 | + html = html.replace( buttonMarkup, '' ); | |
| 340 | 404 | |
| 341 | - if ( allEmojis ) { | |
| 342 | - allEmojis.map( function(image) { | |
| 343 | - let alt = CopyTheCode._getAlt( image ); | |
| 344 | - if ( alt ) { | |
| 345 | - let cleanAlt = alt[0]; | |
| 346 | - cleanAlt = cleanAlt.replaceAll( '"', '' ); | |
| 347 | - cleanAlt = cleanAlt.replaceAll( "'", '' ); | |
| 348 | - cleanAlt = cleanAlt.replaceAll( 'alt=', '' ); | |
| 349 | - html = html.replaceAll( image, cleanAlt ); | |
| 350 | - } | |
| 351 | - }); | |
| 405 | + // Emojis. | |
| 406 | + let allEmojis = CopyTheCode._getImages( html ); | |
| 352 | 407 | |
| 353 | - } | |
| 408 | + if ( allEmojis ) { | |
| 409 | + allEmojis.map( | |
| 410 | + function(image) { | |
| 411 | + let alt = CopyTheCode._getAlt( image ); | |
| 412 | + if ( alt ) { | |
| 413 | + let cleanAlt = alt[0]; | |
| 414 | + cleanAlt = cleanAlt.replaceAll( '"', '' ); | |
| 415 | + cleanAlt = cleanAlt.replaceAll( "'", '' ); | |
| 416 | + cleanAlt = cleanAlt.replaceAll( 'alt=', '' ); | |
| 417 | + html = html.replaceAll( image, cleanAlt ); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + ); | |
| 354 | 421 | |
| 355 | - // Remove all duplicate empty lines. | |
| 356 | - if( copyTheCode.remove_spaces ) { | |
| 357 | - html = html.replace(/^(?=\n)$|\s*$|\n\n+/gm,""); | |
| 358 | - } | |
| 422 | + } | |
| 359 | 423 | |
| 360 | - if ('html' !== copyTheCode.copy_content_as) { | |
| 361 | - // Convert the <br/> tags into new line. | |
| 362 | - var brRegex = /<br\s*[\/]?>/gi; | |
| 363 | - html = html.replace(brRegex, "\n"); | |
| 424 | + // Remove all duplicate empty lines. | |
| 425 | + if ( copyTheCode.remove_spaces ) { | |
| 426 | + html = html.replace( /^(?=\n)$|\s*$|\n\n+/gm,"" ); | |
| 427 | + } | |
| 364 | 428 | |
| 365 | - // Convert the <div> tags into new line. | |
| 366 | - var divRegex = /<div\s*[\/]?>/gi; | |
| 367 | - html = html.replace(divRegex, "\n"); | |
| 429 | + if ('html' !== copyTheCode.copy_content_as) { | |
| 430 | + // Convert the <br/> tags into new line. | |
| 431 | + var brRegex = /<br\s*[\/]?>/gi; | |
| 432 | + html = html.replace( brRegex, "\n" ); | |
| 368 | 433 | |
| 369 | - // Convert the <p> tags into new line. | |
| 370 | - var pRegex = /<p\s*[\/]?>/gi; | |
| 371 | - html = html.replace(pRegex, "\n"); | |
| 434 | + // Convert the <div> tags into new line. | |
| 435 | + var divRegex = /<div\s*[\/]?>/gi; | |
| 436 | + html = html.replace( divRegex, "\n" ); | |
| 372 | 437 | |
| 373 | - // Convert the <li> tags into new line. | |
| 374 | - var pRegex = /<li\s*[\/]?>/gi; | |
| 375 | - html = html.replace(pRegex, "\n"); | |
| 438 | + // Convert the <p> tags into new line. | |
| 439 | + var pRegex = /<p\s*[\/]?>/gi; | |
| 440 | + html = html.replace( pRegex, "\n" ); | |
| 376 | 441 | |
| 377 | - // Remove all tags. | |
| 378 | - html = html.replace(/(<([^>]+)>)/ig, ''); | |
| 379 | - } | |
| 442 | + // Convert the <li> tags into new line. | |
| 443 | + var pRegex = /<li\s*[\/]?>/gi; | |
| 444 | + html = html.replace( pRegex, "\n" ); | |
| 380 | 445 | |
| 381 | - // Remove white spaces. | |
| 382 | - var reWhiteSpace = new RegExp("/^\s+$/"); | |
| 383 | - html = html.replace(reWhiteSpace, ""); | |
| 446 | + // Remove all tags. | |
| 447 | + html = html.replace( /(<([^>]+)>)/ig, '' ); | |
| 448 | + } | |
| 384 | 449 | |
| 385 | - var tempElement = $("<div id='temp-element'></div>"); | |
| 386 | - $("body").append(tempElement); | |
| 387 | - html = $.trim(html); | |
| 388 | - $('#temp-element').html(html); | |
| 389 | - var html = $('#temp-element').html(); | |
| 390 | - $('#temp-element').remove(); | |
| 450 | + // Remove white spaces. | |
| 451 | + var reWhiteSpace = new RegExp( "/^\s+$/" ); | |
| 452 | + html = html.replace( reWhiteSpace, "" ); | |
| 391 | 453 | |
| 392 | - var tempHTML = html; | |
| 393 | - var reWhiteSpace = new RegExp("/^\s+$/"); | |
| 394 | - tempHTML = tempHTML.replace(reWhiteSpace, ""); | |
| 454 | + var tempElement = $( "<div id='temp-element'></div>" ); | |
| 455 | + $( "body" ).append( tempElement ); | |
| 456 | + html = $.trim( html ); | |
| 457 | + $( '#temp-element' ).html( html ); | |
| 458 | + var html = $( '#temp-element' ).html(); | |
| 459 | + $( '#temp-element' ).remove(); | |
| 395 | 460 | |
| 396 | - // Remove button text. | |
| 397 | - tempHTML = tempHTML.replace(button_text, ''); | |
| 461 | + var tempHTML = html; | |
| 462 | + var reWhiteSpace = new RegExp( "/^\s+$/" ); | |
| 463 | + tempHTML = tempHTML.replace( reWhiteSpace, "" ); | |
| 398 | 464 | |
| 399 | - // Copy the Code. | |
| 400 | - var tempPre = $("<textarea id='temp-pre'>"), | |
| 401 | - temp = $("<textarea>"); | |
| 465 | + // Remove button text. | |
| 466 | + tempHTML = tempHTML.replace( button_text, '' ); | |
| 402 | 467 | |
| 403 | - // Append temporary elements to DOM. | |
| 404 | - $("body").append(temp); | |
| 405 | - $("body").append(tempPre); | |
| 468 | + // Copy the Code. | |
| 469 | + var tempPre = $( "<textarea id='temp-pre'>" ), | |
| 470 | + temp = $( "<textarea>" ); | |
| 406 | 471 | |
| 407 | - // Set temporary HTML markup. | |
| 408 | - var content = tempPre.html(tempHTML).text(); | |
| 472 | + // Append temporary elements to DOM. | |
| 473 | + $( "body" ).append( temp ); | |
| 474 | + $( "body" ).append( tempPre ); | |
| 409 | 475 | |
| 410 | - content = $.trim(content); | |
| 476 | + // Set temporary HTML markup. | |
| 477 | + var content = tempPre.html( tempHTML ).text(); | |
| 411 | 478 | |
| 412 | - // Format the HTML markup. | |
| 413 | - temp.val(content).select(); | |
| 479 | + content = $.trim( content ); | |
| 414 | 480 | |
| 415 | - // Support for IOS devices too. | |
| 416 | - CopyTheCodeToClipboard.copy(content); | |
| 481 | + // Format the HTML markup. | |
| 482 | + temp.val( content ).select(); | |
| 417 | 483 | |
| 418 | - // Remove temporary elements. | |
| 419 | - temp.remove(); | |
| 420 | - tempPre.remove(); | |
| 421 | - } | |
| 484 | + // Support for IOS devices too. | |
| 485 | + CopyTheCodeToClipboard.copy( content ); | |
| 422 | 486 | |
| 423 | - // Copied! | |
| 424 | - btn.text(button_copy_text); | |
| 425 | - setTimeout(function () { | |
| 426 | - if ('svg-icon' === style) { | |
| 427 | - btn.html(copyTheCode.buttonSvg); | |
| 428 | - } else { | |
| 429 | - btn.text(oldText); | |
| 430 | - } | |
| 431 | - }, 1000); | |
| 432 | - }, | |
| 487 | + // Remove temporary elements. | |
| 488 | + temp.remove(); | |
| 489 | + tempPre.remove(); | |
| 490 | + } | |
| 433 | 491 | |
| 434 | - _getImages( content ) { | |
| 435 | - return content.match( /<img\s+[^>]*?src=("|')([^"']+)">/gi ); | |
| 436 | - }, | |
| 492 | + // Copied! | |
| 493 | + btn.text( button_copy_text ); | |
| 494 | + setTimeout( | |
| 495 | + function () { | |
| 496 | + if ('svg-icon' === style) { | |
| 497 | + btn.html( copyTheCode.buttonSvg ); | |
| 498 | + } else { | |
| 499 | + btn.text( oldText ); | |
| 500 | + } | |
| 501 | + }, | |
| 502 | + 1000 | |
| 503 | + ); | |
| 504 | + }, | |
| 437 | 505 | |
| 438 | - _getAlt( img ) { | |
| 439 | - return img.match( /alt=("|')([^"']+)"|'/gi ); | |
| 440 | - } | |
| 506 | + _getImages( content ) { | |
| 507 | + return content.match( /<img\s+[^>]*?src=("|')([^"']+)">/gi ); | |
| 508 | + }, | |
| 441 | 509 | |
| 442 | - }; | |
| 510 | + _getAlt( img ) { | |
| 511 | + return img.match( /alt=("|')([^"']+)"|'/gi ); | |
| 512 | + } | |
| 443 | 513 | |
| 444 | - /** | |
| 445 | - * Initialization | |
| 446 | - */ | |
| 447 | - $(function () { | |
| 448 | - CopyTheCode.init(); | |
| 449 | - }); | |
| 514 | + }; | |
| 450 | 515 | |
| 451 | -})(jQuery); | |
| 516 | + /** | |
| 517 | + * Initialization | |
| 518 | + */ | |
| 519 | + $( | |
| 520 | + function () { | |
| 521 | + CopyTheCode.init(); | |
| 522 | + } | |
| 523 | + ); | |
| 524 | + | |
| 525 | +})( jQuery ); | |