Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code=language
#1
So I just wrote a piece of Javascript code and used [ code=javascript] for it.

However, when I went to quick edit the post, the =javascript was stripped from the code tag.

So I have fixed this:

1. Go to /forum/jscripts/bbcodes_sceditor.js

2. Search for this piece of code:

    JAVASCRIPT-Code:
/******************************
* Update code to support PHP *
******************************/
$.sceditor.plugins.bbcode.bbcode.set('code', {
 allowsEmpty: true,
 tags: {
 code: null
 },
 isInline: false,
 allowedChildren: ['#', '#newline'],
 format: function (element, content) {
 if ($(element[0]).hasClass('phpcodeblock')) {
 return '<div class="codeblock phpcodeblock"><div class="title">PHP Code:</div><div class="body"><div dir="ltr"><code><span style="color: #DD0000">'&nbsp;+&nbsp;content&nbsp;+&nbsp;' </span></code></div></div></div>';
 }
 return '<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>' + content + '</code></div></div>';
 },
 html: '<code>{0}</code>'
});


3. Replace with this piece of code:

    JAVASCRIPT-Code:
/******************************
 * Update code to support PHP *
 ******************************/
$.sceditor.plugins.bbcode.bbcode.set('code', {
	allowsEmpty: true,
	tags: {
		code: null
	},
	isInline: false,
	allowedChildren: ['#', '#newline'],
	/*format: function (element, content) {
		if ($(element[0]).hasClass('phpcodeblock')) {
			return '[php]' + content + '[/php]';
		}
		return '[code]' + content + '[/code]';
	},
	html: '<code>{0}</code>'*/
	/* MangaD - Allow language in code tag. e.g. [code=dc]...[/code] */
	format: function ($element, content) {
		var language,
			attribs   = '',
			element   = $element[0];
		if ($(element).hasClass('phpcodeblock')) {
			return '[php]' + content + '[/php]';
		}
		language = $element.attr('class');
		if (language && /^[\w\-]+$/.test(language)) {
			attribs = '=' + language;
		}
		return '[code' + attribs + ']' + content + '[/code]';
	},
	html: function (token, attrs, content) {
		var	undef, language, match,
			attribs = '',
			escapeEnt = function (unsafe) {
			    return unsafe
				 .replace(/&/g, "&")
				 .replace(/</g, "<")
				 .replace(/>/g, ">")
				 .replace(/"/g, """)
				 .replace(/'/g, "'");
			 };
		language  = attrs.language;
		if (attrs.defaultattr) {
			language  = attrs.defaultattr;
		}
		if (language !== undef && /^[\w\-]+$/.test(language)) {
			/*attribs += ' class="' + escapeEntities(language, true) + '"';//f = escapeEntities*/
			attribs += ' class="' + escapeEnt(language) + '"';
 
			var sc_iframe=document.getElementsByClassName('sceditor-container')[0].getElementsByTagName('iframe')[0];
			sc_iframe = sc_iframe.contentDocument ? sc_iframe.contentDocument : sc_iframe.contentWindow.document;
 
			sc_iframe.styleSheets[0].insertRule('code.' + language + '::before { content:"' + language.toUpperCase() + ' Code:"; }', 0);
		}
		return '<code' + attribs + '>' + content + '</code>';
	}
});


Basically when we add a language to the code tag, it will be added to the class of the html code element of the sceditor iframe and won't be stripped. Works in wysiwyg too.

Edit: Since the parser does not allow special characters in the language (e.g. [code=c++] won't work, but [ code=cpp] works), I copied the parser regex over the editor code. So the =c++ will be stripped, but the =cpp won't.
[Image: random.php?pic=random]
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
The meaning of life is to give life a meaning.
Stop existing. Start living.
Reply
Thanks given by: Som1Lse , Bamboori , Rhino.Freak , A-Man




Users browsing this thread: 1 Guest(s)