Skip to content

Commit db7b5a4

Browse files
committed
[#57] Fixes #57, escapes JS regex replacements and re-orders script field and lookup params
1 parent 6cb1036 commit db7b5a4

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/frontend/managers/elasticsearchManager.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,11 @@ var ElasticsearchManager = (function(){
284284
//(grab +1 so we know if there are more pages)
285285
pagination = "LIMIT " + rowsToPull
286286
}
287+
var queryReplacer = function() {
288+
return userQuery
289+
}
287290
var fullSqlQuery = tableConfig.sql_table.query
288-
.replace(/[$][$]query/g, userQuery)
291+
.replace(/[$][$]query/g, queryReplacer)
289292
.replace("$$pagination", pagination)
290293
.replace(/[$][$]index/g, indices)
291294

@@ -374,12 +377,7 @@ var ElasticsearchManager = (function(){
374377
/** Find/replace lookups */
375378
function incorporateLookupsAndScriptFields_(path, tableConfig, lookups, otherReplacements) {
376379
var tableConfigStr = JSON.stringify(tableConfig)
377-
Object.entries(lookups).forEach(function(kv) {
378-
var lookupStr = kv[0]
379-
var lookupRegex = new RegExp(escapeRegExp_(lookupStr), "g")
380-
var lookupJsonStr = JSON.stringify(kv[1])
381-
tableConfigStr = tableConfigStr.replace(lookupRegex, lookupJsonStr)
382-
})
380+
// Script fields first, since they may contain lookups:
383381
var scriptFields = Util.getJson(tableConfig, [ path, "script_fields" ]) || []
384382
scriptFields.forEach(function(scriptField) {
385383
var lookupStr = `"$$script_field(${scriptField.name})"`
@@ -389,11 +387,28 @@ var ElasticsearchManager = (function(){
389387
source: scriptField.script || "",
390388
params: scriptField.params || {}
391389
})
392-
tableConfigStr = tableConfigStr.replace(lookupRegex, lookupJsonStr)
390+
var replacer = function() {
391+
return lookupJsonStr //(so don't need to worry about special chars in the replacement)
392+
}
393+
tableConfigStr = tableConfigStr.replace(lookupRegex, replacer)
393394
})
395+
// Lookups:
396+
Object.entries(lookups).forEach(function(kv) {
397+
var lookupStr = kv[0]
398+
var lookupRegex = new RegExp(escapeRegExp_(lookupStr), "g")
399+
var lookupJsonStr = JSON.stringify(kv[1])
400+
var replacer = function() {
401+
return lookupJsonStr //(so don't need to worry about special chars in the replacement)
402+
}
403+
tableConfigStr = tableConfigStr.replace(lookupRegex, replacer)
404+
})
405+
// Other:
394406
if (otherReplacements) {
395407
Object.entries(otherReplacements).forEach(function(kv) {
396-
tableConfigStr = tableConfigStr.replace(new RegExp(kv[0], "g"), kv[1])
408+
var replacer = function() {
409+
return kv[1] //(so don't need to worry about special chars in the replacement)
410+
}
411+
tableConfigStr = tableConfigStr.replace(new RegExp(kv[0], "g"), replacer)
397412
})
398413
}
399414
return JSON.parse(tableConfigStr)

0 commit comments

Comments
 (0)