Navigation Menu

Skip to content

Commit

Permalink
Support query including single quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
darashi committed Aug 1, 2012
1 parent e615627 commit cab5a32
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/q-translator.js
Expand Up @@ -28,6 +28,10 @@ function throwTranslateError(query, context, detail) {
throw new Error(message);
}

function escapeTerm(term) {
return term.replace(/'/, "\\'");
}

QueryTranslator.prototype = {
translateIndividualTerm: function(query, context) {
var term = '';
Expand All @@ -37,7 +41,7 @@ QueryTranslator.prototype = {
}
term += query[context.offset];
}
return context.defaultField + ":'" + term + "'";
return context.defaultField + ":'" + escapeTerm(term) + "'";
},
skipSpaces: function(query, context) {
for (; context.offset < query.length; context.offset++) {
Expand Down
4 changes: 4 additions & 0 deletions test/q-translator.test.js
Expand Up @@ -32,4 +32,8 @@ suite('QueryTranslator', function() {
"star wars",
"field:'star'",
"star".length);
testIndividualTerm("an individual term: single quote",
"let's go",
"field:'let\\'s'",
"let's".length);
});

0 comments on commit cab5a32

Please sign in to comment.