alasql 0.5.12 → 0.5.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/alasql-cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  //
3
3
  // Command line interface for Alasql
4
- // Version: 0.5.12
4
+ // Version: 0.5.13
5
5
  // Date: 28.07.2015
6
6
  // (c) 2014-2015, Andrey Gershun & M. Rangel Wulff
7
7
  //
@@ -1,7 +1,7 @@
1
- //! AlaSQL v0.5.12 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v0.5.13 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 0.5.12
4
+ @version 0.5.13
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
@@ -1,2 +1,2 @@
1
- //! AlaSQL v0.5.12 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v0.5.13 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  !function(r,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():r.alasql=e()}(this,function(){function a(r,e,t){"function"==typeof(e=e||[])&&(scope=t,t=e,e=[]),"object"!=typeof e&&(e=[e]);var o=a.lastid++;a.buffer[o]=t,a.webworker.postMessage({id:o,sql:r,params:e})}if(a.options={},a.options.progress=function(){},isArray=function(r){return"[object Array]"===Object.prototype.toString.call(r)},a.promise=function(){throw new Error("Please include a Promise/A+ library")},"undefined"!=typeof Promise){function i(r,e,n,s){return new Promise(function(t,o){a(r,e,function(r,e){e?o(e):(n&&s&&!1!==a.options.progress&&a.options.progress(n,s),t(r))})})}a.promise=function(r,e){if("undefined"==typeof Promise)throw new Error("Please include a Promise/A+ library");if("string"==typeof r)return i(r,e);if(!isArray(r)||r.length<1||void 0!==e)throw new Error("Error in .promise parameters");return function(r){if(!(r.length<1)){for(var e,t,o,n=[],s=0;s<r.length;s++){if("string"==typeof(e=r[s])&&(e=[e]),!isArray(e)||e.length<1||2<e.length)throw new Error("Error in .promise parameter");t=e[0],o=e[1]||void 0,n.push(i(t,o,s,r.length))}return Promise.all(n)}}(r)}}if(!(a=a||!1))throw new Error("alasql was not found");return a.worker=function(){throw new Error("Can find webworker in this enviroment")},"undefined"!=typeof Worker&&(a.worker=function(r,e,t){if(!0===r&&(r=void 0),void 0===r)for(var o=document.getElementsByTagName("script"),n=0;n<o.length;n++){if("alasql-worker.js"===o[n].src.substr(-16).toLowerCase()){r=o[n].src.substr(0,o[n].src.length-16)+"alasql.js";break}if("alasql-worker.min.js"===o[n].src.substr(-20).toLowerCase()){r=o[n].src.substr(0,o[n].src.length-20)+"alasql.min.js";break}if("alasql.js"===o[n].src.substr(-9).toLowerCase()){r=o[n].src;break}if("alasql.min.js"===o[n].src.substr(-13).toLowerCase()){r=o[n].src.substr(0,o[n].src.length-13)+"alasql.min.js";break}}if(void 0===r)throw new Error("Path to alasql.js is not specified");if(!1!==r){var s="importScripts('";s+=r,s+="');self.onmessage = function(event) {alasql(event.data.sql,event.data.params, function(data){postMessage({id:event.data.id, data:data});});}";var i=new Blob([s],{type:"text/plain"});if(a.webworker=new Worker(URL.createObjectURL(i)),a.webworker.onmessage=function(r){var e=r.data.id;a.buffer[e](r.data.data),delete a.buffer[e]},a.webworker.onerror=function(r){throw r},1<arguments.length)a("REQUIRE "+e.map(function(r){return'"'+r+'"'}).join(","),[],t)}else if(!1===r)return void delete a.webworker}),a.lastid=0,a.buffer={},a.worker(),a});
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v0.5.12 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v0.5.13 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 0.5.12
4
+ @version 0.5.13
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
@@ -142,7 +142,7 @@ var alasql = function(sql, params, cb, scope) {
142
142
  Current version of alasql
143
143
  @constant {string}
144
144
  */
145
- alasql.version = '0.5.12';
145
+ alasql.version = '0.5.13';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -4427,9 +4427,201 @@ alasql.parser.parseError = function(str, hash) {
4427
4427
  }
4428
4428
  */
4429
4429
  alasql.parse = function(sql) {
4430
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4430
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4431
+ checkSqlNames(ast);
4432
+ return ast;
4431
4433
  };
4432
4434
 
4435
+ /*
4436
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4437
+ holds a quote, a backslash or a line break would end the string and make the rest code
4438
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4439
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4440
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4441
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4442
+ of a JSON value are data, not names.
4443
+ */
4444
+ var checkSqlNames = (function () {
4445
+ var hasOwn = Object.prototype.hasOwnProperty;
4446
+ var badName = /['\\\n\r\u2028\u2029]/;
4447
+ var nameKeys = {
4448
+ as: 1,
4449
+ columnid: 1,
4450
+ tableid: 1,
4451
+ databaseid: 1,
4452
+ viewid: 1,
4453
+ indexid: 1,
4454
+ trigger: 1,
4455
+ variable: 1,
4456
+ dropcolumn: 1,
4457
+ renamecolumn: 1,
4458
+ renameto: 1,
4459
+ to: 1,
4460
+ tocolumnid: 1,
4461
+ forcolumnid: 1,
4462
+ };
4463
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4464
+
4465
+ function checkName(name) {
4466
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4467
+ }
4468
+
4469
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4470
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4471
+ function checkOrder(columns, order) {
4472
+ var aliases = {};
4473
+ (columns || []).forEach(function (col) {
4474
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4475
+ });
4476
+ order.forEach(function (ord) {
4477
+ var e = ord && ord.expression;
4478
+ var name;
4479
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4480
+ if (typeof name === 'string') {
4481
+ if (!hasOwn.call(aliases, name)) checkName(name);
4482
+ return;
4483
+ }
4484
+ checkNames(ord, false, false);
4485
+ });
4486
+ }
4487
+
4488
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4489
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4490
+ // graph item, name is data.
4491
+ function entryOf(node, key) {
4492
+ var yy = alasql.yy;
4493
+ if (
4494
+ (node instanceof yy.Select && key === 'columns') ||
4495
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4496
+ (node.srchid === 'RETURN' && key === 'args')
4497
+ )
4498
+ return 'alias';
4499
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4500
+ return '';
4501
+ }
4502
+
4503
+ // Names that never reach generated code: they are used only at run time, as the key of a
4504
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4505
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4506
+ // names are free (what they hold is still walked), and skipChild are children that are not
4507
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4508
+ var varValueFree = false;
4509
+ var intoVarFree = false;
4510
+ var freeKeys = [
4511
+ ['CreateIndex', {indexid: 1}],
4512
+ ['DropIndex', {indexid: 1}],
4513
+ ['Reindex', {indexid: 1}],
4514
+ ['CreateTrigger', {trigger: 1}],
4515
+ ['DropTrigger', {trigger: 1}],
4516
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4517
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4518
+ ['DetachDatabase', {databaseid: 1}],
4519
+ ['DropDatabase', {databaseid: 1}],
4520
+ ['ShowTables', {databaseid: 1}],
4521
+ ['ShowColumns', {databaseid: 1}],
4522
+ ['ShowIndex', {databaseid: 1}],
4523
+ ['ShowCreateTable', {databaseid: 1}],
4524
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4525
+ ];
4526
+ var freeChild = [
4527
+ ['CreateTable', {table: 1}],
4528
+ ['DropTable', {tables: 1}],
4529
+ ['TruncateTable', {table: 1}],
4530
+ ['AlterTable', {table: 1}],
4531
+ ['ShowColumns', {table: 1}],
4532
+ ['ShowIndex', {table: 1}],
4533
+ ['ShowCreateTable', {table: 1}],
4534
+ ['CreateIndex', {table: 1}],
4535
+ ['CreateTrigger', {table: 1}],
4536
+ ['Declare', {declares: 1}],
4537
+ ];
4538
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4539
+
4540
+ function freeOf(list, node) {
4541
+ var yy = alasql.yy;
4542
+ for (var i = 0; i < list.length; i++) {
4543
+ var cls = yy[list[i][0]];
4544
+ if (cls && node instanceof cls) return list[i][1];
4545
+ }
4546
+ return null;
4547
+ }
4548
+
4549
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4550
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4551
+ function freeVar(node, key, entry) {
4552
+ var yy = alasql.yy;
4553
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4554
+ if (entry === 'source') return false;
4555
+ if (entry === 'into') return intoVarFree;
4556
+ return varValueFree;
4557
+ }
4558
+
4559
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4560
+ function freeSet(node, key) {
4561
+ var yy = alasql.yy;
4562
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4563
+ }
4564
+
4565
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4566
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4567
+ // names never reach generated code.
4568
+ function checkNames(node, data, entry) {
4569
+ if (node === null || typeof node !== 'object') return;
4570
+ var i;
4571
+ if (Array.isArray(node)) {
4572
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4573
+ return;
4574
+ }
4575
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4576
+ if (data && !isNode) {
4577
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4578
+ return;
4579
+ }
4580
+ var yy = alasql.yy;
4581
+ if (node instanceof yy.Json) {
4582
+ checkNames(node.value, true, false);
4583
+ return;
4584
+ }
4585
+ var fk = freeOf(freeKeys, node);
4586
+ var fc = freeOf(freeChild, node);
4587
+ var sc = freeOf(skipChild, node);
4588
+ for (var key in node) {
4589
+ if (!hasOwn.call(node, key)) continue;
4590
+ var value = node[key];
4591
+ if (sc && sc[key] === 1) continue;
4592
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4593
+ checkOrder(node.columns, value);
4594
+ continue;
4595
+ }
4596
+ // the column list of a WITH entry renames the result at run time
4597
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4598
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4599
+ if (typeof value === 'string') {
4600
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4601
+ continue;
4602
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4603
+ checkName(value);
4604
+ } else if (value !== null && typeof value === 'object') {
4605
+ if (Array.isArray(value)) {
4606
+ for (i = 0; i < value.length; i++) {
4607
+ if (typeof value[i] === 'string') {
4608
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4609
+ } else {
4610
+ checkNames(value[i], false, child || entryOf(node, key));
4611
+ }
4612
+ }
4613
+ } else {
4614
+ checkNames(value, false, child);
4615
+ }
4616
+ }
4617
+ }
4618
+ }
4619
+
4620
+ return function (ast) {
4621
+ checkNames(ast, false, false);
4622
+ };
4623
+ })();
4624
+
4433
4625
  /**
4434
4626
  List of engines of external databases
4435
4627
  @type {object}
@@ -8011,7 +8203,7 @@ yy.Select.prototype.compile = function(databaseid, params) {
8011
8203
  // Save data into parameters array
8012
8204
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
8013
8205
  //
8014
- query.intofns = "params['" + this.into.param + "'].push(r)";
8206
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
8015
8207
  }
8016
8208
 
8017
8209
  if (query.intofns) {
@@ -8418,7 +8610,7 @@ yy.Select.prototype.compileFrom = function(query) {
8418
8610
  return res;
8419
8611
  };
8420
8612
  } else if (tq instanceof yy.ParamValue) {
8421
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8613
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8422
8614
 
8423
8615
  if (tq.array) ps += ',true';
8424
8616
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8673,7 +8865,7 @@ yy.Select.prototype.compileJoins = function(query) {
8673
8865
  // source.data = ;
8674
8866
  var jnparam = jn.param.param;
8675
8867
 
8676
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8868
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8677
8869
  if (jn.array) ps += ',true';
8678
8870
  ps += ');if(cb)res=cb(res, idx, query);return res';
8679
8871
 
@@ -9377,16 +9569,17 @@ function compileSelectStar(query, aliases, joinstar) {
9377
9569
 
9378
9570
  if (columns && columns.length > 0) {
9379
9571
  columns.forEach(function(tcol) {
9572
+ var escapedColumnId = escapeq(tcol.columnid);
9380
9573
  if (joinstar && alasql.options.joinstar == 'underscore') {
9381
9574
  ss.push(
9382
9575
  "'" +
9383
9576
  alias +
9384
9577
  '_' +
9385
- tcol.columnid +
9578
+ escapedColumnId +
9386
9579
  "':p['" +
9387
9580
  alias +
9388
9581
  "']['" +
9389
- tcol.columnid +
9582
+ escapedColumnId +
9390
9583
  "']"
9391
9584
  );
9392
9585
  } else if (joinstar && alasql.options.joinstar == 'json') {
@@ -9395,16 +9588,16 @@ function compileSelectStar(query, aliases, joinstar) {
9395
9588
  "r['" +
9396
9589
  alias +
9397
9590
  "']['" +
9398
- tcol.columnid +
9591
+ escapedColumnId +
9399
9592
  "']=p['" +
9400
9593
  alias +
9401
9594
  "']['" +
9402
- tcol.columnid +
9595
+ escapedColumnId +
9403
9596
  "'];";
9404
9597
  } else {
9405
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
9598
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
9406
9599
  if (!columnIds[tcol.columnid]) {
9407
- var key = "'" + tcol.columnid + "':";
9600
+ var key = "'" + escapedColumnId + "':";
9408
9601
  ss.push(key + value);
9409
9602
  columnIds[tcol.columnid] = {
9410
9603
  id: ss.length - 1,
@@ -9439,7 +9632,7 @@ function compileSelectStar(query, aliases, joinstar) {
9439
9632
  } else {
9440
9633
 
9441
9634
  // if column not exist, then copy all
9442
- sp += 'var w=p["' + alias + '"];for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
9635
+ sp += 'var w=p["' + escapeq(alias) + '"];for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
9443
9636
 
9444
9637
  query.dirtyColumns = true;
9445
9638
  }
@@ -9467,7 +9660,7 @@ yy.Select.prototype.compileSelect1 = function(query, params) {
9467
9660
  if (col.func) {
9468
9661
  sp +=
9469
9662
  "r=params['" +
9470
- col.param +
9663
+ escapeq(col.param) +
9471
9664
  "'](p['" +
9472
9665
  query.sources[0].alias +
9473
9666
  "'],p,params,alasql);";
@@ -9528,7 +9721,7 @@ yy.Select.prototype.compileSelect1 = function(query, params) {
9528
9721
  if (isMultisheetParam) {
9529
9722
  sp =
9530
9723
  'var r={};var w=p["' +
9531
- tbid +
9724
+ escapeq(tbid) +
9532
9725
  '"];' +
9533
9726
  'var cols=[' +
9534
9727
  self.columns
@@ -9539,7 +9732,7 @@ yy.Select.prototype.compileSelect1 = function(query, params) {
9539
9732
  '];var colas=[' +
9540
9733
  self.columns
9541
9734
  .map(function(col) {
9542
- return "'" + (col.as || col.columnid) + "'";
9735
+ return "'" + escapeq(col.as || col.columnid) + "'";
9543
9736
  })
9544
9737
  .join(',') +
9545
9738
  '];' +
@@ -9704,9 +9897,9 @@ yy.Select.prototype.compileSelect2 = function(query, params) {
9704
9897
  this.orderColumns.forEach(function(v, idx) {
9705
9898
  var key = '$$$' + idx;
9706
9899
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9707
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9900
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9708
9901
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9709
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9902
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9710
9903
  } else {
9711
9904
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9712
9905
  }
@@ -9799,7 +9992,7 @@ yy.Select.prototype.compileSelectGroup1 = function(query) {
9799
9992
  query.groupColumns[colas] = col.nick;
9800
9993
 
9801
9994
  // if(col.as) {
9802
- s += "r['" + colas + "']=";
9995
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9803
9996
  // // } else {
9804
9997
  // // s += 'r[\''+escapeq()+'\']=';
9805
9998
  // // };
@@ -9826,7 +10019,7 @@ yy.Select.prototype.compileSelectGroup2 = function(query) {
9826
10019
  self.columns.forEach(function(col) {
9827
10020
 
9828
10021
  if (query.ingroup.indexOf(col.nick) > -1) {
9829
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
10022
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9830
10023
  }
9831
10024
  });
9832
10025
 
@@ -9836,7 +10029,7 @@ yy.Select.prototype.compileSelectGroup2 = function(query) {
9836
10029
  var key = '$$$' + idx;
9837
10030
 
9838
10031
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9839
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10032
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9840
10033
  } else {
9841
10034
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9842
10035
  }
@@ -10889,7 +11082,7 @@ yy.Op.prototype.toJS = function(context, tableid, defcols) {
10889
11082
  var ljs = '(' + leftJS() + '||{})';
10890
11083
 
10891
11084
  if (typeof this.right === 'string') {
10892
- s = ljs + '["' + this.right + '"]';
11085
+ s = ljs + '["' + escapeq(this.right) + '"]';
10893
11086
  } else if (typeof this.right === 'number') {
10894
11087
  s = ljs + '[' + this.right + ']';
10895
11088
  } else if (this.right instanceof yy.FuncValue) {
@@ -10910,7 +11103,7 @@ yy.Op.prototype.toJS = function(context, tableid, defcols) {
10910
11103
  'alasql.databases[alasql.useid].objects[' +
10911
11104
  leftJS() +
10912
11105
  ']["' +
10913
- this.right +
11106
+ escapeq(this.right) +
10914
11107
  '"]';
10915
11108
  }
10916
11109
  // TODO - add other cases
@@ -11252,7 +11445,7 @@ yy.ParamValue.prototype.toString = function() {
11252
11445
  };
11253
11446
  yy.ParamValue.prototype.toJS = function() {
11254
11447
  if (typeof this.param === 'string') {
11255
- return "params['" + this.param + "']";
11448
+ return "params['" + escapeq(this.param) + "']";
11256
11449
  }
11257
11450
 
11258
11451
  return 'params[' + this.param + ']';
@@ -12312,9 +12505,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function(obj) {
12312
12505
  return s;
12313
12506
  });
12314
12507
 
12508
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12509
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12510
+ // are escaped, so the string cannot end early.
12511
+ function escapeJSONString(s) {
12512
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12513
+ if (c.length === 2) return c;
12514
+ if (c === '\\') return '\\\\';
12515
+ if (c === '"') return '\\"';
12516
+ if (c === '\n') return '\\n';
12517
+ if (c === '\r') return '\\r';
12518
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12519
+ });
12520
+ }
12521
+
12315
12522
  function JSONtoJS(obj, context, tableid, defcols) {
12316
12523
  var s = '';
12317
- if (typeof obj == 'string') s = '"' + obj + '"';
12524
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12318
12525
  else if (typeof obj == 'number') s = '(' + obj + ')';
12319
12526
  else if (typeof obj == 'boolean') s = obj;
12320
12527
  else if (typeof obj == 'object') {
@@ -12333,7 +12540,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12333
12540
  var ss = [];
12334
12541
  for (var k in obj) {
12335
12542
  var s1 = '';
12336
- if (typeof k == 'string') s1 += '"' + k + '"';
12543
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12337
12544
  else if (typeof k == 'number') s1 += k;
12338
12545
  else if (typeof k == 'boolean') s1 += k;
12339
12546
  else {