Skip to content

Commit

Permalink
Generalize escaping of selector to include other chars that have spec…
Browse files Browse the repository at this point in the history
…ial meaning to jQuery
  • Loading branch information
jon-ide committed Sep 30, 2024
1 parent db4b895 commit 03a792a
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions webapp/views/data_tables/templates/clone_attributes_4.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@
select_default(this.id);
});
}
function escapeSelector(selector) {
// There are a few characters that need to be escaped in a jQuery selector so they aren't interpreted as
// class selectors and the like. This function escapes those characters.
return selector.replace(/([!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~])/g, '\\$1');
}
function select_default(_select_id) {
// We need to replace spaces with underscores in the SELECT id for jQuery to work properly
let select_id = _select_id.replace(/ /g, '_');
// Get rid of SELECT__ and __SELECT substrings
let default_val = select_id.substring(8, select_id.length - 8);
// jQuery interprets '.' as a class selector, so we need to escape '.' with '\\'
// It's a double backslash because the first backslash is an escape character in a string. I.e., we're
// saying we want the backslash to go into the string, not be interpreted as an escape character.
// Now the selector will correctly find the option with the value that has a period in it.
select_id = select_id.replace(/\./g, '\\.');
// jQuery interprets '.' as a class selector, so we need to escape '.' with '\\'.
// There are various other chars that may be similarly problematic. We'll escape them all.
select_id = escapeSelector(select_id);
let selector = '#' + select_id + ' option';
$(selector).filter(function() {
let this_text = $(this).text().replace(/ /g, '_');
Expand Down

0 comments on commit 03a792a

Please sign in to comment.