WordPress.org

Make WordPress Core

Opened 11 days ago

Last modified 8 days ago

#44571 new defect (bug)

force_balance_tags breaks JavaScript

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

Description

Input:

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

Output:

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

(no longer valid JavaScript)

Context:

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 https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements

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 8 days ago.
Tests for force_balance_tags

Download all attachments as: .zip

Change History (2)

#1 @soulseekah
8 days 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.

@soulseekah
8 days ago

Tests for force_balance_tags

Note: See TracTickets for help on using tickets.