diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2023-04-08 23:58:12 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2023-04-09 01:28:21 -0600 |
| commit | c98422f9bd642dbca411985ffebb231aeade2da4 (patch) | |
| tree | d3deb1f32cf3a48ce752c031747a93b051e84134 | |
| parent | 2fd7523bd7631a3f5d343f50c44b108e11b81eb5 (diff) | |
change Block#content to return empty string instead of nil if raw or verbatim block has no lines
* makes return value type consistent
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | lib/asciidoctor/block.rb | 2 | ||||
| -rw-r--r-- | lib/asciidoctor/converter/docbook5.rb | 4 | ||||
| -rw-r--r-- | lib/asciidoctor/converter/html5.rb | 2 | ||||
| -rw-r--r-- | test/blocks_test.rb | 30 |
5 files changed, 26 insertions, 13 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 3a8f7254..0e4ccb45 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -60,6 +60,7 @@ Improvements:: * Remove form styles from the default stylesheet (#4186) * Set linenums option on source block when line numbering is enabled (#3313) * Warn if include target is remote and `allow-uri-read` attribute is not set (#2284) + * Return empty string instead of nil if raw or verbatim block has no lines Bug Fixes:: diff --git a/lib/asciidoctor/block.rb b/lib/asciidoctor/block.rb index a1bf00cc..ecd2a1bd 100644 --- a/lib/asciidoctor/block.rb +++ b/lib/asciidoctor/block.rb @@ -116,7 +116,7 @@ class Block < AbstractBlock # maybe apply_subs can know how to strip whitespace? result = apply_subs @lines, @subs if result.size < 2 - result[0] + result[0] || '' else result.shift while (first = result[0]) && first.rstrip.empty? result.pop while (last = result[-1]) && last.rstrip.empty? diff --git a/lib/asciidoctor/converter/docbook5.rb b/lib/asciidoctor/converter/docbook5.rb index 30fad2d1..4ae11127 100644 --- a/lib/asciidoctor/converter/docbook5.rb +++ b/lib/asciidoctor/converter/docbook5.rb @@ -250,10 +250,10 @@ class Converter::DocBook5Converter < Converter::Base def convert_stem node if (idx = node.subs.index :specialcharacters) node.subs.delete_at idx - equation = node.content || '' + equation = node.content idx > 0 ? (node.subs.insert idx, :specialcharacters) : (node.subs.unshift :specialcharacters) else - equation = node.content || '' + equation = node.content end if node.style == 'asciimath' # NOTE fop requires jeuclid to process mathml markup diff --git a/lib/asciidoctor/converter/html5.rb b/lib/asciidoctor/converter/html5.rb index 46efb92e..0c0b6362 100644 --- a/lib/asciidoctor/converter/html5.rb +++ b/lib/asciidoctor/converter/html5.rb @@ -678,7 +678,7 @@ Your browser does not support the audio tag. title_element = node.title? ? %(<div class="title">#{node.captioned_title}</div>\n) : '' %(<div#{id_attribute} class="listingblock#{(role = node.role) ? " #{role}" : ''}"> #{title_element}<div class="content"> -#{syntax_hl ? (syntax_hl.format node, lang, opts) : pre_open + (node.content || '') + pre_close} +#{syntax_hl ? (syntax_hl.format node, lang, opts) : pre_open + node.content + pre_close} </div> </div>) end diff --git a/test/blocks_test.rb b/test/blocks_test.rb index ccb15e1e..ba6182f7 100644 --- a/test/blocks_test.rb +++ b/test/blocks_test.rb @@ -1105,14 +1105,19 @@ context 'Blocks' do assert_message @logger, :WARN, '<stdin>: line 3: unterminated listing block', Hash end - test 'should not crash if listing block has no lines' do - input = <<~'EOS' - ---- - ---- - EOS - output = convert_string_to_embedded input - assert_css 'pre', output, 1 - assert_css 'pre:empty', output, 1 + test 'should not crash when converting verbatim block that has no lines' do + [%(----\n----), %(....\n....)].each do |input| + output = convert_string_to_embedded input + assert_css 'pre', output, 1 + assert_css 'pre:empty', output, 1 + end + end + + test 'should return content as empty string for verbatim or raw block that has no lines' do + [%(----\n----), %(....\n....)].each do |input| + doc = document_from_string input + assert_equal '', doc.blocks[0].content + end end test 'should preserve newlines in literal block' do @@ -1848,7 +1853,7 @@ context 'Blocks' do end context 'Math blocks' do - test 'should not crash when converting to HTML if stem block is empty' do + test 'should not crash when converting stem block that has no lines' do input = <<~'EOS' [stem] ++++ @@ -1859,6 +1864,13 @@ context 'Blocks' do assert_css '.stemblock', output, 1 end + test 'should return content as empty string for stem or pass block that has no lines' do + [%(++++\n++++), %([stem]\n++++\n++++)].each do |input| + doc = document_from_string input + assert_equal '', doc.blocks[0].content + end + end + test 'should add LaTeX math delimiters around latexmath block content' do input = <<~'EOS' [latexmath] |
