From 6d9bab7e9f24a53f268dcf69c7bc2d0a125f2be5 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 17 Sep 2016 13:48:20 +0200 Subject: [PATCH] Allow empty string to supress prefix Closes GH-4. --- lib/core.js | 12 ++++++++++-- test/index.js | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/core.js b/lib/core.js index 226f277..dfc8974 100644 --- a/lib/core.js +++ b/lib/core.js @@ -78,7 +78,7 @@ var own = {}.hasOwnProperty; */ function autoHighlight(value, options) { var settings = options || {}; - var prefix = settings.prefix || DEFAULT_PREFIX; + var prefix = settings.prefix; var subset = settings.subset || languageNames; var length = subset.length; var index = -1; @@ -87,6 +87,10 @@ function autoHighlight(value, options) { var current; var name; + if (prefix === null || prefix === undefined) { + prefix = DEFAULT_PREFIX; + } + if (typeof value !== 'string') { throw new Error('Expected `string` for value, got `' + value + '`'); } @@ -134,7 +138,11 @@ function autoHighlight(value, options) { */ function highlight(language, value, options) { var settings = options || {}; - var prefix = settings.prefix || DEFAULT_PREFIX; + var prefix = settings.prefix; + + if (prefix === null || prefix === undefined) { + prefix = DEFAULT_PREFIX; + } return normalize(coreHighlight(language, value, true, prefix)); } diff --git a/test/index.js b/test/index.js index 3f488e4..f0046a4 100644 --- a/test/index.js +++ b/test/index.js @@ -173,6 +173,20 @@ test('lowlight.highlight(language, value[, options])', function (t) { st.end(); }); + t.test('empty `prefix`', function (st) { + var result = low.highlight('js', '"use strict";', { + prefix: '' + }); + + t.deepEqual( + result.value[0].properties.className, + ['meta'], + 'should support an empty `prefix`' + ); + + st.end(); + }); + t.end(); }); @@ -269,6 +283,18 @@ test('lowlight(value[, options])', function (t) { st.end(); }); + t.test('empty `prefix`', function (st) { + var result = low.highlightAuto('"use strict";', {prefix: ''}); + + t.deepEqual( + result.value[0].properties.className, + ['meta'], + 'should support an empty `prefix`' + ); + + st.end(); + }); + t.test('custom `subset`', function (st) { var result = low.highlightAuto('"use strict";', {subset: ['java']});