/**
* @package Sourcerer
* @version 10.0.4
*
* @author Peter van Westen <[email protected]>
* @link https://regularlabs.com
* @copyright Copyright © 2023 Regular Labs All Rights Reserved
* @license GNU General Public License version 2 or later
*/
(function() {
'use strict';
window.RegularLabs = window.RegularLabs || {};
window.RegularLabs.SourcererButton = window.RegularLabs.SourcererButton || {
form : null,
options: {},
setForm: function(form) {
this.form = form;
},
insertText: function(editor_name) {
this.options = Joomla.getOptions ? Joomla.getOptions('rl_sourcerer_button', {}) : Joomla.optionsStorage.rl_sourcerer_button || {};
const tag_word = this.options.syntax_word;
const tag_start = this.options.tag_characters[0];
const tag_end = this.options.tag_characters[1];
const source_editor = Joomla.editors.instances[editor_name];
let pre_php = [];
const codes = [];
const code = this.form.editors['code'].getValue().trim();
if (code.length) {
codes.push(code);
}
let string = codes.join('\n');
// convert to html entities
string = this.htmlentities(string, 'ENT_NOQUOTES');
// replace indentation with tab images
string = this.indent2Images(string);
// replace linebreaks with br tags
string = string.nl2br();
const attributes = [];
if (this.form['raw'].value === '1') {
attributes.push('raw="true"');
}
if (this.form['trim'].value === '1') {
attributes.push('trim="true"');
}
string = this.styleCode(string);
string = tag_start + (tag_word + ' ' + attributes.join(' ')).trim() + tag_end
+ string
+ tag_start + '/' + tag_word + tag_end;
source_editor.replaceSelection(string);
},
joinPrePhp: function(string, pre_php) {
},
preparePrePhp: function(string, pre_php) {
},
styleCode: function(string) {
if ( ! string.length) {
return '';
}
const color_code = '#555555';
const color_php = '#0000cc';
const color_tags = '#117700';
const color_plugin_tags = '#770088';
if (this.options.color_code) {
// Style entire php block
string = string.replace(
/<\?php(.*?)\?>/gim,
`<span style="color:${color_php};">[-php-start-]$1[-php-end-]</span>`
);
// Style tags
string = string.replace(
/(<\/?[a-z][a-z0-9-_]*( .*?)?>)/gim,
`<span style="color:${color_tags};">$1</span>`
);
// Style plugin tags
string = string.replace(
/({\/?[a-z].*?})/gim,
`<span style="color:${color_plugin_tags};">$1</span>`
);
// Remove temporary php start/end tags
string = string.replace(
/\[-php-start-\]/gim,
'<?php'
).replace(
/\[-php-end-\]/gim,
'?>'
);
}
string = `<span style="font-family:monospace;color:${color_code};">${string}</span>`;
return string;
},
removeSourceTags: function(string) {
const tag_word = this.options.syntax_word;
const tag_start = this.options.tag_characters[0];
const tag_end = this.options.tag_characters[1];
let start_tag = this.preg_quote(tag_start + tag_word) + '.*?' + this.preg_quote(tag_end);
let end_tag = this.preg_quote(tag_start + '/' + tag_word + tag_end);
let regex = new RegExp('(' + start_tag + ')\\s*', 'gim');
if (string.match(regex)) {
string = string.replace(regex, '');
}
regex = new RegExp('\\s*' + end_tag, 'gim');
string = string.replace(regex, '');
return string.trim();
},
preg_quote: function(str) {
return (str + '').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!<>\|\:])/g, '\\$1');
},
indent2Images: function(string) {
const regex = new RegExp('((^|\n)( |\t)*)( ? ?|\t)', 'gm');
while (regex.test(string)) {
string = string.replace(regex, '$1<img src="' + this.options.root + '/media/sourcerer/images/tab.svg">');
}
return string.replace(regex, '<br>');
},
htmlentities: function(string, quote_style) {
let tmp_str = string.toString();
const histogram = this.get_html_translation_table('HTML_ENTITIES', quote_style);
if (histogram === false) {
return false;
}
for (let symbol in histogram) {
const entity = histogram[symbol];
tmp_str = tmp_str.split(symbol).join(entity);
}
return tmp_str;
},
get_html_translation_table: function(table, quote_style) {
var entities = {}, histogram = {}, decimal = 0, symbol = '';
var constMappingTable = {}, constMappingQuoteStyle = {};
var useTable = {}, useQuoteStyle = {};
// Translate arguments
constMappingTable[0] = 'HTML_SPECIALCHARS';
constMappingTable[1] = 'HTML_ENTITIES';
constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
constMappingQuoteStyle[2] = 'ENT_COMPAT';
constMappingQuoteStyle[3] = 'ENT_QUOTES';
useTable = ! isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
useQuoteStyle = ! isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
throw Error('Table: ' + useTable + ' not supported');
// return false;
}
// ascii decimals for better compatibility
entities['38'] = '&';
if (useQuoteStyle !== 'ENT_NOQUOTES') {
entities['34'] = '"';
}
if (useQuoteStyle === 'ENT_QUOTES') {
entities['39'] = ''';
}
entities['60'] = '<';
entities['62'] = '>';
if (useTable === 'HTML_ENTITIES') {
entities['160'] = ' ';
entities['161'] = '¡';
entities['162'] = '¢';
entities['163'] = '£';
entities['164'] = '¤';
entities['165'] = '¥';
entities['166'] = '¦';
entities['167'] = '§';
entities['168'] = '¨';
entities['169'] = '©';
entities['170'] = 'ª';
entities['171'] = '«';
entities['172'] = '¬';
entities['173'] = '­';
entities['174'] = '®';
entities['175'] = '¯';
entities['176'] = '°';
entities['177'] = '±';
entities['178'] = '²';
entities['179'] = '³';
entities['180'] = '´';
entities['181'] = 'µ';
entities['182'] = '¶';
entities['183'] = '·';
entities['184'] = '¸';
entities['185'] = '¹';
entities['186'] = 'º';
entities['187'] = '»';
entities['188'] = '¼';
entities['189'] = '½';
entities['190'] = '¾';
entities['191'] = '¿';
entities['192'] = 'À';
entities['193'] = 'Á';
entities['194'] = 'Â';
entities['195'] = 'Ã';
entities['196'] = 'Ä';
entities['197'] = 'Å';
entities['198'] = 'Æ';
entities['199'] = 'Ç';
entities['200'] = 'È';
entities['201'] = 'É';
entities['202'] = 'Ê';
entities['203'] = 'Ë';
entities['204'] = 'Ì';
entities['205'] = 'Í';
entities['206'] = 'Î';
entities['207'] = 'Ï';
entities['208'] = 'Ð';
entities['209'] = 'Ñ';
entities['210'] = 'Ò';
entities['211'] = 'Ó';
entities['212'] = 'Ô';
entities['213'] = 'Õ';
entities['214'] = 'Ö';
entities['215'] = '×';
entities['216'] = 'Ø';
entities['217'] = 'Ù';
entities['218'] = 'Ú';
entities['219'] = 'Û';
entities['220'] = 'Ü';
entities['221'] = 'Ý';
entities['222'] = 'Þ';
entities['223'] = 'ß';
entities['224'] = 'à';
entities['225'] = 'á';
entities['226'] = 'â';
entities['227'] = 'ã';
entities['228'] = 'ä';
entities['229'] = 'å';
entities['230'] = 'æ';
entities['231'] = 'ç';
entities['232'] = 'è';
entities['233'] = 'é';
entities['234'] = 'ê';
entities['235'] = 'ë';
entities['236'] = 'ì';
entities['237'] = 'í';
entities['238'] = 'î';
entities['239'] = 'ï';
entities['240'] = 'ð';
entities['241'] = 'ñ';
entities['242'] = 'ò';
entities['243'] = 'ó';
entities['244'] = 'ô';
entities['245'] = 'õ';
entities['246'] = 'ö';
entities['247'] = '÷';
entities['248'] = 'ø';
entities['249'] = 'ù';
entities['250'] = 'ú';
entities['251'] = 'û';
entities['252'] = 'ü';
entities['253'] = 'ý';
entities['254'] = 'þ';
entities['255'] = 'ÿ';
}
// ascii decimals to real symbols
for (decimal in entities) {
symbol = String.fromCharCode(decimal);
histogram[symbol] = entities[decimal];
}
return histogram;
},
};
String.prototype.escapeQuotes = function() {
return this.replace(/'/g, '\\\'');
};
String.prototype.hasLineBreaks = function() {
const regex = new RegExp('\n', 'gm');
return regex.test(this);
};
String.prototype.indent = function() {
const regex = new RegExp('\n', 'gm');
return '\n ' + this.replace(regex, '\n ') + '\n';
};
String.prototype.nl2br = function() {
const regex = new RegExp('\n', 'gm');
return this.replace(regex, '<br>');
};
})();