Skip to content

Commit 8c74229

Browse files
authored
(fix) fixMarkup would rarely destroy markup when useBR was enabled (#2532)
1 parent 04e4991 commit 8c74229

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Parser Engine:
99
- (enh) Added `on:end` callback for modes (#2261) [Josh Goebel][]
1010
- (enh) Added ability to programatically ignore begin and end matches (#2261) [Josh Goebel][]
1111
- (enh) Added `END_SAME_AS_BEGIN` mode to replace `endSameAsBegin` parser attribute (#2261) [Josh Goebel][]
12+
- (fix) `fixMarkup` would rarely destroy markup when `useBR` was enabled (#2532) [Josh Goebel][]
1213

1314
Deprecations:
1415

src/highlight.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const HLJS = function(hljs) {
3232
var SAFE_MODE = true;
3333

3434
// Regular expressions used throughout the highlight.js library.
35-
var fixMarkupRe = /((^(<[^>]+>|\t|)+|(?:\n)))/gm;
35+
var fixMarkupRe = /(^(<[^>]+>|\t|)+|\n)/gm;
3636

3737
var LANGUAGE_NOT_FOUND = "Could not find the language '{}', did you forget to load/include a language module?";
3838

@@ -540,13 +540,13 @@ const HLJS = function(hljs) {
540540
return value;
541541
}
542542

543-
return value.replace(fixMarkupRe, function(match, p1) {
544-
if (options.useBR && match === '\n') {
545-
return '<br>';
543+
return value.replace(fixMarkupRe, match => {
544+
if (match === '\n') {
545+
return options.useBR ? '<br>' : match;
546546
} else if (options.tabReplace) {
547-
return p1.replace(/\t/g, options.tabReplace);
547+
return match.replace(/\t/g, options.tabReplace);
548548
}
549-
return '';
549+
return match;
550550
});
551551
}
552552

test/api/fixmarkup.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ const hljs = require('../../build');
55

66
describe('.fixmarkup()', () => {
77
after(() => {
8-
hljs.configure({ useBR: false })
9-
})
8+
hljs.configure({ useBR: false });
9+
});
10+
11+
it('should not strip HTML from beginning of strings', () => {
12+
hljs.configure({ useBR: true });
13+
const value = '<span class="hljs-attr">"some"</span>: \n <span class="hljs-string">"json"</span>';
14+
const result = hljs.fixMarkup(value);
15+
16+
result.should.equal(
17+
'<span class="hljs-attr">"some"</span>: <br> <span class="hljs-string">"json"</span>'
18+
);
19+
});
1020

1121
it('should not add "undefined" to the beginning of the result (#1452)', () => {
12-
hljs.configure({ useBR: true })
22+
hljs.configure({ useBR: true });
1323
const value = '{ <span class="hljs-attr">"some"</span>: \n <span class="hljs-string">"json"</span> }';
1424
const result = hljs.fixMarkup(value);
1525

0 commit comments

Comments
 (0)