Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

zygmoont on "Comment post - different Firefox and Chrome results"

$
0
0

Hello everybody,
I do not often post questions regarding programming. I always believed that 99% of things that I need are already on the internet (mostly SO and WP.org) and the remaining 1% should be easy enough to do it on my own.

Unfortunately, this time I gave up - I have no idea what is going on here. I am currently working on localhost.

Here is what I do with comments posted on my blog:
1. I add filter to 'preprocess_comment' to get the 'comment_content'.
2. I strip all html tags. I don't want users to use any html.
3. I convert BBCode tags to HTML (I have already filtered out the user-input HTML tags in step 2, so these won't be stripped).
4. I return the whole 'preprocess_comment' (after modifications to 'comment_content').

And below is how I do it. I used code I found on the web - to strip HTML and to convert BBCode. I convert these BBcode tags: [b][/b], [i][/i], [u][/u], [p][/p] (to < p >< /p >), [quote][/quote] (to < blockquote >) and [url][/url] (to < a > with blank and nofollow). The code is placed in theme's functions.php.

function bbcodeToHtml($text) {
	// BBcode array
	$find = array(
		'~\[b\](.*?)\[/b\]~s',
		'~\[i\](.*?)\[/i\]~s',
		'~\[u\](.*?)\[/u\]~s',
		'~\[p\](.*?)\[/p\]~s',
		'~\[quote\](.*?)\[/quote\]~s',
		'~\[url\]((?:ftp|https?)://.*?)\[/url\]~s',
	);
	// HTML tags to replace BBcode
	$replace = array(
		'<strong>$1</strong>',
		'<span style="font-style:italic;">$1</span>',
		'<span style="text-decoration:underline;">$1</span>',
		'<p>$1</p>',
		'<blockquote>$1</'.'blockquote>',
		'<a href="$1" target="_blank" rel="nofollow">$1</a>',
	);
	// Replacing the BBcodes with corresponding HTML tags
	return preg_replace($find,$replace,$text);
}

function plc_comment_post( $incoming_comment ) {
	// convert everything in a comment to display literally
	$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
	// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
	$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
	$incoming_comment['comment_content'] = bbcodeToHtml($incoming_comment['comment_content']);
	return( $incoming_comment );
}

add_filter( 'preprocess_comment', 'plc_comment_post', '', 1 );

I believe it is all technically correct. And it works perfectly. Except it only works in Chrome. In Firefox and Opera it doesn't work the same way. I can't currently test on IE or Safari. I am currently testing it on Windows.

What is the problem? First of all, the [url] tag - while it does what it should be doing when posting a comment from Chrome, while posting from FF the result is different. Here is what I am posting:
[url]http://google.com[/url]
When posting from Chrome it becomes:
<a href="http://example.com" target="_blank" rel="nofollow">http://example.com</a>
Which is good. When posting from FF or Opera it becomes:
<a href="http://google.com" rel="nofollow">http://google.com</a>
Which is wrong because it stripped target atribute.
It all depends on the browser that the comment has been posted from. So if I post from Firefox it is always wrong - no matter if I check the output in Chrome or Firefox. If I post from Chrome - it is always working. So I can actually see in Firefox what the output of certain comment should be (after it has been posted in Chrome). But then I post using Friefox the exact same comment I have posted earlier from Chrome and it's broken again.
How is this possible? Does FF and Opera strip HTML attirbute from the string already processed on server?!

Another example I discovered. I am posting:
[quote][p][b]John Doe[/b] write:[/p][p]Something something something[/p][/quote]
When I post it from Chrome I get:
<blockquote><p><strong>John Doe</strong> wrote:</p><p>Something something something</p></blockquote>
Which is good. When I post from Firefox or Opera the result is:
<blockquote><p><strong>John Doe</strong> wrote:Something something something</p></blockquote>
Which is obviously not good. Where did the middle </p><p> go?

I don't understand it. It would have been easier to diagnose if it wasn't working at all. But it is working perfectly when posting from Chrome and not so perfectly when posting from FF or Opera. I am truly amazed that this is possible - server gets the [url]http://example.com[/url] but instead of converting it to http://example.com, which is written in code, it converts it to http://example.com, but the conversion is wrong only when posted from FF or Opera. And also the <p> problem.

How is that even possible? It shouldn't depend on browser, the browser just posts the data to the server. Once the data get to the server, it shouldn't behave differently depending on the browser that posted data.

Can someone tell me how is that possible? I don't even know what to search for - I googled "firefox chrome post different" and similar but no results. I don't even understand what's going on here so it's difficult to look for.

Thank you for your time.

Daniel


Viewing all articles
Browse latest Browse all 8245

Trending Articles