Skip to content

Commit

Permalink
move normalization from tracking_replace to apply_formatters #427
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Sep 1, 2018
1 parent b9ccc33 commit 4f9b7b5
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 26 deletions.
46 changes: 38 additions & 8 deletions js/jquery.terminal-1.21.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* emoji regex v7.0.0 by Mathias Bynens
* MIT license
*
* Date: Sat, 01 Sep 2018 11:09:17 +0000
* Date: Sat, 01 Sep 2018 11:55:49 +0000
*/

/* TODO:
Expand Down Expand Up @@ -2861,6 +2861,33 @@
return result;
}
// -------------------------------------------------------------------------
// normalize position for counting emoji
// -------------------------------------------------------------------------
function normalize_position(string, position) {
if (position === 0) {
return position;
}
string = $.terminal.strip(string);
var result = split_characters(string).reduce(function(acc, chr) {
if (typeof acc === 'number') {
return acc;
}
var length = acc.length + chr.length;
if (length >= position) {
return acc.position + 1;
}
return {
position: acc.position + 1,
length: length
};
}, {position: 0, length: 0});
if (typeof result === 'number') {
return result;
} else {
return result.position;
}
}
// -------------------------------------------------------------------------
function char_width_prop(len, options) {
if (is_ch_unit_supported) {
return 'width: ' + len + 'ch';
Expand Down Expand Up @@ -3142,7 +3169,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Sat, 01 Sep 2018 11:09:17 +0000',
date: 'Sat, 01 Sep 2018 11:55:49 +0000',
// colors from http://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -3230,10 +3257,10 @@
// ---------------------------------------------------------------------
tracking_replace: function tracking_replace(string, rex, replacement, position) {
function substring(string, start, end) {
return $.terminal.substring(string, start, end);
return string.substring(start, end);
}
function length(string) {
return $.terminal.length(string);
return $.terminal.strip(string).length;
}
// we need to correct position from regex and match to take into account
// characters that are created from more when one code point like
Expand Down Expand Up @@ -3804,17 +3831,20 @@
} else {
position = position_partial[1];
}
// to make sure that output position is not outside the string
if (position >= $.terminal.length(input[0])) {
position = $.terminal.length(string);
}
if (string === input[0]) {
return input;
}
return [string, position];
}
}, input);
if (typeof settings.position === 'number') {
var position = result[1];
position = normalize_position(string, position);
var max = $.terminal.length(result[0]);
if (position > max) {
position = max;
}
result[1] = position;
return result;
} else {
return result[0];
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal-1.21.0.min.js

Large diffs are not rendered by default.

42 changes: 36 additions & 6 deletions js/jquery.terminal-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,33 @@
return result;
}
// -------------------------------------------------------------------------
// normalize position for counting emoji
// -------------------------------------------------------------------------
function normalize_position(string, position) {
if (position === 0) {
return position;
}
string = $.terminal.strip(string);
var result = split_characters(string).reduce(function(acc, chr) {
if (typeof acc === 'number') {
return acc;
}
var length = acc.length + chr.length;
if (length >= position) {
return acc.position + 1;
}
return {
position: acc.position + 1,
length: length
};
}, {position: 0, length: 0});
if (typeof result === 'number') {
return result;
} else {
return result.position;
}
}
// -------------------------------------------------------------------------
function char_width_prop(len, options) {
if (is_ch_unit_supported) {
return 'width: ' + len + 'ch';
Expand Down Expand Up @@ -3230,10 +3257,10 @@
// ---------------------------------------------------------------------
tracking_replace: function tracking_replace(string, rex, replacement, position) {
function substring(string, start, end) {
return $.terminal.substring(string, start, end);
return string.substring(start, end);
}
function length(string) {
return $.terminal.length(string);
return $.terminal.strip(string).length;
}
// we need to correct position from regex and match to take into account
// characters that are created from more when one code point like
Expand Down Expand Up @@ -3804,17 +3831,20 @@
} else {
position = position_partial[1];
}
// to make sure that output position is not outside the string
if (position >= $.terminal.length(input[0])) {
position = $.terminal.length(string);
}
if (string === input[0]) {
return input;
}
return [string, position];
}
}, input);
if (typeof settings.position === 'number') {
var position = result[1];
position = normalize_position(string, position);
var max = $.terminal.length(result[0]);
if (position > max) {
position = max;
}
result[1] = position;
return result;
} else {
return result[0];
Expand Down
46 changes: 38 additions & 8 deletions js/jquery.terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* emoji regex v7.0.0 by Mathias Bynens
* MIT license
*
* Date: Sat, 01 Sep 2018 11:09:17 +0000
* Date: Sat, 01 Sep 2018 11:55:49 +0000
*/

/* TODO:
Expand Down Expand Up @@ -2861,6 +2861,33 @@
return result;
}
// -------------------------------------------------------------------------
// normalize position for counting emoji
// -------------------------------------------------------------------------
function normalize_position(string, position) {
if (position === 0) {
return position;
}
string = $.terminal.strip(string);
var result = split_characters(string).reduce(function(acc, chr) {
if (typeof acc === 'number') {
return acc;
}
var length = acc.length + chr.length;
if (length >= position) {
return acc.position + 1;
}
return {
position: acc.position + 1,
length: length
};
}, {position: 0, length: 0});
if (typeof result === 'number') {
return result;
} else {
return result.position;
}
}
// -------------------------------------------------------------------------
function char_width_prop(len, options) {
if (is_ch_unit_supported) {
return 'width: ' + len + 'ch';
Expand Down Expand Up @@ -3142,7 +3169,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Sat, 01 Sep 2018 11:09:17 +0000',
date: 'Sat, 01 Sep 2018 11:55:49 +0000',
// colors from http://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -3230,10 +3257,10 @@
// ---------------------------------------------------------------------
tracking_replace: function tracking_replace(string, rex, replacement, position) {
function substring(string, start, end) {
return $.terminal.substring(string, start, end);
return string.substring(start, end);
}
function length(string) {
return $.terminal.length(string);
return $.terminal.strip(string).length;
}
// we need to correct position from regex and match to take into account
// characters that are created from more when one code point like
Expand Down Expand Up @@ -3804,17 +3831,20 @@
} else {
position = position_partial[1];
}
// to make sure that output position is not outside the string
if (position >= $.terminal.length(input[0])) {
position = $.terminal.length(string);
}
if (string === input[0]) {
return input;
}
return [string, position];
}
}, input);
if (typeof settings.position === 'number') {
var position = result[1];
position = normalize_position(string, position);
var max = $.terminal.length(result[0]);
if (position > max) {
position = max;
}
result[1] = position;
return result;
} else {
return result[0];
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal.min.js

Large diffs are not rendered by default.

0 comments on commit 4f9b7b5

Please sign in to comment.