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 getElementsForSearch() to replace tags with spaces #897

Merged

Conversation

andrewandante
Copy link
Contributor

Having an issue where this method combines <p> tags into a single string, muddying my search results, i.e.

<p>banana</p>
<p>hamburger</p>

becomes bananahamburger when this method is called.

Starting with a failing test - going to propose a fix which should replace these with spaces

@andrewandante andrewandante marked this pull request as ready for review April 30, 2021 03:00
Copy link

@adrhumphreys adrhumphreys left a comment

Choose a reason for hiding this comment

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

Can you check the tests? Other than those ones failing this is good and I'd be keen to get it merged in 👍🏽

@@ -43,7 +43,8 @@ public function getElementsForSearch()
/** @var ElementalArea $area */
$area = $this->owner->$key();
if ($area) {
$output[] = strip_tags($area->forTemplate());
// Replace HTML tags with spaces
$output[] = strip_tags(str_replace('<', ' <', $area->forTemplate()));

Choose a reason for hiding this comment

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

I think this presents a trade off that's worth it in this instance, as now if you have the string There are <5 potatoes it will become There are < 5 potatoes which means content previously indexable by ElementSiteTreeFilterSearch::applyDefaultFilters will no longer be picked up. But I think this is enough of an edge case that this is a good update ❤️

Copy link
Contributor

Choose a reason for hiding this comment

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

It's easy to target HTML tags via regex, not sure what is the benefit of not doing that here, while possibly changing the meaning of the indexed content (marginally, depending on the settings, but can't say it's definitely not).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is that we fall back to strip_tags() to do the heavy lifting in terms of HTML detection.

There's a lot of edge-cases either way really, if we hand-roll our own regex we are just as likely to add another issue, like "we wanted to make sure there were < 5 errors, but > 10 comments" being caught, or innerhtml such as:

<button onclick="document.getElementById('alert').innerHTML='<strong>MESSAGE</strong>';">

Not being rendered properly. I admit that's unlikely but it's possible.

@adrhumphreys it will become There are <5 potatoes and shouldn't break anything, the space is added before the < - I'll take a look at those tests, not sure exactly why they are failing, it seems unrelated to my changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suspicion that the Behat failures are related to chromedriver 91? Having trouble replicating locally

@emteknetnz emteknetnz mentioned this pull request May 3, 2021
@emteknetnz emteknetnz closed this Jan 17, 2022
@emteknetnz emteknetnz reopened this Jan 17, 2022
@emteknetnz
Copy link
Member

@andrewandante I've rerun the tests. Looks like there's a failure related to phpunit9 - https://app.travis-ci.com/github/silverstripe/silverstripe-elemental/jobs/555927169

@andrewandante andrewandante force-pushed the hotfix/get_elements_for_search_spaces branch from bf16e9b to d0d539e Compare January 18, 2022 04:00
@andrewandante
Copy link
Contributor Author

@emteknetnz PHPUnit tests updated, still seeing Behat failures though. I see that they are also failing on 4, so can we assume they are unrelated?

@emteknetnz
Copy link
Member

Yes unrelated

@emteknetnz emteknetnz merged commit d5f21ec into silverstripe:4 Jan 18, 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

Successfully merging this pull request may close these issues.

4 participants