Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#44571 new defect (bug)

force_balance_tags breaks JavaScript

Reported by: yellowafterlife's profile yellowafterlife Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.7
Component: Shortcodes Keywords:
Focuses: Cc:



echo force_balance_tags('<script>console.log("extest:", 0<=1);</script>')


<script>console.log("extest:", 0< =1);</script></script>

(no longer valid JavaScript)


I use tiny (<20KB) JS programs for interactive illustrations inside tutorials on a WP blog. These would most commonly break on pages where excerpts are shown (post list, search) and it took a little while to narrow this down to force_balance_tags running for !--more splitter in get_the_content.

Workarounds: enclosing JS code in a comment

<script><!-- code --></script>

which is a legacy part of spec

Could the script-tags be treated like the comments in this regard? This is a rather unobvious behaviour.

Attachments (1)

44571.tests.diff (1.1 KB) - added by soulseekah 6 years ago.
Tests for force_balance_tags

Download all attachments as: .zip

Change History (2)

#1 @soulseekah
6 years ago

This is a very common issue among formatting functions in WordPress, wherein "stray" brackets break tag detection. This happens in scripts, styles and even regular HTML attributes with angle brackets in them. There are a bunch of tickets open regarding this.

The issue here is that <=1);</script> is detected as a tag.

Also there are no unit tests. Let's start writing some.

6 years ago

Tests for force_balance_tags

Note: See TracTickets for help on using tickets.