aboutsummaryrefslogtreecommitdiff
path: root/poll.rb
diff options
context:
space:
mode:
authorBenjamin Kellermann <Benjamin.Kellermann@gmx.de>2010-07-19 16:12:03 +0200
committerBenjamin Kellermann <Benjamin.Kellermann@gmx.de>2010-07-19 16:12:03 +0200
commit231da940da1842740b842a06d126d3562b5b7822 (patch)
tree2add80022678e65e1d3a354ad1466c494bfc5c90 /poll.rb
parent51323c7866f43a6edf7196bff5dd90bb55944395 (diff)
bugfix: htmlid works same as in javascript
Diffstat (limited to 'poll.rb')
-rw-r--r--poll.rb30
1 files changed, 29 insertions, 1 deletions
diff --git a/poll.rb b/poll.rb
index 37e9eae..14f30d5 100644
--- a/poll.rb
+++ b/poll.rb
@@ -25,8 +25,23 @@ require "timepollhead"
$KCODE = "u"
class String
+ @@htmlidcache = {}
+ @@htmlidncache = {}
+
+ # FIXME: htmlid should not depend on the order it is requested
def to_htmlID
- CGI.escapeHTML(self.gsub(/[^A-Za-z0-9_:.\-]/,"."))
+ if @@htmlidcache[self]
+ id = @@htmlidcache[self]
+ else
+ id = self.gsub(/[^A-Za-z0-9_:.\-]/,".")
+ if @@htmlidncache[id]
+ @@htmlidncache[id] += 1
+ id += @@htmlidncache[id].to_s
+ end
+ @@htmlidncache[id] = -1
+ @@htmlidcache[self] = id
+ end
+ return id
end
end
class Poll
@@ -510,4 +525,17 @@ class PollTest < Test::Unit::TestCase
end
end
+class StringTest < Test::Unit::TestCase
+ def test_htmlid
+ assert_equal("foo.bar.", "foo bar ".to_htmlID);
+ assert_equal("foo.bar.", "foo bar ".to_htmlID);
+ assert_equal("foo.bar.0", "foo.bar ".to_htmlID);
+ assert_equal("foo.bar.00", "foo.bar 0".to_htmlID);
+ assert_equal("foo.bar.", "foo bar ".to_htmlID);
+ assert_equal("foo.bar.1", "foo bar.".to_htmlID);
+ assert_equal("foo.bar.2", "foo?bar.".to_htmlID);
+ assert_equal("foo.bar.3", "foo bar?".to_htmlID);
+ assert_equal("foo.bar.2", "foo?bar.".to_htmlID);
+ end
+end
end