Skip to content

Commit

Permalink
Fixed invalid escaping of HTML in search results
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Aug 24, 2021
1 parent 702de82 commit 829820a
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 99 deletions.
29 changes: 0 additions & 29 deletions material/assets/javascripts/bundle.56838a2c.min.js

This file was deleted.

7 changes: 0 additions & 7 deletions material/assets/javascripts/bundle.56838a2c.min.js.map

This file was deleted.

48 changes: 0 additions & 48 deletions material/assets/javascripts/workers/search.709b4209.min.js

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions material/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ <h1>{{ page.title | d(config.site_name, true)}}</h1>
"base": base_url,
"features": features,
"translations": {},
"search": "assets/javascripts/workers/search.709b4209.min.js" | url,
"search": "assets/javascripts/workers/search.409db549.min.js" | url,
"version": config.extra.version or None
} -%}
{%- set translations = app.translations -%}
Expand All @@ -223,7 +223,7 @@ <h1>{{ page.title | d(config.site_name, true)}}</h1>
</script>
{% endblock %}
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.56838a2c.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.56a63758.min.js' | url }}"></script>
{% for path in config["extra_javascript"] %}
<script src="{{ path | url }}"></script>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function mountSearchHiglight(
)
])
.pipe(
map(([index, url]) => setupSearchHighlighter(index.config)(
map(([index, url]) => setupSearchHighlighter(index.config, true)(
url.searchParams.get("h")!
)),
map(fn => {
Expand Down
2 changes: 1 addition & 1 deletion src/assets/javascripts/integrations/search/_/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class Search {

/* Set up document map and highlighter factory */
this.documents = setupSearchDocumentMap(docs)
this.highlight = setupSearchHighlighter(config)
this.highlight = setupSearchHighlighter(config, false)

/* Set separator for tokenizer */
lunr.tokenizer.separator = new RegExp(config.separator)
Expand Down
13 changes: 9 additions & 4 deletions src/assets/javascripts/integrations/search/highlighter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ export type SearchHighlightFactoryFn = (query: string) => SearchHighlightFn
* Create a search highlighter
*
* @param config - Search index configuration
* @param escape - Whether to escape HTML
*
* @returns Search highlight factory function
*/
export function setupSearchHighlighter(
config: SearchIndexConfig
config: SearchIndexConfig, escape: boolean
): SearchHighlightFactoryFn {
const separator = new RegExp(config.separator, "img")
const highlight = (_: unknown, data: string, term: string) => {
Expand All @@ -79,8 +80,12 @@ export function setupSearchHighlighter(
})`, "img")

/* Highlight string value */
return value => escapeHTML(value)
.replace(match, highlight)
.replace(/<\/mark>(\s+)<mark[^>]*>/img, "$1")
return value => (
escape
? escapeHTML(value)
: value
)
.replace(match, highlight)
.replace(/<\/mark>(\s+)<mark[^>]*>/img, "$1")
}
}

0 comments on commit 829820a

Please sign in to comment.