Fix search highlighting conflict with jsx code (#1886)

Replaces "<>", "</>" highlighting marks with "<|[[--" and "--]]|>" to avoid conflicts when highlighting jsx code.
This commit is contained in:
Sebastian Sdorra
2021-12-06 16:49:31 +01:00
committed by GitHub
parent e4936260a1
commit ad5bbfeef3
5 changed files with 13 additions and 9 deletions

View File

@@ -0,0 +1,2 @@
- type: fixed
description: Search highlighting in jsx ([#1886](https://github.com/scm-manager/scm-manager/pull/1886))

View File

@@ -24,6 +24,9 @@
import React, { FC } from "react"; import React, { FC } from "react";
const PRE_TAG = "<|[[--";
const POST_TAG = "--]]|>";
type Props = { type Props = {
value: string; value: string;
}; };
@@ -33,14 +36,14 @@ const HighlightedFragment: FC<Props> = ({ value }) => {
const result = []; const result = [];
while (content.length > 0) { while (content.length > 0) {
const start = content.indexOf("<>"); const start = content.indexOf(PRE_TAG);
const end = content.indexOf("</>"); const end = content.indexOf(POST_TAG);
if (start >= 0 && end > 0) { if (start >= 0 && end > 0) {
if (start > 0) { if (start > 0) {
result.push(content.substring(0, start)); result.push(content.substring(0, start));
} }
result.push(<strong>{content.substring(start + 2, end)}</strong>); result.push(<strong>{content.substring(start + PRE_TAG.length, end)}</strong>);
content = content.substring(end + 3); content = content.substring(end + POST_TAG.length);
} else { } else {
result.push(content); result.push(content);
break; break;

View File

@@ -55,7 +55,6 @@ public final class LuceneHighlighter {
fragments = keepWholeLine(value, fragments); fragments = keepWholeLine(value, fragments);
} }
return Arrays.stream(fragments) return Arrays.stream(fragments)
.map(fragment -> fragment.replace(PRE_TAG, "<>").replace(POST_TAG, "</>"))
.toArray(String[]::new); .toArray(String[]::new);
} }

View File

@@ -52,7 +52,7 @@ class LuceneHighlighterTest {
String[] snippets = highlighter.highlight("content", Indexed.Analyzer.DEFAULT, content); String[] snippets = highlighter.highlight("content", Indexed.Analyzer.DEFAULT, content);
assertThat(snippets).hasSize(1).allSatisfy( assertThat(snippets).hasSize(1).allSatisfy(
snippet -> assertThat(snippet).contains("<>Golgafrinchan</>") snippet -> assertThat(snippet).contains("<|[[--Golgafrinchan--]]|>")
); );
} }
@@ -64,7 +64,7 @@ class LuceneHighlighterTest {
snippet -> assertThat(snippet.split("\n")).contains( snippet -> assertThat(snippet.split("\n")).contains(
"\t\t\t\tint neighbors= getNeighbors(above, same, below);", "\t\t\t\tint neighbors= getNeighbors(above, same, below);",
"\t\t\t\tif(neighbors < 2 || neighbors > 3){", "\t\t\t\tif(neighbors < 2 || neighbors > 3){",
"\t\t\t\t\tnewGen[row]+= \"_\";//<2 or >3 neighbors -> <>die</>", "\t\t\t\t\tnewGen[row]+= \"_\";//<2 or >3 neighbors -> <|[[--die--]]|>",
"\t\t\t\t}else if(neighbors == 3){", "\t\t\t\t}else if(neighbors == 3){",
"\t\t\t\t\tnewGen[row]+= \"#\";//3 neighbors -> spawn/live" "\t\t\t\t\tnewGen[row]+= \"#\";//3 neighbors -> spawn/live"
) )
@@ -79,7 +79,7 @@ class LuceneHighlighterTest {
snippet -> assertThat(snippet.split("\n")).contains( snippet -> assertThat(snippet.split("\n")).contains(
"}) => {", "}) => {",
" const renderIcon = () => {", " const renderIcon = () => {",
" return <>{icon ? <Icon name={icon} color=\"<>inherit</>\" className=\"is-medium pr-1\" /> : null}</>;", " return <>{icon ? <Icon name={icon} color=\"<|[[--inherit--]]|>\" className=\"is-medium pr-1\" /> : null}</>;",
" };" " };"
) )
); );

View File

@@ -499,7 +499,7 @@ class LuceneQueryBuilderTest {
JsonNode displayName = fields.get("displayName"); JsonNode displayName = fields.get("displayName");
assertThat(displayName.get("highlighted").asBoolean()).isTrue(); assertThat(displayName.get("highlighted").asBoolean()).isTrue();
assertThat(displayName.get("fragments").get(0).asText()).contains("<>Arthur</>"); assertThat(displayName.get("fragments").get(0).asText()).contains("<|[[--Arthur--]]|>");
} }
@Test @Test