Certain URL fragments cause javascript error

Created on 17 December 2014, over 9 years ago
Updated 18 June 2024, 8 days ago

Problem/Motivation

When a page contains a collapsible fieldset element, such that $('fieldset.collapsible') contains elements, and a fragment with special characters is in the url, such as http://mysite.com/#badfragment=broken, a javascript error will be thrown: Uncaught Error: Syntax error, unrecognized expression: .error, #badfragment=broken.

This issue happens in collapse.js near line 63:

var anchor = location.hash && location.hash != '#' ? ', ' + location.hash : '';
if ($fieldset.find('.error' + anchor).length) {
  $fieldset.removeClass('collapsed');
}

Notice that the anchor is being directly pulled from the URL and used in the selector. However, '.error' + anchor will not yield a valid selector, resulting in an error being thrown (breaking all JS on the page).

Steps to reproduce

Proposed resolution

Not sure the best way to fix this. Ideally, the fragment for collapsed fieldsets would be namespaced in the URL so that it wasn't pulling ANYTHING that is a fragment. So perhaps have it look for #collapsed=NAME_OF_ELEMENT.
A simpler/quicker fix would be to check if the selector is valid, or do some regex replacement on special (invalid selector) characters.

Remaining tasks

Add tests
Issue Summary update
Review

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Javascriptย  โ†’

Last updated about 8 hours ago

  • Maintained by
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom @justafish
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance @nod_
Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States donutdan4114

Live updates comments and jobs are added and updated live.
  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024