Skip to content

Commit

Permalink
(fix) fixMarkup would rarely destroy markup when useBR was enabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed May 8, 2020
1 parent 04e4991 commit 8c74229
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -9,6 +9,7 @@ Parser Engine:
- (enh) Added `on:end` callback for modes (#2261) [Josh Goebel][]
- (enh) Added ability to programatically ignore begin and end matches (#2261) [Josh Goebel][]
- (enh) Added `END_SAME_AS_BEGIN` mode to replace `endSameAsBegin` parser attribute (#2261) [Josh Goebel][]
- (fix) `fixMarkup` would rarely destroy markup when `useBR` was enabled (#2532) [Josh Goebel][]

Deprecations:

Expand Down
12 changes: 6 additions & 6 deletions src/highlight.js
Expand Up @@ -32,7 +32,7 @@ const HLJS = function(hljs) {
var SAFE_MODE = true;

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

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

Expand Down Expand Up @@ -540,13 +540,13 @@ const HLJS = function(hljs) {
return value;
}

return value.replace(fixMarkupRe, function(match, p1) {
if (options.useBR && match === '\n') {
return '<br>';
return value.replace(fixMarkupRe, match => {
if (match === '\n') {
return options.useBR ? '<br>' : match;
} else if (options.tabReplace) {
return p1.replace(/\t/g, options.tabReplace);
return match.replace(/\t/g, options.tabReplace);
}
return '';
return match;
});
}

Expand Down
16 changes: 13 additions & 3 deletions test/api/fixmarkup.js
Expand Up @@ -5,11 +5,21 @@ const hljs = require('../../build');

describe('.fixmarkup()', () => {
after(() => {
hljs.configure({ useBR: false })
})
hljs.configure({ useBR: false });
});

it('should not strip HTML from beginning of strings', () => {
hljs.configure({ useBR: true });
const value = '<span class="hljs-attr">"some"</span>: \n <span class="hljs-string">"json"</span>';
const result = hljs.fixMarkup(value);

result.should.equal(
'<span class="hljs-attr">"some"</span>: <br> <span class="hljs-string">"json"</span>'
);
});

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

Expand Down

0 comments on commit 8c74229

Please sign in to comment.