From 8c7422958b8cca585e1866cde0d9f9ea6c7a8e43 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 8 May 2020 02:03:31 -0400 Subject: [PATCH] (fix) `fixMarkup` would rarely destroy markup when `useBR` was enabled (#2532) --- CHANGES.md | 1 + src/highlight.js | 12 ++++++------ test/api/fixmarkup.js | 16 +++++++++++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 65b4771d18..eacc0a0d0d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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: diff --git a/src/highlight.js b/src/highlight.js index c2779be23f..bc34fb4fc3 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -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?"; @@ -540,13 +540,13 @@ const HLJS = function(hljs) { return value; } - return value.replace(fixMarkupRe, function(match, p1) { - if (options.useBR && match === '\n') { - return '
'; + return value.replace(fixMarkupRe, match => { + if (match === '\n') { + return options.useBR ? '
' : match; } else if (options.tabReplace) { - return p1.replace(/\t/g, options.tabReplace); + return match.replace(/\t/g, options.tabReplace); } - return ''; + return match; }); } diff --git a/test/api/fixmarkup.js b/test/api/fixmarkup.js index 25600926ef..360b6a51e9 100644 --- a/test/api/fixmarkup.js +++ b/test/api/fixmarkup.js @@ -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 = '"some": \n "json"'; + const result = hljs.fixMarkup(value); + + result.should.equal( + '"some":
"json"' + ); + }); it('should not add "undefined" to the beginning of the result (#1452)', () => { - hljs.configure({ useBR: true }) + hljs.configure({ useBR: true }); const value = '{ "some": \n "json" }'; const result = hljs.fixMarkup(value);