Bug Tracker

Ticket #2147 (closed enhancement: invalid)

Opened 7 years ago

Last modified 3 years ago

need a selector to match full string

Reported by: G_Gus Owned by:
Priority: minor Milestone: 1.2.2
Component: core Version: 1.2.1
Keywords: contains selector text Cc:
Blocking: Blocked by:

Description

:contains() is the very only selector that operates on text.

Here's the actual jQuery code:

// Text Check
contains: "(a.textContent||a.innerText||jQuery(a).text()||'').indexOf(m[3])>=0",

the method for matching text is .indexOf() , so it returns true if the text is contained, with no option for exact matching.

I think that another selector that operates on text is needed. I wrote a simple exact matching selector:

  equals: "(a.textContent||a.innerText||jQuery(a).text()||'')==m[3]", 

it does make use of == operand instead of .indexOf() method

What do you think about it?

Attachments

testcase.html Download (2.0 KB) - added by G_Gus 7 years ago.
testcase and a semi-real case

Change History

Changed 7 years ago by G_Gus

testcase and a semi-real case

comment:1 Changed 4 years ago by dmethvin

  • Summary changed from need for a selector to match full string, as :contians() selector match only substring to need a selector to match full string

Because of differences in the way browsers treat whitespace (newlines in particular) when they parse HTML, a pseudo like :exactly(str) would not work consistently cross-browser. There is a ticket elsewhere to resolve the whitespace issues in .text() but that would require eliminating the .innerText/.textContent optimization.

comment:2 Changed 4 years ago by addyosmani

  • Keywords needsreview added

comment:3 Changed 4 years ago by ajpiano

People can use .filter() if they want an exact text match, IMO.

comment:4 Changed 4 years ago by dmethvin

  • Keywords needsreview removed
  • Status changed from new to closed
  • Resolution set to invalid

We're not encouraging additions to the selector syntax because non-standard selectors prevent the use of native querySelectorAll() implementations supplied by browsers. You can use .filter() as ajpiano suggests, or create a simple plugin to implement your own .equalsText() method.

comment:5 Changed 4 years ago by paolo.marani@…

I ran into this exact trouble, no selector to match exact text. It seem to me pretty straightforward to expect an exact matching selector, :contains works but may risk of matching extra elements when the query string is very short, or ALL text elements when the argument is an empty string !!!

Please re-evaluate the addition of an :exactText() selector or a function like .filterText() that return all object having the specified exact text.

I was trying to use .filter for the same purpose but unsuccessfully.

comment:6 Changed 4 years ago by Paolo Marani

I suggest adding this very nice extension directly into codebase:

$.extend($.expr[':'],{
containsExact: function(a,i,m){
return $.trim(a.innerHTML.toLowerCase()) === m[3].toLowerCase();
},
containsExactCase: function(a,i,m){
return $.trim(a.innerHTML) === m[3];
},
containsRegex: function(a,i,m){
 var regreg =  /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})$/,
  reg = regreg.exec(m[3]);
 return RegExp(reg[1], reg[2]).test($.trim(a.innerHTML));
}
});
Note: See TracTickets for help on using tickets.