Skip to content

Commit

Permalink
run tests in debug mode, throw 0 width error for bad regex
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Jan 31, 2020
1 parent ec4c9f9 commit 67b19a1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/highlight.js
Expand Up @@ -694,6 +694,12 @@ https://highlightjs.org/
if (lastMatch.type=="begin" && match.type=="end" && lastMatch.index == match.index && lexeme === "") {
// spit the "skipped" character that our regex choked on back into the output sequence
mode_buffer += codeToHighlight.slice(match.index, match.index + 1);
if (!SAFE_MODE) {
var err = new Error('0 width match regex');
err.languageName = languageName;
err.badRule = lastMatch.rule;
throw(err);
}
return 1;
}
lastMatch = match;
Expand Down Expand Up @@ -1018,6 +1024,7 @@ https://highlightjs.org/
hljs.inherit = inherit;
hljs.addPlugin = addPlugin;
hljs.debugMode = function() { SAFE_MODE = false; }
hljs.safeMode = function() { SAFE_MODE = true; }

// Common regexps
hljs.IDENT_RE = '[a-zA-Z]\\w*';
Expand Down
1 change: 1 addition & 0 deletions test/detect/index.js
Expand Up @@ -5,6 +5,7 @@ delete require.cache[require.resolve('../../build/lib/highlight')]

const fs = require('fs').promises;
const hljs = require('../../build');
hljs.debugMode(); // tests run in debug mode so errors are raised
const path = require('path');
const utility = require('../utility');

Expand Down
3 changes: 3 additions & 0 deletions test/index.js
@@ -1,5 +1,8 @@
'use strict';

const hljs = require('../build');
hljs.debugMode(); // tests run in debug mode so errors are raised

// Tests specific to the API exposed inside the hljs object.
// Right now, that only includes tests for several common regular expressions.
require('./api');
Expand Down
7 changes: 7 additions & 0 deletions test/parser/should-not-destroyData.js
Expand Up @@ -5,6 +5,7 @@ describe("bugs", function () {
// CONTEXT: https://github.com/highlightjs/highlight.js/pull/2219
describe("a grammar with a mode that makes a 0 width match", () => {
it("should instead count it as a 1 character match", () => {
hljs.safeMode();
hljs.registerLanguage('test-language', (hljs) => {

// broken regex from old Fortran ruleset
Expand All @@ -28,6 +29,12 @@ describe("bugs", function () {
// Incorrect prior output:
// 'The number is <span class="hljs-number"></span>23_longint yes.'
)
hljs.debugMode();
should(() => {
hljs.highlight('test-language', 'The number is 123_longint yes.').value
}).throw(Error, {
message: "0 width match regex",
languageName: "test-language"})
})
})
})

0 comments on commit 67b19a1

Please sign in to comment.