#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)
Change History (7)
Changed 15 years ago by
Attachment: | testcase.html added |
---|
comment:1 Changed 13 years ago by
Summary: | need for a selector to match full string, as :contians() selector match only substring → 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 12 years ago by
Keywords: | needsreview added |
---|
comment:4 Changed 12 years ago by
Keywords: | needsreview removed |
---|---|
Resolution: | → invalid |
Status: | new → closed |
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
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
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)); } });
testcase and a semi-real case