Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#36473 closed defect (bug) (fixed)

JavaScript error in theme customizer a specific value of before_widget

Reported by: martinkrcho's profile martin.krcho Owned by: westonruter's profile westonruter
Milestone: 4.6 Priority: normal
Severity: normal Version: 3.9
Component: Customize Keywords: good-first-bug has-patch
Focuses: ui Cc:


I just came across an issue with the theme customizer. It is failing with the following JavaScript error

Uncaught Error: Syntax error, unrecognized expression: SECTION.widget.widget-%2$s,SECTION.widget.widget-%2$s,SECTION.widget.widget-%2$s,SECTION.widget.widget-%2$s,SECTION.widget.widget-%2$s,SECTION.widget.widget-%2$s

I tracked this issue down to the function buildWidgetSelectors in customize-preview-widgets.min.js, specififcally to this bit of code:

d.before_widget.replace("%1$s", "").replace("%2$s", "")

I am using theme called Bearded that defines the "before_widget" as follows:

<section id="%1$s" class="widget %2$s widget-%2$s">

A possible fix would be to make both of the replace actions mentioned above replace ALL occurencies of the pattern:

d.before_widget.replace(/%1\$s/g, "").replace(/%2\$s/g, "")

Attachments (1)

36473.diff (949 bytes) - added by rsusanto 8 years ago.

Download all attachments as: .zip

Change History (8)

#1 @westonruter
8 years ago

  • Version changed from 4.4.2 to 3.9

The code for buildWidgetSelectors has been in place since 3.9.

@martin.krcho could you please share the register_sidebar() calls that the theme has?

You're right that a problem here is that String.replace() will only replace the first instance if the replacement arg is a string instead of a regular expression. So changing the pattern it to be a regular expression instead of string should do it, but I want to see how the sidebars are registered to see what is going on.

#3 @westonruter
8 years ago

  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to Future Release

Thanks. OK, that confirms it. It registers a sidebar with:

'before_widget' => '<section id="%1$s" class="widget %2$s widget-%2$s">',

Only the first instance of %2$s is getting replaced, so implementing the regex global replacement will do the trick.

#4 @westonruter
8 years ago

  • Milestone changed from Future Release to 4.6

8 years ago

#5 @rsusanto
8 years ago

  • Keywords has-patch added; needs-patch removed

#6 @westonruter
8 years ago

  • Owner set to westonruter
  • Status changed from new to reviewing

#7 @westonruter
8 years ago

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

In 37322:

Customize: Remove format placeholders from widget templates and selectors, fixing a jQuery selector syntax error and the broken highlight/shift-click behaviors.

The issues occur in themes that register sidebars that reference a single format placeholder (%1$s and %2$s) multiple times, such as in the id and class attributes for $before_widget.

Props martin.krcho, westonruter.
Fixes #36473.

Note: See TracTickets for help on using tickets.