mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Fix regression introduced in dae0bb4c67.
Add tests so it doesn't bite me again.
This commit is contained in:
@@ -1,14 +1,16 @@
|
|||||||
import { PHRASE_MATCH_REGEXP_PATTERN } from "discourse/lib/concerns/search-constants";
|
import { PHRASE_MATCH_REGEXP_PATTERN } from "discourse/lib/concerns/search-constants";
|
||||||
|
|
||||||
|
export const CLASS_NAME = "search-highlight";
|
||||||
|
|
||||||
export default function($elem, term) {
|
export default function($elem, term) {
|
||||||
if (!_.isEmpty(term)) {
|
if (!_.isEmpty(term)) {
|
||||||
// special case ignore "l" which is used for magic sorting
|
// special case ignore "l" which is used for magic sorting
|
||||||
let words = _.reject(
|
let words = _.reject(
|
||||||
term.match(new RegExp(`${PHRASE_MATCH_REGEXP_PATTERN}|[^\s]+`, "g")),
|
term.match(new RegExp(`${PHRASE_MATCH_REGEXP_PATTERN}|[^\\s]+`, "g")),
|
||||||
t => t === "l"
|
t => t === "l"
|
||||||
);
|
);
|
||||||
|
|
||||||
words = words.map(w => w.replace(/^"(.*)"$/, "$1"));
|
words = words.map(w => w.replace(/^"(.*)"$/, "$1"));
|
||||||
$elem.highlight(words, { className: "search-highlight", wordsOnly: true });
|
$elem.highlight(words, { className: CLASS_NAME, wordsOnly: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
test/javascripts/lib/highlight-text-test.js.es6
Normal file
28
test/javascripts/lib/highlight-text-test.js.es6
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import {
|
||||||
|
default as highlightText,
|
||||||
|
CLASS_NAME
|
||||||
|
} from "discourse/lib/highlight-text";
|
||||||
|
|
||||||
|
QUnit.module("lib:highlight-text");
|
||||||
|
|
||||||
|
QUnit.test("highlighting text", assert => {
|
||||||
|
fixture().html(
|
||||||
|
`
|
||||||
|
<p>This is some text to highlight</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
|
||||||
|
highlightText(fixture(), "some text");
|
||||||
|
|
||||||
|
const terms = [];
|
||||||
|
|
||||||
|
fixture(`.${CLASS_NAME}`).each((_, elem) => {
|
||||||
|
terms.push(elem.textContent);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
terms.join(" "),
|
||||||
|
"some text",
|
||||||
|
"it should highlight the terms correctly"
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user