TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Client-side full text search in CSS

213 pointsby fzaninottoover 11 years ago

16 comments

Groxxover 11 years ago
First off, this is <i>fascinating</i>, and I love it. I never thought of doing anything like this, and I have no idea how they came up with it. Awesome idea and great execution.<p>But this:<p>&gt;<i>The advantage of using CSS selectors rather than JavaScript indexOf() for search is speed: you only change one element at each keystroke (the &lt;style&gt; tag) instead of changing all the elements matching the query.</i><p>makes it sound like they just don&#x27;t know how to write DOM-efficient JS, and probably never profiled it or their implementation. I would be <i>shocked</i> if you can&#x27;t relatively-trivially make a faster JS implementation, and even more if you can&#x27;t make a significantly faster &#x27;smarter&#x27; one with e.g. a more optimized search index, since you can make tradeoffs the CSS processor very likely cannot.
评论 #6365075 未加载
andrewmunsellover 11 years ago
Pretty cool, but yeah, you have to be careful of CSS injection (as mentioned by the author). There isn&#x27;t too much harm that can be done if the user is typing this in himself or herself, but if the search query is pulled from the URL there might be some security implications.<p>For example, enter this into the search field:<p><pre><code> &quot;]), body, a:not([data-index=&quot; </code></pre> This will hide the entire page. The last &quot;a:not&quot; selector is really inconsequential-- I just had to close the opening parenthesis and this just happens to work.
评论 #6364168 未加载
mistercowover 11 years ago
This is a very clever idea. There are a few limitations that I think would prevent this from being very usable in practice. One is that it only supports a single word. If you type &quot;Ona Bednar&quot; into the field, you get nothing.<p>Another problem that would only start to show up on a larger dataset is that because the index is all concatenated directly together, it matches strings that span several words. A user searching for their pal Harry Mesbro in the list might be confused to find that typing in his last name also brings up Yvette Hammes.
评论 #6364762 未加载
评论 #6364764 未加载
kbensonover 11 years ago
Interesting, but I can&#x27;t help feeling that a better implementation would be to split ethe input on white space, and build a slightly more complex selector such that a search for &quot;term1 term2&quot; would set the style to:<p><pre><code> .searchable { display: none; } .searchable[data-index*=&quot;term1&quot;][date-index*=&quot;term2&quot;] { display; block; } </code></pre> and an empty input would hav eno selectors (or .searchable {display:block;} ).<p>It&#x27;s <i>slightly</i> more code, but much more usable.
评论 #6369159 未加载
corfordover 11 years ago
Fun hack but since it relies on JS it&#x27;s difficult to see why cutting IE8 out makes sense for such a negligible speed up (assuming said speed up actually exists).
评论 #6364323 未加载
评论 #6369178 未加载
DigitalSeaover 11 years ago
This is quite simply a very clever hack. Obviously it isn&#x27;t up to production standard, but from a hack point-of-view it&#x27;s thinking outside-of-the-box and I love it. Good to see people thinking of nifty ideas like this. CSS and HTML are getting to the point where they can do what was once only possible in Flash, then Javascript and now in CSS.
namenotrequiredover 11 years ago
That is great, I could immediately think of a few possible uses for this. Client side searching can be quite heavy on the machine if there&#x27;s a lot of data.
评论 #6363988 未加载
a3r0over 11 years ago
The point about it being efficient because you&#x27;re only changing one element doesn&#x27;t sound correct. If you change the styles on the page, the browser is at least going to have to iterate over all of the items with the searchable class (assuming that it doesn&#x27;t build some sort of index). If you did it in JS, you could try to make it more efficient by indexing the data first.
dpcxover 11 years ago
This is interesting, but requires you to transfer essentially your entire data-set to the client, increasing your overall transfer.
评论 #6363896 未加载
评论 #6364046 未加载
mk4pover 11 years ago
First off, very clever. Second, I have a case for which I might actually use this.<p>Using multiple textboxes, apply each search term to a nested mongo resultset. Visually narrow down the data structure you want to get out of mongo, and generate the mongo query you&#x27;d use to get that data.
Flenserover 11 years ago
It needs separators between the fields in the data attribute, otherwise it could have false positives. e.g. &quot;abe&quot; will find &quot;ona bednar&quot; because &quot;abe&quot; is in the data attribute &quot;an_abe_ndar...&quot;
legierskiover 11 years ago
Pretty cool. The only drawback that I can see right now is the need to send all searchable data twice, increasing overall amount of data that needs to be sent to client.
评论 #6364088 未加载
评论 #6364052 未加载
tantalorover 11 years ago
<i>The advantage of using CSS selectors rather than JavaScript indexOf() for search is speed</i><p>Where&#x27;s the performance comparison?
erichurkmanover 11 years ago
The biggest drawback that I could do easily in JavaScript is that it can&#x27;t highlight the matching terms.
评论 #6365638 未加载
mck-over 11 years ago
I would probably use Angular&#x27;s filter for this
stultusover 11 years ago
off - the site is not mobile friendly and I hate that.