diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2021-04-09 15:07:46 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 15:07:46 -0600 |
| commit | ac3391dba2ff630760a67b945ff93aa1876004d9 (patch) | |
| tree | 0846e85345b638fd5b84d51706f1268e9ea159f8 | |
| parent | 15a6fe6fe4ceea69c62d68f612836ca6c378e7f4 (diff) | |
resolves #3940 fix crash when resolving next value in sequence for non-numeric counter (PR #4003)
| -rw-r--r-- | CHANGELOG.adoc | 3 | ||||
| -rw-r--r-- | lib/asciidoctor/helpers.rb | 9 | ||||
| -rw-r--r-- | test/attributes_test.rb | 108 |
3 files changed, 102 insertions, 18 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index dbd8274c..fef21bb6 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -17,7 +17,8 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ Bug Fixes:: - * Don't allow counter and counter2 directives in attribute reference to override locked attributes (#3939) + * Don't allow counter and counter2 attribute directives to override locked attributes (#3939) + * Fix crash when resolving next value in sequence for counter with non-numeric value (#3940) * Update default stylesheet to remove dash in front of cite on nested quote block (#3847) * Don't escape hyphen in manname in manpage output * Remove extra .sp before content of verse block in manpage output diff --git a/lib/asciidoctor/helpers.rb b/lib/asciidoctor/helpers.rb index fb56bd4d..789cafe0 100644 --- a/lib/asciidoctor/helpers.rb +++ b/lib/asciidoctor/helpers.rb @@ -274,13 +274,10 @@ module Helpers def nextval current if ::Integer === current current + 1 + elsif (intval = current.to_i).to_s == current.to_s + intval + 1 else - intval = current.to_i - if intval.to_s != current.to_s - (current[0].ord + 1).chr - else - intval + 1 - end + current.succ end end diff --git a/test/attributes_test.rb b/test/attributes_test.rb index be4f744e..d1c44e19 100644 --- a/test/attributes_test.rb +++ b/test/attributes_test.rb @@ -1032,34 +1032,120 @@ context 'Attributes' do assert_equal 'A', doc.attributes['mycounter'] end - test 'increments counter with numeric value' do + test 'can seed counter to start at 1' do input = <<~'EOS' - :mycounter: 1 + :mycounter: 0 {counter:mycounter} + EOS + + output = convert_string_to_embedded input + assert_xpath '//p[text()="1"]', output, 1 + end + + test 'can seed counter to start at A' do + input = <<~'EOS' + :mycounter: @ + + {counter:mycounter} + EOS + + output = convert_string_to_embedded input + assert_xpath '//p[text()="A"]', output, 1 + end + test 'increments counter with positive numeric value' do + input = <<~'EOS' + [subs=attributes] + ++++ + {counter:mycounter:1} + {counter:mycounter} + {counter:mycounter} {mycounter} + ++++ EOS - doc = document_from_string input + doc = document_from_string input, standalone: false output = doc.convert - assert_equal 2, doc.attributes['mycounter'] - assert_xpath '//p[text()="2"]', output, 2 + assert_equal 3, doc.attributes['mycounter'] + assert_equal %w(1 2 3 3), output.lines.map {|l| l.rstrip } end - test 'increments counter with character value' do + test 'increments counter with negative numeric value' do input = <<~'EOS' - :mycounter: @ + [subs=attributes] + ++++ + {counter:mycounter:-2} + {counter:mycounter} + {counter:mycounter} + {mycounter} + ++++ + EOS + + doc = document_from_string input, standalone: false + output = doc.convert + assert_equal 0, doc.attributes['mycounter'] + assert_equal %w(-2 -1 0 0), output.lines.map {|l| l.rstrip } + end + test 'increments counter with ASCII character value' do + input = <<~'EOS' + [subs=attributes] + ++++ + {counter:mycounter:A} {counter:mycounter} + {counter:mycounter} + {mycounter} + ++++ + EOS + + output = convert_string_to_embedded input + assert_equal %w(A B C C), output.lines.map {|l| l.rstrip } + end + test 'increments counter with non-ASCII character value' do + input = <<~'EOS' + [subs=attributes] + ++++ + {counter:mycounter:é} + {counter:mycounter} + {counter:mycounter} {mycounter} + ++++ EOS - doc = document_from_string input - output = doc.convert - assert_equal 'A', doc.attributes['mycounter'] - assert_xpath '//p[text()="A"]', output, 2 + output = convert_string_to_embedded input + assert_equal %w(é ê ë ë), output.lines.map {|l| l.rstrip } + end + + test 'increments counter with emoji character value' do + input = <<~'EOS' + [subs=attributes] + ++++ + {counter:smiley:😋} + {counter:smiley} + {counter:smiley} + {smiley} + ++++ + EOS + + output = convert_string_to_embedded input + assert_equal %w(😋 😌 😍 😍), output.lines.map {|l| l.rstrip } + end + + test 'increments counter with multi-character value' do + input = <<~'EOS' + [subs=attributes] + ++++ + {counter:math:1x} + {counter:math} + {counter:math} + {math} + ++++ + EOS + + output = convert_string_to_embedded input + assert_equal %w(1x 1y 1z 1z), output.lines.map {|l| l.rstrip } end test 'counter uses 0 as seed value if seed attribute is nil' do |
