Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The highlighter highlights itself #274

Open
stein-j opened this issue Sep 12, 2022 · 0 comments
Open

The highlighter highlights itself #274

stein-j opened this issue Sep 12, 2022 · 0 comments

Comments

@stein-j
Copy link

stein-j commented Sep 12, 2022

Description:

In certain situations, the highlighter will highlight itself which creates two big issues:

  1. The returned statement is all messed up for the visual
  2. This issue is exponentielle which can lead to memory issues.

I have in my config file (Note the tag and the tagOptions):

[
   'highlight' => [
        'tag' => 'span',
        'options' => [
            'simple' => true,
            'wholeWord' => true,
            'caseSensitive' => false,
            'stripLinks' => false,
            'tagOptions' => [
                'class' => 'font-weight-bold',
            ],
        ],
    ],

I currently have a company called "MyCompany", if I search it, it finds it:

Screen Shot 2022-09-12 at 10 27 06

Now if I add any keyword (from the tag of in the tagOptions), it will find it and highlight it:

Screen Shot 2022-09-12 at 10 27 32

OR

Screen Shot 2022-09-12 at 10 27 37

And this can be cumulated, so if I put multiple key words. Which is really bad because everytime you put another keyword it will exponentially re-appear and be re-highlighted. This can lead to out of memory on the server (only 7 keywords makes my server crash).

Screen Shot 2022-09-12 at 10 27 51

But, no one will search those keywords, right ?

Actually, yes it can happen more easily then you think. In the above examples I have the config wholeWord set to true to prevent the current issue. But if set to false, things can be even more sensitive.

Now I have a company called "Fire Wire", I simply search "f w" for a quick search, the issues arrises :

Screen Shot 2022-09-12 at 10 39 19

Why is this happening ?

This is because when you search and have spaces, each "term" are separately highlighted in This foreach:

foreach ($needle as $needle_s) {

One the first term has been highlighted, is proceeds to the seconds term, but now the highlighter is working on the newly changed string containing the highlighted word so considers it when searching.

How to resolve ?

When the search is performed is should be done without the previous highlighter, so either :

  1. Each found result is replaced by a unique token. So only after when each search is done, it replaces the words. But this might lead to the same issue when you search small words like "lat" and the token contains it.

  2. Each result removes the result from the string and keeps track of the position in the string, all added at the end. But the issue is that the position will change when proceeding to the next search term (same applies when inserting each needle back to the string).

  3. Before performing the search, use regex to exclude previous tokens/needles. The only thing I can think of is that if a user is crazy enough to name its item the same name as the token, it wont be highlighted.

@stein-j stein-j changed the title The highlighter highlight itself The highlighter highlights itself Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant