File: 0002-ANSI-BBCode.ansi_to_bbcode-don-t-crash-when-finding-.patch

package info (click to toggle)
ruby-ansi 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: ruby: 1,880; makefile: 5
file content (53 lines) | stat: -rw-r--r-- 1,737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
From: Antonio Terceiro <terceiro@debian.org>
Date: Thu, 24 Oct 2024 11:52:47 -0300
Subject: ANSI::BBCode.ansi_to_bbcode: don't crash when finding a unmatched
 reset ANSI code

---
 lib/ansi/bbcode.rb  | 9 ++++++---
 test/case_bbcode.rb | 5 +++++
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/lib/ansi/bbcode.rb b/lib/ansi/bbcode.rb
index 294f267..045ed8f 100644
--- a/lib/ansi/bbcode.rb
+++ b/lib/ansi/bbcode.rb
@@ -117,8 +117,11 @@ module ANSI
                 ## Pop last tag and form closing tag
                 if ansiname == "reset"
                     lasttag = tagstack.pop
-                    bbname = "/" + String.new( lasttag.split("=")[0] )
-
+                    if lasttag
+                        bbname = "/" + String.new( lasttag.split("=")[0] )
+                    else
+                        bbname = nil
+                    end
                 ## Get corresponding BBCode tag + Push to stack
                 else
                     bbname   = ANSINAME2BBCODE[ansiname]
@@ -126,7 +129,7 @@ module ANSI
                 end
 
                 ## Replace ansi sequence by BBCode tag
-                replace = sprintf("[%s]", bbname)
+                replace = bbname && sprintf("[%s]", bbname) || ""
                 line.sub!(seq, replace)
             end
 
diff --git a/test/case_bbcode.rb b/test/case_bbcode.rb
index 995dddf..0355559 100644
--- a/test/case_bbcode.rb
+++ b/test/case_bbcode.rb
@@ -26,6 +26,11 @@ testcase ANSI::BBCode do
       out = ANSI::BBCode.ansi_to_bbcode(str)
       out.assert == expected
     end
+
+    test "just the reset code" do
+      out = ANSI::BBCode.ansi_to_bbcode("\e[0m")
+      out.assert == "\n"
+    end
   end
 
   class_method :ansi_to_html do