Skip to main content

Bug Tracker

Side navigation

#2147 closed enhancement (invalid)

Opened January 11, 2008 03:39PM UTC

Closed November 11, 2010 01:04AM UTC

Last modified March 15, 2012 09:39AM UTC

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 January 11, 2008 03:43PM UTC.

    testcase and a semi-real case

Change History (6)

Changed June 20, 2010 05:05PM UTC by dmethvin comment:1

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.

Changed November 04, 2010 04:13PM UTC by addyosmani comment:2

keywords: contains selector textcontains selector text needsreview

Changed November 10, 2010 06:38PM UTC by ajpiano comment:3

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

Changed November 11, 2010 01:04AM UTC by dmethvin comment:4

keywords: contains selector text needsreviewcontains selector text
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.

Changed April 19, 2011 02:59PM UTC by paolo.marani@engineering.it comment:5

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.

Changed April 19, 2011 03:13PM UTC by Paolo Marani comment:6

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));
}
});