Skip to content

Commit

Permalink
Fixes #72
Browse files Browse the repository at this point in the history
  • Loading branch information
drudru committed Mar 16, 2023
1 parent aa1627c commit 94cc84b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
11 changes: 8 additions & 3 deletions ansi_up.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ var AnsiUp = (function () {
return pkt;
}
if (pos == 0) {
if (len == 1) {
if (len < 3) {
pkt.kind = PacketKind.Incomplete;
return pkt;
}
var next_char = this._buffer.charAt(1);
if ((next_char != '[') && (next_char != ']')) {
if ((next_char != '[') && (next_char != ']') && (next_char != '(')) {
pkt.kind = PacketKind.ESC;
pkt.text = this._buffer.slice(0, 1);
this._buffer = this._buffer.slice(1);
Expand Down Expand Up @@ -186,7 +186,7 @@ var AnsiUp = (function () {
this._buffer = this._buffer.slice(rpos);
return pkt;
}
if (next_char == ']') {
else if (next_char == ']') {
if (len < 4) {
pkt.kind = PacketKind.Incomplete;
return pkt;
Expand Down Expand Up @@ -245,6 +245,11 @@ var AnsiUp = (function () {
this._buffer = this._buffer.slice(rpos);
return pkt;
}
else if (next_char == '(') {
pkt.kind = PacketKind.Unknown;
this._buffer = this._buffer.slice(3);
return pkt;
}
}
};
AnsiUp.prototype.ansi_to_html = function (txt) {
Expand Down
23 changes: 18 additions & 5 deletions ansi_up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ class AnsiUp
// NOW WE HANDLE ESCAPES
if (pos == 0)
{

if (len == 1) // Lone ESC in Buffer, We don't know yet
// All of the sequences typically need at least 3 characters
// So, wait until we have at least that many
if (len < 3)
{
pkt.kind = PacketKind.Incomplete;
return pkt;
Expand All @@ -236,8 +237,8 @@ class AnsiUp
var next_char = this._buffer.charAt(1);

// We treat this as a single ESC
// Which effecitvely shows
if ((next_char != '[') && (next_char != ']')) // DeMorgan
// No transformation
if ((next_char != '[') && (next_char != ']') && (next_char != '('))
{
pkt.kind = PacketKind.ESC;
pkt.text = this._buffer.slice(0, 1);
Expand Down Expand Up @@ -333,7 +334,7 @@ class AnsiUp
this._buffer = this._buffer.slice(rpos);
return pkt;
}

else
// OSC CHECK
if (next_char == ']')
{
Expand Down Expand Up @@ -507,6 +508,18 @@ class AnsiUp
this._buffer = this._buffer.slice(rpos);
return pkt;
}
else
// Other ESC CHECK
if (next_char == '(')
{
// This specifies the character set, which
// should just be ignored

// We have at least 3, so drop the sequence
pkt.kind = PacketKind.Unknown;
this._buffer = this._buffer.slice(3);
return pkt;
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions dist/ansi_up.js.include
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ var AnsiUp = (function () {
return pkt;
}
if (pos == 0) {
if (len == 1) {
if (len < 3) {
pkt.kind = PacketKind.Incomplete;
return pkt;
}
var next_char = this._buffer.charAt(1);
if ((next_char != '[') && (next_char != ']')) {
if ((next_char != '[') && (next_char != ']') && (next_char != '(')) {
pkt.kind = PacketKind.ESC;
pkt.text = this._buffer.slice(0, 1);
this._buffer = this._buffer.slice(1);
Expand Down Expand Up @@ -167,7 +167,7 @@ var AnsiUp = (function () {
this._buffer = this._buffer.slice(rpos);
return pkt;
}
if (next_char == ']') {
else if (next_char == ']') {
if (len < 4) {
pkt.kind = PacketKind.Incomplete;
return pkt;
Expand Down Expand Up @@ -226,6 +226,11 @@ var AnsiUp = (function () {
this._buffer = this._buffer.slice(rpos);
return pkt;
}
else if (next_char == '(') {
pkt.kind = PacketKind.Unknown;
this._buffer = this._buffer.slice(3);
return pkt;
}
}
};
AnsiUp.prototype.ansi_to_html = function (txt) {
Expand Down
2 changes: 1 addition & 1 deletion dist/ansi_up.js.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions test/ansi_up-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,18 @@ describe('ansi_up', function () {
});
});

describe('ignore controls beginning with ESC', function () {

it('Designate G0 Character Set (UK)', function () {
var start = "foo\033(Abar";
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql('foobar');
});

});


describe('buffering situations', function () {

it('should transform an incomplete prefix', function () {
Expand Down

0 comments on commit 94cc84b

Please sign in to comment.