WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#40049 new defect (bug)

Javascript For-Loop Inside Shortcode (PHP) Breaks On Output

Reported by: Kelderic Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:
PR Number:

Description

If I have a shortcode that outputs inline Javascript, placing a greater than arrow < will cause the following code to break.

Example:

add_shortcode('output-javascript', function() {

    $output = "

        <script>

            function getData() {


                for ( var i = 0; i < 5; i++ ) {

                    // run logic

                }

            }

            getData();

            console.log('<b>Text</b><br/>Second Line of text');

        </script>

    ";

    return $output;

});

If you place this as a shortcut, it would be expected that the contents of $output are displayed on the page. However, the closing </b> tag is stripped out. What is written to the page is this:

        <script>

            function getData() {


                for ( var i = 0; i < 5; i++ ) {

                    // run logic

                }

            }

            getData();

            console.log('<b>Text<br/>Second Line of text');

        </script>

});

Change History (1)

#1 @Kelderic
3 years ago

As a follow up, what causes the breakage is the < inside the For-Loop. Javascript that doesn't contain < will work perfectly.

Note: See TracTickets for help on using tickets.