Opened 7 years ago

Last modified 8 months ago

#2833 reopened defect (bug)

wpautop breaks style and script tags

Reported by: Nazgul Owned by: anonymous
Priority: low Milestone: Future Release
Component: Formatting Version: 2.0.3
Severity: normal Keywords: needs-patch needs-unit-tests
Cc:

Description

When I create a post in which I want to include Javascript or some styles, WordPress 'breaks'when showing those posts, because all newlines in the SCRIPT and STYLE tag are converted into BR tags.

Example:

<style type="text/css>
.matt { color: #FFFFFF; }
</style>

Becomes:

<style type="text/css><br />
.matt { color: #FFFFFF; }<br />
</style><br />

And:

<script type="text/javascript"><!--
google_ad_client = "xxxxxxxx";
google_ad_width = 120;
google_ad_height = 60;
google_ad_format = "120x60_as_rimg";
google_cpa_choice = "CAAQ2eOZzgEaCD4zuVkdzt_CKI-293M";
//--></script>

Becomes

<script type="text/javascript"><!--<br />
google_ad_client = "xxxxxxxx";<br />
google_ad_width = 120;<br />
google_ad_height = 60;<br />
google_ad_format = "120x60_as_rimg";<br />
google_cpa_choice = "CAAQ2eOZzgEaCD4zuVkdzt_CKI-293M";<br />
//--></script><br />

This happens because wpautop adds those BR tags to the post. (As it should, just not within STYLE or SCRIPT tags.)

I've made a (temporary?) workaround for this by creating a pre and post event for wpautop, which substitute the necessary newlines by a temporary value in the pre event and placing them back in the post event. Although I think this should be incorporated in wpautop itself.

See also: http://wordpress.org/support/topic/76433 and http://wordpress.org/support/topic/76297

While searching trac I also found ticket #2346, which is about the same problem, but which was for 2.0 and self-closed by the submitter?

P.S. I have TinyMCE turned of.

Attachments (2)

2833.diff (1.4 KB) - added by Nazgul 7 years ago.
2833b.diff (1.4 KB) - added by Nazgul 7 years ago.

Download all attachments as: .zip

Change History (19)

Nazgul7 years ago

  • Keywords bg|has-patch bg|needs-testing added

This change to wpautop should fix the aforementioned behaviour.
I've tested it on my box, but would like to get some feedback.

Just looking at your code... the brackets on line 74 look like a mistake to me. As is, it will also match tags like p, pre, tr, li. Once you've dropped the brackets it should be fine.

Nazgul7 years ago

You are correct. An updated patch, without the brackets, has been uploaded.

comment:4   ryan7 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [3965]) Preserve newlines inside style and script tags. Props Nazgul. fixes #2833

  • Resolution fixed deleted
  • Status changed from closed to reopened

In 2.7 JavaScript is still damaged by wpautop if it contains empty lines:

  test
  
  <script language="javascript">

    alert( 'test' );

  </script>
 
  test

will be changed to

  <p>test</p>
  <p><script language="javascript"></p>
  <p>  alert( 'test' );</p>
  <p></script></p>
  <p>test</p>

It seems that there is already a fix within this plugin that maybe could be used: http://www.automateyourbusiness.com/updates/2007/07/18/javascript-in-wordpress-posts/

  • Milestone changed from 2.1 to 2.8

confirmed in wp 2.8. and switching back and forth with tinymce breaks it to:

<p><script language="javascript"><!-- alert( 'test' ); // --></script></p>

  • Cc ShaneF added
  • Keywords bg bg needs-patch needs-testing added; bg|has-patch bg|needs-testing removed

After several attempts it seems this has yet to be fixed. The current patch was commit a while back but it still doesn't resolve spaced style or javascript code.

  • Cc ShaneF removed
  • Component changed from General to Formatting

I've had a similar issue on one of my plugins, at one point. (As in, things got processed when they really shouldn't.)

To fix, I went something like:

  1. Replace entire area with a unique place holder
  2. Process everything
  3. Replace the unique place holder with its original content

In case it prompts ideas... It would then also fix the SVG problem: #9437

  • Keywords wpautop break script style br bg bg needs-testing removed
  • Milestone changed from 2.8 to Future Release
  • Milestone changed from Future Release to 2.9

Please close as wontfix. Fixing things will break other things and vice-versa. I do not know a single seriuos developer that wants to touch this.

Best would be to get a decription first what must (not)/should (not)/can (not) be done by wpautop. Then testcases must be written and the the function must be re-worked.

  • Keywords needs-unit-tests added
  • Milestone changed from 2.9 to Future Release

Here is what presently happens in trunk:

<p>test</p>
<p><script type="text/javascript" language="javascript">// <![CDATA[
alert( 'test' );
// ]]&gt;</script></p>
<p>test</p>

So seems like the only remaining issue for *this ticket* is incorrectly <p>'ing the inline script

Note: See TracTickets for help on using tickets.