MyBB Community Forums

Full Version: Spaces inside code blocks
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all! First post here.

I'm not sure if this is the proper place to post this, any moderator can feel free to move it if necessary.

I was having trouble with some z80 assembly code I was posting, because the forum would eat all tabulation spaces in the first line, so I checked the forum software name, came here, downloaded the source code, and looked for the way to fix it.

I suggested the webmaster to replace this line in
file class_parser.php (line 748 at version 1.6.09), in function mycode_parse_code:

$code = preg_replace('#^(\t*)(\n|\r|\0|\x0B| )*#', '\\1', $code);

with this one:

$code = preg_replace('#^(\t*)(\n|\r|\0|\x0B|)*#', '\\1', $code);

and it did fix the issue! Big Grin

But testing if it worked, we found that if there's only one space, it doesn't show. This can be fixed adding in the same function, before the return line, the next line:

$code = str_replace("\n ", "\n ", $code);

This replaces the case when there's only one space after a newline with a   that always get shown by browsers.


Here's the problem in this very forum:

     5 spaces before text
 1 space before text

And an example after both patches have been applied (in spanish):
http://retrolandia.net/foro/showthread.p...224#pid224
This regular expression is weird in general.

Your change fixes a symptom, not the cause.
(2012-12-19, 08:25 PM)frostschutz Wrote: [ -> ]This regular expression is weird in general.

Your change fixes a symptom, not the cause.

I'm not sure what you mean by that. I'm no expert (at all) at regular expressions, but from what I see, this line:

$code = preg_replace('#^(\t*)(\n|\r|\0|\x0B| )*#', '\\1', $code);

is meant to trim several elements from the beginning of the code block content: newlines, tabs, null elements... and spaces too, but I don't know if that's a bug or is a desired effect. But I do know that this line is the direct cause of my problem.

I for sure don't want spaces to be removed because it may screw up the code identation (and maybe tabulations should be allowed too). I'm now thinking I also should have removed the vertical bar next to the space from the expression in my fix. But it doesn't seem to break anything! Toungue

Should I report this as a bug?


The other effect is a side effect of not using <pre> to format the code blocks. In HTML spaces after a newline are ignored (unless they're encoded as &nbsp;

When you have more than one space, this line ensures that they show correctly:

$code = str_replace("  ", '&nbsp;&nbsp;', $code);

because many spaces are encoded as pairs of &nbsp; , followed by a single space character if the number of spaces was odd. That single space doesn't follow a newline, so it's never ignored.

Replacing all spaces with &nbsp; (instead of just double spaces) would also fix the problem, but the output would be unnecessarely too big. My solution is therefore neater.

Again, should I report it as a bug?
(2012-12-20, 06:03 PM)Metalbrain Wrote: [ -> ]Again, should I report it as a bug?

I would consider it a bug, but who knows what the MyBB dev team would make of it.

Feel free to report it along with those testcase codeblocks of yours.

Worst case it will be rejected.

I haven't looked at the context of that code myself but it feels like it should be something like
$code = rtrim(ltrim($code, "\n\r\0\x0B"));
so it would trim newlines at the beginning of the code (which happen due to writing it as [ code ] newline actual code starts here newline [/code] and any whitespace at the end of it.

As for your &nbsp; issue, if not using <pre> (why not anyway? you can use html inside pre to color stuff) it would suffice to replace every other space with &nbsp; so you could replace ' ' (two spaces) with ' &nbsp;' (space followed by &nbsp; ) thus cutting down the number of &nbsp; by half. Not that it makes a real difference when HTML transfer is gzipped anyway.

My forum doesn't use php/code tags at all so I never had to tackle these myself.