Skip to content
This repository was archived by the owner on Sep 24, 2025. It is now read-only.

Commit fcd939d

Browse files
author
Jason Eberle
committed
Fix error in remove block w/ regex special chars
Previously, `remove` blocks that contained regular expression special characters were throwing an error when instantiating a RegExp object to match with. This commit escapes all regex special characters in the offending block to ensure that a proper regular expression can be made.
1 parent 3b58307 commit fcd939d

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

lib/blocktypes/remove.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
module.exports = remove;
44

55
function remove(content, block, blockLine, blockContent) {
6+
var safeBlockLine = blockLine.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
67
// Replace blockLine with surrounding new line symbols (if present) with empty string
7-
var linefeedStart = content.match(this.linefeed + blockLine) ? this.linefeed : '';
8+
var linefeedStart = content.match(this.linefeed + safeBlockLine) ? this.linefeed : '';
89
return content.split(linefeedStart + blockLine + this.linefeed).join('');
910
}

test/expected/remove/remove_with_unescaped_regex_chars.html

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<!-- build:remove:testa -->
2+
-[]{}()*+?.,\^$|#
3+
<!-- /build -->

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,23 @@ describe('remove_with_newline', function () {
294294
});
295295
});
296296

297+
describe('remove_with_unescaped_regex_chars', function () {
298+
it('should remove block also when the block contains unescaped regular expression special characters', function (done) {
299+
htmlprocessor({
300+
src: ['test/fixtures/remove_with_unescaped_regex_chars.html'],
301+
dest: 'test/fixtures/remove/remove_with_unescaped_regex_chars.processed.html'
302+
}, {
303+
environment: 'testa'
304+
});
305+
306+
var actual = utils.read('test/fixtures/remove/remove_with_unescaped_regex_chars.processed.html');
307+
var expected = utils.read('test/expected/remove/remove_with_unescaped_regex_chars.html');
308+
assert.equal(actual, expected);
309+
310+
done();
311+
});
312+
});
313+
297314
describe('inline', function () {
298315
it('should inline css and js for dist target', function (done) {
299316
htmlprocessor({

0 commit comments

Comments
 (0)