Ticket #2059 (closed defect: fixed)

Opened 3 years ago

Last modified 1 year ago

Slashes stripped in <pre> (caused by #1793)

Reported by: allan Assigned to: anonymous
Priority: normal Milestone: 2.3
Component: General Version: 1.5.2
Severity: normal Keywords: has-patch dev-feedback
Cc:

Description

Using a backslash in <pre> will cause it to be stripped, e.g.:

<pre>printf("test\n");</pre>

results in (after the wpautop filter):

<pre>printf("testn")</pre>

Attachments

wpautop_no_removal_of_slashes_in_pre.patch (1.1 kB) - added by allan on 12/11/05 18:27:19.
Patch to fix the problem
formatting.php.patch (0.8 kB) - added by DelGurth on 09/07/07 18:12:52.
Patch (for wordpress HEAD) to fix the removal of slashes in <pre> tags
2059.diff (1.1 kB) - added by mdawaffe on 09/13/07 01:44:05.
uses preg_replace_callback() and modifies clean_pre()

Change History

12/11/05 18:27:19 changed by allan

  • attachment wpautop_no_removal_of_slashes_in_pre.patch added.

Patch to fix the problem

08/07/06 08:38:48 changed by plaes

  • version changed from 1.5.2 to 2.0.4.

This is still present in 2.0.4

The patch submitted earlier by Allan fixes the problem :)

08/07/06 08:45:32 changed by error

  • keywords set to bg|has-patch bg|dev-feedback.
  • version changed from 2.0.4 to 1.5.2.
  • milestone set to 2.1.

11/29/06 23:11:33 changed by matt

  • milestone changed from 2.1 to 2.2.

03/27/07 20:08:20 changed by foolswisdom

  • keywords changed from bg|has-patch bg|dev-feedback to has-patch dev-feedback.
  • milestone changed from 2.2 to 2.3.

08/31/07 16:36:06 changed by mdawaffe

Messing with stripslashes is the wrong approach.

A good rule of thumb, never use preg_replace(/e); the slashing is too complicated. Use preg_replace_callback() instead.

(follow-up: ↓ 7 ) 08/31/07 20:21:26 changed by Albertane

What is the right approach? I'd really like to get this fixed, it makes it a pain to try and post code snippets to my blog. Does the attached patch still work on 2.2? Is it going to cause other problems? Thanks.

(in reply to: ↑ 6 ) 09/07/07 17:53:22 changed by DelGurth

Replying to Albertane:

What is the right approach? I'd really like to get this fixed, it makes it a pain to try and post code snippets to my blog. Does the attached patch still work on 2.2? Is it going to cause other problems? Thanks.


The right approach would be something like (at least, this code seems to work for me):

$pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', create_function('$matches', 'return $matches[1] . clean_pre($matches[2]) . "</pre>";'), $pee);

Replying to mdawaffe:

Messing with stripslashes is the wrong approach. A good rule of thumb, never use preg_replace(/e); the slashing is too complicated. Use preg_replace_callback() instead.

It can be a good rule of thumb, but it was in there already. So why not use it? But true, it sounds like you can better use preg_replace_callback then just the /e variant, since you can trap yourself in some nice problems (see the PHP documentation about it(1)).
But the stripslashes should not be needed in the first place. At least, according to me slashes should be allowed within a <pre> tag.



I was personally wondering why the stripslashes was there in the first place. But I guess that has something to do with a PHP bug(2) I found when I was looking why I was loosing slashes in wordpress within my <pre> tags.

(1) http://www.php.net/preg_replace
(2) http://bugs.php.net/bug.php?id=10666

09/07/07 18:12:52 changed by DelGurth

  • attachment formatting.php.patch added.

Patch (for wordpress HEAD) to fix the removal of slashes in <pre> tags

(follow-up: ↓ 10 ) 09/13/07 01:43:36 changed by mdawaffe

Since wpautop can be called dozens of times per page loead, we should probably not use create_function().

I suggest preg_replace_callback( regex, 'clean_pre' ) and modifying clean_pre() to accept ether the regex match array or a string of text for backward compatibility.

Attached is a patch which does this based on DelGurth?'s create_function() code.

09/13/07 01:44:05 changed by mdawaffe

  • attachment 2059.diff added.

uses preg_replace_callback() and modifies clean_pre()

09/13/07 04:11:21 changed by ryan

  • status changed from new to closed.
  • resolution set to fixed.

(In [6102]) Don't strip slashes from pre. Props DelGurth? and mdawaffe. fixes #2059

(in reply to: ↑ 8 ) 09/21/07 17:52:40 changed by DelGurth

Replying to mdawaffe:

Since wpautop can be called dozens of times per page loead, we should probably not use create_function().

Sounds like a wise idea. I'm new to wordpress and with the first real item I wanted to post I encountered this bug, so I was kinda in a hurry to create a fix. I was not sure if I could alter the clean_pre() function, but if I had used your method I should not have worried about it.

Thanks for using my input to kill the bug.