Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) fixMarkup would rarely destroy markup when useBR was enabled #2532

Merged
merged 4 commits into from May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -8,6 +8,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;
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved

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>';
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
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