Make WordPress Core

Opened 21 months ago

Closed 21 months ago

Last modified 20 months ago

#62025 closed defect (bug) (duplicate)

Unable to use wp_kses* with JavaScript templating

Reported by: jernstjernst's profile jernstjernst Owned by:
Milestone: Priority: normal
Severity: normal Version: 1.0
Component: Formatting Keywords:
Focuses: Cc:

Description

We're trying to use wp_kses* with our templates that use JavaScript templating (e.g. https://underscorejs.org/#template, https://ejs.co/) syntax, however to no avail.

Example 1 (<%=):

<?php
$html = '<script type="text/javascript"><%= data.answer %></script>';
echo wp_kses($html, ['script' => ['type' => true], '%' => []]);
?>

Expected result:

<script type="text/javascript"><%= data.answer %></script>

Actual result:

<script type="text/javascript"></script>

Example 2 (<%):

<?php
$html = '<script type="text/javascript"><% print('Hello'); %></script>';
echo wp_kses($html, ['script' => ['type' => true], '%' => []]);
?>

Expected result:

<script type="text/javascript"><% print('Hello'); %></script>

Actual result:

<script type="text/javascript"></script>

Change History (3)

#1 @swissspidy
21 months ago

  • Focuses coding-standards removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version changed from trunk to 1.0

This is technically working as intended, as KSES is supposed to strip invalid/unsupported HTML markup. It is not meant to be used on things like Underscore templates.

See #30920 where this was previously discussed.

#2 @jernstjernst
20 months ago

Thanks @swissspidy and sorry I was not able to find this #30920.

In that case what is the preferred way to handle outputting underscore templates from plugins as we are not supposed to use echo?

#3 @swissspidy
20 months ago

IMO echo is a legitimate use case for such scenarios. You could also use wp_print_inline_script_tag

Note: See TracTickets for help on using tickets.