Skip to content

Commit

Permalink
fix parsing strings #389
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Apr 26, 2018
1 parent 8aba622 commit 79b8b3d
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* fix wider characters in IE (#380)[https://github.com/jcubic/jquery.terminal/issues/380]
* fix issue with number of characters when terminal is added to DOM after creation in IE
* fix scrolling on body in Safari
* fix exception when entering JSON with literal strings (#389)[https://github.com/jcubic/jquery.terminal/issues/389]

## 1.14.0

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ http://terminal.jcubic.pl

[![npm](https://img.shields.io/badge/npm-DEV-blue.svg)](https://www.npmjs.com/package/jquery.terminal)
![bower](https://img.shields.io/badge/bower-DEV-yellow.svg)
[![travis](https://travis-ci.org/jcubic/jquery.terminal.svg?branch=devel&8438b44b4f44c63bd6a2c1a5e3ff3f01cec448d0)](https://travis-ci.org/jcubic/jquery.terminal)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&8499de646897bbd4969a7aaff3a674c2)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
[![travis](https://travis-ci.org/jcubic/jquery.terminal.svg?branch=devel&8aba622f40f2cc1609a641a59b01af052f85b570)](https://travis-ci.org/jcubic/jquery.terminal)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&fca6583b00588eb1553fd2207e4c8a6e)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
![downloads](https://img.shields.io/npm/dm/jquery.terminal.svg?style=flat)
[![package quality](http://npm.packagequality.com/shield/jquery.terminal.svg)](http://packagequality.com/#?package=jquery.terminal)
[![](https://data.jsdelivr.com/v1/package/npm/jquery.terminal/badge?style=rounded)](https://www.jsdelivr.com/package/npm/jquery.terminal)
Expand Down
3 changes: 2 additions & 1 deletion css/jquery.terminal-1.14.0.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Copyright (c) 2011-2018 Jakub Jankiewicz <http://jcubic.pl>
* Released under the MIT license
*
* Date: Thu, 05 Apr 2018 15:44:14 +0000
* Date: Thu, 26 Apr 2018 08:59:58 +0000
*/
.terminal .terminal-output .format, .cmd .format,
.cmd .prompt, .cmd .prompt div, .terminal .terminal-output div div{
Expand Down Expand Up @@ -244,6 +244,7 @@ terminal .terminal-output > div {
}
.terminal .terminal-output div.error, .terminal .terminal-output div.error div {
color: red;
color: var(--error-color, red);
}
.tilda {
position: fixed;
Expand Down
4 changes: 2 additions & 2 deletions css/jquery.terminal-1.14.0.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/jquery.terminal.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Copyright (c) 2011-2018 Jakub Jankiewicz <http://jcubic.pl>
* Released under the MIT license
*
* Date: Thu, 05 Apr 2018 15:44:14 +0000
* Date: Thu, 26 Apr 2018 08:59:58 +0000
*/
.terminal .terminal-output .format, .cmd .format,
.cmd .prompt, .cmd .prompt div, .terminal .terminal-output div div{
Expand Down
4 changes: 2 additions & 2 deletions css/jquery.terminal.min.css

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions js/jquery.terminal-1.14.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Copyright (c) 2007-2013 Alexandru Marasteanu <hello at alexei dot ro>
* licensed under 3 clause BSD license
*
* Date: Thu, 05 Apr 2018 15:44:13 +0000
* Date: Thu, 26 Apr 2018 09:27:11 +0000
*/

/* TODO:
Expand Down Expand Up @@ -2589,6 +2589,7 @@
var format_exec_re = /(\[\[(?:[^\]]|\\\])+\]\])/;
var float_re = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
var re_re = /^\/((?:\\\/|[^/]|\[[^\]]*\/[^\]]*\])+)\/([gimy]*)$/;
var string_re = /("(?:[^"\\]|\\(?:\\\\)*")*"|'(?:[^'\\]|\\(?:\\\\)*')*')/;
var unclosed_strings_re = /^(?=((?:[^"']+|"[^"\\]*(?:\\[^][^"\\]*)*"|'[^'\\]*(?:\\[^][^'\\]*)*')*))\1./;
/* eslint-enable */
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -2867,7 +2868,7 @@
}
$.terminal = {
version: 'DEV',
date: 'Thu, 05 Apr 2018 15:44:13 +0000',
date: 'Thu, 26 Apr 2018 09:27:11 +0000',
// colors from http://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -3526,20 +3527,23 @@
// ---------------------------------------------------------------------
parse_argument: function parse_argument(arg, strict) {
function parse_string(string) {
// remove quotes if before are even number of slashes
// we don't remove slases becuase they are handled by JSON.parse
//string = string.replace(/([^\\])['"]$/, '$1');
if (string.match(/^['"]/)) {
// fixing regex to match empty string is not worth it
if (string === '""' || string === "''") {
return '';
}
var quote = string[0];
var re = new RegExp("((^|[^\\\\])(?:\\\\\\\\)*)" + quote, "g");
string = string.replace(re, "$1");
}
// use build in function to parse rest of escaped characters
return JSON.parse('"' + string + '"');
// split string to string literals and non-strings
return string.split(string_re).map(function(string) {
// remove quotes if before are even number of slashes
// we don't remove slases becuase they are handled by JSON.parse
if (string.match(/^['"]/)) {
// fixing regex to match empty string is not worth it
if (string === '""' || string === "''") {
return '';
}
var quote = string[0];
var re = new RegExp("(^|(?:\\\\(?:\\\\)*)?)" + quote, "g");
string = string.replace(re, "$1");
}
string = '"' + string + '"';
// use build in function to parse rest of escaped characters
return JSON.parse(string);
}).join('');
}
if (strict === false) {
if (arg[0] === "'" && arg[arg.length - 1] === "'") {
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal-1.14.0.min.js

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions js/jquery.terminal-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,7 @@
var format_exec_re = /(\[\[(?:[^\]]|\\\])+\]\])/;
var float_re = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
var re_re = /^\/((?:\\\/|[^/]|\[[^\]]*\/[^\]]*\])+)\/([gimy]*)$/;
var string_re = /("(?:[^"\\]|\\(?:\\\\)*")*"|'(?:[^'\\]|\\(?:\\\\)*')*')/;
var unclosed_strings_re = /^(?=((?:[^"']+|"[^"\\]*(?:\\[^][^"\\]*)*"|'[^'\\]*(?:\\[^][^'\\]*)*')*))\1./;
/* eslint-enable */
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -3526,20 +3527,23 @@
// ---------------------------------------------------------------------
parse_argument: function parse_argument(arg, strict) {
function parse_string(string) {
// remove quotes if before are even number of slashes
// we don't remove slases becuase they are handled by JSON.parse
//string = string.replace(/([^\\])['"]$/, '$1');
if (string.match(/^['"]/)) {
// fixing regex to match empty string is not worth it
if (string === '""' || string === "''") {
return '';
}
var quote = string[0];
var re = new RegExp("((^|[^\\\\])(?:\\\\\\\\)*)" + quote, "g");
string = string.replace(re, "$1");
}
// use build in function to parse rest of escaped characters
return JSON.parse('"' + string + '"');
// split string to string literals and non-strings
return string.split(string_re).map(function(string) {
// remove quotes if before are even number of slashes
// we don't remove slases becuase they are handled by JSON.parse
if (string.match(/^['"]/)) {
// fixing regex to match empty string is not worth it
if (string === '""' || string === "''") {
return '';
}
var quote = string[0];
var re = new RegExp("(^|(?:\\\\(?:\\\\)*)?)" + quote, "g");
string = string.replace(re, "$1");
}
string = '"' + string + '"';
// use build in function to parse rest of escaped characters
return JSON.parse(string);
}).join('');
}
if (strict === false) {
if (arg[0] === "'" && arg[arg.length - 1] === "'") {
Expand Down
36 changes: 20 additions & 16 deletions js/jquery.terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Copyright (c) 2007-2013 Alexandru Marasteanu <hello at alexei dot ro>
* licensed under 3 clause BSD license
*
* Date: Thu, 05 Apr 2018 15:44:13 +0000
* Date: Thu, 26 Apr 2018 09:27:11 +0000
*/

/* TODO:
Expand Down Expand Up @@ -2589,6 +2589,7 @@
var format_exec_re = /(\[\[(?:[^\]]|\\\])+\]\])/;
var float_re = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
var re_re = /^\/((?:\\\/|[^/]|\[[^\]]*\/[^\]]*\])+)\/([gimy]*)$/;
var string_re = /("(?:[^"\\]|\\(?:\\\\)*")*"|'(?:[^'\\]|\\(?:\\\\)*')*')/;
var unclosed_strings_re = /^(?=((?:[^"']+|"[^"\\]*(?:\\[^][^"\\]*)*"|'[^'\\]*(?:\\[^][^'\\]*)*')*))\1./;
/* eslint-enable */
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -2867,7 +2868,7 @@
}
$.terminal = {
version: 'DEV',
date: 'Thu, 05 Apr 2018 15:44:13 +0000',
date: 'Thu, 26 Apr 2018 09:27:11 +0000',
// colors from http://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -3526,20 +3527,23 @@
// ---------------------------------------------------------------------
parse_argument: function parse_argument(arg, strict) {
function parse_string(string) {
// remove quotes if before are even number of slashes
// we don't remove slases becuase they are handled by JSON.parse
//string = string.replace(/([^\\])['"]$/, '$1');
if (string.match(/^['"]/)) {
// fixing regex to match empty string is not worth it
if (string === '""' || string === "''") {
return '';
}
var quote = string[0];
var re = new RegExp("((^|[^\\\\])(?:\\\\\\\\)*)" + quote, "g");
string = string.replace(re, "$1");
}
// use build in function to parse rest of escaped characters
return JSON.parse('"' + string + '"');
// split string to string literals and non-strings
return string.split(string_re).map(function(string) {
// remove quotes if before are even number of slashes
// we don't remove slases becuase they are handled by JSON.parse
if (string.match(/^['"]/)) {
// fixing regex to match empty string is not worth it
if (string === '""' || string === "''") {
return '';
}
var quote = string[0];
var re = new RegExp("(^|(?:\\\\(?:\\\\)*)?)" + quote, "g");
string = string.replace(re, "$1");
}
string = '"' + string + '"';
// use build in function to parse rest of escaped characters
return JSON.parse(string);
}).join('');
}
if (strict === false) {
if (arg[0] === "'" && arg[arg.length - 1] === "'") {
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal.min.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions spec/terminalSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ function tests_on_ready() {
rest: '"foo bar" baz /^asd [x]/ str\\ str 10 1e10 ""'
});
});
it('should handle JSON string', function() {
var cmd = jQuery.terminal.parse_command('{"demo": ["error"]}');
expect(cmd).toEqual({
command: '{"demo": ["error"]}',
name: '{"demo":',
args: ['[error]}'],
args_quotes: [""],
rest: '["error"]}'
});
});
});
var ansi_string = '\x1b[2;31;46mFoo\x1b[1;3;4;32;45mB[[sb;;]a]r\x1b[0m\x1b[7mBaz';
describe('$.terminal.from_ansi', function() {
Expand Down

0 comments on commit 79b8b3d

Please sign in to comment.