Permalink
Browse files

escaping define 'identifiers' even since it currently allows almost a…

…ny character
  • Loading branch information...
1 parent 30cc3a7 commit 82828dab14bb3c213415ba0bdc5ec7ce71ab4e92 @jayphelps committed Apr 4, 2012
Showing with 9 additions and 7 deletions.
  1. +9 −7 stutter.js
View
@@ -8,7 +8,7 @@
(function (root) {
"use strict";
- // Internal type checkers
+ /* Internal helpers */
var toString = Object.prototype.toString;
@@ -20,6 +20,11 @@
return toString.call(obj) == '[object String]';
};
+ // From http://stackoverflow.com/questions/3446170
+ var escapeStringForRegExp = function (str) {
+ return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
+ }
+
// Assigned when we come across a directive and used if a StutterError
// exception is raised
var currentDirectiveName;
@@ -43,7 +48,8 @@
directives: {},
handlers: [],
directiveRegEx: null,
- expandNewlineEscapes: true
+ expandNewlineEscapes: true,
+ escapeStringForRegExp: escapeStringForRegExp
};
var isBrowser;
@@ -188,12 +194,11 @@
var defines = {};
Stutter.register('define', function (identifier, replacement) {
- var errorToken = Stutter.token + 'define: ';
if (!identifier) {
throw new StutterError('Invalid define syntax');
}
- var identifierRegEx = new RegExp(identifier, 'g');
+ var identifierRegEx = new RegExp(escapeStringForRegExp(identifier), 'g');
var replacement = this[identifier] = replacement || true;
return function (line) {
@@ -204,7 +209,6 @@
// FIXME: Remove endif from here and also add the rest of the conditionals
Stutter.register('ifdef', function (identifier) {
- var errorToken = Stutter.token + 'ifdef: ';
var endif = Stutter.token + 'endif';
if (!identifier) {
@@ -231,8 +235,6 @@
// Doesn't prevent recursive/infinite imports! (intentional at this point)
Stutter.register('import', function (filePath) {
- var errorToken = Stutter.token + 'import: ';
-
if (!filePath) {
throw new StutterError('Missing file path');
}

0 comments on commit 82828da

Please sign in to comment.