diff --git a/app/assets/javascripts/discourse/lib/highlight-text.js.es6 b/app/assets/javascripts/discourse/lib/highlight-text.js.es6 index 8a0d4d48aef..c888b4967f6 100644 --- a/app/assets/javascripts/discourse/lib/highlight-text.js.es6 +++ b/app/assets/javascripts/discourse/lib/highlight-text.js.es6 @@ -1,14 +1,16 @@ import { PHRASE_MATCH_REGEXP_PATTERN } from "discourse/lib/concerns/search-constants"; +export const CLASS_NAME = "search-highlight"; + export default function($elem, term) { if (!_.isEmpty(term)) { // special case ignore "l" which is used for magic sorting 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" ); words = words.map(w => w.replace(/^"(.*)"$/, "$1")); - $elem.highlight(words, { className: "search-highlight", wordsOnly: true }); + $elem.highlight(words, { className: CLASS_NAME, wordsOnly: true }); } } diff --git a/test/javascripts/lib/highlight-text-test.js.es6 b/test/javascripts/lib/highlight-text-test.js.es6 new file mode 100644 index 00000000000..1c1ceaec2c9 --- /dev/null +++ b/test/javascripts/lib/highlight-text-test.js.es6 @@ -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( + ` +

This is some text to highlight

+ ` + ); + + 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" + ); +});