3

I have some code running with jQuery 1.7.2 I encountered an error when trying to find using this selector

var x = "span:contains(\"C) Foo (Bar)\")";
$('body').find(x)

https://jsfiddle.net/elewinso/dfbn82wo/7/

This issue is fixed in JQuery 1.8 and up but since I cannot switch my jQuery I was hoping to find a patch.

elewinso
  • 2,453
  • 5
  • 22
  • 27
  • What are you trying to match at HTML? See https://stackoverflow.com/help/mcve – guest271314 Oct 23 '17 at 15:08
  • what is the error you are getting? – Sanchit Patiyal Oct 23 '17 at 15:09
  • 1
    @guest271314 it is not a selector here, his problem comes from the round brackets, he is trying to select a span that contains the text `"C) Foo (Bar)"` – Kaddath Oct 23 '17 at 15:11
  • 1
    @Kaddath No `` element exists at linked jsfiddle – guest271314 Oct 23 '17 at 15:19
  • Why can you not update jQuery version from 1.7.2? – guest271314 Oct 23 '17 at 15:30
  • @guest271314 the span is not needed to show the error, i guess that's a real minimal example ;) – Kaddath Oct 23 '17 at 15:33
  • @Kaddath `:contains()` checks `.textContent` or `.innerText` using `.indexOf()`, OP can use `.filter()` and `.indexOf()`. What would be the purpose of trying to patch jQuery version 1.7.2? The premise of not being able to update needs clarification; why? If we can adjust jQuery source code for `:contains()` we can adjust the entire jQuery script – guest271314 Oct 23 '17 at 15:37
  • @guest271314 well i am not the OP, either ask him, or write an answer if you know how to do it with these methods. I have not much time right now.. – Kaddath Oct 23 '17 at 15:56
  • @Kaddath The answer is to update to a newer version of jQuery, or not use jQuery at all for the procedure, or use `.filter()`, as demonstrated at Answer https://jsfiddle.net/dfbn82wo/9/ – guest271314 Oct 23 '17 at 16:00
  • @elewinso What is the requirement? – guest271314 Oct 23 '17 at 16:06
  • @guest271314 I am working on a legacy system. I cannot change the code base that calls find. My only real option is to try and patch sizzle as I cannot upgrade the entire Jquery either. – elewinso Oct 26 '17 at 06:29
  • @elewinso _"I cannot change the code base that calls find. My only real option is to try and patch sizzle as I cannot upgrade the entire Jquery either."_ See https://stackoverflow.com/help/self-answer – guest271314 Oct 26 '17 at 07:14

1 Answers1

2

The problem is your selector is confusing jQuery's parser because it contains brackets. It thinks the ) you are trying to match is the closing bracket of its contains: content filter.

You can get around this by using filter() instead.

$('span').filter(function() {
    return $(this).text().indexOf('"C) Foo (Bar)"') != -1;
});
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • I am afraid I cannot change the JS code. my best bet is to be able to patch sizzle without updating the entire JQuery. – elewinso Oct 26 '17 at 06:30
  • It's tough to see how you could allow for your current selector in Sizzle. It will always interpret the closing bracket as part of selector syntax. Stopping it doing that could have wide-ranging consequences. – Mitya Oct 26 '17 at 12:01