test/links_test.rb in asciidoctor-1.5.6.2 vs test/links_test.rb in asciidoctor-1.5.7

- old
+ new

@@ -9,11 +9,11 @@ test 'qualified url inline with text' do assert_xpath "//a[@href='http://asciidoc.org'][@class='bare'][text() = 'http://asciidoc.org']", render_string("The AsciiDoc project is located at http://asciidoc.org.") end test 'qualified url with role inline with text' do - assert_xpath "//a[@href='http://asciidoc.org'][@class='bare project'][text() = 'http://asciidoc.org']", render_string("The AsciiDoc project is located at http://asciidoc.org[,role=project].", :attributes => {'linkattrs' => ''}) + assert_xpath "//a[@href='http://asciidoc.org'][@class='bare project'][text() = 'http://asciidoc.org']", render_string("The AsciiDoc project is located at http://asciidoc.org[role=project].") end test 'qualified http url inline with hide-uri-scheme set' do assert_xpath "//a[@href='http://asciidoc.org'][@class='bare'][text() = 'asciidoc.org']", render_string("The AsciiDoc project is located at http://asciidoc.org.", :attributes => {'hide-uri-scheme' => ''}) end @@ -41,11 +41,11 @@ test 'qualified url with label using link macro' do assert_xpath "//a[@href='http://asciidoc.org'][text() = 'AsciiDoc']", render_string("We're parsing link:http://asciidoc.org[AsciiDoc] markup") end test 'qualified url with role using link macro' do - assert_xpath "//a[@href='http://asciidoc.org'][@class='bare project'][text() = 'http://asciidoc.org']", render_string("We're parsing link:http://asciidoc.org[,role=project] markup", :attributes => {'linkattrs' => ''}) + assert_xpath "//a[@href='http://asciidoc.org'][@class='bare project'][text() = 'http://asciidoc.org']", render_string("We're parsing link:http://asciidoc.org[role=project] markup") end test 'qualified url using macro syntax with multi-line label inline with text' do assert_xpath %{//a[@href='http://asciidoc.org'][text() = 'AsciiDoc\nmarkup']}, render_string("We're parsing link:http://asciidoc.org[AsciiDoc\nmarkup]") end @@ -80,43 +80,63 @@ test 'qualified url surrounded by round brackets' do assert_xpath '//a[@href="http://asciidoc.org"][text()="http://asciidoc.org"]', render_string('(http://asciidoc.org) is the project page for AsciiDoc.'), 1 end test 'qualified url with trailing round bracket' do - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]', render_string('Asciidoctor is a Ruby-based AsciiDoc processor (see http://asciidoctor.org)'), 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', render_string('Asciidoctor is a Ruby-based AsciiDoc processor (see https://asciidoctor.org)'), 1 end test 'qualified url with trailing semi-colon' do - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]', render_string('http://asciidoctor.org; where text gets parsed'), 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', render_string('https://asciidoctor.org; where text gets parsed'), 1 end test 'qualified url with trailing colon' do - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]', render_string('http://asciidoctor.org: where text gets parsed'), 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', render_string('https://asciidoctor.org: where text gets parsed'), 1 end test 'qualified url in round brackets with trailing colon' do - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]', render_string('(http://asciidoctor.org): where text gets parsed'), 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', render_string('(https://asciidoctor.org): where text gets parsed'), 1 end test 'qualified url with trailing round bracket followed by colon' do - result = render_embedded_string '(from http://asciidoctor.org): where text gets parsed' - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]', result, 1 - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]/preceding-sibling::text()[.="(from "]', result, 1 - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]/following-sibling::text()[.="): where text gets parsed"]', result, 1 + result = render_embedded_string '(from https://asciidoctor.org): where text gets parsed' + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/preceding-sibling::text()[.="(from "]', result, 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[.="): where text gets parsed"]', result, 1 end test 'qualified url in round brackets with trailing semi-colon' do - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]', render_string('(http://asciidoctor.org); where text gets parsed'), 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', render_string('(https://asciidoctor.org); where text gets parsed'), 1 end test 'qualified url with trailing round bracket followed by semi-colon' do - result = render_embedded_string '(from http://asciidoctor.org); where text gets parsed' - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]', result, 1 - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]/preceding-sibling::text()[.="(from "]', result, 1 - assert_xpath '//a[@href="http://asciidoctor.org"][text()="http://asciidoctor.org"]/following-sibling::text()[.="); where text gets parsed"]', result, 1 + result = render_embedded_string '(from https://asciidoctor.org); where text gets parsed' + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/preceding-sibling::text()[.="(from "]', result, 1 + assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[.="); where text gets parsed"]', result, 1 end + test 'URI scheme with trailing characters should not be converted to a link' do + input_sources = %w( + (https://) + http://; + file://: + <ftp://> + ) + expected_outputs = %w( + (https://) + http://; + file://: + &lt;ftp://&gt; + ) + input_sources.each_with_index do |input_source, i| + expected_output = expected_outputs[i] + actual = block_from_string input_source + assert_equal expected_output, actual.content + end + end + test 'qualified url containing round brackets' do assert_xpath '//a[@href="http://jruby.org/apidocs/org/jruby/Ruby.html#addModule(org.jruby.RubyModule)"][text()="addModule() adds a Ruby module"]', render_string('http://jruby.org/apidocs/org/jruby/Ruby.html#addModule(org.jruby.RubyModule)[addModule() adds a Ruby module]'), 1 end test 'qualified url adjacent to text in square brackets' do @@ -174,58 +194,71 @@ test 'inline quoted qualified url should not consume surrounding angled brackets' do assert_xpath '//a[@href="https://github.com/asciidoctor"]', render_string('Asciidoctor GitHub organization: <**https://github.com/asciidoctor**>'), 1 end - test 'link with quoted text should not be separated into attributes when linkattrs is set' do - assert_xpath '//a[@href="http://search.example.com"][text()="Google, Yahoo, Bing = Search Engines"]', render_embedded_string('http://search.example.com["Google, Yahoo, Bing = Search Engines"]', :attributes => {'linkattrs' => ''}), 1 + test 'link with quoted text should not be separated into attributes when text contains an equal sign' do + assert_xpath '//a[@href="http://search.example.com"][text()="Google, Yahoo, Bing = Search Engines"]', render_embedded_string('http://search.example.com["Google, Yahoo, Bing = Search Engines"]'), 1 end - test 'link with comma in text but no equal sign should not be separated into attributes when linkattrs is set' do - assert_xpath '//a[@href="http://search.example.com"][text()="Google, Yahoo, Bing"]', render_embedded_string('http://search.example.com[Google, Yahoo, Bing]', :attributes => {'linkattrs' => ''}), 1 + test 'link with quoted text but no equal sign should carry quotes over to output' do + assert_xpath %(//a[@href="http://search.example.com"][text()='"Google, Yahoo, Bing"']), render_embedded_string('http://search.example.com["Google, Yahoo, Bing"]'), 1 end - test 'role and window attributes on link are processed when linkattrs is set' do - assert_xpath '//a[@href="http://google.com"][@class="external"][@target="_blank"]', render_embedded_string('http://google.com[Google, role="external", window="_blank"]', :attributes => {'linkattrs' => ''}), 1 + test 'link with comma in text but no equal sign should not be separated into attributes' do + assert_xpath '//a[@href="http://search.example.com"][text()="Google, Yahoo, Bing"]', render_embedded_string('http://search.example.com[Google, Yahoo, Bing]'), 1 end - test 'link macro with attributes but no text should use URL as text when linkattrs is set' do + test 'role and window attributes on link are processed' do + assert_xpath '//a[@href="http://google.com"][@class="external"][@target="_blank"]', render_embedded_string('http://google.com[Google, role=external, window="_blank"]'), 1 + end + + test 'link macro with attributes but no text should use URL as text' do url = 'https://fonts.googleapis.com/css?family=Roboto:400,400italic,' - assert_xpath %(//a[@href="#{url}"][text()="#{url}"]), render_embedded_string(%(link:#{url}[family=Roboto,weight=400]), :attributes => {'linkattrs' => ''}), 1 + assert_xpath %(//a[@href="#{url}"][text()="#{url}"]), render_embedded_string(%(link:#{url}[family=Roboto,weight=400])), 1 end - test 'link macro with comma but no explicit attributes in text should not parse text when linkattrs is set' do + test 'link macro with attributes but blank text should use URL as text' do url = 'https://fonts.googleapis.com/css?family=Roboto:400,400italic,' - assert_xpath %(//a[@href="#{url}"][text()="Roboto,400"]), render_embedded_string(%(link:#{url}[Roboto,400]), :attributes => {'linkattrs' => ''}), 1 + assert_xpath %(//a[@href="#{url}"][text()="#{url}"]), render_embedded_string(%(link:#{url}[,family=Roboto,weight=400])), 1 end + test 'link macro with comma but no explicit attributes in text should not parse text' do + url = 'https://fonts.googleapis.com/css?family=Roboto:400,400italic,' + assert_xpath %(//a[@href="#{url}"][text()="Roboto,400"]), render_embedded_string(%(link:#{url}[Roboto,400])), 1 + end + test 'link text that ends in ^ should set link window to _blank' do assert_xpath '//a[@href="http://google.com"][@target="_blank"]', render_embedded_string('http://google.com[Google^]'), 1 end test 'rel=noopener should be added to a link that targets the _blank window' do assert_xpath '//a[@href="http://google.com"][@target="_blank"][@rel="noopener"]', render_embedded_string('http://google.com[Google^]'), 1 end test 'rel=noopener should be added to a link that targets a named window when the noopener option is set' do - assert_xpath '//a[@href="http://google.com"][@target="name"][@rel="noopener"]', render_embedded_string('http://google.com[Google,window=name,opts=noopener]', :attributes => {'linkattrs' => ''}), 1 + assert_xpath '//a[@href="http://google.com"][@target="name"][@rel="noopener"]', render_embedded_string('http://google.com[Google,window=name,opts=noopener]'), 1 end test 'rel=noopener should not be added to a link if it does not target a window' do - result = render_embedded_string 'http://google.com[Google,opts=noopener]', :attributes => {'linkattrs' => ''} + result = render_embedded_string 'http://google.com[Google,opts=noopener]' assert_xpath '//a[@href="http://google.com"]', result, 1 assert_xpath '//a[@href="http://google.com"][@rel="noopener"]', result, 0 end - test 'id attribute on link are processed when linkattrs is set' do - assert_xpath '//a[@href="http://google.com"][@id="link-1"]', render_embedded_string('http://google.com[Google, id="link-1"]', :attributes => {'linkattrs' => ''}), 1 + test 'rel=nofollow should be added to a link when the nofollow option is set' do + assert_xpath '//a[@href="http://google.com"][@target="name"][@rel="nofollow noopener"]', render_embedded_string('http://google.com[Google,window=name,opts="nofollow,noopener"]'), 1 end - test 'title attribute on link are processed when linkattrs is set' do - assert_xpath '//a[@href="http://google.com"][@title="title-1"]', render_embedded_string('http://google.com[Google, title="title-1"]', :attributes => {'linkattrs' => ''}), 1 + test 'id attribute on link is processed' do + assert_xpath '//a[@href="http://google.com"][@id="link-1"]', render_embedded_string('http://google.com[Google, id="link-1"]'), 1 end + test 'title attribute on link is processed' do + assert_xpath '//a[@href="http://google.com"][@title="title-1"]', render_embedded_string('http://google.com[Google, title="title-1"]'), 1 + end + test 'inline irc link' do assert_xpath '//a[@href="irc://irc.freenode.net"][text()="irc://irc.freenode.net"]', render_embedded_string('irc://irc.freenode.net'), 1 end test 'inline irc link with text' do @@ -234,24 +267,24 @@ test 'inline ref' do variations = %w([[tigers]] anchor:tigers[]) variations.each do |anchor| doc = document_from_string %(Here you can read about tigers.#{anchor}) - output = doc.render + output = doc.convert assert_equal '[tigers]', doc.catalog[:ids]['tigers'] - assert doc.catalog[:refs]['tigers'].is_a? Asciidoctor::Inline + assert_kind_of Asciidoctor::Inline, doc.catalog[:refs]['tigers'] assert_nil doc.catalog[:refs]['tigers'].text assert_xpath '//a[@id = "tigers"]', output, 1 assert_xpath '//a[@id = "tigers"]/child::text()', output, 0 end end test 'escaped inline ref' do variations = %w([[tigers]] anchor:tigers[]) variations.each do |anchor| doc = document_from_string %(Here you can read about tigers.\\#{anchor}) - output = doc.render + output = doc.convert refute doc.catalog[:ids].key?('tigers') refute doc.catalog[:refs].key?('tigers') assert_xpath '//a[@id = "tigers"]', output, 0 end end @@ -270,13 +303,13 @@ end test 'inline ref with reftext' do %w([[tigers,Tigers]] anchor:tigers[Tigers]).each do |anchor| doc = document_from_string %(Here you can read about tigers.#{anchor}) - output = doc.render + output = doc.convert assert_equal 'Tigers', doc.catalog[:ids]['tigers'] - assert doc.catalog[:refs]['tigers'].is_a? Asciidoctor::Inline + assert_kind_of Asciidoctor::Inline, doc.catalog[:refs]['tigers'] assert_equal 'Tigers', doc.catalog[:refs]['tigers'].text assert_xpath '//a[@id = "tigers"]', output, 1 assert_xpath '//a[@id = "tigers"]/child::text()', output, 0 end end @@ -288,22 +321,22 @@ end test 'should substitute attribute references in reftext when registering inline ref' do %w([[tigers,{label-tigers}]] anchor:tigers[{label-tigers}]).each do |anchor| doc = document_from_string %(Here you can read about tigers.#{anchor}), :attributes => { 'label-tigers' => 'Tigers' } - doc.render - assert doc.catalog[:refs]['tigers'].is_a? Asciidoctor::Inline + doc.convert + assert_kind_of Asciidoctor::Inline, doc.catalog[:refs]['tigers'] assert_equal 'Tigers', doc.catalog[:refs]['tigers'].text assert_equal 'Tigers', doc.catalog[:ids]['tigers'] end end test 'inline ref with reftext converted to DocBook' do %w([[tigers,<Tigers>]] anchor:tigers[<Tigers>]).each do |anchor| doc = document_from_string %(Here you can read about tigers.#{anchor}), :backend => :docbook45 output = doc.convert :header_footer => false - assert doc.catalog[:refs]['tigers'].is_a? Asciidoctor::Inline + assert_kind_of Asciidoctor::Inline, doc.catalog[:refs]['tigers'] assert_equal '<Tigers>', doc.catalog[:refs]['tigers'].text assert_equal '<Tigers>', doc.references[:ids]['tigers'] assert_includes output, '<anchor id="tigers" xreflabel="&lt;Tigers&gt;"/>' end end @@ -347,17 +380,17 @@ end test 'xref using angled bracket syntax' do doc = document_from_string '<<tigers>>' doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, '[tigers]', :type => :ref, :target => 'tigers'), '[tigers]'] - assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', doc.render, 1 + assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', doc.convert, 1 end test 'xref using angled bracket syntax with explicit hash' do doc = document_from_string '<<#tigers>>' doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, 'Tigers', :type => :ref, :target => 'tigers'), 'Tigers'] - assert_xpath '//a[@href="#tigers"][text() = "Tigers"]', doc.render, 1 + assert_xpath '//a[@href="#tigers"][text() = "Tigers"]', doc.convert, 1 end test 'xref using angled bracket syntax with label' do input = <<-EOS <<tigers,About Tigers>> @@ -376,73 +409,127 @@ == Tigers EOS assert_xpath %q(//a[@href="#tigers"][text() = '"About Tigers"']), render_string(input), 1 end + test 'should not interpret path sans extension in xref with angled bracket syntax in compat mode' do + using_memory_logger do |logger| + doc = document_from_string '<<tigers#>>', :header_footer => false, :attributes => { 'compat-mode' => '' } + assert_xpath '//a[@href="#tigers#"][text() = "[tigers#]"]', doc.convert, 1 + end + end + test 'xref using angled bracket syntax with path sans extension' do doc = document_from_string '<<tigers#>>', :header_footer => false - assert_xpath '//a[@href="tigers.html"][text() = "tigers.html"]', doc.render, 1 + assert_xpath '//a[@href="tigers.html"][text() = "tigers.html"]', doc.convert, 1 end - test 'inter-document xref should not truncate after period if path has no extension' do - result = render_embedded_string '<<using-.net-web-services#,Using .NET web services>>' - assert_xpath '//a[@href="using-.net-web-services.html"][text() = "Using .NET web services"]', result, 1 + test 'inter-document xref should not add suffix to path with a non-AsciiDoc extension' do + { + 'using-.net-web-services' => 'Using .NET web services', + '../file.pdf' => 'Download the .pdf file' + }.each do |path, text| + result = render_embedded_string %(<<#{path}#,#{text}>>) + assert_xpath %(//a[@href="#{path}"][text() = "#{text}"]), result, 1 + end end test 'inter-document xref should only remove the file extension part if the path contains a period elsewhere' do result = render_embedded_string '<<using-.net-web-services.adoc#,Using .NET web services>>' assert_xpath '//a[@href="using-.net-web-services.html"][text() = "Using .NET web services"]', result, 1 end test 'xref using angled bracket syntax with path sans extension using docbook backend' do doc = document_from_string '<<tigers#>>', :header_footer => false, :backend => 'docbook' - assert_match '<link xl:href="tigers.xml">tigers.xml</link>', doc.render, 1 + assert_match '<link xl:href="tigers.xml">tigers.xml</link>', doc.convert, 1 doc = document_from_string '<<tigers#>>', :header_footer => false, :backend => 'docbook45' - assert_match '<ulink url="tigers.xml">tigers.xml</ulink>', doc.render, 1 + assert_match '<ulink url="tigers.xml">tigers.xml</ulink>', doc.convert, 1 end test 'xref using angled bracket syntax with ancestor path sans extension' do doc = document_from_string '<<../tigers#,tigers>>', :header_footer => false - assert_xpath '//a[@href="../tigers.html"][text() = "tigers"]', doc.render, 1 + assert_xpath '//a[@href="../tigers.html"][text() = "tigers"]', doc.convert, 1 end test 'xref using angled bracket syntax with absolute path sans extension' do doc = document_from_string '<</path/to/tigers#,tigers>>', :header_footer => false - assert_xpath '//a[@href="/path/to/tigers.html"][text() = "tigers"]', doc.render, 1 + assert_xpath '//a[@href="/path/to/tigers.html"][text() = "tigers"]', doc.convert, 1 end test 'xref using angled bracket syntax with path and extension' do + using_memory_logger do |logger| + doc = document_from_string '<<tigers.adoc>>', :header_footer => false + assert_xpath '//a[@href="#tigers.adoc"][text() = "[tigers.adoc]"]', doc.convert, 1 + end + end + + test 'xref using angled bracket syntax with path and extension with hash' do doc = document_from_string '<<tigers.adoc#>>', :header_footer => false - assert_xpath '//a[@href="tigers.html"][text() = "tigers.html"]', doc.render, 1 + assert_xpath '//a[@href="tigers.html"][text() = "tigers.html"]', doc.convert, 1 end + test 'xref using angled bracket syntax with path and extension with fragment' do + doc = document_from_string '<<tigers.adoc#id>>', :header_footer => false + assert_xpath '//a[@href="tigers.html#id"][text() = "tigers.html"]', doc.convert, 1 + end + + test 'xref using macro syntax with path and extension in compat mode' do + using_memory_logger do |logger| + doc = document_from_string 'xref:tigers.adoc[]', :header_footer => false, :attributes => { 'compat-mode' => '' } + assert_xpath '//a[@href="#tigers.adoc"][text() = "[tigers.adoc]"]', doc.convert, 1 + end + end + + test 'xref using macro syntax with path and extension' do + doc = document_from_string 'xref:tigers.adoc[]', :header_footer => false + assert_xpath '//a[@href="tigers.html"][text() = "tigers.html"]', doc.convert, 1 + end + test 'xref using angled bracket syntax with path and fragment' do doc = document_from_string '<<tigers#about>>', :header_footer => false - assert_xpath '//a[@href="tigers.html#about"][text() = "tigers.html"]', doc.render, 1 + assert_xpath '//a[@href="tigers.html#about"][text() = "tigers.html"]', doc.convert, 1 end test 'xref using angled bracket syntax with path, fragment and text' do doc = document_from_string '<<tigers#about,About Tigers>>', :header_footer => false - assert_xpath '//a[@href="tigers.html#about"][text() = "About Tigers"]', doc.render, 1 + assert_xpath '//a[@href="tigers.html#about"][text() = "About Tigers"]', doc.convert, 1 end test 'xref using angled bracket syntax with path and custom relfilesuffix and outfilesuffix' do attributes = {'relfileprefix' => '../', 'outfilesuffix' => '/'} doc = document_from_string '<<tigers#about,About Tigers>>', :header_footer => false, :attributes => attributes - assert_xpath '//a[@href="../tigers/#about"][text() = "About Tigers"]', doc.render, 1 + assert_xpath '//a[@href="../tigers/#about"][text() = "About Tigers"]', doc.convert, 1 end + test 'xref using angled bracket syntax with path and custom relfilesuffix' do + attributes = { 'relfilesuffix' => '/' } + doc = document_from_string '<<tigers#about,About Tigers>>', :header_footer => false, :attributes => attributes + assert_xpath '//a[@href="tigers/#about"][text() = "About Tigers"]', doc.convert, 1 + end + test 'xref using angled bracket syntax with path which has been included in this document' do - doc = document_from_string '<<tigers#about,About Tigers>>', :header_footer => false - doc.catalog[:includes] << 'tigers' - assert_xpath '//a[@href="#about"][text() = "About Tigers"]', doc.render, 1 + using_memory_logger do |logger| + in_verbose_mode do + doc = document_from_string '<<tigers#about,About Tigers>>', :header_footer => false + doc.catalog[:includes]['tigers'] = true + output = doc.convert + assert_xpath '//a[@href="#about"][text() = "About Tigers"]', output, 1 + assert_message logger, :WARN, 'invalid reference: about' + end + end end test 'xref using angled bracket syntax with nested path which has been included in this document' do - doc = document_from_string '<<part1/tigers#about,About Tigers>>', :header_footer => false - doc.catalog[:includes] << 'part1/tigers' - assert_xpath '//a[@href="#about"][text() = "About Tigers"]', doc.render, 1 + using_memory_logger do |logger| + in_verbose_mode do + doc = document_from_string '<<part1/tigers#about,About Tigers>>', :header_footer => false + doc.catalog[:includes]['part1/tigers'] = true + output = doc.convert + assert_xpath '//a[@href="#about"][text() = "About Tigers"]', output, 1 + assert_message logger, :WARN, 'invalid reference: about' + end + end end test 'xref using angled bracket syntax inline with text' do input = <<-EOS Want to learn <<tigers,about tigers>>? @@ -494,11 +581,11 @@ end test 'xref using macro syntax' do doc = document_from_string 'xref:tigers[]' doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, '[tigers]', :type => :ref, :target => 'tigers'), '[tigers]'] - assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', doc.render, 1 + assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', doc.convert, 1 end test 'multiple xref macros with implicit text in single line' do input = <<-EOS This document has two sections, xref:sect-a[] and xref:sect-b[]. @@ -515,11 +602,11 @@ end test 'xref using macro syntax with explicit hash' do doc = document_from_string 'xref:#tigers[]' doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, 'Tigers', :type => :ref, :target => 'tigers'), 'Tigers'] - assert_xpath '//a[@href="#tigers"][text() = "Tigers"]', doc.render, 1 + assert_xpath '//a[@href="#tigers"][text() = "Tigers"]', doc.convert, 1 end test 'xref using macro syntax with label' do input = <<-EOS xref:tigers[About Tigers] @@ -550,20 +637,30 @@ == Tigers EOS assert_xpath %{//a[@href="#tigers"][normalize-space(text()) = "about tigers"]}, render_string(input), 1 end - test 'xref using macro syntax with text that contains an escaped closing bracket' do + test 'xref using macro syntax with text that ends with an escaped closing bracket' do input = <<-EOS xref:tigers[[tigers\\]] [#tigers] == Tigers EOS - assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', render_string(input), 1 + assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', render_embedded_string(input), 1 end + test 'xref using macro syntax with text that contains an escaped closing bracket' do + input = <<-EOS +xref:tigers[[tigers\\] are cats] + +[#tigers] +== Tigers + EOS + assert_xpath '//a[@href="#tigers"][text() = "[tigers] are cats"]', render_embedded_string(input), 1 + end + test 'unescapes square bracket in reftext used by xref' do input = 'anchor:foo[b[a\]r]about see <<foo>>' result = render_embedded_string input @@ -572,11 +669,11 @@ end test 'xref using invalid macro syntax does not create link' do doc = document_from_string 'xref:tigers' doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, 'Tigers', :type => :ref, :target => 'tigers'), 'Tigers'] - assert_xpath '//a', doc.render, 0 + assert_xpath '//a', doc.convert, 0 end test 'should warn and create link if verbose flag is set and reference is not found' do input = <<-EOS [#foobar] @@ -584,17 +681,114 @@ == Section B See <<foobaz>>. EOS - begin - old_verbose, $VERBOSE = $VERBOSE, true - output, warnings = redirect_streams {|_, err| [(render_embedded_string input), err.string] } - assert_xpath '//a[@href="#foobaz"][text() = "[foobaz]"]', output, 1 - refute_empty warnings - assert_includes warnings, 'asciidoctor: WARNING: invalid reference: foobaz' - ensure - $VERBOSE = old_verbose + using_memory_logger do |logger| + in_verbose_mode do + output = render_embedded_string input + assert_xpath '//a[@href="#foobaz"][text() = "[foobaz]"]', output, 1 + assert_message logger, :WARN, 'invalid reference: foobaz' + end + end + end + + test 'should warn and create link if verbose flag is set and reference using # notation is not found' do + input = <<-EOS +[#foobar] +== Foobar + +== Section B + +See <<#foobaz>>. + EOS + using_memory_logger do |logger| + in_verbose_mode do + output = render_embedded_string input + assert_xpath '//a[@href="#foobaz"][text() = "[foobaz]"]', output, 1 + assert_message logger, :WARN, 'invalid reference: foobaz' + end + end + end + + test 'should produce an internal anchor from an inter-document xref to file included into current file' do + input = <<-'EOS' += Book Title +:doctype: book + +[#ch1] +== Chapter 1 + +So it begins. + +Read <<other-chapters.adoc#ch2>> to find out what happens next! + +include::other-chapters.adoc[] + EOS + + doc = document_from_string input, :safe => :safe, :base_dir => fixturedir + assert doc.catalog[:includes].key?('other-chapters') + assert doc.catalog[:includes]['other-chapters'] + output = doc.convert + assert_xpath '//a[@href="#ch2"][text()="Chapter 2"]', output, 1 + end + + test 'should produce an internal anchor from an inter-document xref to file included entirely into current file using tags' do + input = <<-'EOS' += Book Title +:doctype: book + +[#ch1] +== Chapter 1 + +So it begins. + +Read <<other-chapters.adoc#ch2>> to find out what happens next! + +include::other-chapters.adoc[tags=**] + EOS + + output = render_embedded_string input, :safe => :safe, :base_dir => fixturedir + assert_xpath '//a[@href="#ch2"][text()="Chapter 2"]', output, 1 + end + + test 'should not produce an internal anchor for inter-document xref to file partially included into current file' do + input = <<-'EOS' += Book Title +:doctype: book + +[#ch1] +== Chapter 1 + +So it begins. + +Read <<other-chapters.adoc#ch2,the next chapter>> to find out what happens next! + +include::other-chapters.adoc[tags=ch2] + EOS + + doc = document_from_string input, :safe => :safe, :base_dir => fixturedir + assert doc.catalog[:includes].key?('other-chapters') + refute doc.catalog[:includes]['other-chapters'] + output = doc.convert + assert_xpath '//a[@href="other-chapters.html#ch2"][text()="the next chapter"]', output, 1 + end + + test 'should warn and create link if debug mode is enabled, inter-document xref points to current doc, and reference not found' do + input = <<-EOS +[#foobar] +== Foobar + +== Section B + +See <<test.adoc#foobaz>>. + EOS + using_memory_logger do |logger| + in_verbose_mode do + output = render_embedded_string input, :attributes => { 'docname' => 'test' } + assert_xpath '//a[@href="#foobaz"][text() = "[foobaz]"]', output, 1 + assert_message logger, :WARN, 'invalid reference: foobaz' + end end end test 'xref uses title of target as label for forward and backward references in html output' do input = <<-EOS