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

Fix html entity in search results #2844

Merged
merged 4 commits into from
Aug 22, 2017
Merged

Conversation

satchmorun
Copy link
Contributor

Don't search escaped HTML text

Before this change, if you tried to search for "&" to match "Stuff & Nonsense", what we would do is html-escape both things and compare the escaped strings.

This works fine in the case where you just type "&". However, if you type "mp", that will match the html entity "&" in the middle of the html-escaped text we're comparing it to. Which is incorrect.

Additionally, we highlight our "matched" result. When we do, we break up the entity so that the resulting HTML looks like:

Stuff &a<em>mp</em>; Nonsense

And you get to see what basically looks like garbage on the screen:

screen garbage

The meat of this PR and this PR description comes from this commit. The subsequent commits are just refactorings and tidyings.

PR made in a pairing session with @braddunbar.

Copy link
Contributor

@adunkman adunkman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can’t see anything amiss!

/cc @harvesthq/chosen-developers

if query.length
startpos = text.search highlightRegex
prefix = text.slice(0, startpos)
fix = text.slice(startpos, startpos + query.length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A+ variable name.

Copy link
Collaborator

@koenpunt koenpunt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has been changes (and issues) in this department quite some times, so we should be certain that this won't reintroduce one of the earlier problems.

@tjschuck
Copy link
Member

tjschuck commented Jul 6, 2017

See also:

Plus like a scrillion other issues and PRs linked off of those.

satchmorun pushed a commit that referenced this pull request Aug 22, 2017
The conflict was in `abstract-chosen-coffee`, and was a result of
slightly overlapping areas of effort between this and
#2844.
@satchmorun
Copy link
Contributor Author

I'm sorry – I had forgotten about this PR.

Just got this up-to-speed and mergeable with 800b7c9.

There has been changes (and issues) in this department quite some times, so we should be certain that this won't reintroduce one of the earlier problems.

@koenpunt I feel pretty certain that we aren't reintroducing the earlier problems. The first commit in this PR is what prevents the same problem that was fixed in #2254. The main problem with the prior attempts was not escaping the HTML as it's built up for the highlighted result, and here we're very explicit about it: 0adf2d5#diff-a3162852f1360124c43007c8130261dcR201

/cc @adunkman @tjschuck

prefix = text.slice(0, startpos)
fix = text.slice(startpos, startpos + query.length)
suffix = text.slice(startpos + query.length)
option.highlighted_html = "#{@escape_html(prefix)}<em>#{@escape_html(fix)}</em>#{@escape_html(suffix)}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For functions we use this instead of @, so please use this.escape_html(prefix)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 5940e3b.

@koenpunt
Copy link
Collaborator

Probably not a change that you can add some tests for this? 😅

@satchmorun
Copy link
Contributor Author

Probably not a change that you can add some tests for this? 😅

I think maybe I can. I'll try and see what happens.

@satchmorun
Copy link
Contributor Author

@koenpunt we've now got regression tests for this! d4715b1

Bing bang boom.

@tjschuck
Copy link
Member

@satchmorun Can you squash this down to one (or a few) logical commits?

@satchmorun satchmorun force-pushed the fix-html-entity-in-search-results branch from d4715b1 to d500495 Compare August 22, 2017 19:59
Arun Srinivasan added 2 commits August 22, 2017 16:02
Before this change, if you tried to search for "&" to match "Stuff
& Nonsense", what we would do is html-escape _both_ things and compare
the escaped strings.

This works fine in the case where you just type "&". However, if you
type "mp", that will match the html entity "&amp;" in the middle of the
html-escaped text we're comparing it to. Which is incorrect.

Additionally, we highlight our "matched" result. When we do, we break up
the entity so that the resulting HTML looks like:

    Stuff &a<em>mp</em>; Nonsense

And you get to see what basically looks like garbage on the screen.

![screen garbage](https://camo.githubusercontent.com/b5e384f3f622f3560739c287c481e05a7b0668af/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f7370612f7470786279757478626b747069347a2f6c7566336c7835792e706e67)

This commit fixes all of that by just comparing the actual text of
things. We avoid the problem previous addressed in #2254 by escaping
each piece of the to-be-highlighted option text before assembling it
into HTML.
@satchmorun satchmorun force-pushed the fix-html-entity-in-search-results branch from d500495 to 8c638e9 Compare August 22, 2017 20:04
Arun Srinivasan added 2 commits August 22, 2017 16:07
It's no longer being used for the searching/matching process and is now simply
a string of highlighted html.
@satchmorun satchmorun force-pushed the fix-html-entity-in-search-results branch from 8c638e9 to 07e5179 Compare August 22, 2017 20:12
@satchmorun
Copy link
Contributor Author

@tjschuck done and done

@koenpunt
Copy link
Collaborator

Bing bang boom.

Great! :shipit:

@satchmorun satchmorun merged commit 1273c80 into master Aug 22, 2017
@satchmorun satchmorun deleted the fix-html-entity-in-search-results branch August 22, 2017 20:16
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

Successfully merging this pull request may close these issues.

4 participants