Bug Tracker

Opened 15 years ago

Closed 12 years ago

Last modified 11 years ago

#2147 closed enhancement (invalid)

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:
Blocked by: Blocking:

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 (1)

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

Download all attachments as: .zip

Change History (7)

Changed 15 years ago by G_Gus

Attachment: testcase.html added

testcase and a semi-real case

comment:1 Changed 13 years ago by dmethvin

Summary: need for a selector to match full string, as :contians() selector match only substringneed 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 12 years ago by addyosmani

Keywords: needsreview added

comment:3 Changed 12 years ago by ajpiano

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

comment:4 Changed 12 years ago by dmethvin

Keywords: needsreview removed
Resolution: invalid
Status: newclosed

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 12 years ago by [email protected]

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 12 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.