// Adapted from Asciidoctor's blocks test suite, found in
// https://github.com/asciidoctor/asciidoctor/blob/main/test/blocks_test.rb.
//
// The tests in this tree are adapted from the Ruby implementation of
// Asciidoctor, which comes with the following license:
//
// MIT License
//
// Copyright (C) 2012-present Dan Allen, Sarah White, Ryan Waldron, and the
// individual contributors to Asciidoctor.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//! Port of Asciidoctor's `blocks_test.rb`.
//!
//! Each Ruby `context` becomes a Rust `mod` and each `test '..'` becomes a
//! `#[test] fn`, driven through [`Parser`](crate::Parser) and asserted with the
//! `assert_*` DOM helpers. Every ported `test '..'` block is reproduced
//! verbatim in a `verifies!` block so the `sdd` coverage tool can measure which
//! lines of the Ruby suite are verified line-by-line.
//!
//! DocBook and other non-HTML backends are out of scope for this crate, as are
//! compatibility mode and icon-font/asset-resolution concerns. Ruby tests
//! exercising those are reproduced verbatim in `non_normative!` blocks
//! (accounting for those Ruby lines without asserting behavior this crate does
//! not model); each carries a comment noting why it is not ported. Tests
//! blocked on an unimplemented but planned feature are kept as `#[ignore]`d
//! `#[test] fn`s whose body preserves the Ruby assertions, tagged with a
//! `TODO`.
use crate::tests::sdd::*;
track_file!("ref/asciidoctor/test/blocks_test.rb");
non_normative!(
r#"
# frozen_string_literal: true
require_relative 'test_helper'
context 'Blocks' do
default_logger = Asciidoctor::LoggerManager.logger
setup do
Asciidoctor::LoggerManager.logger = (@logger = Asciidoctor::MemoryLogger.new)
end
teardown do
Asciidoctor::LoggerManager.logger = default_logger
end
"#
);
mod layout_breaks {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Layout Breaks' do
"#
);
#[test]
fn horizontal_rule() {
verifies!(
r#"
test 'horizontal rule' do
%w(''' '''' '''''').each do |line|
output = convert_string_to_embedded line
assert_includes output, '<hr>'
end
end
"#
);
for line in ["'''", "''''", "'''''"] {
let doc = Parser::default().parse(line);
assert_css(&doc, "hr", 1);
}
}
#[test]
fn horizontal_rule_with_markdown_syntax_disabled() {
non_normative!(
r#"
test 'horizontal rule with markdown syntax disabled' do
old_markdown_syntax = Asciidoctor::Compliance.markdown_syntax
begin
Asciidoctor::Compliance.markdown_syntax = false
%w(''' '''' '''''').each do |line|
output = convert_string_to_embedded line
assert_includes output, '<hr>'
end
%w(--- *** ___).each do |line|
output = convert_string_to_embedded line
refute_includes output, '<hr>'
end
ensure
Asciidoctor::Compliance.markdown_syntax = old_markdown_syntax
end
end
"#
);
// Not ported: toggles `Asciidoctor::Compliance.markdown_syntax`, a
// global compliance setting this crate does not model. This crate
// always recognizes the Markdown-style thematic breaks `---` and
// `***`.
}
#[test]
fn less_than_3_chars_does_not_make_horizontal_rule() {
verifies!(
r#"
test '< 3 chars does not make horizontal rule' do
%w(' '').each do |line|
output = convert_string_to_embedded line
refute_includes output, '<hr>'
assert_includes output, %(<p>#{line}</p>)
end
end
"#
);
for line in ["'", "''"] {
let doc = Parser::default().parse(line);
assert_css(&doc, "hr", 0);
assert_xpath(&doc, &format!("//p[text()=\"{line}\"]"), 1);
}
}
#[test]
fn mixed_chars_does_not_make_horizontal_rule() {
verifies!(
r#"
test 'mixed chars does not make horizontal rule' do
[%q(''<), %q('''<), %q(' ' ')].each do |line|
output = convert_string_to_embedded line
refute_includes output, '<hr>'
assert_includes output, %(<p>#{line.sub '<', '<'}</p>)
end
end
"#
);
// The Ruby test escapes `<` to `<` because it inspects the raw HTML
// string; `assert_xpath` matches against the decoded DOM text, so the
// literal `<` is used here.
for line in ["''<", "'''<", "' ' '"] {
let doc = Parser::default().parse(line);
assert_css(&doc, "hr", 0);
assert_xpath(&doc, &format!("//p[text()=\"{line}\"]"), 1);
}
}
#[test]
fn horizontal_rule_between_blocks() {
verifies!(
r#"
test 'horizontal rule between blocks' do
output = convert_string_to_embedded %(Block above\n\n'''\n\nBlock below)
assert_xpath '/hr', output, 1
assert_xpath '/hr/preceding-sibling::*', output, 1
assert_xpath '/hr/following-sibling::*', output, 1
end
"#
);
let doc = Parser::default().parse("Block above\n\n'''\n\nBlock below");
assert_xpath(&doc, "/hr", 1);
assert_xpath(&doc, "/hr/preceding-sibling::*", 1);
assert_xpath(&doc, "/hr/following-sibling::*", 1);
}
#[test]
fn page_break() {
verifies!(
r#"
test 'page break' do
output = convert_string_to_embedded %(page 1\n\n<<<\n\npage 2)
assert_xpath '/*[translate(@style, ";", "")="page-break-after: always"]', output, 1
assert_xpath '/*[translate(@style, ";", "")="page-break-after: always"]/preceding-sibling::div/p[text()="page 1"]', output, 1
assert_xpath '/*[translate(@style, ";", "")="page-break-after: always"]/following-sibling::div/p[text()="page 2"]', output, 1
end
"#
);
// This crate renders a page break as `<div class="page-break">` rather
// than the inline `style="page-break-after: always"` Asciidoctor emits,
// so the assertions track the crate's actual output.
let doc = Parser::default().parse("page 1\n\n<<<\n\npage 2");
assert_css(&doc, "div.page-break", 1);
assert_xpath(
&doc,
"/*[@class=\"page-break\"]/preceding-sibling::div/p[text()=\"page 1\"]",
1,
);
assert_xpath(
&doc,
"/*[@class=\"page-break\"]/following-sibling::div/p[text()=\"page 2\"]",
1,
);
}
non_normative!(
r#"
end
"#
);
}
mod comments {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Comments' do
"#
);
// NOTE: divergence from Asciidoctor. Asciidoctor drops a line comment
// entirely, leaving two paragraphs; this crate currently emits an empty
// paragraph where the blank-line-delimited `// line comment` sits, so
// `//p` counts 3 rather than 2. The comment text itself is removed. Kept
// `#[ignore]`d with the Ruby-intended assertions until the empty paragraph
// is suppressed.
// TODO: suppress the empty paragraph left by a standalone line comment.
#[ignore]
#[test]
fn line_comment_between_paragraphs_offset_by_blank_lines() {
verifies!(
r#"
test 'line comment between paragraphs offset by blank lines' do
input = <<~'EOS'
first paragraph
// line comment
second paragraph
EOS
output = convert_string_to_embedded input
refute_match(/line comment/, output)
assert_xpath '//p', output, 2
end
"#
);
let doc =
Parser::default().parse("first paragraph\n\n// line comment\n\nsecond paragraph\n");
refute_output_contains(&doc, "line comment");
assert_xpath(&doc, "//p", 2);
}
#[test]
fn adjacent_line_comment_between_paragraphs() {
verifies!(
r#"
test 'adjacent line comment between paragraphs' do
input = <<~'EOS'
first line
// line comment
second line
EOS
output = convert_string_to_embedded input
refute_match(/line comment/, output)
assert_xpath '//p', output, 1
assert_xpath "//p[1][text()='first line\nsecond line']", output, 1
end
"#
);
let doc = Parser::default().parse("first line\n// line comment\nsecond line\n");
refute_output_contains(&doc, "line comment");
assert_xpath(&doc, "//p", 1);
assert_xpath(&doc, "//p[text()=\"first line\nsecond line\"]", 1);
}
#[test]
fn comment_block_between_paragraphs_offset_by_blank_lines() {
verifies!(
r#"
test 'comment block between paragraphs offset by blank lines' do
input = <<~'EOS'
first paragraph
////
block comment
////
second paragraph
EOS
output = convert_string_to_embedded input
refute_match(/block comment/, output)
assert_xpath '//p', output, 2
end
"#
);
let doc = Parser::default()
.parse("first paragraph\n\n////\nblock comment\n////\n\nsecond paragraph\n");
refute_rendered_contains(&doc, "block comment");
assert_xpath(&doc, "//p", 2);
}
#[test]
fn comment_block_between_paragraphs_offset_by_blank_lines_inside_delimited_block() {
verifies!(
r#"
test 'comment block between paragraphs offset by blank lines inside delimited block' do
input = <<~'EOS'
====
first paragraph
////
block comment
////
second paragraph
====
EOS
output = convert_string_to_embedded input
refute_match(/block comment/, output)
assert_xpath '//p', output, 2
end
"#
);
let doc = Parser::default().parse(
"====\nfirst paragraph\n\n////\nblock comment\n////\n\nsecond paragraph\n====\n",
);
refute_rendered_contains(&doc, "block comment");
assert_xpath(&doc, "//p", 2);
}
#[test]
fn adjacent_comment_block_between_paragraphs() {
verifies!(
r#"
test 'adjacent comment block between paragraphs' do
input = <<~'EOS'
first paragraph
////
block comment
////
second paragraph
EOS
output = convert_string_to_embedded input
refute_match(/block comment/, output)
assert_xpath '//p', output, 2
end
"#
);
let doc = Parser::default()
.parse("first paragraph\n////\nblock comment\n////\nsecond paragraph\n");
refute_rendered_contains(&doc, "block comment");
assert_xpath(&doc, "//p", 2);
}
#[test]
fn can_convert_with_block_comment_at_end_of_document_with_trailing_newlines() {
verifies!(
r#"
test "can convert with block comment at end of document with trailing newlines" do
input = <<~'EOS'
paragraph
////
block comment
////
EOS
output = convert_string_to_embedded input
refute_match(/block comment/, output)
end
"#
);
let doc = Parser::default().parse("paragraph\n\n////\nblock comment\n////\n\n\n");
refute_rendered_contains(&doc, "block comment");
}
#[test]
fn trailing_newlines_after_block_comment_at_end_of_document_does_not_create_paragraph() {
verifies!(
r#"
test "trailing newlines after block comment at end of document does not create paragraph" do
input = <<~'EOS'
paragraph
////
block comment
////
EOS
d = document_from_string input
assert_equal 1, d.blocks.size
assert_xpath '//p', d.convert, 1
end
"#
);
// NOTE: divergence from Asciidoctor. Asciidoctor drops the comment
// block from the parsed document (`blocks.size == 1`); this crate
// retains it as a `comment`-context block, so there are two top-level
// blocks (the paragraph and the comment). The essential guarantee this
// test checks — that the trailing newlines do not create a spurious
// paragraph — holds: only one `<p>` is rendered.
let doc = Parser::default().parse("paragraph\n\n////\nblock comment\n////\n\n\n");
let blocks: Vec<_> = doc.child_blocks().collect();
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[1].raw_context().as_ref(), "comment");
assert_xpath(&doc, "//p", 1);
}
#[test]
fn line_starting_with_three_slashes_should_not_be_line_comment() {
verifies!(
r#"
test 'line starting with three slashes should not be line comment' do
input = '/// not a line comment'
output = convert_string_to_embedded input
refute_empty output.strip, "Line should be emitted => #{input.rstrip}"
end
"#
);
let doc = Parser::default().parse("/// not a line comment");
assert_xpath(&doc, "//p[text()=\"/// not a line comment\"]", 1);
}
#[test]
fn preprocessor_directives_should_not_be_processed_within_comment_block_within_block_metadata()
{
verifies!(
r#"
test 'preprocessor directives should not be processed within comment block within block metadata' do
input = <<~'EOS'
.sample title
////
ifdef::asciidoctor[////]
////
line should be shown
EOS
output = convert_string_to_embedded input
assert_xpath '//p[text()="line should be shown"]', output, 1
end
"#
);
let doc = Parser::default()
.parse(".sample title\n////\nifdef::asciidoctor[////]\n////\nline should be shown\n");
assert_xpath(&doc, "//p[text()=\"line should be shown\"]", 1);
}
#[test]
fn preprocessor_directives_should_not_be_processed_within_comment_block() {
verifies!(
r#"
test 'preprocessor directives should not be processed within comment block' do
input = <<~'EOS'
dummy line
////
ifdef::asciidoctor[////]
////
line should be shown
EOS
output = convert_string_to_embedded input
assert_xpath '//p[text()="line should be shown"]', output, 1
end
"#
);
let doc = Parser::default()
.parse("dummy line\n\n////\nifdef::asciidoctor[////]\n////\n\nline should be shown\n");
assert_xpath(&doc, "//p[text()=\"line should be shown\"]", 1);
}
#[test]
fn should_warn_if_unterminated_comment_block_is_detected_in_body() {
verifies!(
r#"
test 'should warn if unterminated comment block is detected in body' do
input = <<~'EOS'
before comment block
////
content that has been disabled
supposed to be after comment block, except it got swallowed by block comment
EOS
convert_string_to_embedded input
assert_message @logger, :WARN, '<stdin>: line 3: unterminated comment block', Hash
end
"#
);
let doc = Parser::default().parse(
"before comment block\n\n////\ncontent that has been disabled\n\nsupposed to be after comment block, except it got swallowed by block comment\n",
);
let warnings: Vec<_> = doc.warnings().collect();
assert_eq!(warnings.len(), 1);
assert_eq!(warnings[0].warning, WarningType::UnterminatedDelimitedBlock);
assert_eq!(warnings[0].source.line(), 3);
}
#[test]
fn should_warn_if_unterminated_comment_block_is_detected_inside_another_block() {
verifies!(
r#"
test 'should warn if unterminated comment block is detected inside another block' do
input = <<~'EOS'
before sidebar block
****
////
content that has been disabled
****
supposed to be after sidebar block, except it got swallowed by block comment
EOS
convert_string_to_embedded input
assert_message @logger, :WARN, '<stdin>: line 4: unterminated comment block', Hash
end
"#
);
let doc = Parser::default().parse(
"before sidebar block\n\n****\n////\ncontent that has been disabled\n****\n\nsupposed to be after sidebar block, except it got swallowed by block comment\n",
);
let unterminated: Vec<_> = doc
.warnings()
.filter(|w| w.warning == WarningType::UnterminatedDelimitedBlock)
.collect();
assert_eq!(unterminated.len(), 1);
assert_eq!(unterminated[0].source.line(), 4);
}
#[test]
fn preprocessor_directives_should_not_be_processed_within_comment_open_block() {
verifies!(
r#"
# WARNING if first line of content is a directive, it will get interpreted before we know it's a comment block
# it happens because we always look a line ahead...not sure what we can do about it
test 'preprocessor directives should not be processed within comment open block' do
input = <<~'EOS'
[comment]
--
first line of comment
ifdef::asciidoctor[--]
line should not be shown
--
EOS
output = convert_string_to_embedded input
assert_xpath '//p', output, 0
end
"#
);
let doc = Parser::default().parse(
"[comment]\n--\nfirst line of comment\nifdef::asciidoctor[--]\nline should not be shown\n--\n\n",
);
assert_xpath(&doc, "//p", 0);
}
#[test]
fn preprocessor_directives_should_not_be_processed_on_subsequent_lines_of_a_comment_paragraph()
{
verifies!(
r#"
# WARNING this assertion fails if the directive is the first line of the paragraph instead of the second
# it happens because we always look a line ahead; not sure what we can do about it
test 'preprocessor directives should not be processed on subsequent lines of a comment paragraph' do
input = <<~'EOS'
[comment]
first line of content
ifdef::asciidoctor[////]
this line should be shown
EOS
output = convert_string_to_embedded input
assert_xpath '//p[text()="this line should be shown"]', output, 1
end
"#
);
let doc = Parser::default()
.parse("[comment]\nfirst line of content\nifdef::asciidoctor[////]\n\nthis line should be shown\n");
assert_xpath(&doc, "//p[text()=\"this line should be shown\"]", 1);
}
#[test]
fn comment_style_on_open_block_should_only_skip_block() {
verifies!(
r#"
test 'comment style on open block should only skip block' do
input = <<~'EOS'
[comment]
--
skip
this block
--
not this text
EOS
result = convert_string_to_embedded input
assert_xpath '//p', result, 1
assert_xpath '//p[text()="not this text"]', result, 1
end
"#
);
let doc =
Parser::default().parse("[comment]\n--\nskip\n\nthis block\n--\n\nnot this text\n");
assert_xpath(&doc, "//p", 1);
assert_xpath(&doc, "//p[text()=\"not this text\"]", 1);
}
#[test]
fn comment_style_on_paragraph_should_only_skip_paragraph() {
verifies!(
r#"
test 'comment style on paragraph should only skip paragraph' do
input = <<~'EOS'
[comment]
skip
this paragraph
not this text
EOS
result = convert_string_to_embedded input
assert_xpath '//p', result, 1
assert_xpath '//p[text()="not this text"]', result, 1
end
"#
);
let doc = Parser::default().parse("[comment]\nskip\nthis paragraph\n\nnot this text\n");
assert_xpath(&doc, "//p", 1);
assert_xpath(&doc, "//p[text()=\"not this text\"]", 1);
}
#[test]
fn comment_style_on_paragraph_should_not_cause_adjacent_block_to_be_skipped() {
verifies!(
r#"
test 'comment style on paragraph should not cause adjacent block to be skipped' do
input = <<~'EOS'
[comment]
skip
this paragraph
[example]
not this text
EOS
result = convert_string_to_embedded input
assert_xpath '/*[@class="exampleblock"]', result, 1
assert_xpath '/*[@class="exampleblock"]//*[normalize-space(text())="not this text"]', result, 1
end
"#
);
let doc =
Parser::default().parse("[comment]\nskip\nthis paragraph\n[example]\nnot this text\n");
assert_xpath(&doc, "/*[@class=\"exampleblock\"]", 1);
assert_xpath(
&doc,
"/*[@class=\"exampleblock\"]//*[normalize-space(text())=\"not this text\"]",
1,
);
}
#[test]
fn should_not_drop_content_that_follows_skipped_content_inside_a_delimited_block() {
verifies!(
r#"
# NOTE this test verifies the nil return value of Parser#next_block
test 'should not drop content that follows skipped content inside a delimited block' do
input = <<~'EOS'
====
paragraph
[comment#idname]
skip
paragraph
====
EOS
result = convert_string_to_embedded input
assert_xpath '/*[@class="exampleblock"]', result, 1
assert_xpath '/*[@class="exampleblock"]//*[@class="paragraph"]', result, 2
assert_xpath '//*[@class="paragraph"][@id="idname"]', result, 0
end
"#
);
let doc = Parser::default()
.parse("====\nparagraph\n\n[comment#idname]\nskip\n\nparagraph\n====\n");
assert_xpath(&doc, "/*[@class=\"exampleblock\"]", 1);
assert_xpath(
&doc,
"/*[@class=\"exampleblock\"]//*[@class=\"paragraph\"]",
2,
);
assert_xpath(&doc, "//*[@class=\"paragraph\"][@id=\"idname\"]", 0);
}
non_normative!(
r#"
end
"#
);
}
mod sidebar_blocks {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Sidebar Blocks' do
"#
);
#[test]
fn should_parse_sidebar_block() {
verifies!(
r#"
test 'should parse sidebar block' do
input = <<~'EOS'
== Section
.Sidebar
****
Content goes here
****
EOS
result = convert_string input
assert_xpath "//*[@class='sidebarblock']//p", result, 1
end
"#
);
// The Ruby xpath single-quotes the class value; the crate's xpath
// engine matches attribute values only when double-quoted.
let doc =
Parser::default().parse("== Section\n\n.Sidebar\n****\nContent goes here\n****\n");
assert_xpath(&doc, "//*[@class=\"sidebarblock\"]//p", 1);
}
non_normative!(
r#"
end
"#
);
}
mod quote_and_verse_blocks {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Quote and Verse Blocks' do
"#
);
#[test]
fn quote_block_with_no_attribution() {
verifies!(
r#"
test 'quote block with no attribution' do
input = <<~'EOS'
____
A famous quote.
____
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph > p', output, 1
assert_css '.quoteblock > .attribution', output, 0
assert_xpath '//*[@class="quoteblock"]//p[text()="A famous quote."]', output, 1
end
"#
);
let doc = Parser::default().parse("____\nA famous quote.\n____\n");
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph > p", 1);
assert_css(&doc, ".quoteblock > .attribution", 0);
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]//p[text()=\"A famous quote.\"]",
1,
);
}
#[test]
fn quote_block_with_attribution() {
verifies!(
r##"
test 'quote block with attribution' do
input = <<~'EOS'
[quote, Famous Person, Famous Book (1999)]
____
A famous quote.
____
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph > p', output, 1
assert_css '.quoteblock > .attribution', output, 1
assert_css '.quoteblock > .attribution > cite', output, 1
assert_css '.quoteblock > .attribution > br + cite', output, 1
assert_xpath '//*[@class="quoteblock"]/*[@class="attribution"]/cite[text()="Famous Book (1999)"]', output, 1
attribution = xmlnodes_at_xpath '//*[@class="quoteblock"]/*[@class="attribution"]', output, 1
author = attribution.children.first
assert_equal "#{decode_char 8212} Famous Person", author.text.strip
end
"##
);
let doc = Parser::default()
.parse("[quote, Famous Person, Famous Book (1999)]\n____\nA famous quote.\n____\n");
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph > p", 1);
assert_css(&doc, ".quoteblock > .attribution", 1);
assert_css(&doc, ".quoteblock > .attribution > cite", 1);
// NOTE: divergence from Asciidoctor: this crate renders the
// attribution as the author text followed directly by `<cite>`,
// with no intervening `<br>`, so the Ruby `br + cite` assertion is
// not reproduced.
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]/*[@class=\"attribution\"]/cite[text()=\"Famous Book (1999)\"]",
1,
);
// Ruby reads the attribution's first child text node; this crate emits
// it as "— Famous Person" (em dash + author).
assert_rendered_contains(&doc, "\u{2014} Famous Person");
}
#[test]
fn quote_block_with_attribute_and_id_and_role_shorthand() {
verifies!(
r#"
test 'quote block with attribute and id and role shorthand' do
input = <<~'EOS'
[quote#justice-to-all.solidarity, Martin Luther King, Jr.]
____
Injustice anywhere is a threat to justice everywhere.
____
EOS
output = convert_string_to_embedded input
assert_css '.quoteblock', output, 1
assert_css '#justice-to-all.quoteblock.solidarity', output, 1
assert_css '.quoteblock > .attribution', output, 1
end
"#
);
let doc = Parser::default().parse(
"[quote#justice-to-all.solidarity, Martin Luther King, Jr.]\n____\nInjustice anywhere is a threat to justice everywhere.\n____\n",
);
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, "#justice-to-all.quoteblock.solidarity", 1);
assert_css(&doc, ".quoteblock > .attribution", 1);
}
#[test]
fn setting_id_using_style_shorthand_should_not_reset_block_style() {
verifies!(
r#"
test 'setting ID using style shorthand should not reset block style' do
input = <<~'EOS'
[quote]
[#justice-to-all.solidarity, Martin Luther King, Jr.]
____
Injustice anywhere is a threat to justice everywhere.
____
EOS
output = convert_string_to_embedded input
assert_css '.quoteblock', output, 1
assert_css '#justice-to-all.quoteblock.solidarity', output, 1
assert_css '.quoteblock > .attribution', output, 1
end
"#
);
let doc = Parser::default().parse(
"[quote]\n[#justice-to-all.solidarity, Martin Luther King, Jr.]\n____\nInjustice anywhere is a threat to justice everywhere.\n____\n",
);
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, "#justice-to-all.quoteblock.solidarity", 1);
assert_css(&doc, ".quoteblock > .attribution", 1);
}
#[test]
fn quote_block_with_complex_content() {
verifies!(
r#"
test 'quote block with complex content' do
input = <<~'EOS'
____
A famous quote.
NOTE: _That_ was inspiring.
____
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph', output, 1
assert_css '.quoteblock > blockquote > .paragraph + .admonitionblock', output, 1
end
"#
);
let doc =
Parser::default().parse("____\nA famous quote.\n\nNOTE: _That_ was inspiring.\n____\n");
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph", 1);
assert_css(
&doc,
".quoteblock > blockquote > .paragraph + .admonitionblock",
1,
);
}
#[test]
fn quote_block_with_attribution_converted_to_docbook() {
non_normative!(
r#"
test 'quote block with attribution converted to DocBook' do
input = <<~'EOS'
[quote, Famous Person, Famous Book (1999)]
____
A famous quote.
____
EOS
output = convert_string input, backend: :docbook
assert_css 'blockquote', output, 1
assert_css 'blockquote > simpara', output, 1
assert_css 'blockquote > attribution', output, 1
assert_css 'blockquote > attribution > citetitle', output, 1
assert_xpath '//blockquote/attribution/citetitle[text()="Famous Book (1999)"]', output, 1
attribution = xmlnodes_at_xpath '//blockquote/attribution', output, 1
author = attribution.children.first
assert_equal 'Famous Person', author.text.strip
end
"#
);
// Backend-specific test omitted: DocBook.
}
#[test]
fn epigraph_quote_block_with_attribution_converted_to_docbook() {
non_normative!(
r#"
test 'epigraph quote block with attribution converted to DocBook' do
input = <<~'EOS'
[.epigraph, Famous Person, Famous Book (1999)]
____
A famous quote.
____
EOS
output = convert_string input, backend: :docbook
assert_css 'epigraph', output, 1
assert_css 'epigraph > simpara', output, 1
assert_css 'epigraph > attribution', output, 1
assert_css 'epigraph > attribution > citetitle', output, 1
assert_xpath '//epigraph/attribution/citetitle[text()="Famous Book (1999)"]', output, 1
attribution = xmlnodes_at_xpath '//epigraph/attribution', output, 1
author = attribution.children.first
assert_equal 'Famous Person', author.text.strip
end
"#
);
// Backend-specific test omitted: DocBook.
}
#[test]
fn markdown_style_quote_block_with_single_paragraph_and_no_attribution() {
verifies!(
r#"
test 'markdown-style quote block with single paragraph and no attribution' do
input = <<~'EOS'
> A famous quote.
> Some more inspiring words.
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph > p', output, 1
assert_css '.quoteblock > .attribution', output, 0
assert_xpath %(//*[@class="quoteblock"]//p[text()="A famous quote.\nSome more inspiring words."]), output, 1
end
"#
);
let doc = Parser::default().parse("> A famous quote.\n> Some more inspiring words.\n");
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph > p", 1);
assert_css(&doc, ".quoteblock > .attribution", 0);
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]//p[text()=\"A famous quote.\nSome more inspiring words.\"]",
1,
);
}
#[test]
fn lazy_markdown_style_quote_block_with_single_paragraph_and_no_attribution() {
verifies!(
r#"
test 'lazy markdown-style quote block with single paragraph and no attribution' do
input = <<~'EOS'
> A famous quote.
Some more inspiring words.
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph > p', output, 1
assert_css '.quoteblock > .attribution', output, 0
assert_xpath %(//*[@class="quoteblock"]//p[text()="A famous quote.\nSome more inspiring words."]), output, 1
end
"#
);
let doc = Parser::default().parse("> A famous quote.\nSome more inspiring words.\n");
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph > p", 1);
assert_css(&doc, ".quoteblock > .attribution", 0);
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]//p[text()=\"A famous quote.\nSome more inspiring words.\"]",
1,
);
}
#[test]
fn markdown_style_quote_block_with_multiple_paragraphs_and_no_attribution() {
verifies!(
r#"
test 'markdown-style quote block with multiple paragraphs and no attribution' do
input = <<~'EOS'
> A famous quote.
>
> Some more inspiring words.
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph > p', output, 2
assert_css '.quoteblock > .attribution', output, 0
assert_xpath %((//*[@class="quoteblock"]//p)[1][text()="A famous quote."]), output, 1
assert_xpath %((//*[@class="quoteblock"]//p)[2][text()="Some more inspiring words."]), output, 1
end
"#
);
let doc = Parser::default().parse("> A famous quote.\n>\n> Some more inspiring words.\n");
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph > p", 2);
assert_css(&doc, ".quoteblock > .attribution", 0);
assert_xpath(
&doc,
"(//*[@class=\"quoteblock\"]//p)[1][text()=\"A famous quote.\"]",
1,
);
assert_xpath(
&doc,
"(//*[@class=\"quoteblock\"]//p)[2][text()=\"Some more inspiring words.\"]",
1,
);
}
#[test]
fn markdown_style_quote_block_with_multiple_blocks_and_no_attribution() {
verifies!(
r#"
test 'markdown-style quote block with multiple blocks and no attribution' do
input = <<~'EOS'
> A famous quote.
>
> NOTE: Some more inspiring words.
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph > p', output, 1
assert_css '.quoteblock > blockquote > .admonitionblock', output, 1
assert_css '.quoteblock > .attribution', output, 0
assert_xpath %((//*[@class="quoteblock"]//p)[1][text()="A famous quote."]), output, 1
assert_xpath %((//*[@class="quoteblock"]//*[@class="admonitionblock note"]//*[@class="content"])[1][normalize-space(text())="Some more inspiring words."]), output, 1
end
"#
);
let doc =
Parser::default().parse("> A famous quote.\n>\n> NOTE: Some more inspiring words.\n");
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph > p", 1);
assert_css(&doc, ".quoteblock > blockquote > .admonitionblock", 1);
assert_css(&doc, ".quoteblock > .attribution", 0);
assert_xpath(
&doc,
"(//*[@class=\"quoteblock\"]//p)[1][text()=\"A famous quote.\"]",
1,
);
assert_xpath(
&doc,
"(//*[@class=\"quoteblock\"]//*[@class=\"admonitionblock note\"]//*[@class=\"content\"])[1][normalize-space(text())=\"Some more inspiring words.\"]",
1,
);
}
#[test]
fn markdown_style_quote_block_with_single_paragraph_and_attribution() {
verifies!(
r##"
test 'markdown-style quote block with single paragraph and attribution' do
input = <<~'EOS'
> A famous quote.
> Some more inspiring words.
> -- Famous Person, Famous Source, Volume 1 (1999)
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph > p', output, 1
assert_xpath %(//*[@class="quoteblock"]//p[text()="A famous quote.\nSome more inspiring words."]), output, 1
assert_css '.quoteblock > .attribution', output, 1
assert_css '.quoteblock > .attribution > cite', output, 1
assert_css '.quoteblock > .attribution > br + cite', output, 1
assert_xpath '//*[@class="quoteblock"]/*[@class="attribution"]/cite[text()="Famous Source, Volume 1 (1999)"]', output, 1
attribution = xmlnodes_at_xpath '//*[@class="quoteblock"]/*[@class="attribution"]', output, 1
author = attribution.children.first
assert_equal "#{decode_char 8212} Famous Person", author.text.strip
end
"##
);
let doc = Parser::default().parse(
"> A famous quote.\n> Some more inspiring words.\n> -- Famous Person, Famous Source, Volume 1 (1999)\n",
);
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph > p", 1);
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]//p[text()=\"A famous quote.\nSome more inspiring words.\"]",
1,
);
assert_css(&doc, ".quoteblock > .attribution", 1);
assert_css(&doc, ".quoteblock > .attribution > cite", 1);
// NOTE: divergence from Asciidoctor: this crate renders the
// attribution as the author text followed directly by `<cite>`,
// with no intervening `<br>`, so the Ruby `br + cite` assertion is
// not reproduced.
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]/*[@class=\"attribution\"]/cite[text()=\"Famous Source, Volume 1 (1999)\"]",
1,
);
assert_rendered_contains(&doc, "\u{2014} Famous Person");
}
#[test]
fn markdown_style_quote_block_with_only_attribution() {
verifies!(
r#"
test 'markdown-style quote block with only attribution' do
input = '> -- Anonymous'
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > *', output, 0
assert_css '.quoteblock > .attribution', output, 1
assert_xpath %(//*[@class="quoteblock"]//*[@class="attribution"][contains(text(),"Anonymous")]), output, 1
end
"#
);
let doc = Parser::default().parse("> -- Anonymous");
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_css(&doc, ".quoteblock > blockquote > *", 0);
assert_css(&doc, ".quoteblock > .attribution", 1);
// The xpath engine does not implement `contains(text(), ..)`; assert the
// attribution text via the rendered output instead.
assert_rendered_contains(&doc, "Anonymous");
}
#[test]
fn should_parse_credit_line_in_markdown_style_quote_block_like_positional_block_attributes() {
verifies!(
r#"
test 'should parse credit line in markdown-style quote block like positional block attributes' do
input = <<~'EOS'
> I hold it that a little rebellion now and then is a good thing,
> and as necessary in the political world as storms in the physical.
-- Thomas Jefferson, https://jeffersonpapers.princeton.edu/selected-documents/james-madison-1[The Papers of Thomas Jefferson, Volume 11]
EOS
output = convert_string_to_embedded input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock cite a[href="https://jeffersonpapers.princeton.edu/selected-documents/james-madison-1"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"> I hold it that a little rebellion now and then is a good thing,\n> and as necessary in the political world as storms in the physical.\n-- Thomas Jefferson, https://jeffersonpapers.princeton.edu/selected-documents/james-madison-1[The Papers of Thomas Jefferson, Volume 11]\n",
);
assert_css(&doc, ".quoteblock", 1);
// The crate's CSS engine does not parse a descendant attribute
// selector of this shape; the equivalent xpath is used instead.
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]//cite//a[@href=\"https://jeffersonpapers.princeton.edu/selected-documents/james-madison-1\"]",
1,
);
}
#[test]
fn quoted_paragraph_style_quote_block_with_attribution() {
verifies!(
r##"
test 'quoted paragraph-style quote block with attribution' do
input = <<~'EOS'
"A famous quote.
Some more inspiring words."
-- Famous Person, Famous Source, Volume 1 (1999)
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_xpath %(//*[@class="quoteblock"]/blockquote[normalize-space(text())="A famous quote. Some more inspiring words."]), output, 1
assert_css '.quoteblock > .attribution', output, 1
assert_css '.quoteblock > .attribution > cite', output, 1
assert_css '.quoteblock > .attribution > br + cite', output, 1
assert_xpath '//*[@class="quoteblock"]/*[@class="attribution"]/cite[text()="Famous Source, Volume 1 (1999)"]', output, 1
attribution = xmlnodes_at_xpath '//*[@class="quoteblock"]/*[@class="attribution"]', output, 1
author = attribution.children.first
assert_equal "#{decode_char 8212} Famous Person", author.text.strip
end
"##
);
let doc = Parser::default().parse(
"\"A famous quote.\nSome more inspiring words.\"\n-- Famous Person, Famous Source, Volume 1 (1999)\n",
);
assert_css(&doc, ".quoteblock", 1);
assert_css(&doc, ".quoteblock > blockquote", 1);
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]/blockquote[normalize-space(text())=\"A famous quote. Some more inspiring words.\"]",
1,
);
assert_css(&doc, ".quoteblock > .attribution", 1);
assert_css(&doc, ".quoteblock > .attribution > cite", 1);
// NOTE: divergence from Asciidoctor: this crate renders the
// attribution as the author text followed directly by `<cite>`,
// with no intervening `<br>`, so the Ruby `br + cite` assertion is
// not reproduced.
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]/*[@class=\"attribution\"]/cite[text()=\"Famous Source, Volume 1 (1999)\"]",
1,
);
assert_rendered_contains(&doc, "\u{2014} Famous Person");
}
#[test]
fn should_parse_credit_line_in_quoted_paragraph_style_quote_block_like_positional_block_attributes()
{
verifies!(
r#"
test 'should parse credit line in quoted paragraph-style quote block like positional block attributes' do
input = <<~'EOS'
"I hold it that a little rebellion now and then is a good thing,
and as necessary in the political world as storms in the physical."
-- Thomas Jefferson, https://jeffersonpapers.princeton.edu/selected-documents/james-madison-1[The Papers of Thomas Jefferson, Volume 11]
EOS
output = convert_string_to_embedded input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock cite a[href="https://jeffersonpapers.princeton.edu/selected-documents/james-madison-1"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"\"I hold it that a little rebellion now and then is a good thing,\nand as necessary in the political world as storms in the physical.\"\n-- Thomas Jefferson, https://jeffersonpapers.princeton.edu/selected-documents/james-madison-1[The Papers of Thomas Jefferson, Volume 11]\n",
);
assert_css(&doc, ".quoteblock", 1);
// The crate's CSS engine does not parse a descendant attribute
// selector of this shape; the equivalent xpath is used instead.
assert_xpath(
&doc,
"//*[@class=\"quoteblock\"]//cite//a[@href=\"https://jeffersonpapers.princeton.edu/selected-documents/james-madison-1\"]",
1,
);
}
#[test]
fn single_line_verse_block_without_attribution() {
verifies!(
r#"
test 'single-line verse block without attribution' do
input = <<~'EOS'
[verse]
____
A famous verse.
____
EOS
output = convert_string input
assert_css '.verseblock', output, 1
assert_css '.verseblock > pre', output, 1
assert_css '.verseblock > .attribution', output, 0
assert_css '.verseblock p', output, 0
assert_xpath '//*[@class="verseblock"]/pre[normalize-space(text())="A famous verse."]', output, 1
end
"#
);
let doc = Parser::default().parse("[verse]\n____\nA famous verse.\n____\n");
assert_css(&doc, ".verseblock", 1);
assert_css(&doc, ".verseblock > pre", 1);
assert_css(&doc, ".verseblock > .attribution", 0);
assert_css(&doc, ".verseblock p", 0);
assert_xpath(
&doc,
"//*[@class=\"verseblock\"]/pre[normalize-space(text())=\"A famous verse.\"]",
1,
);
}
#[test]
fn single_line_verse_block_with_attribution() {
verifies!(
r##"
test 'single-line verse block with attribution' do
input = <<~'EOS'
[verse, Famous Poet, Famous Poem]
____
A famous verse.
____
EOS
output = convert_string input
assert_css '.verseblock', output, 1
assert_css '.verseblock p', output, 0
assert_css '.verseblock > pre', output, 1
assert_css '.verseblock > .attribution', output, 1
assert_css '.verseblock > .attribution > cite', output, 1
assert_css '.verseblock > .attribution > br + cite', output, 1
assert_xpath '//*[@class="verseblock"]/*[@class="attribution"]/cite[text()="Famous Poem"]', output, 1
attribution = xmlnodes_at_xpath '//*[@class="verseblock"]/*[@class="attribution"]', output, 1
author = attribution.children.first
assert_equal "#{decode_char 8212} Famous Poet", author.text.strip
end
"##
);
let doc = Parser::default()
.parse("[verse, Famous Poet, Famous Poem]\n____\nA famous verse.\n____\n");
assert_css(&doc, ".verseblock", 1);
assert_css(&doc, ".verseblock p", 0);
assert_css(&doc, ".verseblock > pre", 1);
assert_css(&doc, ".verseblock > .attribution", 1);
assert_css(&doc, ".verseblock > .attribution > cite", 1);
// NOTE: divergence from Asciidoctor: this crate renders the
// attribution as the author text followed directly by `<cite>`,
// with no intervening `<br>`, so the Ruby `br + cite` assertion is
// not reproduced.
assert_xpath(
&doc,
"//*[@class=\"verseblock\"]/*[@class=\"attribution\"]/cite[text()=\"Famous Poem\"]",
1,
);
assert_rendered_contains(&doc, "\u{2014} Famous Poet");
}
#[test]
fn single_line_verse_block_with_attribution_converted_to_docbook() {
non_normative!(
r#"
test 'single-line verse block with attribution converted to DocBook' do
input = <<~'EOS'
[verse, Famous Poet, Famous Poem]
____
A famous verse.
____
EOS
output = convert_string input, backend: :docbook
assert_css 'blockquote', output, 1
assert_css 'blockquote simpara', output, 0
assert_css 'blockquote > literallayout', output, 1
assert_css 'blockquote > attribution', output, 1
assert_css 'blockquote > attribution > citetitle', output, 1
assert_xpath '//blockquote/attribution/citetitle[text()="Famous Poem"]', output, 1
attribution = xmlnodes_at_xpath '//blockquote/attribution', output, 1
author = attribution.children.first
assert_equal 'Famous Poet', author.text.strip
end
"#
);
// Backend-specific test omitted: DocBook.
}
#[test]
fn single_line_epigraph_verse_block_with_attribution_converted_to_docbook() {
non_normative!(
r#"
test 'single-line epigraph verse block with attribution converted to DocBook' do
input = <<~'EOS'
[verse.epigraph, Famous Poet, Famous Poem]
____
A famous verse.
____
EOS
output = convert_string input, backend: :docbook
assert_css 'epigraph', output, 1
assert_css 'epigraph simpara', output, 0
assert_css 'epigraph > literallayout', output, 1
assert_css 'epigraph > attribution', output, 1
assert_css 'epigraph > attribution > citetitle', output, 1
assert_xpath '//epigraph/attribution/citetitle[text()="Famous Poem"]', output, 1
attribution = xmlnodes_at_xpath '//epigraph/attribution', output, 1
author = attribution.children.first
assert_equal 'Famous Poet', author.text.strip
end
"#
);
// Backend-specific test omitted: DocBook.
}
#[test]
fn multi_stanza_verse_block() {
verifies!(
r#"
test 'multi-stanza verse block' do
input = <<~'EOS'
[verse]
____
A famous verse.
Stanza two.
____
EOS
output = convert_string input
assert_xpath '//*[@class="verseblock"]', output, 1
assert_xpath '//*[@class="verseblock"]/pre', output, 1
assert_xpath '//*[@class="verseblock"]//p', output, 0
assert_xpath '//*[@class="verseblock"]/pre[contains(text(), "A famous verse.")]', output, 1
assert_xpath '//*[@class="verseblock"]/pre[contains(text(), "Stanza two.")]', output, 1
end
"#
);
let doc = Parser::default().parse("[verse]\n____\nA famous verse.\n\nStanza two.\n____\n");
assert_xpath(&doc, "//*[@class=\"verseblock\"]", 1);
assert_xpath(&doc, "//*[@class=\"verseblock\"]/pre", 1);
assert_xpath(&doc, "//*[@class=\"verseblock\"]//p", 0);
// The xpath engine does not implement `contains(text(), ..)`; the pre's
// text (which spans both stanzas) is checked via the rendered output.
assert_rendered_contains(&doc, "A famous verse.");
assert_rendered_contains(&doc, "Stanza two.");
}
#[test]
fn verse_block_does_not_contain_block_elements() {
verifies!(
r#"
test 'verse block does not contain block elements' do
input = <<~'EOS'
[verse]
____
A famous verse.
....
not a literal
....
____
EOS
output = convert_string input
assert_css '.verseblock', output, 1
assert_css '.verseblock > pre', output, 1
assert_css '.verseblock p', output, 0
assert_css '.verseblock .literalblock', output, 0
end
"#
);
let doc = Parser::default()
.parse("[verse]\n____\nA famous verse.\n\n....\nnot a literal\n....\n____\n");
assert_css(&doc, ".verseblock", 1);
assert_css(&doc, ".verseblock > pre", 1);
assert_css(&doc, ".verseblock p", 0);
assert_css(&doc, ".verseblock .literalblock", 0);
}
#[test]
fn verse_should_have_normal_subs() {
non_normative!(
r#"
test 'verse should have normal subs' do
input = <<~'EOS'
[verse]
____
A famous verse
____
EOS
verse = block_from_string input
assert_equal Asciidoctor::Substitutors::NORMAL_SUBS, verse.subs
end
"#
);
// Not ported: asserts the Ruby-internal `Substitutors::NORMAL_SUBS`
// subs list on the block. The behavior (a verse receives the normal
// substitutions) is verified by
// `should_perform_normal_subs_on_a_verse_block`.
}
#[test]
fn should_not_recognize_callouts_in_a_verse() {
verifies!(
r#"
test 'should not recognize callouts in a verse' do
input = <<~'EOS'
[verse]
____
La la la <1>
____
<1> Not pointing to a callout
EOS
output = convert_string_to_embedded input
assert_xpath '//pre[text()="La la la <1>"]', output, 1
assert_message @logger, :WARN, '<stdin>: line 5: no callout found for <1>', Hash
end
"#
);
let doc = Parser::default()
.parse("[verse]\n____\nLa la la <1>\n____\n<1> Not pointing to a callout\n");
assert_xpath(&doc, "//pre[text()=\"La la la <1>\"]", 1);
let warnings: Vec<_> = doc
.warnings()
.filter(|w| matches!(w.warning, WarningType::NoCalloutFound(_)))
.collect();
assert_eq!(warnings.len(), 1);
assert_eq!(warnings[0].source.line(), 5);
}
#[test]
fn should_perform_normal_subs_on_a_verse_block() {
verifies!(
r##"
test 'should perform normal subs on a verse block' do
input = <<~'EOS'
[verse]
____
_GET /groups/link:#group-id[\{group-id\}]_
____
EOS
output = convert_string_to_embedded input
assert_includes output, '<pre class="content"><em>GET /groups/<a href="#group-id">{group-id}</a></em></pre>'
end
"##
);
let doc = Parser::default()
.parse("[verse]\n____\n_GET /groups/link:#group-id[\\{group-id\\}]_\n____\n");
assert_rendered_contains(
&doc,
"<em>GET /groups/<a href=\"#group-id\">{group-id}</a></em>",
);
}
non_normative!(
r#"
end
"#
);
}
mod example_blocks {
use crate::{document::InterpretedValue, parser::ModificationContext, tests::prelude::*};
non_normative!(
r#"
context "Example Blocks" do
"#
);
// Returns the top-level example blocks, skipping the `attribute`-context
// blocks this crate emits for mid-document attribute entries (Asciidoctor
// does not model those as blocks).
fn example_blocks<'a>(doc: &'a crate::Document<'a>) -> Vec<&'a crate::blocks::Block<'a>> {
doc.child_blocks()
.filter(|b| b.raw_context().as_ref() == "example")
.collect()
}
#[test]
fn can_convert_example_block() {
verifies!(
r#"
test "can convert example block" do
input = <<~'EOS'
====
This is an example of an example block.
How crazy is that?
====
EOS
output = convert_string input
assert_xpath '//*[@class="exampleblock"]//p', output, 2
end
"#
);
let doc = Parser::default()
.parse("====\nThis is an example of an example block.\n\nHow crazy is that?\n====\n");
assert_xpath(&doc, "//*[@class=\"exampleblock\"]//p", 2);
}
#[test]
fn assigns_sequential_numbered_caption_to_example_block_with_title() {
verifies!(
r#"
test 'assigns sequential numbered caption to example block with title' do
input = <<~'EOS'
.Writing Docs with AsciiDoc
====
Here's how you write AsciiDoc.
You just write.
====
.Writing Docs with DocBook
====
Here's how you write DocBook.
You futz with XML.
====
EOS
doc = document_from_string input
assert_equal 1, doc.blocks[0].numeral
assert_equal 1, doc.blocks[0].number
assert_equal 2, doc.blocks[1].numeral
assert_equal 2, doc.blocks[1].number
output = doc.convert
assert_xpath '(//*[@class="exampleblock"])[1]/*[@class="title"][text()="Example 1. Writing Docs with AsciiDoc"]', output, 1
assert_xpath '(//*[@class="exampleblock"])[2]/*[@class="title"][text()="Example 2. Writing Docs with DocBook"]', output, 1
assert_equal 2, doc.attributes['example-number']
end
"#
);
let doc = Parser::default().parse(
".Writing Docs with AsciiDoc\n====\nHere's how you write AsciiDoc.\n\nYou just write.\n====\n\n.Writing Docs with DocBook\n====\nHere's how you write DocBook.\n\nYou futz with XML.\n====\n",
);
// This crate exposes `number()` (usize) rather than Ruby's `numeral`.
let blocks = example_blocks(&doc);
assert_eq!(blocks[0].number(), Some(1));
assert_eq!(blocks[1].number(), Some(2));
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[1]/*[@class=\"title\"][text()=\"Example 1. Writing Docs with AsciiDoc\"]",
1,
);
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[2]/*[@class=\"title\"][text()=\"Example 2. Writing Docs with DocBook\"]",
1,
);
assert_eq!(
doc.attribute_value("example-number"),
InterpretedValue::Value("2".to_string())
);
}
#[test]
fn assigns_sequential_character_caption_to_example_block_with_title() {
verifies!(
r#"
test 'assigns sequential character caption to example block with title' do
input = <<~'EOS'
:example-number: @
.Writing Docs with AsciiDoc
====
Here's how you write AsciiDoc.
You just write.
====
.Writing Docs with DocBook
====
Here's how you write DocBook.
You futz with XML.
====
EOS
doc = document_from_string input
assert_equal 'A', doc.blocks[0].numeral
assert_equal 'A', doc.blocks[0].number
assert_equal 'B', doc.blocks[1].numeral
assert_equal 'B', doc.blocks[1].number
output = doc.convert
assert_xpath '(//*[@class="exampleblock"])[1]/*[@class="title"][text()="Example A. Writing Docs with AsciiDoc"]', output, 1
assert_xpath '(//*[@class="exampleblock"])[2]/*[@class="title"][text()="Example B. Writing Docs with DocBook"]', output, 1
assert_equal 'B', doc.attributes['example-number']
end
"#
);
let doc = Parser::default().parse(
":example-number: @\n\n.Writing Docs with AsciiDoc\n====\nHere's how you write AsciiDoc.\n\nYou just write.\n====\n\n.Writing Docs with DocBook\n====\nHere's how you write DocBook.\n\nYou futz with XML.\n====\n",
);
// NOTE: divergence from Asciidoctor: Ruby's `numeral`/`number` carry the
// character sequence ('A', 'B'); this crate's `number()` is a `usize` and
// returns `None` for character captions. The character caption itself is
// rendered in the title and reflected in the `example-number` attribute.
let blocks = example_blocks(&doc);
assert_eq!(blocks[0].number(), None);
assert_eq!(blocks[1].number(), None);
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[1]/*[@class=\"title\"][text()=\"Example A. Writing Docs with AsciiDoc\"]",
1,
);
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[2]/*[@class=\"title\"][text()=\"Example B. Writing Docs with DocBook\"]",
1,
);
assert_eq!(
doc.attribute_value("example-number"),
InterpretedValue::Value("B".to_string())
);
}
#[test]
fn should_increment_counter_for_example_even_when_example_number_is_locked_by_the_api() {
verifies!(
r#"
test 'should increment counter for example even when example-number is locked by the API' do
input = <<~'EOS'
.Writing Docs with AsciiDoc
====
Here's how you write AsciiDoc.
You just write.
====
.Writing Docs with DocBook
====
Here's how you write DocBook.
You futz with XML.
====
EOS
doc = document_from_string input, attributes: { 'example-number' => '`' }
output = doc.convert
assert_xpath '(//*[@class="exampleblock"])[1]/*[@class="title"][text()="Example a. Writing Docs with AsciiDoc"]', output, 1
assert_xpath '(//*[@class="exampleblock"])[2]/*[@class="title"][text()="Example b. Writing Docs with DocBook"]', output, 1
assert_equal 'b', doc.attributes['example-number']
end
"#
);
let doc = Parser::default()
.with_intrinsic_attribute("example-number", "`", ModificationContext::ApiOnly)
.parse(
".Writing Docs with AsciiDoc\n====\nHere's how you write AsciiDoc.\n\nYou just write.\n====\n\n.Writing Docs with DocBook\n====\nHere's how you write DocBook.\n\nYou futz with XML.\n====\n",
);
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[1]/*[@class=\"title\"][text()=\"Example a. Writing Docs with AsciiDoc\"]",
1,
);
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[2]/*[@class=\"title\"][text()=\"Example b. Writing Docs with DocBook\"]",
1,
);
assert_eq!(
doc.attribute_value("example-number"),
InterpretedValue::Value("b".to_string())
);
}
#[test]
fn should_use_explicit_caption_if_specified() {
verifies!(
r#"
test 'should use explicit caption if specified' do
input = <<~'EOS'
[caption="Look! "]
.Writing Docs with AsciiDoc
====
Here's how you write AsciiDoc.
You just write.
====
EOS
doc = document_from_string input
assert_nil doc.blocks[0].numeral
output = doc.convert
assert_xpath '(//*[@class="exampleblock"])[1]/*[@class="title"][text()="Look! Writing Docs with AsciiDoc"]', output, 1
refute doc.attributes.key? 'example-number'
end
"#
);
let doc = Parser::default().parse(
"[caption=\"Look! \"]\n.Writing Docs with AsciiDoc\n====\nHere's how you write AsciiDoc.\n\nYou just write.\n====\n",
);
let blocks = example_blocks(&doc);
assert_eq!(blocks[0].number(), None);
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[1]/*[@class=\"title\"][text()=\"Look! Writing Docs with AsciiDoc\"]",
1,
);
assert_eq!(
doc.attribute_value("example-number"),
InterpretedValue::Unset
);
}
// NOTE: divergence from Asciidoctor. Asciidoctor honors an empty `:caption:`
// attribute to disable the automatic block caption (so the second example's
// title is just "second example"); this crate keeps numbering it
// ("Example 2. second example"). Kept `#[ignore]`d with the Ruby-intended
// assertions until the empty-`:caption:` toggle is honored.
// TODO: honor an empty `:caption:` to suppress the block caption.
#[ignore]
#[test]
fn automatic_caption_can_be_turned_off_and_on_and_modified() {
verifies!(
r#"
test 'automatic caption can be turned off and on and modified' do
input = <<~'EOS'
.first example
====
an example
====
:caption:
.second example
====
another example
====
:caption!:
:example-caption: Exhibit
.third example
====
yet another example
====
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="exampleblock"]', output, 3
assert_xpath '(/*[@class="exampleblock"])[1]/*[@class="title"][starts-with(text(), "Example ")]', output, 1
assert_xpath '(/*[@class="exampleblock"])[2]/*[@class="title"][text()="second example"]', output, 1
assert_xpath '(/*[@class="exampleblock"])[3]/*[@class="title"][starts-with(text(), "Exhibit ")]', output, 1
end
"#
);
let doc = Parser::default().parse(
".first example\n====\nan example\n====\n\n:caption:\n\n.second example\n====\nanother example\n====\n\n:caption!:\n:example-caption: Exhibit\n\n.third example\n====\nyet another example\n====\n",
);
assert_xpath(&doc, "/*[@class=\"exampleblock\"]", 3);
assert_xpath(
&doc,
"(/*[@class=\"exampleblock\"])[1]/*[@class=\"title\"][starts-with(text(), \"Example \")]",
1,
);
assert_xpath(
&doc,
"(/*[@class=\"exampleblock\"])[2]/*[@class=\"title\"][text()=\"second example\"]",
1,
);
assert_xpath(
&doc,
"(/*[@class=\"exampleblock\"])[3]/*[@class=\"title\"][starts-with(text(), \"Exhibit \")]",
1,
);
}
#[test]
fn should_use_explicit_caption_if_specified_even_if_block_specific_global_caption_is_disabled()
{
verifies!(
r#"
test 'should use explicit caption if specified even if block-specific global caption is disabled' do
input = <<~'EOS'
:!example-caption:
[caption="Look! "]
.Writing Docs with AsciiDoc
====
Here's how you write AsciiDoc.
You just write.
====
EOS
doc = document_from_string input
assert_nil doc.blocks[0].numeral
output = doc.convert
assert_xpath '(//*[@class="exampleblock"])[1]/*[@class="title"][text()="Look! Writing Docs with AsciiDoc"]', output, 1
refute doc.attributes.key? 'example-number'
end
"#
);
let doc = Parser::default().parse(
":!example-caption:\n\n[caption=\"Look! \"]\n.Writing Docs with AsciiDoc\n====\nHere's how you write AsciiDoc.\n\nYou just write.\n====\n",
);
let blocks = example_blocks(&doc);
assert_eq!(blocks[0].number(), None);
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[1]/*[@class=\"title\"][text()=\"Look! Writing Docs with AsciiDoc\"]",
1,
);
assert_eq!(
doc.attribute_value("example-number"),
InterpretedValue::Unset
);
}
#[test]
fn should_use_global_caption_if_specified_even_if_block_specific_global_caption_is_disabled() {
verifies!(
r#"
test 'should use global caption if specified even if block-specific global caption is disabled' do
input = <<~'EOS'
:!example-caption:
:caption: Look!{sp}
.Writing Docs with AsciiDoc
====
Here's how you write AsciiDoc.
You just write.
====
EOS
doc = document_from_string input
assert_nil doc.blocks[0].numeral
output = doc.convert
assert_xpath '(//*[@class="exampleblock"])[1]/*[@class="title"][text()="Look! Writing Docs with AsciiDoc"]', output, 1
refute doc.attributes.key? 'example-number'
end
"#
);
let doc = Parser::default().parse(
":!example-caption:\n:caption: Look!{sp}\n\n.Writing Docs with AsciiDoc\n====\nHere's how you write AsciiDoc.\n\nYou just write.\n====\n",
);
let blocks = example_blocks(&doc);
assert_eq!(blocks[0].number(), None);
assert_xpath(
&doc,
"(//*[@class=\"exampleblock\"])[1]/*[@class=\"title\"][text()=\"Look! Writing Docs with AsciiDoc\"]",
1,
);
assert_eq!(
doc.attribute_value("example-number"),
InterpretedValue::Unset
);
}
#[test]
fn should_not_process_caption_attribute_on_block_that_does_not_support_a_caption() {
verifies!(
r#"
test 'should not process caption attribute on block that does not support a caption' do
input = <<~'EOS'
[caption="Look! "]
.No caption here
--
content
--
EOS
doc = document_from_string input
assert_nil doc.blocks[0].caption
assert_equal 'Look! ', (doc.blocks[0].attr 'caption')
output = doc.convert
assert_xpath '(//*[@class="openblock"])[1]/*[@class="title"][text()="No caption here"]', output, 1
end
"#
);
let doc =
Parser::default().parse("[caption=\"Look! \"]\n.No caption here\n--\ncontent\n--\n");
// The open block does not support a caption, so `caption()` is `None`
// even though the `caption` attribute is present on the block. (Ruby
// additionally reads the raw `caption` attribute back off the block.)
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.caption(), None);
assert_xpath(
&doc,
"(//*[@class=\"openblock\"])[1]/*[@class=\"title\"][text()=\"No caption here\"]",
1,
);
}
#[test]
fn should_create_details_summary_set_if_collapsible_option_is_set() {
verifies!(
r#"
test 'should create details/summary set if collapsible option is set' do
input = <<~'EOS'
.Toggle Me
[%collapsible]
====
This content is revealed when the user clicks the words "Toggle Me".
====
EOS
output = convert_string_to_embedded input
assert_css 'details', output, 1
assert_css 'details[open]', output, 0
assert_css 'details > summary.title', output, 1
assert_xpath '//details/summary[text()="Toggle Me"]', output, 1
assert_css 'details > summary.title + .content', output, 1
assert_css 'details > summary.title + .content p', output, 1
end
"#
);
let doc = Parser::default().parse(
".Toggle Me\n[%collapsible]\n====\nThis content is revealed when the user clicks the words \"Toggle Me\".\n====\n",
);
assert_css(&doc, "details", 1);
assert_css(&doc, "details[open]", 0);
assert_css(&doc, "details > summary.title", 1);
assert_xpath(&doc, "//details/summary[text()=\"Toggle Me\"]", 1);
assert_css(&doc, "details > summary.title + .content", 1);
// The crate's CSS engine does not resolve a sibling-then-descendant
// chain (`+ .content p`); the equivalent xpath is used instead.
assert_xpath(&doc, "//details//*[@class=\"content\"]//p", 1);
}
#[test]
fn should_open_details_summary_set_if_collapsible_and_open_options_are_set() {
verifies!(
r#"
test 'should open details/summary set if collapsible and open options are set' do
input = <<~'EOS'
.Toggle Me
[%collapsible%open]
====
This content is revealed when the user clicks the words "Toggle Me".
====
EOS
output = convert_string_to_embedded input
assert_css 'details', output, 1
assert_css 'details[open]', output, 1
assert_css 'details > summary.title', output, 1
assert_xpath '//details/summary[text()="Toggle Me"]', output, 1
end
"#
);
let doc = Parser::default().parse(
".Toggle Me\n[%collapsible%open]\n====\nThis content is revealed when the user clicks the words \"Toggle Me\".\n====\n",
);
assert_css(&doc, "details", 1);
assert_css(&doc, "details[open]", 1);
assert_css(&doc, "details > summary.title", 1);
assert_xpath(&doc, "//details/summary[text()=\"Toggle Me\"]", 1);
}
#[test]
fn should_add_default_summary_element_if_collapsible_option_is_set_and_title_is_not_specifed() {
verifies!(
r#"
test 'should add default summary element if collapsible option is set and title is not specifed' do
input = <<~'EOS'
[%collapsible]
====
This content is revealed when the user clicks the words "Details".
====
EOS
output = convert_string_to_embedded input
assert_css 'details', output, 1
assert_css 'details > summary.title', output, 1
assert_xpath '//details/summary[text()="Details"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"[%collapsible]\n====\nThis content is revealed when the user clicks the words \"Details\".\n====\n",
);
assert_css(&doc, "details", 1);
assert_css(&doc, "details > summary.title", 1);
assert_xpath(&doc, "//details/summary[text()=\"Details\"]", 1);
}
#[test]
fn should_not_allow_collapsible_block_to_increment_example_number() {
verifies!(
r#"
test 'should not allow collapsible block to increment example number' do
input = <<~'EOS'
.Before
====
before
====
.Show Me The Goods
[%collapsible]
====
This content is revealed when the user clicks the words "Show Me The Goods".
====
.After
====
after
====
EOS
output = convert_string_to_embedded input
assert_xpath '//*[@class="title"][text()="Example 1. Before"]', output, 1
assert_xpath '//*[@class="title"][text()="Example 2. After"]', output, 1
assert_css 'details', output, 1
assert_css 'details > summary.title', output, 1
assert_xpath '//details/summary[text()="Show Me The Goods"]', output, 1
end
"#
);
let doc = Parser::default().parse(
".Before\n====\nbefore\n====\n\n.Show Me The Goods\n[%collapsible]\n====\nThis content is revealed when the user clicks the words \"Show Me The Goods\".\n====\n\n.After\n====\nafter\n====\n",
);
assert_xpath(
&doc,
"//*[@class=\"title\"][text()=\"Example 1. Before\"]",
1,
);
assert_xpath(
&doc,
"//*[@class=\"title\"][text()=\"Example 2. After\"]",
1,
);
assert_css(&doc, "details", 1);
assert_css(&doc, "details > summary.title", 1);
assert_xpath(&doc, "//details/summary[text()=\"Show Me The Goods\"]", 1);
}
#[test]
fn should_warn_if_example_block_is_not_terminated() {
verifies!(
r#"
test 'should warn if example block is not terminated' do
input = <<~'EOS'
outside
====
inside
still inside
eof
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="exampleblock"]', output, 1
assert_message @logger, :WARN, '<stdin>: line 3: unterminated example block', Hash
end
"#
);
let doc = Parser::default().parse("outside\n\n====\ninside\n\nstill inside\n\neof\n");
assert_xpath(&doc, "/*[@class=\"exampleblock\"]", 1);
let warnings: Vec<_> = doc
.warnings()
.filter(|w| w.warning == WarningType::UnterminatedDelimitedBlock)
.collect();
assert_eq!(warnings.len(), 1);
assert_eq!(warnings[0].source.line(), 3);
}
non_normative!(
r#"
end
"#
);
}
mod admonition_blocks {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Admonition Blocks' do
"#
);
#[test]
fn caption_block_level_attribute_should_be_used_as_caption() {
verifies!(
r#"
test 'caption block-level attribute should be used as caption' do
input = <<~'EOS'
:tip-caption: Pro Tip
[caption="Pro Tip"]
TIP: Override the caption of an admonition block using an attribute entry
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="admonitionblock tip"]//*[@class="icon"]/*[@class="title"][text()="Pro Tip"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":tip-caption: Pro Tip\n\n[caption=\"Pro Tip\"]\nTIP: Override the caption of an admonition block using an attribute entry\n",
);
assert_xpath(
&doc,
"/*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/*[@class=\"title\"][text()=\"Pro Tip\"]",
1,
);
}
#[test]
fn can_override_caption_of_admonition_block_using_document_attribute() {
verifies!(
r#"
test 'can override caption of admonition block using document attribute' do
input = <<~'EOS'
:tip-caption: Pro Tip
TIP: Override the caption of an admonition block using an attribute entry
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="admonitionblock tip"]//*[@class="icon"]/*[@class="title"][text()="Pro Tip"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":tip-caption: Pro Tip\n\nTIP: Override the caption of an admonition block using an attribute entry\n",
);
assert_xpath(
&doc,
"/*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/*[@class=\"title\"][text()=\"Pro Tip\"]",
1,
);
}
#[test]
fn blank_caption_document_attribute_should_not_blank_admonition_block_caption() {
verifies!(
r#"
test 'blank caption document attribute should not blank admonition block caption' do
input = <<~'EOS'
:caption:
TIP: Override the caption of an admonition block using an attribute entry
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="admonitionblock tip"]//*[@class="icon"]/*[@class="title"][text()="Tip"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":caption:\n\nTIP: Override the caption of an admonition block using an attribute entry\n",
);
assert_xpath(
&doc,
"/*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/*[@class=\"title\"][text()=\"Tip\"]",
1,
);
}
non_normative!(
r#"
end
"#
);
}
mod preformatted_blocks {
use crate::tests::prelude::*;
non_normative!(
r#"
context "Preformatted Blocks" do
"#
);
// Returns the text of the document's single `<pre>` element.
fn pre_text(doc: &crate::Document) -> String {
let vd = doc.to_virtual_dom();
let pres = crate::tests::assert_dom::query_xpath(&vd, "//pre");
assert_eq!(pres.len(), 1, "expected exactly one <pre>");
pres[0].text.clone().unwrap_or_default()
}
#[test]
fn should_separate_adjacent_paragraphs_and_listing_into_blocks() {
verifies!(
r#"
test 'should separate adjacent paragraphs and listing into blocks' do
input = <<~'EOS'
paragraph 1
----
listing content
----
paragraph 2
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="paragraph"]/p', output, 2
assert_xpath '/*[@class="listingblock"]', output, 1
assert_xpath '(/*[@class="paragraph"]/following-sibling::*)[1][@class="listingblock"]', output, 1
end
"#
);
let doc =
Parser::default().parse("paragraph 1\n----\nlisting content\n----\nparagraph 2\n");
assert_xpath(&doc, "/*[@class=\"paragraph\"]/p", 2);
assert_xpath(&doc, "/*[@class=\"listingblock\"]", 1);
assert_xpath(
&doc,
"(/*[@class=\"paragraph\"]/following-sibling::*)[1][@class=\"listingblock\"]",
1,
);
}
#[test]
fn should_warn_if_listing_block_is_not_terminated() {
verifies!(
r#"
test 'should warn if listing block is not terminated' do
input = <<~'EOS'
outside
----
inside
still inside
eof
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="listingblock"]', output, 1
assert_message @logger, :WARN, '<stdin>: line 3: unterminated listing block', Hash
end
"#
);
let doc = Parser::default().parse("outside\n\n----\ninside\n\nstill inside\n\neof\n");
assert_xpath(&doc, "/*[@class=\"listingblock\"]", 1);
// This crate emits a single generic `UnterminatedDelimitedBlock` warning
// rather than Asciidoctor's block-type-specific message text.
let warnings: Vec<_> = doc
.warnings()
.filter(|w| w.warning == WarningType::UnterminatedDelimitedBlock)
.collect();
assert_eq!(warnings.len(), 1);
assert_eq!(warnings[0].source.line(), 3);
}
#[test]
fn should_not_crash_when_converting_verbatim_block_that_has_no_lines() {
verifies!(
r#"
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
"#
);
for input in ["----\n----", "....\n...."] {
let doc = Parser::default().parse(input);
assert_css(&doc, "pre", 1);
assert_css(&doc, "pre:empty", 1);
}
}
#[test]
fn should_return_content_as_empty_string_for_verbatim_or_raw_block_that_has_no_lines() {
verifies!(
r#"
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
"#
);
// The crate exposes the verbatim content through the rendered `<pre>`,
// which is empty for a block with no lines.
for input in ["----\n----", "....\n...."] {
let doc = Parser::default().parse(input);
assert_eq!(pre_text(&doc), "");
}
}
#[test]
fn should_preserve_newlines_in_literal_block() {
verifies!(
r#"
test 'should preserve newlines in literal block' do
input = <<~'EOS'
....
line one
line two
line three
....
EOS
[true, false].each do |standalone|
output = convert_string input, standalone: standalone
assert_xpath '//pre', output, 1
assert_xpath '//pre/text()', output, 1
text = xmlnodes_at_xpath('//pre/text()', output, 1).text
lines = text.lines
assert_equal 5, lines.size
expected = "line one\n\nline two\n\nline three".lines
assert_equal expected, lines
blank_lines = output.scan(/\n[ \t]*\n/).size
assert blank_lines >= 2
end
end
"#
);
let doc = Parser::default().parse("....\nline one\n\nline two\n\nline three\n....\n");
assert_eq!(pre_text(&doc), "line one\n\nline two\n\nline three");
}
#[test]
fn should_preserve_newlines_in_listing_block() {
verifies!(
r#"
test 'should preserve newlines in listing block' do
input = <<~'EOS'
----
line one
line two
line three
----
EOS
[true, false].each do |standalone|
output = convert_string input, standalone: standalone
assert_xpath '//pre', output, 1
assert_xpath '//pre/text()', output, 1
text = xmlnodes_at_xpath('//pre/text()', output, 1).text
lines = text.lines
assert_equal 5, lines.size
expected = "line one\n\nline two\n\nline three".lines
assert_equal expected, lines
blank_lines = output.scan(/\n[ \t]*\n/).size
assert blank_lines >= 2
end
end
"#
);
let doc = Parser::default().parse("----\nline one\n\nline two\n\nline three\n----\n");
assert_eq!(pre_text(&doc), "line one\n\nline two\n\nline three");
}
#[test]
fn should_preserve_newlines_in_verse_block() {
verifies!(
r#"
test 'should preserve newlines in verse block' do
input = <<~'EOS'
--
[verse]
____
line one
line two
line three
____
--
EOS
[true, false].each do |standalone|
output = convert_string input, standalone: standalone
assert_xpath '//*[@class="verseblock"]/pre', output, 1
assert_xpath '//*[@class="verseblock"]/pre/text()', output, 1
text = xmlnodes_at_xpath('//*[@class="verseblock"]/pre/text()', output, 1).text
lines = text.lines
assert_equal 5, lines.size
expected = "line one\n\nline two\n\nline three".lines
assert_equal expected, lines
blank_lines = output.scan(/\n[ \t]*\n/).size
assert blank_lines >= 2
end
end
"#
);
let doc = Parser::default()
.parse("--\n[verse]\n____\nline one\n\nline two\n\nline three\n____\n--\n");
assert_xpath(&doc, "//*[@class=\"verseblock\"]/pre", 1);
assert_eq!(pre_text(&doc), "line one\n\nline two\n\nline three");
}
// NOTE: divergence from Asciidoctor. Asciidoctor strips the leading and
// trailing blank lines of a verbatim block (here yielding
// " first line\n\nlast line"); this crate preserves them. Kept
// `#[ignore]`d with the Ruby-intended pre text.
// TODO: strip leading/trailing blank lines of verbatim blocks.
#[ignore]
#[test]
fn should_strip_leading_and_trailing_blank_lines_when_converting_verbatim_block() {
verifies!(
r#"
test 'should strip leading and trailing blank lines when converting verbatim block' do
# NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
input = <<~EOS
[subs="attributes"]
....
first line
last line
{empty}
....
EOS
doc = document_from_string input, standalone: false
block = doc.blocks.first
assert_equal ['', '', ' first line', '', 'last line', '', '{empty}', ''], block.lines
result = doc.convert
assert_xpath %(//pre[text()=" first line\n\nlast line"]), result, 1
end
"#
);
let doc = Parser::default().parse(
"[subs=\"attributes\"]\n....\n\n\n first line\n\nlast line\n\n{empty}\n\n....\n",
);
assert_xpath(&doc, "//pre[text()=\" first line\n\nlast line\"]", 1);
}
// NOTE: divergence from Asciidoctor. This crate does not normalize CRLF
// line endings to LF in verbatim content, so the `<pre>` text retains the
// carriage returns. Kept `#[ignore]`d with the Ruby-intended text.
// TODO: normalize CRLF line endings in verbatim blocks.
#[ignore]
#[test]
fn should_process_block_with_crlf_line_endings() {
verifies!(
r#"
test 'should process block with CRLF line endings' do
input = <<~EOS
----\r
source line 1\r
source line 2\r
----\r
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="listingblock"]//pre', output, 1
assert_xpath %(/*[@class="listingblock"]//pre[text()="source line 1\nsource line 2"]), output, 1
end
"#
);
let doc = Parser::default().parse("----\r\nsource line 1\r\nsource line 2\r\n----\r\n");
assert_xpath(&doc, "/*[@class=\"listingblock\"]//pre", 1);
assert_xpath(
&doc,
"/*[@class=\"listingblock\"]//pre[text()=\"source line 1\nsource line 2\"]",
1,
);
}
// NOTE: divergence from Asciidoctor. This crate does not honor the `indent`
// attribute to reindent verbatim content. Kept `#[ignore]`d with the
// Ruby-intended (reindented) text.
// TODO: honor the `indent` attribute on verbatim blocks.
#[ignore]
#[test]
fn should_remove_block_indent_if_indent_attribute_is_0() {
verifies!(
r#"
test 'should remove block indent if indent attribute is 0' do
# NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
input = <<~EOS
[indent="0"]
----
def names
@names.split
end
----
EOS
# NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
expected = <<~EOS.chop
def names
@names.split
end
EOS
output = convert_string_to_embedded input
assert_css 'pre', output, 1
assert_css '.listingblock pre', output, 1
result = xmlnodes_at_xpath('//pre', output, 1).text
assert_equal expected, result
end
"#
);
let doc = Parser::default()
.parse("[indent=\"0\"]\n----\n def names\n\n @names.split\n\n end\n----\n");
assert_eq!(pre_text(&doc), "def names\n\n @names.split\n\nend");
}
#[test]
fn should_not_remove_block_indent_if_indent_attribute_is_minus_1() {
verifies!(
r#"
test 'should not remove block indent if indent attribute is -1' do
# NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
input = <<~EOS
[indent="-1"]
----
def names
@names.split
end
----
EOS
expected = (input.lines.slice 2, 5).join.chop
output = convert_string_to_embedded input
assert_css 'pre', output, 1
assert_css '.listingblock pre', output, 1
result = xmlnodes_at_xpath('//pre', output, 1).text
assert_equal expected, result
end
"#
);
// indent="-1" preserves the source indentation, which is this crate's
// default behavior.
let doc = Parser::default()
.parse("[indent=\"-1\"]\n----\n def names\n\n @names.split\n\n end\n----\n");
assert_eq!(
pre_text(&doc),
" def names\n\n @names.split\n\n end"
);
}
// NOTE: divergence from Asciidoctor (see
// `should_remove_block_indent_if_indent_attribute_is_0`): the `indent`
// attribute is not honored, so content is not reindented to one space.
// TODO: honor the `indent` attribute on verbatim blocks.
#[ignore]
#[test]
fn should_set_block_indent_to_value_specified_by_indent_attribute() {
verifies!(
r#"
test 'should set block indent to value specified by indent attribute' do
# NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
input = <<~EOS
[indent="1"]
----
def names
@names.split
end
----
EOS
expected = (input.lines.slice 2, 5).map {|l| l.sub ' ', ' ' }.join.chop
output = convert_string_to_embedded input
assert_css 'pre', output, 1
assert_css '.listingblock pre', output, 1
result = xmlnodes_at_xpath('//pre', output, 1).text
assert_equal expected, result
end
"#
);
let doc = Parser::default()
.parse("[indent=\"1\"]\n----\n def names\n\n @names.split\n\n end\n----\n");
assert_eq!(pre_text(&doc), " def names\n\n @names.split\n\n end");
}
// NOTE: divergence from Asciidoctor (see
// `should_remove_block_indent_if_indent_attribute_is_0`): the
// `source-indent` document attribute is not honored.
// TODO: honor the `source-indent` attribute on verbatim blocks.
#[ignore]
#[test]
fn should_set_block_indent_to_value_specified_by_indent_document_attribute() {
verifies!(
r#"
test 'should set block indent to value specified by indent document attribute' do
# NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
input = <<~EOS
:source-indent: 1
[source,ruby]
----
def names
@names.split
end
----
EOS
expected = (input.lines.slice 4, 5).map {|l| l.sub ' ', ' ' }.join.chop
output = convert_string_to_embedded input
assert_css 'pre', output, 1
assert_css '.listingblock pre', output, 1
result = xmlnodes_at_xpath('//pre', output, 1).text
assert_equal expected, result
end
"#
);
let doc = Parser::default().parse(
":source-indent: 1\n\n[source,ruby]\n----\n def names\n\n @names.split\n\n end\n----\n",
);
assert_eq!(pre_text(&doc), " def names\n\n @names.split\n\n end");
}
// NOTE: divergence from Asciidoctor. This crate does not expand tabs based
// on the `tabsize` attribute. Kept `#[ignore]`d with the Ruby-intended
// (tab-expanded) text.
// TODO: honor the `tabsize` attribute on verbatim blocks.
#[ignore]
#[test]
fn should_expand_tabs_if_tabsize_attribute_is_positive() {
verifies!(
r#"
test 'should expand tabs if tabsize attribute is positive' do
input = <<~EOS
:tabsize: 4
[indent=0]
----
\tdef names
\t\t@names.split
\tend
----
EOS
# NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
expected = <<~EOS.chop
def names
@names.split
end
EOS
output = convert_string_to_embedded input
assert_css 'pre', output, 1
assert_css '.listingblock pre', output, 1
result = xmlnodes_at_xpath('//pre', output, 1).text
assert_equal expected, result
end
"#
);
let doc = Parser::default().parse(
":tabsize: 4\n\n[indent=0]\n----\n\tdef names\n\n\t\t@names.split\n\n\tend\n----\n",
);
assert_eq!(pre_text(&doc), "def names\n\n @names.split\n\nend");
}
// NOTE: divergence from Asciidoctor. This crate does not apply the `nowrap`
// option (nor the `prewrap` document attribute) as a `nowrap` class on the
// `<pre>` element.
// TODO: honor the `nowrap` option / `prewrap` attribute on verbatim blocks.
#[ignore]
#[test]
fn literal_block_should_honor_nowrap_option() {
verifies!(
r#"
test 'literal block should honor nowrap option' do
input = <<~'EOS'
[options="nowrap"]
----
Do not wrap me if I get too long.
----
EOS
output = convert_string_to_embedded input
assert_css 'pre.nowrap', output, 1
end
"#
);
let doc = Parser::default()
.parse("[options=\"nowrap\"]\n----\nDo not wrap me if I get too long.\n----\n");
assert_css(&doc, "pre.nowrap", 1);
}
// NOTE: divergence from Asciidoctor (see
// `literal_block_should_honor_nowrap_option`): the `prewrap` document
// attribute is not honored.
// TODO: honor the `prewrap` attribute on verbatim blocks.
#[ignore]
#[test]
fn literal_block_should_set_nowrap_class_if_prewrap_document_attribute_is_disabled() {
verifies!(
r#"
test 'literal block should set nowrap class if prewrap document attribute is disabled' do
input = <<~'EOS'
:prewrap!:
----
Do not wrap me if I get too long.
----
EOS
output = convert_string_to_embedded input
assert_css 'pre.nowrap', output, 1
end
"#
);
let doc = Parser::default()
.parse(":prewrap!:\n\n----\nDo not wrap me if I get too long.\n----\n");
assert_css(&doc, "pre.nowrap", 1);
}
#[test]
fn should_preserve_guard_in_front_of_callout_if_icons_are_not_enabled() {
verifies!(
r#"
test 'should preserve guard in front of callout if icons are not enabled' do
input = <<~'EOS'
----
puts 'Hello, World!' # <1>
puts 'Goodbye, World ;(' # <2>
----
EOS
result = convert_string_to_embedded input
assert_include ' # <b class="conum">(1)</b>', result
assert_include ' # <b class="conum">(2)</b>', result
end
"#
);
let doc = Parser::default()
.parse("----\nputs 'Hello, World!' # <1>\nputs 'Goodbye, World ;(' # <2>\n----\n");
// The assert helpers operate on the decoded virtual DOM, where the
// conum is a `<b class="conum">` element; the guard text (` # `) is
// preserved in the text node immediately before it.
assert_xpath(&doc, "//pre//b[@class=\"conum\"][text()=\"(1)\"]", 1);
assert_xpath(&doc, "//pre//b[@class=\"conum\"][text()=\"(2)\"]", 1);
assert_rendered_contains(&doc, " # ");
}
#[test]
fn should_preserve_guard_around_callout_if_icons_are_not_enabled() {
verifies!(
r#"
test 'should preserve guard around callout if icons are not enabled' do
input = <<~'EOS'
----
<parent> <!--1-->
<child/> <!--2-->
</parent>
----
EOS
result = convert_string_to_embedded input
assert_include ' <!--<b class="conum">(1)</b>-->', result
assert_include ' <!--<b class="conum">(2)</b>-->', result
end
"#
);
let doc = Parser::default()
.parse("----\n<parent> <!--1-->\n <child/> <!--2-->\n</parent>\n----\n");
// Decoded virtual DOM: the conum `<b class="conum">` sits between the
// preserved guard text ` <!--` and `-->`.
assert_xpath(&doc, "//pre//b[@class=\"conum\"][text()=\"(1)\"]", 1);
assert_xpath(&doc, "//pre//b[@class=\"conum\"][text()=\"(2)\"]", 1);
assert_rendered_contains(&doc, " <!--");
assert_rendered_contains(&doc, "-->");
}
#[test]
fn literal_block_should_honor_explicit_subs_list() {
verifies!(
r#"
test 'literal block should honor explicit subs list' do
input = <<~'EOS'
[subs="verbatim,quotes"]
----
Map<String, String> *attributes*; //<1>
----
EOS
block = block_from_string input
assert_equal [:specialcharacters, :callouts, :quotes], block.subs
output = block.convert
assert_includes output, 'Map<String, String> <strong>attributes</strong>;'
assert_xpath '//pre/b[text()="(1)"]', output, 1
end
"#
);
// The Ruby subs-list introspection (`block.subs`) is exercised through
// the rendered output here.
let doc = Parser::default().parse(
"[subs=\"verbatim,quotes\"]\n----\nMap<String, String> *attributes*; //<1>\n----\n",
);
// Decoded virtual DOM: specialcharacters escaping is reflected as the
// literal `<`/`>` in the text node, and `*attributes*` becomes a
// `<strong>` element.
assert_rendered_contains(&doc, "Map<String, String> ");
assert_xpath(&doc, "//pre//strong[text()=\"attributes\"]", 1);
assert_xpath(&doc, "//pre//b[@class=\"conum\"][text()=\"(1)\"]", 1);
}
#[test]
fn should_be_able_to_disable_callouts_for_literal_block() {
verifies!(
r#"
test 'should be able to disable callouts for literal block' do
input = <<~'EOS'
[subs="specialcharacters"]
----
No callout here <1>
----
EOS
block = block_from_string input
assert_equal [:specialcharacters], block.subs
output = block.convert
assert_xpath '//pre/b[text()="(1)"]', output, 0
end
"#
);
let doc = Parser::default()
.parse("[subs=\"specialcharacters\"]\n----\nNo callout here <1>\n----\n");
assert_xpath(&doc, "//pre//b[text()=\"(1)\"]", 0);
}
#[test]
fn listing_block_should_honor_explicit_subs_list() {
verifies!(
r#"
test 'listing block should honor explicit subs list' do
input = <<~'EOS'
[subs="specialcharacters,quotes"]
----
$ *python functional_tests.py*
Traceback (most recent call last):
File "functional_tests.py", line 4, in <module>
assert 'Django' in browser.title
AssertionError
----
EOS
output = convert_string_to_embedded input
assert_css '.listingblock pre', output, 1
assert_css '.listingblock pre strong', output, 1
assert_css '.listingblock pre em', output, 0
input2 = <<~'EOS'
[subs="specialcharacters,macros"]
----
$ pass:quotes[*python functional_tests.py*]
Traceback (most recent call last):
File "functional_tests.py", line 4, in <module>
assert pass:quotes['Django'] in browser.title
AssertionError
----
EOS
output2 = convert_string_to_embedded input2
# FIXME JRuby is adding extra trailing newlines in the second document,
# for now, rstrip is necessary
assert_equal output.rstrip, output2.rstrip
end
"#
);
let doc = Parser::default().parse(
"[subs=\"specialcharacters,quotes\"]\n----\n$ *python functional_tests.py*\nTraceback (most recent call last):\n File \"functional_tests.py\", line 4, in <module>\n assert 'Django' in browser.title\nAssertionError\n----\n",
);
assert_css(&doc, ".listingblock pre", 1);
assert_css(&doc, ".listingblock pre strong", 1);
assert_css(&doc, ".listingblock pre em", 0);
let doc2 = Parser::default().parse(
"[subs=\"specialcharacters,macros\"]\n----\n$ pass:quotes[*python functional_tests.py*]\nTraceback (most recent call last):\n File \"functional_tests.py\", line 4, in <module>\n assert pass:quotes['Django'] in browser.title\nAssertionError\n----\n",
);
assert_eq!(pre_text(&doc), pre_text(&doc2));
}
// NOTE: divergence from Asciidoctor. This crate does not treat a block
// title whose first character is a period (`..gitignore`) as a title;
// Asciidoctor renders the title ".gitignore". Kept `#[ignore]`d.
// TODO: allow a leading period in a block title when not followed by a space.
#[ignore]
#[test]
fn first_character_of_block_title_may_be_a_period_if_not_followed_by_space() {
verifies!(
r#"
test 'first character of block title may be a period if not followed by space' do
input = <<~'EOS'
..gitignore
----
/.bundle/
/build/
/Gemfile.lock
----
EOS
output = convert_string_to_embedded input
assert_xpath '//*[@class="title"][text()=".gitignore"]', output
end
"#
);
let doc =
Parser::default().parse("..gitignore\n----\n/.bundle/\n/build/\n/Gemfile.lock\n----\n");
assert_xpath(&doc, "//*[@class=\"title\"][text()=\".gitignore\"]", 1);
}
#[test]
fn listing_block_without_title_should_generate_screen_element_in_docbook() {
non_normative!(
r#"
test 'listing block without title should generate screen element in docbook' do
input = <<~'EOS'
----
listing block
----
EOS
output = convert_string_to_embedded input, backend: 'docbook'
assert_xpath '/screen[text()="listing block"]', output, 1
end
"#
);
// Backend-specific test omitted: DocBook.
}
#[test]
fn listing_block_with_title_should_generate_screen_element_inside_formalpara_element_in_docbook()
{
non_normative!(
r#"
test 'listing block with title should generate screen element inside formalpara element in docbook' do
input = <<~'EOS'
.title
----
listing block
----
EOS
output = convert_string_to_embedded input, backend: 'docbook'
assert_xpath '/formalpara', output, 1
assert_xpath '/formalpara/title[text()="title"]', output, 1
assert_xpath '/formalpara/para/screen[text()="listing block"]', output, 1
end
"#
);
// Backend-specific test omitted: DocBook.
}
#[test]
fn should_not_prepend_caption_to_title_of_listing_block_with_title_if_listing_caption_attribute_is_not_set()
{
verifies!(
r#"
test 'should not prepend caption to title of listing block with title if listing-caption attribute is not set' do
input = <<~'EOS'
.title
----
listing block content
----
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="listingblock"][1]/*[@class="title"][text()="title"]', output, 1
end
"#
);
let doc = Parser::default().parse(".title\n----\nlisting block content\n----\n");
assert_xpath(
&doc,
"/*[@class=\"listingblock\"][1]/*[@class=\"title\"][text()=\"title\"]",
1,
);
}
// NOTE: divergence from Asciidoctor. This crate does not honor the
// `listing-caption` attribute to prepend a numbered caption to a listing
// block's title. Kept `#[ignore]`d with the Ruby-intended title.
// TODO: honor the `listing-caption` attribute.
#[ignore]
#[test]
fn should_prepend_caption_specified_by_listing_caption_attribute_and_number_to_title_of_listing_block_with_title()
{
verifies!(
r#"
test 'should prepend caption specified by listing-caption attribute and number to title of listing block with title' do
input = <<~'EOS'
:listing-caption: Listing
.title
----
listing block content
----
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="listingblock"][1]/*[@class="title"][text()="Listing 1. title"]', output, 1
end
"#
);
let doc = Parser::default()
.parse(":listing-caption: Listing\n\n.title\n----\nlisting block content\n----\n");
assert_xpath(
&doc,
"/*[@class=\"listingblock\"][1]/*[@class=\"title\"][text()=\"Listing 1. title\"]",
1,
);
}
// NOTE: divergence from Asciidoctor. This crate does not evaluate a
// `caption` attribute containing a `{counter:..}` reference to prepend a
// numbered caption. Kept `#[ignore]`d with the Ruby-intended title.
// TODO: honor a `caption` attribute with a counter on a listing block.
#[ignore]
#[test]
fn should_prepend_caption_specified_by_caption_attribute_on_listing_block_even_if_listing_caption_attribute_is_not_set()
{
verifies!(
r#"
test 'should prepend caption specified by caption attribute on listing block even if listing-caption attribute is not set' do
input = <<~'EOS'
[caption="Listing {counter:listing-number}. "]
.Behold!
----
listing block content
----
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="listingblock"][1]/*[@class="title"][text()="Listing 1. Behold!"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"[caption=\"Listing {counter:listing-number}. \"]\n.Behold!\n----\nlisting block content\n----\n",
);
assert_xpath(
&doc,
"/*[@class=\"listingblock\"][1]/*[@class=\"title\"][text()=\"Listing 1. Behold!\"]",
1,
);
}
// NOTE: divergence from Asciidoctor. This crate does not promote a listing
// block with an implicit style and a language positional argument to a
// source block. Kept `#[ignore]`d with the Ruby-intended assertions.
// TODO: promote `[,lang]` listing blocks to source blocks.
#[ignore]
#[test]
fn listing_block_without_an_explicit_style_and_with_a_second_positional_argument_should_be_promoted_to_a_source_block()
{
verifies!(
r#"
test 'listing block without an explicit style and with a second positional argument should be promoted to a source block' do
input = <<~'EOS'
[,ruby]
----
puts 'Hello, Ruby!'
----
EOS
matches = (document_from_string input).find_by context: :listing, style: 'source'
assert_equal 1, matches.length
assert_equal 'ruby', (matches[0].attr 'language')
end
"#
);
let doc = Parser::default().parse("[,ruby]\n----\nputs 'Hello, Ruby!'\n----\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.declared_style(), Some("source"));
assert_eq!(
block
.attrlist()
.and_then(|a| a.named_attribute("language"))
.map(|v| v.value().to_string()),
Some("ruby".to_string())
);
}
// NOTE: divergence from Asciidoctor (see the `[,lang]` promotion case): a
// listing block is not promoted to source when `source-language` is set.
// TODO: promote listing blocks to source when `source-language` is set.
#[ignore]
#[test]
fn listing_block_without_an_explicit_style_should_be_promoted_to_a_source_block_if_source_language_is_set()
{
verifies!(
r#"
test 'listing block without an explicit style should be promoted to a source block if source-language is set' do
input = <<~'EOS'
:source-language: ruby
----
puts 'Hello, Ruby!'
----
EOS
matches = (document_from_string input).find_by context: :listing, style: 'source'
assert_equal 1, matches.length
assert_equal 'ruby', (matches[0].attr 'language')
end
"#
);
let doc =
Parser::default().parse(":source-language: ruby\n\n----\nputs 'Hello, Ruby!'\n----\n");
let block = doc
.child_blocks()
.find(|b| b.raw_context().as_ref() == "listing")
.unwrap();
assert_eq!(block.declared_style(), Some("source"));
}
#[test]
fn listing_block_with_an_explicit_style_and_a_second_positional_argument_should_not_be_promoted_to_a_source_block()
{
verifies!(
r#"
test 'listing block with an explicit style and a second positional argument should not be promoted to a source block' do
input = <<~'EOS'
[listing,ruby]
----
puts 'Hello, Ruby!'
----
EOS
matches = (document_from_string input).find_by context: :listing
assert_equal 1, matches.length
assert_equal 'listing', matches[0].style
assert_nil matches[0].attr 'language'
end
"#
);
let doc = Parser::default().parse("[listing,ruby]\n----\nputs 'Hello, Ruby!'\n----\n");
let matches: Vec<_> = doc
.child_blocks()
.filter(|b| b.raw_context().as_ref() == "listing")
.collect();
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].declared_style(), Some("listing"));
assert_eq!(
matches[0]
.attrlist()
.and_then(|a| a.named_attribute("language")),
None
);
}
#[test]
fn listing_block_with_an_explicit_style_should_not_be_promoted_to_a_source_block_if_source_language_is_set()
{
verifies!(
r#"
test 'listing block with an explicit style should not be promoted to a source block if source-language is set' do
input = <<~'EOS'
:source-language: ruby
[listing]
----
puts 'Hello, Ruby!'
----
EOS
matches = (document_from_string input).find_by context: :listing
assert_equal 1, matches.length
assert_equal 'listing', matches[0].style
assert_nil matches[0].attr 'language'
end
"#
);
let doc = Parser::default()
.parse(":source-language: ruby\n\n[listing]\n----\nputs 'Hello, Ruby!'\n----\n");
let matches: Vec<_> = doc
.child_blocks()
.filter(|b| b.raw_context().as_ref() == "listing")
.collect();
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].declared_style(), Some("listing"));
assert_eq!(
matches[0]
.attrlist()
.and_then(|a| a.named_attribute("language")),
None
);
}
#[test]
fn source_block_with_no_title_or_language_should_generate_screen_element_in_docbook() {
non_normative!(
r#"
test 'source block with no title or language should generate screen element in docbook' do
input = <<~'EOS'
[source]
----
source block
----
EOS
output = convert_string_to_embedded input, backend: 'docbook'
assert_xpath '/screen[@linenumbering="unnumbered"][text()="source block"]', output, 1
end
"#
);
// Backend-specific test omitted: DocBook.
}
#[test]
fn source_block_with_title_and_no_language_should_generate_screen_element_inside_formalpara_element_for_docbook()
{
non_normative!(
r#"
test 'source block with title and no language should generate screen element inside formalpara element for docbook' do
input = <<~'EOS'
[source]
.title
----
source block
----
EOS
output = convert_string_to_embedded input, backend: 'docbook'
assert_xpath '/formalpara', output, 1
assert_xpath '/formalpara/title[text()="title"]', output, 1
assert_xpath '/formalpara/para/screen[@linenumbering="unnumbered"][text()="source block"]', output, 1
end
"#
);
// Backend-specific test omitted: DocBook.
}
non_normative!(
r#"
end
"#
);
}
mod open_blocks {
use crate::tests::prelude::*;
non_normative!(
r#"
context "Open Blocks" do
"#
);
#[test]
fn can_convert_open_block() {
verifies!(
r#"
test "can convert open block" do
input = <<~'EOS'
--
This is an open block.
It can span multiple lines.
--
EOS
output = convert_string input
assert_xpath '//*[@class="openblock"]//p', output, 2
end
"#
);
let doc = Parser::default()
.parse("--\nThis is an open block.\n\nIt can span multiple lines.\n--\n");
assert_xpath(&doc, "//*[@class=\"openblock\"]//p", 2);
}
#[test]
fn open_block_can_contain_another_block() {
verifies!(
r#"
test "open block can contain another block" do
input = <<~'EOS'
--
This is an open block.
It can span multiple lines.
____
It can hold great quotes like this one.
____
--
EOS
output = convert_string input
assert_xpath '//*[@class="openblock"]//p', output, 3
assert_xpath '//*[@class="openblock"]//*[@class="quoteblock"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"--\nThis is an open block.\n\nIt can span multiple lines.\n\n____\nIt can hold great quotes like this one.\n____\n--\n",
);
assert_xpath(&doc, "//*[@class=\"openblock\"]//p", 3);
assert_xpath(
&doc,
"//*[@class=\"openblock\"]//*[@class=\"quoteblock\"]",
1,
);
}
non_normative!(
r#"
test 'should transfer id and reftext on open block to DocBook output' do
input = <<~'EOS'
Check out that <<open>>!
[[open,Open Block]]
--
This is an open block.
TIP: An open block can have other blocks inside of it.
--
Back to our regularly scheduled programming.
EOS
output = convert_string input, backend: :docbook, keep_namespaces: true
assert_css 'article:root > para[xml|id="open"]', output, 1
assert_css 'article:root > para[xreflabel="Open Block"]', output, 1
assert_css 'article:root > simpara', output, 2
assert_css 'article:root > para', output, 1
assert_css 'article:root > para > simpara', output, 1
assert_css 'article:root > para > tip', output, 1
end
test 'should transfer id and reftext on open paragraph to DocBook output' do
input = <<~'EOS'
[open#openpara,reftext="Open Paragraph"]
This is an open paragraph.
EOS
output = convert_string input, backend: :docbook, keep_namespaces: true
assert_css 'article:root > simpara', output, 1
assert_css 'article:root > simpara[xml|id="openpara"]', output, 1
assert_css 'article:root > simpara[xreflabel="Open Paragraph"]', output, 1
end
test 'should transfer title on open block to DocBook output' do
input = <<~'EOS'
.Behold the open
--
This is an open block with a title.
--
EOS
output = convert_string input, backend: :docbook
assert_css 'article > formalpara', output, 1
assert_css 'article > formalpara > *', output, 2
assert_css 'article > formalpara > title', output, 1
assert_xpath '/article/formalpara/title[text()="Behold the open"]', output, 1
assert_css 'article > formalpara > para', output, 1
assert_css 'article > formalpara > para > simpara', output, 1
end
test 'should transfer title on open paragraph to DocBook output' do
input = <<~'EOS'
.Behold the open
This is an open paragraph with a title.
EOS
output = convert_string input, backend: :docbook
assert_css 'article > formalpara', output, 1
assert_css 'article > formalpara > *', output, 2
assert_css 'article > formalpara > title', output, 1
assert_xpath '/article/formalpara/title[text()="Behold the open"]', output, 1
assert_css 'article > formalpara > para', output, 1
assert_css 'article > formalpara > para[text()="This is an open paragraph with a title."]', output, 1
end
test 'should transfer role on open block to DocBook output' do
input = <<~'EOS'
[.container]
--
This is an open block.
It holds stuff.
--
EOS
output = convert_string input, backend: :docbook
assert_css 'article > para[role=container]', output, 1
assert_css 'article > para[role=container] > simpara', output, 1
end
test 'should transfer role on open paragraph to DocBook output' do
input = <<~'EOS'
[.container]
This is an open block.
It holds stuff.
EOS
output = convert_string input, backend: :docbook
assert_css 'article > simpara[role=container]', output, 1
end
"#
);
// The six preceding tests are backend-specific (DocBook) and out of scope.
non_normative!(
r#"
end
"#
);
}
mod passthrough_blocks {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Passthrough Blocks' do
"#
);
// Returns the text of the single `<pre>` this crate renders inside the
// `div.passblock` wrapper for a passthrough block. Asciidoctor emits the
// passthrough content with no enclosing element; the substantive behavior
// (which substitutions are or are not applied) is captured in this text.
fn pass_text(doc: &crate::Document) -> String {
let vd = doc.to_virtual_dom();
let pres = crate::tests::assert_dom::query_xpath(&vd, "//pre");
assert_eq!(pres.len(), 1);
pres[0].text.clone().unwrap_or_default()
}
#[test]
fn can_parse_a_passthrough_block() {
verifies!(
r#"
test 'can parse a passthrough block' do
input = <<~'EOS'
++++
This is a passthrough block.
++++
EOS
block = block_from_string input
refute_nil block
assert_equal 1, block.lines.size
assert_equal 'This is a passthrough block.', block.source
end
"#
);
let doc = Parser::default().parse("++++\nThis is a passthrough block.\n++++\n");
assert_eq!(pass_text(&doc), "This is a passthrough block.");
}
#[test]
fn does_not_perform_subs_on_a_passthrough_block_by_default() {
verifies!(
r#"
test 'does not perform subs on a passthrough block by default' do
input = <<~'EOS'
:type: passthrough
++++
This is a '{type}' block.
http://asciidoc.org
image:tiger.png[]
++++
EOS
expected = %(This is a '{type}' block.\nhttp://asciidoc.org\nimage:tiger.png[])
output = convert_string_to_embedded input
assert_equal expected, output.strip
end
"#
);
let doc = Parser::default().parse(
":type: passthrough\n\n++++\nThis is a '{type}' block.\nhttp://asciidoc.org\nimage:tiger.png[]\n++++\n",
);
assert_eq!(
pass_text(&doc),
"This is a '{type}' block.\nhttp://asciidoc.org\nimage:tiger.png[]"
);
}
#[test]
fn does_not_perform_subs_on_a_passthrough_block_with_pass_style_by_default() {
verifies!(
r#"
test 'does not perform subs on a passthrough block with pass style by default' do
input = <<~'EOS'
:type: passthrough
[pass]
++++
This is a '{type}' block.
http://asciidoc.org
image:tiger.png[]
++++
EOS
expected = %(This is a '{type}' block.\nhttp://asciidoc.org\nimage:tiger.png[])
output = convert_string_to_embedded input
assert_equal expected, output.strip
end
"#
);
let doc = Parser::default().parse(
":type: passthrough\n\n[pass]\n++++\nThis is a '{type}' block.\nhttp://asciidoc.org\nimage:tiger.png[]\n++++\n",
);
assert_eq!(
pass_text(&doc),
"This is a '{type}' block.\nhttp://asciidoc.org\nimage:tiger.png[]"
);
}
#[test]
fn passthrough_block_honors_explicit_subs_list() {
verifies!(
r##"
test 'passthrough block honors explicit subs list' do
input = <<~'EOS'
:type: passthrough
[subs="attributes,quotes,macros"]
++++
This is a _{type}_ block.
http://asciidoc.org
++++
EOS
expected = %(This is a <em>passthrough</em> block.\n<a href="http://asciidoc.org" class="bare">http://asciidoc.org</a>)
output = convert_string_to_embedded input
assert_equal expected, output.strip
end
"##
);
// The explicit subs turn `{type}` into "passthrough" (attributes),
// `_.._` into `<em>` (quotes), and the URL into a link (macros).
let doc = Parser::default().parse(
":type: passthrough\n\n[subs=\"attributes,quotes,macros\"]\n++++\nThis is a _{type}_ block.\nhttp://asciidoc.org\n++++\n",
);
assert_xpath(&doc, "//em[text()=\"passthrough\"]", 1);
assert_xpath(&doc, "//a[@href=\"http://asciidoc.org\"]", 1);
}
// NOTE: divergence from Asciidoctor. Asciidoctor strips the leading and
// trailing blank lines of a raw (passthrough) block; this crate preserves
// them. Kept `#[ignore]`d with the Ruby-intended converted result.
// TODO: strip leading/trailing blank lines of raw blocks.
#[ignore]
#[test]
fn should_strip_leading_and_trailing_blank_lines_when_converting_raw_block() {
verifies!(
r#"
test 'should strip leading and trailing blank lines when converting raw block' do
# NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
input = <<~EOS
++++
line above
++++
++++
first line
last line
++++
++++
line below
++++
EOS
doc = document_from_string input, standalone: false
block = doc.blocks[1]
assert_equal ['', '', ' first line', '', 'last line', '', ''], block.lines
result = doc.convert
assert_equal "line above\n first line\n\nlast line\nline below", result, 1
end
"#
);
let doc = Parser::default().parse(
"++++\nline above\n++++\n\n++++\n\n\n first line\n\nlast line\n\n\n++++\n\n++++\nline below\n++++\n",
);
let vd = doc.to_virtual_dom();
let pres = crate::tests::assert_dom::query_xpath(&vd, "//pre");
assert_eq!(
pres[1].text.clone().unwrap_or_default(),
" first line\n\nlast line"
);
}
non_normative!(
r#"
end
"#
);
}
mod math_blocks {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Math blocks' do
"#
);
// Text of the single `<pre>` this crate renders inside a `div.stemblock`.
// NOTE: divergence from Asciidoctor pervasive to this context — this crate
// renders stem content as `div.stemblock > pre` with the content verbatim.
// Asciidoctor wraps the content in a `.content` element and surrounds it
// with math delimiters (`\[..\]` for LaTeX, `\$..\$` for AsciiMath),
// splits AsciiMath equations on newlines, and emits MathJax configuration
// in standalone output. None of that is modeled here, so the tests that
// assert it are kept `#[ignore]`d with the Ruby-intended assertions.
fn stem_pre_text(doc: &crate::Document) -> String {
let vd = doc.to_virtual_dom();
let pres = crate::tests::assert_dom::query_xpath(&vd, "//pre");
assert_eq!(pres.len(), 1);
pres[0].text.clone().unwrap_or_default()
}
#[test]
fn should_not_crash_when_converting_stem_block_that_has_no_lines() {
verifies!(
r#"
test 'should not crash when converting stem block that has no lines' do
input = <<~'EOS'
[stem]
++++
++++
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
end
"#
);
let doc = Parser::default().parse("[stem]\n++++\n++++\n");
assert_css(&doc, ".stemblock", 1);
}
#[test]
fn should_return_content_as_empty_string_for_stem_or_pass_block_that_has_no_lines() {
verifies!(
r#"
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
"#
);
for input in ["++++\n++++", "[stem]\n++++\n++++"] {
let doc = Parser::default().parse(input);
assert_eq!(stem_pre_text(&doc), "");
}
}
// TODO: wrap latexmath content in `\[..\]` delimiters.
#[ignore]
#[test]
fn should_add_latex_math_delimiters_around_latexmath_block_content() {
verifies!(
r#"
test 'should add LaTeX math delimiters around latexmath block content' do
input = <<~'EOS'
[latexmath]
++++
\sqrt{3x-1}+(1+x)^2 < y
++++
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]/child::text()', output
assert_equal '\[\sqrt{3x-1}+(1+x)^2 < y\]', nodes.first.to_s.strip
end
"#
);
let doc = Parser::default().parse("[latexmath]\n++++\n\\sqrt{3x-1}+(1+x)^2 < y\n++++\n");
assert_css(&doc, ".stemblock", 1);
assert_eq!(stem_pre_text(&doc), "\\[\\sqrt{3x-1}+(1+x)^2 < y\\]");
}
// TODO: recognize existing `\[..\]` delimiters in latexmath content.
#[ignore]
#[test]
fn should_not_add_latex_math_delimiters_around_latexmath_block_content_if_already_present() {
verifies!(
r#"
test 'should not add LaTeX math delimiters around latexmath block content if already present' do
input = <<~'EOS'
[latexmath]
++++
\[\sqrt{3x-1}+(1+x)^2 < y\]
++++
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]/child::text()', output
assert_equal '\[\sqrt{3x-1}+(1+x)^2 < y\]', nodes.first.to_s.strip
end
"#
);
let doc =
Parser::default().parse("[latexmath]\n++++\n\\[\\sqrt{3x-1}+(1+x)^2 < y\\]\n++++\n");
assert_css(&doc, ".stemblock", 1);
assert_eq!(stem_pre_text(&doc), "\\[\\sqrt{3x-1}+(1+x)^2 < y\\]");
}
#[test]
fn should_display_latexmath_block_in_alt_of_equation_in_docbook_backend() {
non_normative!(
r#"
test 'should display latexmath block in alt of equation in DocBook backend' do
input = <<~'EOS'
[latexmath]
++++
\sqrt{3x-1}+(1+x)^2 < y
++++
EOS
expect = <<~'EOS'
<informalequation>
<alt><![CDATA[\sqrt{3x-1}+(1+x)^2 < y]]></alt>
<mathphrase><![CDATA[\sqrt{3x-1}+(1+x)^2 < y]]></mathphrase>
</informalequation>
EOS
output = convert_string_to_embedded input, backend: :docbook
assert_equal expect.strip, output.strip
end
"#
);
// Backend-specific test omitted: DocBook.
}
// TODO: emit MathJax equationNumbers configuration in standalone output.
#[ignore]
#[test]
fn should_set_auto_number_option_for_latexmath_to_none_by_default() {
verifies!(
r#"
test 'should set autoNumber option for latexmath to none by default' do
input = <<~'EOS'
:stem: latexmath
[stem]
++++
y = x^2
++++
EOS
output = convert_string input
assert_includes output, 'TeX: { equationNumbers: { autoNumber: "none" } }'
end
"#
);
let doc = Parser::default().parse(":stem: latexmath\n\n[stem]\n++++\ny = x^2\n++++\n");
assert_rendered_contains(&doc, "TeX: { equationNumbers: { autoNumber: \"none\" } }");
}
// TODO: emit MathJax equationNumbers configuration in standalone output.
#[ignore]
#[test]
fn should_set_auto_number_option_for_latexmath_to_none_if_eqnums_is_set_to_none() {
verifies!(
r#"
test 'should set autoNumber option for latexmath to none if eqnums is set to none' do
input = <<~'EOS'
:stem: latexmath
:eqnums: none
[stem]
++++
y = x^2
++++
EOS
output = convert_string input
assert_includes output, 'TeX: { equationNumbers: { autoNumber: "none" } }'
end
"#
);
let doc = Parser::default()
.parse(":stem: latexmath\n:eqnums: none\n\n[stem]\n++++\ny = x^2\n++++\n");
assert_rendered_contains(&doc, "TeX: { equationNumbers: { autoNumber: \"none\" } }");
}
// TODO: emit MathJax equationNumbers configuration in standalone output.
#[ignore]
#[test]
fn should_set_auto_number_option_for_latexmath_to_ams_if_eqnums_is_set() {
verifies!(
r#"
test 'should set autoNumber option for latexmath to AMS if eqnums is set' do
input = <<~'EOS'
:stem: latexmath
:eqnums:
[stem]
++++
\begin{equation}
y = x^2
\end{equation}
++++
EOS
output = convert_string input
assert_includes output, 'TeX: { equationNumbers: { autoNumber: "AMS" } }'
end
"#
);
let doc = Parser::default().parse(
":stem: latexmath\n:eqnums:\n\n[stem]\n++++\n\\begin{equation}\ny = x^2\n\\end{equation}\n++++\n",
);
assert_rendered_contains(&doc, "TeX: { equationNumbers: { autoNumber: \"AMS\" } }");
}
// TODO: emit MathJax equationNumbers configuration in standalone output.
#[ignore]
#[test]
fn should_set_auto_number_option_for_latexmath_to_all_if_eqnums_is_set_to_all() {
verifies!(
r#"
test 'should set autoNumber option for latexmath to all if eqnums is set to all' do
input = <<~'EOS'
:stem: latexmath
:eqnums: all
[stem]
++++
y = x^2
++++
EOS
output = convert_string input
assert_includes output, 'TeX: { equationNumbers: { autoNumber: "all" } }'
end
"#
);
let doc = Parser::default()
.parse(":stem: latexmath\n:eqnums: all\n\n[stem]\n++++\ny = x^2\n++++\n");
assert_rendered_contains(&doc, "TeX: { equationNumbers: { autoNumber: \"all\" } }");
}
// TODO: add AsciiMath `\$..\$` delimiters and equation splitting.
#[ignore]
#[test]
fn should_not_split_equation_in_asciimath_block_at_single_newline() {
verifies!(
r##"
test 'should not split equation in AsciiMath block at single newline' do
input = <<~'EOS'
[asciimath]
++++
f: bbb"N" -> bbb"N"
f: x |-> x + 1
++++
EOS
expected = <<~'EOS'.chop
\$f: bbb"N" -> bbb"N"
f: x |-> x + 1\$
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]', output
assert_equal expected, nodes.first.inner_html.strip
end
"##
);
let doc = Parser::default()
.parse("[asciimath]\n++++\nf: bbb\"N\" -> bbb\"N\"\nf: x |-> x + 1\n++++\n");
assert_css(&doc, ".stemblock", 1);
assert_eq!(
stem_pre_text(&doc),
"\\$f: bbb\"N\" -> bbb\"N\"\nf: x |-> x + 1\\$"
);
}
// TODO: add AsciiMath `\$..\$` delimiters and equation splitting.
#[ignore]
#[test]
fn should_split_equation_in_asciimath_block_at_escaped_newline() {
verifies!(
r##"
test 'should split equation in AsciiMath block at escaped newline' do
input = <<~'EOS'
[asciimath]
++++
f: bbb"N" -> bbb"N" \
f: x |-> x + 1
++++
EOS
expected = <<~'EOS'.chop
\$f: bbb"N" -> bbb"N"\$
\$f: x |-> x + 1\$
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]', output
assert_equal expected, nodes.first.inner_html.strip
end
"##
);
let doc = Parser::default()
.parse("[asciimath]\n++++\nf: bbb\"N\" -> bbb\"N\" \\\nf: x |-> x + 1\n++++\n");
assert_css(&doc, ".stemblock", 1);
}
// TODO: add AsciiMath `\$..\$` delimiters and equation splitting.
#[ignore]
#[test]
fn should_split_equation_in_asciimath_block_at_sequence_of_escaped_newlines() {
verifies!(
r##"
test 'should split equation in AsciiMath block at sequence of escaped newlines' do
input = <<~'EOS'
[asciimath]
++++
f: bbb"N" -> bbb"N" \
\
f: x |-> x + 1
++++
EOS
expected = <<~'EOS'.chop
\$f: bbb"N" -> bbb"N"\$
<br>
\$f: x |-> x + 1\$
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]', output
assert_equal expected, nodes.first.inner_html.strip
end
"##
);
let doc = Parser::default()
.parse("[asciimath]\n++++\nf: bbb\"N\" -> bbb\"N\" \\\n\\\nf: x |-> x + 1\n++++\n");
assert_css(&doc, ".stemblock", 1);
}
// TODO: add AsciiMath `\$..\$` delimiters and equation splitting.
#[ignore]
#[test]
fn should_split_equation_in_asciimath_block_at_newline_sequence_and_preserve_breaks() {
verifies!(
r##"
test 'should split equation in AsciiMath block at newline sequence and preserve breaks' do
input = <<~'EOS'
[asciimath]
++++
f: bbb"N" -> bbb"N"
f: x |-> x + 1
++++
EOS
expected = <<~'EOS'.chop
\$f: bbb"N" -> bbb"N"\$
<br>
<br>
\$f: x |-> x + 1\$
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]', output
assert_equal expected, nodes.first.inner_html.strip
end
"##
);
let doc = Parser::default()
.parse("[asciimath]\n++++\nf: bbb\"N\" -> bbb\"N\"\n\n\nf: x |-> x + 1\n++++\n");
assert_css(&doc, ".stemblock", 1);
}
// TODO: wrap asciimath content in `\$..\$` delimiters.
#[ignore]
#[test]
fn should_add_asciimath_delimiters_around_asciimath_block_content() {
verifies!(
r##"
test 'should add AsciiMath delimiters around asciimath block content' do
input = <<~'EOS'
[asciimath]
++++
sqrt(3x-1)+(1+x)^2 < y
++++
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]/child::text()', output
assert_equal '\$sqrt(3x-1)+(1+x)^2 < y\$', nodes.first.to_s.strip
end
"##
);
let doc = Parser::default().parse("[asciimath]\n++++\nsqrt(3x-1)+(1+x)^2 < y\n++++\n");
assert_css(&doc, ".stemblock", 1);
assert_eq!(stem_pre_text(&doc), "\\$sqrt(3x-1)+(1+x)^2 < y\\$");
}
// TODO: recognize existing `\$..\$` delimiters in asciimath content.
#[ignore]
#[test]
fn should_not_add_asciimath_delimiters_around_asciimath_block_content_if_already_present() {
verifies!(
r##"
test 'should not add AsciiMath delimiters around asciimath block content if already present' do
input = <<~'EOS'
[asciimath]
++++
\$sqrt(3x-1)+(1+x)^2 < y\$
++++
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]/child::text()', output
assert_equal '\$sqrt(3x-1)+(1+x)^2 < y\$', nodes.first.to_s.strip
end
"##
);
let doc =
Parser::default().parse("[asciimath]\n++++\n\\$sqrt(3x-1)+(1+x)^2 < y\\$\n++++\n");
assert_css(&doc, ".stemblock", 1);
assert_eq!(stem_pre_text(&doc), "\\$sqrt(3x-1)+(1+x)^2 < y\\$");
}
#[test]
fn should_convert_contents_of_asciimath_block_to_mathml_in_docbook_output_if_asciimath_gem_is_available()
{
non_normative!(
r#"
test 'should convert contents of asciimath block to MathML in DocBook output if asciimath gem is available' do
asciimath_available = !(Asciidoctor::Helpers.require_library 'asciimath', true, :ignore).nil?
input = <<~'EOS'
[asciimath]
++++
x+b/(2a)<+-sqrt((b^2)/(4a^2)-c/a)
++++
[asciimath]
++++
++++
EOS
expect = <<~'EOS'.chop
<informalequation>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"><mml:mi>x</mml:mi><mml:mo>+</mml:mo><mml:mfrac><mml:mi>b</mml:mi><mml:mrow><mml:mn>2</mml:mn><mml:mi>a</mml:mi></mml:mrow></mml:mfrac><mml:mo><</mml:mo><mml:mo>±</mml:mo><mml:msqrt><mml:mrow><mml:mfrac><mml:msup><mml:mi>b</mml:mi><mml:mn>2</mml:mn></mml:msup><mml:mrow><mml:mn>4</mml:mn><mml:msup><mml:mi>a</mml:mi><mml:mn>2</mml:mn></mml:msup></mml:mrow></mml:mfrac><mml:mo>−</mml:mo><mml:mfrac><mml:mi>c</mml:mi><mml:mi>a</mml:mi></mml:mfrac></mml:mrow></mml:msqrt></mml:math>
</informalequation>
<informalequation>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"></mml:math>
</informalequation>
EOS
using_memory_logger do |logger|
doc = document_from_string input, backend: :docbook, standalone: false
actual = doc.convert
if asciimath_available
assert_equal expect, actual.strip
assert_equal :loaded, doc.converter.instance_variable_get(:@asciimath_status)
else
assert_message logger, :WARN, 'optional gem \'asciimath\' is not available. Functionality disabled.'
assert_equal :unavailable, doc.converter.instance_variable_get(:@asciimath_status)
end
end
end
"#
);
// Backend-specific test omitted: DocBook (MathML conversion).
}
// NOTE: divergence from Asciidoctor. This crate renders the title of a stem
// block twice (once as a sibling of the `div.stemblock` and once inside
// it), so the document-wide `//*[@class="title"]` count is 2 rather than 1.
// Kept `#[ignore]`d with the Ruby-intended assertions.
// TODO: render a stem block's title only once.
#[ignore]
#[test]
fn should_output_title_for_latexmath_block_if_defined() {
verifies!(
r#"
test 'should output title for latexmath block if defined' do
input = <<~'EOS'
.The Lorenz Equations
[latexmath]
++++
\begin{aligned}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{aligned}
++++
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
assert_css '.stemblock .title', output, 1
assert_xpath '//*[@class="title"][text()="The Lorenz Equations"]', output, 1
end
"#
);
let doc = Parser::default().parse(
".The Lorenz Equations\n[latexmath]\n++++\n\\begin{aligned}\n\\dot{x} & = \\sigma(y-x) \\\\\n\\dot{y} & = \\rho x - y - xz \\\\\n\\dot{z} & = -\\beta z + xy\n\\end{aligned}\n++++\n",
);
assert_css(&doc, ".stemblock", 1);
assert_css(&doc, ".stemblock .title", 1);
assert_xpath(
&doc,
"//*[@class=\"title\"][text()=\"The Lorenz Equations\"]",
1,
);
}
// NOTE: divergence from Asciidoctor (duplicate stem-block title; see
// `should_output_title_for_latexmath_block_if_defined`).
// TODO: render a stem block's title only once.
#[ignore]
#[test]
fn should_output_title_for_asciimath_block_if_defined() {
verifies!(
r#"
test 'should output title for asciimath block if defined' do
input = <<~'EOS'
.Simple fraction
[asciimath]
++++
a//b
++++
EOS
output = convert_string_to_embedded input
assert_css '.stemblock', output, 1
assert_css '.stemblock .title', output, 1
assert_xpath '//*[@class="title"][text()="Simple fraction"]', output, 1
end
"#
);
let doc = Parser::default().parse(".Simple fraction\n[asciimath]\n++++\na//b\n++++\n");
assert_css(&doc, ".stemblock", 1);
assert_css(&doc, ".stemblock .title", 1);
assert_xpath(&doc, "//*[@class=\"title\"][text()=\"Simple fraction\"]", 1);
}
// TODO: wrap stem content in AsciiMath delimiters per the `stem` attribute.
#[ignore]
#[test]
fn should_add_asciimath_delimiters_around_stem_block_content_if_stem_attribute_is_asciimath_empty_or_not_set()
{
verifies!(
r##"
test 'should add AsciiMath delimiters around stem block content if stem attribute is asciimath, empty, or not set' do
input = <<~'EOS'
[stem]
++++
sqrt(3x-1)+(1+x)^2 < y
++++
EOS
[
{},
{ 'stem' => '' },
{ 'stem' => 'asciimath' },
{ 'stem' => 'bogus' },
].each do |attributes|
output = convert_string_to_embedded input, attributes: attributes
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]/child::text()', output
assert_equal '\$sqrt(3x-1)+(1+x)^2 < y\$', nodes.first.to_s.strip
end
end
"##
);
let doc = Parser::default().parse("[stem]\n++++\nsqrt(3x-1)+(1+x)^2 < y\n++++\n");
assert_css(&doc, ".stemblock", 1);
assert_eq!(stem_pre_text(&doc), "\\$sqrt(3x-1)+(1+x)^2 < y\\$");
}
// TODO: wrap stem content in LaTeX delimiters per the `stem` attribute.
#[ignore]
#[test]
fn should_add_latex_math_delimiters_around_stem_block_content_if_stem_attribute_is_latexmath_latex_or_tex()
{
verifies!(
r#"
test 'should add LaTeX math delimiters around stem block content if stem attribute is latexmath, latex, or tex' do
input = <<~'EOS'
[stem]
++++
\sqrt{3x-1}+(1+x)^2 < y
++++
EOS
[
{ 'stem' => 'latexmath' },
{ 'stem' => 'latex' },
{ 'stem' => 'tex' },
].each do |attributes|
output = convert_string_to_embedded input, attributes: attributes
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]/child::text()', output
assert_equal '\[\sqrt{3x-1}+(1+x)^2 < y\]', nodes.first.to_s.strip
end
end
"#
);
let doc = Parser::default()
.with_intrinsic_attribute(
"stem",
"latexmath",
crate::parser::ModificationContext::ApiOnly,
)
.parse("[stem]\n++++\n\\sqrt{3x-1}+(1+x)^2 < y\n++++\n");
assert_css(&doc, ".stemblock", 1);
assert_eq!(stem_pre_text(&doc), "\\[\\sqrt{3x-1}+(1+x)^2 < y\\]");
}
// NOTE: divergence from Asciidoctor. The stem-style delimiter wrapping is
// not modeled (see the delimiter tests above). The style is recorded on the
// block as its declared style.
// TODO: wrap stem content per the style set by the second positional
// attribute.
#[ignore]
#[test]
fn should_allow_stem_style_to_be_set_using_second_positional_argument_of_block_attributes() {
verifies!(
r##"
test 'should allow stem style to be set using second positional argument of block attributes' do
input = <<~'EOS'
:stem: latexmath
[stem,asciimath]
++++
sqrt(3x-1)+(1+x)^2 < y
++++
EOS
doc = document_from_string input
stemblock = doc.blocks[0]
assert_equal :stem, stemblock.context
assert_equal 'asciimath', stemblock.attributes['style']
output = doc.convert standalone: false
assert_css '.stemblock', output, 1
nodes = xmlnodes_at_xpath '//*[@class="content"]/child::text()', output
assert_equal '\$sqrt(3x-1)+(1+x)^2 < y\$', nodes.first.to_s.strip
end
"##
);
let doc = Parser::default()
.parse(":stem: latexmath\n\n[stem,asciimath]\n++++\nsqrt(3x-1)+(1+x)^2 < y\n++++\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.declared_style(), Some("asciimath"));
assert_css(&doc, ".stemblock", 1);
assert_eq!(stem_pre_text(&doc), "\\$sqrt(3x-1)+(1+x)^2 < y\\$");
}
non_normative!(
r#"
end
"#
);
}
mod custom_blocks {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Custom Blocks' do
"#
);
#[test]
fn should_not_warn_if_block_style_is_unknown() {
verifies!(
r#"
test 'should not warn if block style is unknown' do
input = <<~'EOS'
[foo]
--
bar
--
EOS
convert_string_to_embedded input
assert_empty @logger.messages
end
"#
);
let doc = Parser::default().parse("[foo]\n--\nbar\n--\n");
assert_eq!(doc.warnings().count(), 0);
}
#[test]
fn should_log_debug_message_if_block_style_is_unknown_and_debug_level_is_enabled() {
non_normative!(
r#"
test 'should log debug message if block style is unknown and debug level is enabled' do
input = <<~'EOS'
[foo]
--
bar
--
EOS
using_memory_logger Logger::Severity::DEBUG do |logger|
convert_string_to_embedded input
assert_message logger, :DEBUG, '<stdin>: line 2: unknown style for open block: foo', Hash
end
end
"#
);
// Not ported: this crate has no DEBUG-severity logging channel for
// unknown block styles.
}
non_normative!(
r#"
end
"#
);
}
mod metadata {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Metadata' do
"#
);
#[test]
fn block_title_above_section_gets_carried_over_to_first_block_in_section() {
verifies!(
r#"
test 'block title above section gets carried over to first block in section' do
input = <<~'EOS'
.Title
== Section
paragraph
EOS
output = convert_string input
assert_xpath '//*[@class="paragraph"]', output, 1
assert_xpath '//*[@class="paragraph"]/*[@class="title"][text()="Title"]', output, 1
assert_xpath '//*[@class="paragraph"]/p[text()="paragraph"]', output, 1
end
"#
);
let doc = Parser::default().parse(".Title\n== Section\n\nparagraph\n");
assert_xpath(&doc, "//*[@class=\"paragraph\"]", 1);
assert_xpath(
&doc,
"//*[@class=\"paragraph\"]/*[@class=\"title\"][text()=\"Title\"]",
1,
);
assert_xpath(&doc, "//*[@class=\"paragraph\"]/p[text()=\"paragraph\"]", 1);
}
// NOTE: out of scope. Both tests below depend on demoting a body-level
// document title (`= Title`) to a level-0 section — the "level 0 sections
// can only be used when doctype is book" behavior. This crate does not
// support level-0 sections in the document body (with or without
// `doctype: book`): a `=` heading in the body is declined as an unsupported
// level-0 heading rather than becoming a level-0 section, so the demoted
// `<h1>`/`.sect1` structure these tests assert is never produced. That is a
// deliberate divergence, not a deferral, so the tests are reproduced
// verbatim (`non_normative!`) rather than ported. The block-title carryover
// they also exercise is itself implemented (#782); see
// `block_title_above_section_gets_carried_over_to_first_block_in_section`.
non_normative!(
r#"
test 'block title above document title demotes document title to a section title' do
input = <<~'EOS'
.Block title
= Section Title
section paragraph
EOS
output = convert_string input
assert_xpath '//*[@id="header"]/*', output, 0
assert_xpath '//*[@id="preamble"]/*', output, 0
assert_xpath '//*[@id="content"]/h1[text()="Section Title"]', output, 1
assert_xpath '//*[@class="paragraph"]', output, 1
assert_xpath '//*[@class="paragraph"]/*[@class="title"][text()="Block title"]', output, 1
assert_message @logger, :ERROR, '<stdin>: line 2: level 0 sections can only be used when doctype is book', Hash
end
test 'block title above document title gets carried over to first block in first section if no preamble' do
input = <<~'EOS'
:doctype: book
.Block title
= Document Title
== First Section
paragraph
EOS
doc = document_from_string input
# NOTE block title demotes document title to level-0 section
refute doc.header?
output = doc.convert
assert_xpath '//*[@class="sect1"]//*[@class="paragraph"]/*[@class="title"][text()="Block title"]', output, 1
end
"#
);
// NOTE: divergence from Asciidoctor. This crate does not render a macro
// link inside a block title (nor were the referenced attributes supplied),
// so `.title a[href]` is absent. Kept `#[ignore]`d with the Ruby-intended
// assertions.
// TODO: apply the normal substitutions (including macros) to a block title.
#[ignore]
#[test]
fn should_apply_substitutions_to_a_block_title_in_normal_order() {
verifies!(
r##"
test 'should apply substitutions to a block title in normal order' do
input = <<~'EOS'
.{link-url}[{link-text}]{tm}
The one and only!
EOS
output = convert_string_to_embedded input, attributes: {
'link-url' => 'https://acme.com',
'link-text' => 'ACME',
'tm' => '(TM)',
}
assert_css '.title', output, 1
assert_css '.title a[href="https://acme.com"]', output, 1
assert_xpath %(//*[@class="title"][contains(text(),"#{decode_char 8482}")]), output, 1
end
"##
);
let doc = Parser::default()
.with_intrinsic_attribute(
"link-url",
"https://acme.com",
crate::parser::ModificationContext::ApiOnly,
)
.with_intrinsic_attribute(
"link-text",
"ACME",
crate::parser::ModificationContext::ApiOnly,
)
.with_intrinsic_attribute("tm", "(TM)", crate::parser::ModificationContext::ApiOnly)
.parse(".{link-url}[{link-text}]{tm}\nThe one and only!\n");
assert_css(&doc, ".title", 1);
assert_css(&doc, ".title a[href=\"https://acme.com\"]", 1);
}
#[test]
fn empty_attribute_list_should_not_appear_in_output() {
verifies!(
r#"
test 'empty attribute list should not appear in output' do
input = <<~'EOS'
[]
--
Block content
--
EOS
output = convert_string_to_embedded input
assert_includes output, 'Block content'
refute_includes output, '[]'
end
"#
);
let doc = Parser::default().parse("[]\n--\nBlock content\n--\n");
assert_rendered_contains(&doc, "Block content");
refute_rendered_contains(&doc, "[]");
}
// NOTE: divergence from Asciidoctor. An empty block anchor `[[]]` is not
// recognized as an (empty) anchor by this crate and is rendered as a
// paragraph containing the literal text `[[]]`. Kept `#[ignore]`d with the
// Ruby-intended assertions.
// TODO: treat an empty block anchor `[[]]` as an ignored anchor.
#[ignore]
#[test]
fn empty_block_anchor_should_not_appear_in_output() {
verifies!(
r#"
test 'empty block anchor should not appear in output' do
input = <<~'EOS'
[[]]
--
Block content
--
EOS
output = convert_string_to_embedded input
assert_includes output, 'Block content'
refute_includes output, '[[]]'
end
"#
);
let doc = Parser::default().parse("[[]]\n--\nBlock content\n--\n");
assert_rendered_contains(&doc, "Block content");
refute_rendered_contains(&doc, "[[]]");
}
non_normative!(
r#"
end
"#
);
}
mod images {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Images' do
"#
);
#[test]
fn can_convert_block_image_with_alt_text_defined_in_macro() {
verifies!(
r#"
test 'can convert block image with alt text defined in macro' do
input = 'image::images/tiger.png[Tiger]'
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc = Parser::default().parse("image::images/tiger.png[Tiger]");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
// The following SVG interactive/inline/data-uri/remote-fetch tests depend
// on safe modes, image files on disk, and network reads that this crate
// does not model; they are reproduced verbatim to account for the Ruby
// source without asserting behavior.
non_normative!(
r#"
test 'converts SVG image using img element by default' do
input = 'image::tiger.svg[Tiger]'
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '/*[@class="imageblock"]//img[@src="tiger.svg"][@alt="Tiger"]', output, 1
end
test 'converts interactive SVG image with alt text using object element' do
input = <<~'EOS'
:imagesdir: images
[%interactive]
image::tiger.svg[Tiger,100]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '/*[@class="imageblock"]//object[@type="image/svg+xml"][@data="images/tiger.svg"][@width="100"]/span[@class="alt"][text()="Tiger"]', output, 1
end
test 'converts SVG image with alt text using img element when safe mode is secure' do
input = <<~'EOS'
[%interactive]
image::images/tiger.svg[Tiger,100]
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.svg"][@alt="Tiger"]', output, 1
end
test 'inserts fallback image for SVG inside object element using same dimensions' do
input = <<~'EOS'
:imagesdir: images
[%interactive]
image::tiger.svg[Tiger,100,fallback=tiger.png]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '/*[@class="imageblock"]//object[@type="image/svg+xml"][@data="images/tiger.svg"][@width="100"]/img[@src="images/tiger.png"][@width="100"]', output, 1
end
test 'detects SVG image URI that contains a query string' do
input = <<~'EOS'
:imagesdir: images
[%interactive]
image::http://example.org/tiger.svg?foo=bar[Tiger,100]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '/*[@class="imageblock"]//object[@type="image/svg+xml"][@data="http://example.org/tiger.svg?foo=bar"][@width="100"]/span[@class="alt"][text()="Tiger"]', output, 1
end
test 'detects SVG image when format attribute is svg' do
input = <<~'EOS'
:imagesdir: images
[%interactive]
image::http://example.org/tiger-svg[Tiger,100,format=svg]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '/*[@class="imageblock"]//object[@type="image/svg+xml"][@data="http://example.org/tiger-svg"][@width="100"]/span[@class="alt"][text()="Tiger"]', output, 1
end
test 'converts to inline SVG image when inline option is set on block' do
input = <<~'EOS'
:imagesdir: fixtures
[%inline]
image::circle.svg[Tiger,100]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docdir' => testdir }
assert_match(/<svg\s[^>]*width="100"[^>]*>/, output, 1)
refute_match(/<svg\s[^>]*width="500"[^>]*>/, output)
refute_match(/<svg\s[^>]*height="500"[^>]*>/, output)
refute_match(/<svg\s[^>]*style="[^>]*>/, output)
end
test 'should honor percentage width for SVG image with inline option' do
input = <<~'EOS'
:imagesdir: fixtures
image::circle.svg[Circle,50%,opts=inline]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docdir' => testdir }
assert_match(/<svg\s[^>]*width="50%"[^>]*>/, output, 1)
end
test 'should not crash if explicit width on SVG image block is an integer' do
input = <<~'EOS'
:imagesdir: fixtures
image::circle.svg[Circle,opts=inline]
EOS
doc = document_from_string input, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docdir' => testdir }
doc.blocks[0].set_attr 'width', 50
output = doc.convert
assert_match %r/<svg\s[^>]*width="50"[^>]*>/, output, 1
end
test 'converts to inline SVG image when inline option is set on block and data-uri is set on document' do
input = <<~'EOS'
:imagesdir: fixtures
:data-uri:
[%inline]
image::circle.svg[Tiger,100]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docdir' => testdir }
assert_match(/<svg\s[^>]*width="100">/, output, 1)
end
test 'should not throw exception if SVG to inline is empty' do
input = 'image::empty.svg[nada,opts=inline]'
output = convert_string_to_embedded input, safe: :safe, attributes: { 'docdir' => testdir, 'imagesdir' => 'fixtures' }
assert_xpath '//svg', output, 0
assert_xpath '//span[@class="alt"][text()="nada"]', output, 1
assert_message @logger, :WARN, '~contents of SVG is empty:'
end
test 'should not throw exception if SVG to inline contains an incomplete start tag and explicit width is specified' do
input = 'image::incomplete.svg[,200,opts=inline]'
output = convert_string_to_embedded input, safe: :safe, attributes: { 'docdir' => testdir, 'imagesdir' => 'fixtures' }
assert_xpath '//svg', output, 1
assert_xpath '//span[@class="alt"]', output, 0
end
test 'embeds remote SVG to inline when inline option is set on block and allow-uri-read is set on document' do
input = %(image::http://#{resolve_localhost}:9876/fixtures/circle.svg[Circle,100,100,opts=inline])
output = using_test_webserver do
convert_string_to_embedded input, safe: :safe, attributes: { 'allow-uri-read' => '' }
end
assert_css 'svg', output, 1
assert_css 'svg[style]', output, 0
assert_css 'svg[width="100"]', output, 1
assert_css 'svg[height="100"]', output, 1
assert_css 'svg circle', output, 1
end
test 'should cache remote SVG when allow-uri-read, cache-uri, and inline option are set' do
begin
if OpenURI.respond_to? :cache_open_uri
OpenURI.singleton_class.send :remove_method, :open_uri
OpenURI.singleton_class.send :alias_method, :open_uri, :cache_open_uri
end
using_test_webserver do |base_url, thr|
image_url = %(#{base_url}/fixtures/circle.svg)
attributes = { 'allow-uri-read' => '', 'cache-uri' => '' }
input = %(image::#{image_url}[Circle,100,100,opts=inline])
output = convert_string_to_embedded input, safe: :safe, attributes: attributes
assert defined? OpenURI::Cache
assert_css 'svg circle', output, 1
# NOTE we can't assert here since this is using the system-wide cache
#assert_equal thr[:requests].size, 1
#assert_equal thr[:requests][0], image_url
thr[:requests].clear
Dir.mktmpdir do |cache_path|
original_cache_path = OpenURI::Cache.cache_path
begin
OpenURI::Cache.cache_path = cache_path
assert_nil OpenURI::Cache.get image_url
2.times do
output = convert_string_to_embedded input, safe: :safe, attributes: attributes
refute_nil OpenURI::Cache.get image_url
assert_css 'svg circle', output, 1
end
assert_equal 1, thr[:requests].size
assert_match %r/ \/fixtures\/circle\.svg /, thr[:requests][0], 1
ensure
OpenURI::Cache.cache_path = original_cache_path
end
end
end
ensure
OpenURI.singleton_class.send :alias_method, :cache_open_uri, :open_uri
OpenURI.singleton_class.send :remove_method, :open_uri
OpenURI.singleton_class.send :alias_method, :open_uri, :original_open_uri
end
end
test 'converts to alt text for SVG with inline option set if SVG cannot be read' do
input = <<~'EOS'
[%inline]
image::no-such-image.svg[Alt Text]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '//span[@class="alt"][text()="Alt Text"]', output, 1
assert_message @logger, :WARN, '~SVG does not exist or cannot be read'
end
"#
);
#[test]
fn can_convert_block_image_with_alt_text_defined_in_macro_containing_square_bracket() {
verifies!(
r#"
test 'can convert block image with alt text defined in macro containing square bracket' do
input = 'image::images/tiger.png[A [Bengal] Tiger]'
output = convert_string input
img = xmlnodes_at_xpath '//img', output, 1
assert_equal 'A [Bengal] Tiger', img.attr('alt')
end
"#
);
let doc = Parser::default().parse("image::images/tiger.png[A [Bengal] Tiger]");
assert_xpath(&doc, "//img[@alt=\"A [Bengal] Tiger\"]", 1);
}
#[test]
fn can_convert_block_image_with_target_containing_spaces() {
verifies!(
r#"
test 'can convert block image with target containing spaces' do
input = 'image::images/big tiger.png[A Big Tiger]'
output = convert_string input
img = xmlnodes_at_xpath '//img', output, 1
assert_equal 'images/big%20tiger.png', img.attr('src')
assert_equal 'A Big Tiger', img.attr('alt')
end
"#
);
let doc = Parser::default().parse("image::images/big tiger.png[A Big Tiger]");
assert_xpath(
&doc,
"//img[@src=\"images/big%20tiger.png\"][@alt=\"A Big Tiger\"]",
1,
);
}
// NOTE: divergence from Asciidoctor. This crate recognizes an image macro
// whose target has a leading or trailing space and renders it as an image
// block; Asciidoctor does not treat such a line as a block image. Kept
// `#[ignore]`d with the Ruby-intended assertion (no `<img>`).
// TODO: reject a block image macro whose target has leading/trailing spaces.
#[ignore]
#[test]
fn should_not_recognize_block_image_if_target_has_leading_or_trailing_spaces() {
verifies!(
r#"
test 'should not recognize block image if target has leading or trailing spaces' do
[' tiger.png', 'tiger.png '].each do |target|
input = %(image::#{target}[Tiger])
output = convert_string_to_embedded input
assert_xpath '//img', output, 0
end
end
"#
);
for target in [" tiger.png", "tiger.png "] {
let doc = Parser::default().parse(&format!("image::{target}[Tiger]"));
assert_xpath(&doc, "//img", 0);
}
}
#[test]
fn can_convert_block_image_with_alt_text_defined_in_block_attribute_above_macro() {
verifies!(
r#"
test 'can convert block image with alt text defined in block attribute above macro' do
input = <<~'EOS'
[Tiger]
image::images/tiger.png[]
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc = Parser::default().parse("[Tiger]\nimage::images/tiger.png[]\n");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
#[test]
fn alt_text_in_macro_overrides_alt_text_above_macro() {
verifies!(
r#"
test 'alt text in macro overrides alt text above macro' do
input = <<~'EOS'
[Alt Text]
image::images/tiger.png[Tiger]
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc = Parser::default().parse("[Alt Text]\nimage::images/tiger.png[Tiger]\n");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
#[test]
fn should_substitute_attribute_references_in_alt_text_defined_in_image_block_macro() {
verifies!(
r#"
test 'should substitute attribute references in alt text defined in image block macro' do
input = <<~'EOS'
:alt-text: Tiger
image::images/tiger.png[{alt-text}]
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc =
Parser::default().parse(":alt-text: Tiger\n\nimage::images/tiger.png[{alt-text}]\n");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
#[test]
fn should_set_direction_css_class_on_image_if_float_attribute_is_set() {
verifies!(
r#"
test 'should set direction CSS class on image if float attribute is set' do
input = <<~'EOS'
[float=left]
image::images/tiger.png[Tiger]
EOS
output = convert_string_to_embedded input
assert_css '.imageblock.left', output, 1
assert_css '.imageblock[style]', output, 0
end
"#
);
let doc = Parser::default().parse("[float=left]\nimage::images/tiger.png[Tiger]\n");
assert_css(&doc, ".imageblock.left", 1);
assert_css(&doc, ".imageblock[style]", 0);
}
#[test]
fn should_set_text_alignment_css_class_on_image_if_align_attribute_is_set() {
verifies!(
r#"
test 'should set text alignment CSS class on image if align attribute is set' do
input = <<~'EOS'
[align=center]
image::images/tiger.png[Tiger]
EOS
output = convert_string_to_embedded input
assert_css '.imageblock.text-center', output, 1
assert_css '.imageblock[style]', output, 0
end
"#
);
let doc = Parser::default().parse("[align=center]\nimage::images/tiger.png[Tiger]\n");
assert_css(&doc, ".imageblock.text-center", 1);
assert_css(&doc, ".imageblock[style]", 0);
}
#[test]
fn style_attribute_is_dropped_from_image_macro() {
verifies!(
r#"
test 'style attribute is dropped from image macro' do
input = <<~'EOS'
[style=value]
image::images/tiger.png[Tiger]
EOS
doc = document_from_string input
img = doc.blocks[0]
refute(img.attributes.key? 'style')
assert_nil img.style
end
"#
);
// This crate does not apply a `style` named attribute as the block
// style (`declared_style()` is `None`), matching `assert_nil img.style`.
// NOTE: divergence — Asciidoctor also removes the `style` key from the
// block's attributes; this crate retains it as a plain named attribute.
let doc = Parser::default().parse("[style=value]\nimage::images/tiger.png[Tiger]\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.declared_style(), None);
}
// NOTE: divergence from Asciidoctor. This crate does not apply the
// specialcharacters and replacement substitutions to image alt text, so the
// alt attribute retains the raw characters. Kept `#[ignore]`d with the
// Ruby-intended (substituted) alt text.
// TODO: apply specialchars/replacement subs to image alt text.
#[ignore]
#[test]
fn should_apply_specialcharacters_and_replacement_substitutions_to_alt_text() {
verifies!(
r##"
test 'should apply specialcharacters and replacement substitutions to alt text' do
input = 'A tiger\'s "roar" is < a bear\'s "growl"'
expected = 'A tiger’s "roar" is < a bear’s "growl"'
result = convert_string_to_embedded %(image::images/tiger-roar.png[#{input}])
assert_includes result, %(alt="#{expected}")
end
"##
);
let doc = Parser::default()
.parse("image::images/tiger-roar.png[A tiger's \"roar\" is < a bear's \"growl\"]");
assert_xpath(
&doc,
"//img[@alt=\"A tiger’s "roar" is < a bear’s "growl"\"]",
1,
);
}
#[test]
fn should_not_encode_double_quotes_in_alt_text_when_converting_to_docbook() {
non_normative!(
r#"
test 'should not encode double quotes in alt text when converting to DocBook' do
input = 'Select "File > Open"'
expected = 'Select "File > Open"'
result = convert_string_to_embedded %(image::images/open.png[#{input}]), backend: :docbook
assert_includes result, %(<phrase>#{expected}</phrase>)
end
"#
);
// Backend-specific test omitted: DocBook.
}
#[test]
fn should_auto_generate_alt_text_for_block_image_if_alt_text_is_not_specified() {
verifies!(
r#"
test 'should auto-generate alt text for block image if alt text is not specified' do
input = 'image::images/lions-and-tigers.png[]'
image = block_from_string input
assert_equal 'lions and tigers', (image.attr 'alt')
assert_equal 'lions and tigers', (image.attr 'default-alt')
output = image.convert
assert_xpath '/*[@class="imageblock"]//img[@src="images/lions-and-tigers.png"][@alt="lions and tigers"]', output, 1
end
"#
);
let doc = Parser::default().parse("image::images/lions-and-tigers.png[]");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/lions-and-tigers.png\"][@alt=\"lions and tigers\"]",
1,
);
}
#[test]
fn can_convert_block_image_with_alt_text_and_height_and_width() {
verifies!(
r#"
test "can convert block image with alt text and height and width" do
input = 'image::images/tiger.png[Tiger, 200, 300]'
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.png"][@alt="Tiger"][@width="200"][@height="300"]', output, 1
end
"#
);
let doc = Parser::default().parse("image::images/tiger.png[Tiger, 200, 300]");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@alt=\"Tiger\"][@width=\"200\"][@height=\"300\"]",
1,
);
}
#[test]
fn should_not_output_empty_width_attribute_if_positional_width_attribute_is_empty() {
verifies!(
r#"
test 'should not output empty width attribute if positional width attribute is empty' do
input = 'image::images/tiger.png[Tiger,]'
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.png"]', output, 1
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.png"][@width]', output, 0
end
"#
);
let doc = Parser::default().parse("image::images/tiger.png[Tiger,]");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"]",
1,
);
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@width]",
0,
);
}
#[test]
fn can_convert_block_image_with_link() {
verifies!(
r##"
test "can convert block image with link" do
input = <<~'EOS'
image::images/tiger.png[Tiger, link='http://en.wikipedia.org/wiki/Tiger']
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//a[@class="image"][@href="http://en.wikipedia.org/wiki/Tiger"]/img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"##
);
let doc = Parser::default()
.parse("image::images/tiger.png[Tiger, link='http://en.wikipedia.org/wiki/Tiger']\n");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//a[@class=\"image\"][@href=\"http://en.wikipedia.org/wiki/Tiger\"]/img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
#[test]
fn adds_rel_noopener_attribute_to_block_image_with_link_that_targets_blank_window() {
verifies!(
r#"
test 'adds rel=noopener attribute to block image with link that targets _blank window' do
input = 'image::images/tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,window=_blank]'
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//a[@class="image"][@href="http://en.wikipedia.org/wiki/Tiger"][@target="_blank"][@rel="noopener"]/img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"image::images/tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,window=_blank]",
);
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//a[@class=\"image\"][@href=\"http://en.wikipedia.org/wiki/Tiger\"][@target=\"_blank\"][@rel=\"noopener\"]/img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
#[test]
fn adds_rel_noopener_attribute_to_block_image_with_link_that_targets_name_window_when_the_noopener_option_is_set()
{
verifies!(
r#"
test 'adds rel=noopener attribute to block image with link that targets name window when the noopener option is set' do
input = 'image::images/tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,window=name,opts=noopener]'
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//a[@class="image"][@href="http://en.wikipedia.org/wiki/Tiger"][@target="name"][@rel="noopener"]/img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"image::images/tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,window=name,opts=noopener]",
);
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//a[@class=\"image\"][@href=\"http://en.wikipedia.org/wiki/Tiger\"][@target=\"name\"][@rel=\"noopener\"]/img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
#[test]
fn adds_rel_nofollow_attribute_to_block_image_with_a_link_when_the_nofollow_option_is_set() {
verifies!(
r#"
test 'adds rel=nofollow attribute to block image with a link when the nofollow option is set' do
input = 'image::images/tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,opts=nofollow]'
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//a[@class="image"][@href="http://en.wikipedia.org/wiki/Tiger"][@rel="nofollow"]/img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"image::images/tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,opts=nofollow]",
);
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//a[@class=\"image\"][@href=\"http://en.wikipedia.org/wiki/Tiger\"][@rel=\"nofollow\"]/img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
#[test]
fn can_convert_block_image_with_caption() {
verifies!(
r#"
test 'can convert block image with caption' do
input = <<~'EOS'
.The AsciiDoc Tiger
image::images/tiger.png[Tiger]
EOS
doc = document_from_string input
assert_equal 1, doc.blocks[0].numeral
output = doc.convert
assert_xpath '//*[@class="imageblock"]//img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
assert_xpath '//*[@class="imageblock"]/*[@class="title"][text()="Figure 1. The AsciiDoc Tiger"]', output, 1
assert_equal 1, doc.attributes['figure-number']
end
"#
);
let doc = Parser::default().parse(".The AsciiDoc Tiger\nimage::images/tiger.png[Tiger]\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.number(), Some(1));
assert_xpath(
&doc,
"//*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
assert_xpath(
&doc,
"//*[@class=\"imageblock\"]/*[@class=\"title\"][text()=\"Figure 1. The AsciiDoc Tiger\"]",
1,
);
assert_eq!(
doc.attribute_value("figure-number"),
crate::document::InterpretedValue::Value("1".to_string())
);
}
#[test]
fn can_convert_block_image_with_explicit_caption() {
verifies!(
r#"
test 'can convert block image with explicit caption' do
input = <<~'EOS'
[caption="Voila! "]
.The AsciiDoc Tiger
image::images/tiger.png[Tiger]
EOS
doc = document_from_string input
assert_nil doc.blocks[0].numeral
output = doc.convert
assert_xpath '//*[@class="imageblock"]//img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
assert_xpath '//*[@class="imageblock"]/*[@class="title"][text()="Voila! The AsciiDoc Tiger"]', output, 1
refute doc.attributes.key?('figure-number')
end
"#
);
let doc = Parser::default()
.parse("[caption=\"Voila! \"]\n.The AsciiDoc Tiger\nimage::images/tiger.png[Tiger]\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.number(), None);
assert_xpath(
&doc,
"//*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
assert_xpath(
&doc,
"//*[@class=\"imageblock\"]/*[@class=\"title\"][text()=\"Voila! The AsciiDoc Tiger\"]",
1,
);
assert_eq!(
doc.attribute_value("figure-number"),
crate::document::InterpretedValue::Unset
);
}
// DocBook-backend image tests (align/content-width/scale/scaledwidth) are
// out of scope for this crate.
non_normative!(
r#"
test 'can align image in DocBook backend' do
input = 'image::images/sunset.jpg[Sunset,align=right]'
output = convert_string_to_embedded input, backend: :docbook
assert_xpath '//imagedata', output, 1
assert_xpath '//imagedata[@align="right"]', output, 1
end
test 'should set content width and depth in DocBook backend if no scaling' do
input = 'image::images/sunset.jpg[Sunset,500,332]'
output = convert_string_to_embedded input, backend: :docbook
assert_xpath '//imagedata', output, 1
assert_xpath '//imagedata[@contentwidth="500"]', output, 1
assert_xpath '//imagedata[@contentdepth="332"]', output, 1
assert_xpath '//imagedata[@width]', output, 0
assert_xpath '//imagedata[@depth]', output, 0
end
test 'can scale image in DocBook backend' do
input = 'image::images/sunset.jpg[Sunset,500,332,scale=200]'
output = convert_string_to_embedded input, backend: :docbook
assert_xpath '//imagedata', output, 1
assert_xpath '//imagedata[@scale="200"]', output, 1
assert_xpath '//imagedata[@width]', output, 0
assert_xpath '//imagedata[@depth]', output, 0
assert_xpath '//imagedata[@contentwidth]', output, 0
assert_xpath '//imagedata[@contentdepth]', output, 0
end
test 'scale image width in DocBook backend' do
input = 'image::images/sunset.jpg[Sunset,500,332,scaledwidth=25%]'
output = convert_string_to_embedded input, backend: :docbook
assert_xpath '//imagedata', output, 1
assert_xpath '//imagedata[@width="25%"]', output, 1
assert_xpath '//imagedata[@depth]', output, 0
assert_xpath '//imagedata[@contentwidth]', output, 0
assert_xpath '//imagedata[@contentdepth]', output, 0
end
test 'adds % to scaled width if no units given in DocBook backend ' do
input = 'image::images/sunset.jpg[Sunset,scaledwidth=25]'
output = convert_string_to_embedded input, backend: :docbook
assert_xpath '//imagedata', output, 1
assert_xpath '//imagedata[@width="25%"]', output, 1
end
"#
);
#[test]
fn keeps_attribute_reference_unprocessed_if_image_target_is_missing_attribute_reference_and_attribute_missing_is_skip()
{
verifies!(
r#"
test 'keeps attribute reference unprocessed if image target is missing attribute reference and attribute-missing is skip' do
input = <<~'EOS'
:attribute-missing: skip
image::{bogus}[]
EOS
output = convert_string_to_embedded input
assert_css 'img[src="{bogus}"]', output, 1
assert_empty @logger
end
"#
);
let doc = Parser::default().parse(":attribute-missing: skip\n\nimage::{bogus}[]\n");
assert_css(&doc, "img[src=\"{bogus}\"]", 1);
assert_eq!(doc.warnings().count(), 0);
}
#[test]
fn should_not_drop_line_if_image_target_is_missing_attribute_reference_and_attribute_missing_is_drop()
{
verifies!(
r#"
test 'should not drop line if image target is missing attribute reference and attribute-missing is drop' do
input = <<~'EOS'
:attribute-missing: drop
image::{bogus}/photo.jpg[]
EOS
output = convert_string_to_embedded input
assert_css 'img[src="/photo.jpg"]', output, 1
assert_empty @logger
end
"#
);
let doc =
Parser::default().parse(":attribute-missing: drop\n\nimage::{bogus}/photo.jpg[]\n");
assert_css(&doc, "img[src=\"/photo.jpg\"]", 1);
assert_eq!(doc.warnings().count(), 0);
}
#[test]
fn drops_line_if_image_target_is_missing_attribute_reference_and_attribute_missing_is_drop_line()
{
verifies!(
r#"
test 'drops line if image target is missing attribute reference and attribute-missing is drop-line' do
input = <<~'EOS'
:attribute-missing: drop-line
image::{bogus}[]
EOS
output = convert_string_to_embedded input
assert_empty output.strip
assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: bogus'
end
"#
);
// The image line is dropped, leaving no image. (This crate does not
// emit Asciidoctor's INFO "dropping line" log message.)
let doc = Parser::default().parse(":attribute-missing: drop-line\n\nimage::{bogus}[]\n");
assert_xpath(&doc, "//img", 0);
}
#[test]
fn should_not_drop_line_if_image_target_resolves_to_blank_and_attribute_missing_is_drop_line() {
verifies!(
r#"
test 'should not drop line if image target resolves to blank and attribute-missing is drop-line' do
input = <<~'EOS'
:attribute-missing: drop-line
image::{blank}[]
EOS
output = convert_string_to_embedded input
assert_css 'img[src=""]', output, 1
assert_empty @logger
end
"#
);
let doc = Parser::default().parse(":attribute-missing: drop-line\n\nimage::{blank}[]\n");
assert_css(&doc, "img[src=\"\"]", 1);
assert_eq!(doc.warnings().count(), 0);
}
#[test]
fn dropped_image_does_not_break_processing_of_following_section_and_attribute_missing_is_drop_line()
{
verifies!(
r#"
test 'dropped image does not break processing of following section and attribute-missing is drop-line' do
input = <<~'EOS'
:attribute-missing: drop-line
image::{bogus}[]
== Section Title
EOS
output = convert_string_to_embedded input
assert_css 'img', output, 0
assert_css 'h2', output, 1
refute_includes output, '== Section Title'
assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: bogus'
end
"#
);
let doc = Parser::default()
.parse(":attribute-missing: drop-line\n\nimage::{bogus}[]\n\n== Section Title\n");
assert_css(&doc, "img", 0);
assert_css(&doc, "h2", 1);
refute_rendered_contains(&doc, "== Section Title");
}
#[test]
fn should_pass_through_image_that_references_uri() {
verifies!(
r#"
test 'should pass through image that references uri' do
input = <<~'EOS'
:imagesdir: images
image::http://asciidoc.org/images/tiger.png[Tiger]
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="http://asciidoc.org/images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc = Parser::default()
.parse(":imagesdir: images\n\nimage::http://asciidoc.org/images/tiger.png[Tiger]\n");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"http://asciidoc.org/images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
// NOTE: divergence from Asciidoctor. This crate percent-encodes only spaces
// in an image target; Asciidoctor applies fuller URI encoding. Kept
// `#[ignore]`d with the Ruby-intended src.
// TODO: apply full URI encoding to image targets that are URIs.
#[ignore]
#[test]
fn should_encode_spaces_in_image_target_if_value_is_a_uri() {
verifies!(
r##"
test 'should encode spaces in image target if value is a URI' do
input = 'image::http://example.org/svg?digraph=digraph G { a -> b; }[diagram]'
output = convert_string_to_embedded input
assert_xpath %(/*[@class="imageblock"]//img[@src="http://example.org/svg?digraph=digraph%20G%20{%20a%20-#{decode_char 62}%20b;%20}"]), output, 1
end
"##
);
let doc = Parser::default()
.parse("image::http://example.org/svg?digraph=digraph G { a -> b; }[diagram]");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"http://example.org/svg?digraph=digraph%20G%20{%20a%20-%3E%20b;%20}\"]",
1,
);
}
// NOTE: divergence from Asciidoctor. This crate's rendered `src` is the
// macro target verbatim; it does not resolve the target relative to the
// `imagesdir` document attribute. Kept `#[ignore]`d with the Ruby-intended
// (imagesdir-prefixed) src.
// TODO: resolve image targets relative to `imagesdir`.
#[ignore]
#[test]
fn can_resolve_image_relative_to_imagesdir() {
verifies!(
r#"
test 'can resolve image relative to imagesdir' do
input = <<~'EOS'
:imagesdir: images
image::tiger.png[Tiger]
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@class="imageblock"]//img[@src="images/tiger.png"][@alt="Tiger"]', output, 1
end
"#
);
let doc = Parser::default().parse(":imagesdir: images\n\nimage::tiger.png[Tiger]\n");
assert_xpath(
&doc,
"/*[@class=\"imageblock\"]//img[@src=\"images/tiger.png\"][@alt=\"Tiger\"]",
1,
);
}
// The data-uri embedding and remote-image tests below require reading image
// files from disk (or over the network) and base64-encoding them, plus safe
// modes and a test web server, none of which this crate models.
non_normative!(
r##"
test 'embeds base64-encoded data uri for image when data-uri attribute is set' do
input = <<~'EOS'
:data-uri:
:imagesdir: fixtures
image::dot.gif[Dot]
EOS
doc = document_from_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_equal 'fixtures', doc.attributes['imagesdir']
output = doc.convert
assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1
end
test 'embeds base64-encoded data uri for image in classloader when data-uri attribute is set', if: jruby? do
require fixture_path 'assets.jar'
input = <<~'EOS'
:data-uri:
:imagesdir: uri:classloader:/images-in-jar
image::dot.gif[Dot]
EOS
doc = document_from_string input, safe: Asciidoctor::SafeMode::UNSAFE, attributes: { 'docdir' => testdir }
assert_equal 'uri:classloader:/images-in-jar', doc.attributes['imagesdir']
output = doc.convert
assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1
end
test 'embeds SVG image with image/svg+xml mimetype when file extension is .svg' do
input = <<~'EOS'
:imagesdir: fixtures
:data-uri:
image::circle.svg[Tiger,100]
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docdir' => testdir }
assert_xpath '//img[starts-with(@src,"data:image/svg+xml;base64,")]', output, 1
end
test 'embeds empty base64-encoded data uri for unreadable image when data-uri attribute is set' do
input = <<~'EOS'
:data-uri:
:imagesdir: fixtures
image::unreadable.gif[Dot]
EOS
doc = document_from_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_equal 'fixtures', doc.attributes['imagesdir']
output = doc.convert
assert_xpath '//img[@src="data:image/gif;base64,"]', output, 1
assert_message @logger, :WARN, '~image to embed not found or not readable'
end
test 'embeds base64-encoded data uri with application/octet-stream mimetype when file extension is missing' do
input = <<~'EOS'
:data-uri:
:imagesdir: fixtures
image::dot[Dot]
EOS
doc = document_from_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_equal 'fixtures', doc.attributes['imagesdir']
output = doc.convert
assert_xpath '//img[starts-with(@src,"data:application/octet-stream;base64,")]', output, 1
end
test 'embeds base64-encoded data uri for remote image when data-uri attribute is set' do
input = <<~EOS
:data-uri:
image::http://#{resolve_localhost}:9876/fixtures/dot.gif[Dot]
EOS
output = using_test_webserver do
convert_string_to_embedded input, safe: :safe, attributes: { 'allow-uri-read' => '' }
end
assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1
end
test 'embeds base64-encoded data uri for remote image when imagesdir is a URI and data-uri attribute is set' do
input = <<~EOS
:data-uri:
:imagesdir: http://#{resolve_localhost}:9876/fixtures
image::dot.gif[Dot]
EOS
output = using_test_webserver do
convert_string_to_embedded input, safe: :safe, attributes: { 'allow-uri-read' => '' }
end
assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1
end
test 'should cache remote image when allow-uri-read, cache-uri, and data-uri are set' do
begin
if OpenURI.respond_to? :cache_open_uri
OpenURI.singleton_class.send :remove_method, :open_uri
OpenURI.singleton_class.send :alias_method, :open_uri, :cache_open_uri
end
using_test_webserver do |base_url, thr|
image_url = %(#{base_url}/fixtures/dot.gif)
image_data_uri = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs='
attributes = { 'allow-uri-read' => '', 'cache-uri' => '', 'data-uri' => '' }
input = %(image::#{image_url}[Dot])
output = convert_string_to_embedded input, safe: :safe, attributes: attributes
assert defined? OpenURI::Cache
assert_xpath %(//img[@src="#{image_data_uri}"][@alt="Dot"]), output, 1
thr[:requests].clear
Dir.mktmpdir do |cache_path|
original_cache_path = OpenURI::Cache.cache_path
begin
OpenURI::Cache.cache_path = cache_path
assert_nil OpenURI::Cache.get image_url
2.times do
output = convert_string_to_embedded input, safe: :safe, attributes: attributes
refute_nil OpenURI::Cache.get image_url
assert_xpath %(//img[@src="#{image_data_uri}"][@alt="Dot"]), output, 1
end
assert_equal 1, thr[:requests].size
assert_match %r/ \/fixtures\/dot\.gif /, thr[:requests][0], 1
ensure
OpenURI::Cache.cache_path = original_cache_path
end
end
end
ensure
OpenURI.singleton_class.send :alias_method, :cache_open_uri, :open_uri
OpenURI.singleton_class.send :remove_method, :open_uri
OpenURI.singleton_class.send :alias_method, :open_uri, :original_open_uri
end
end
test 'uses remote image uri when data-uri attribute is set and image cannot be retrieved' do
image_uri = "http://#{resolve_localhost}:9876/fixtures/missing-image.gif"
input = <<~EOS
:data-uri:
image::#{image_uri}[Missing image]
EOS
output = using_test_webserver do
convert_string_to_embedded input, safe: :safe, attributes: { 'allow-uri-read' => '' }
end
assert_xpath %(/*[@class="imageblock"]//img[@src="#{image_uri}"][@alt="Missing image"]), output, 1
assert_message @logger, :WARN, '~could not retrieve image data from URI'
end
test 'uses remote image uri when data-uri attribute is set and allow-uri-read is not set' do
image_uri = "http://#{resolve_localhost}:9876/fixtures/dot.gif"
input = <<~EOS
:data-uri:
image::#{image_uri}[Dot]
EOS
output = using_test_webserver do
convert_string_to_embedded input, safe: :safe
end
assert_xpath %(/*[@class="imageblock"]//img[@src="#{image_uri}"][@alt="Dot"]), output, 1
end
"##
);
#[test]
fn can_handle_embedded_data_uri_images() {
verifies!(
r#"
test 'can handle embedded data uri images' do
input = 'image::data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=[Dot]'
output = convert_string_to_embedded input
assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"image::data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=[Dot]",
);
assert_xpath(
&doc,
"//img[@src=\"data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=\"][@alt=\"Dot\"]",
1,
);
}
#[test]
fn can_handle_embedded_data_uri_images_when_data_uri_attribute_is_set() {
verifies!(
r#"
test 'can handle embedded data uri images when data-uri attribute is set' do
input = <<~'EOS'
:data-uri:
image::data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=[Dot]
EOS
output = convert_string_to_embedded input
assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":data-uri:\n\nimage::data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=[Dot]\n",
);
assert_xpath(
&doc,
"//img[@src=\"data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=\"][@alt=\"Dot\"]",
1,
);
}
// Safe-mode jail / ancestor-directory cleaning during data-uri file reads is
// out of scope.
non_normative!(
r#"
test 'cleans reference to ancestor directories in imagesdir before reading image if safe mode level is at least SAFE' do
input = <<~'EOS'
:data-uri:
:imagesdir: ../..//fixtures/./../../fixtures
image::dot.gif[Dot]
EOS
doc = document_from_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_equal '../..//fixtures/./../../fixtures', doc.attributes['imagesdir']
output = doc.convert
assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1
assert_message @logger, :WARN, 'image has illegal reference to ancestor of jail; recovering automatically'
end
test 'cleans reference to ancestor directories in target before reading image if safe mode level is at least SAFE' do
input = <<~'EOS'
:data-uri:
:imagesdir: ./
image::../..//fixtures/./../../fixtures/dot.gif[Dot]
EOS
doc = document_from_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_equal './', doc.attributes['imagesdir']
output = doc.convert
assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1
assert_message @logger, :WARN, 'image has illegal reference to ancestor of jail; recovering automatically'
end
"#
);
non_normative!(
r#"
end
"#
);
}
mod media {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Media' do
"#
);
#[test]
fn should_detect_and_convert_video_macro() {
verifies!(
r#"
test 'should detect and convert video macro' do
input = 'video::cats-vs-dogs.avi[]'
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_css 'video[src="cats-vs-dogs.avi"]', output, 1
end
"#
);
let doc = Parser::default().parse("video::cats-vs-dogs.avi[]");
assert_css(&doc, "video", 1);
assert_css(&doc, "video[src=\"cats-vs-dogs.avi\"]", 1);
}
#[test]
fn should_detect_and_convert_video_macro_with_positional_attributes_for_poster_and_dimensions()
{
verifies!(
r#"
test 'should detect and convert video macro with positional attributes for poster and dimensions' do
input = 'video::cats-vs-dogs.avi[cats-and-dogs.png, 200, 300]'
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_css 'video[src="cats-vs-dogs.avi"]', output, 1
assert_css 'video[poster="cats-and-dogs.png"]', output, 1
assert_css 'video[width="200"]', output, 1
assert_css 'video[height="300"]', output, 1
end
"#
);
let doc = Parser::default().parse("video::cats-vs-dogs.avi[cats-and-dogs.png, 200, 300]");
assert_css(&doc, "video", 1);
assert_css(&doc, "video[src=\"cats-vs-dogs.avi\"]", 1);
assert_css(&doc, "video[poster=\"cats-and-dogs.png\"]", 1);
assert_css(&doc, "video[width=\"200\"]", 1);
assert_css(&doc, "video[height=\"300\"]", 1);
}
#[test]
fn should_set_direction_css_class_on_video_block_if_float_attribute_is_set() {
verifies!(
r#"
test 'should set direction CSS class on video block if float attribute is set' do
input = 'video::cats-vs-dogs.avi[cats-and-dogs.png,float=right]'
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_css 'video[src="cats-vs-dogs.avi"]', output, 1
assert_css '.videoblock.right', output, 1
end
"#
);
let doc = Parser::default().parse("video::cats-vs-dogs.avi[cats-and-dogs.png,float=right]");
assert_css(&doc, "video", 1);
assert_css(&doc, "video[src=\"cats-vs-dogs.avi\"]", 1);
assert_css(&doc, ".videoblock.right", 1);
}
#[test]
fn should_set_text_alignment_css_class_on_video_block_if_align_attribute_is_set() {
verifies!(
r#"
test 'should set text alignment CSS class on video block if align attribute is set' do
input = 'video::cats-vs-dogs.avi[cats-and-dogs.png,align=center]'
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_css 'video[src="cats-vs-dogs.avi"]', output, 1
assert_css '.videoblock.text-center', output, 1
end
"#
);
let doc =
Parser::default().parse("video::cats-vs-dogs.avi[cats-and-dogs.png,align=center]");
assert_css(&doc, "video", 1);
assert_css(&doc, "video[src=\"cats-vs-dogs.avi\"]", 1);
assert_css(&doc, ".videoblock.text-center", 1);
}
#[test]
fn video_macro_should_honor_all_options() {
verifies!(
r#"
test 'video macro should honor all options' do
input = 'video::cats-vs-dogs.avi[options="autoplay,muted,nocontrols,loop",preload="metadata"]'
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_css 'video[autoplay]', output, 1
assert_css 'video[muted]', output, 1
assert_css 'video:not([controls])', output, 1
assert_css 'video[loop]', output, 1
assert_css 'video[preload=metadata]', output, 1
end
"#
);
let doc = Parser::default()
.parse("video::cats-vs-dogs.avi[options=\"autoplay,muted,nocontrols,loop\",preload=\"metadata\"]");
assert_css(&doc, "video", 1);
assert_css(&doc, "video[autoplay]", 1);
assert_css(&doc, "video[muted]", 1);
assert_css(&doc, "video:not([controls])", 1);
assert_css(&doc, "video[loop]", 1);
assert_css(&doc, "video[preload=metadata]", 1);
}
#[test]
fn video_macro_should_add_time_range_anchor_with_start_time_if_start_attribute_is_set() {
verifies!(
r#"
test 'video macro should add time range anchor with start time if start attribute is set' do
input = 'video::cats-vs-dogs.avi[start="30"]'
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_xpath '//video[@src="cats-vs-dogs.avi#t=30"]', output, 1
end
"#
);
let doc = Parser::default().parse("video::cats-vs-dogs.avi[start=\"30\"]");
assert_css(&doc, "video", 1);
assert_xpath(&doc, "//video[@src=\"cats-vs-dogs.avi#t=30\"]", 1);
}
#[test]
fn video_macro_should_add_time_range_anchor_with_end_time_if_end_attribute_is_set() {
verifies!(
r#"
test 'video macro should add time range anchor with end time if end attribute is set' do
input = 'video::cats-vs-dogs.avi[end="30"]'
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_xpath '//video[@src="cats-vs-dogs.avi#t=,30"]', output, 1
end
"#
);
let doc = Parser::default().parse("video::cats-vs-dogs.avi[end=\"30\"]");
assert_css(&doc, "video", 1);
assert_xpath(&doc, "//video[@src=\"cats-vs-dogs.avi#t=,30\"]", 1);
}
#[test]
fn video_macro_should_add_time_range_anchor_with_start_and_end_time_if_start_and_end_attributes_are_set()
{
verifies!(
r#"
test 'video macro should add time range anchor with start and end time if start and end attributes are set' do
input = 'video::cats-vs-dogs.avi[start="30",end="60"]'
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_xpath '//video[@src="cats-vs-dogs.avi#t=30,60"]', output, 1
end
"#
);
let doc = Parser::default().parse("video::cats-vs-dogs.avi[start=\"30\",end=\"60\"]");
assert_css(&doc, "video", 1);
assert_xpath(&doc, "//video[@src=\"cats-vs-dogs.avi#t=30,60\"]", 1);
}
// NOTE: divergence from Asciidoctor. This crate's context-free rendering
// does not resolve the video target/poster relative to `imagesdir`. Kept
// `#[ignore]`d with the Ruby-intended (imagesdir-prefixed) attributes.
// TODO: resolve video target and poster relative to `imagesdir`.
#[ignore]
#[test]
fn video_macro_should_use_imagesdir_attribute_to_resolve_target_and_poster() {
verifies!(
r#"
test 'video macro should use imagesdir attribute to resolve target and poster' do
input = <<~'EOS'
:imagesdir: assets
video::cats-vs-dogs.avi[cats-and-dogs.png, 200, 300]
EOS
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_css 'video[src="assets/cats-vs-dogs.avi"]', output, 1
assert_css 'video[poster="assets/cats-and-dogs.png"]', output, 1
assert_css 'video[width="200"]', output, 1
assert_css 'video[height="300"]', output, 1
end
"#
);
let doc = Parser::default()
.parse(":imagesdir: assets\n\nvideo::cats-vs-dogs.avi[cats-and-dogs.png, 200, 300]\n");
assert_css(&doc, "video[src=\"assets/cats-vs-dogs.avi\"]", 1);
assert_css(&doc, "video[poster=\"assets/cats-and-dogs.png\"]", 1);
}
#[test]
fn video_macro_should_not_use_imagesdir_attribute_to_resolve_target_if_target_is_a_url() {
verifies!(
r#"
test 'video macro should not use imagesdir attribute to resolve target if target is a URL' do
input = <<~'EOS'
:imagesdir: assets
video::http://example.org/videos/cats-vs-dogs.avi[]
EOS
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_css 'video[src="http://example.org/videos/cats-vs-dogs.avi"]', output, 1
end
"#
);
let doc = Parser::default()
.parse(":imagesdir: assets\n\nvideo::http://example.org/videos/cats-vs-dogs.avi[]\n");
assert_css(&doc, "video", 1);
// The crate's CSS engine does not parse an attribute selector whose
// value is a URL; the equivalent xpath is used instead.
assert_xpath(
&doc,
"//video[@src=\"http://example.org/videos/cats-vs-dogs.avi\"]",
1,
);
}
// The vimeo/youtube service video tests below require rendering a custom
// `<iframe>` embed (with a service-specific URL and query string) instead of
// a `<video>` element. This crate does not model service video embedding, so
// they are kept `#[ignore]`d with the Ruby-intended assertions.
// TODO: render vimeo/youtube service videos as `<iframe>` embeds.
#[ignore]
#[test]
fn video_macro_should_output_custom_html_with_iframe_for_vimeo_service() {
verifies!(
r#"
test 'video macro should output custom HTML with iframe for vimeo service' do
input = 'video::67480300[vimeo, 400, 300, start=60, options="autoplay,muted"]'
output = convert_string_to_embedded input
assert_css 'video', output, 0
assert_css 'iframe', output, 1
assert_css 'iframe[src="https://player.vimeo.com/video/67480300?autoplay=1&muted=1#at=60"]', output, 1
assert_css 'iframe[width="400"]', output, 1
assert_css 'iframe[height="300"]', output, 1
end
"#
);
let doc = Parser::default()
.parse("video::67480300[vimeo, 400, 300, start=60, options=\"autoplay,muted\"]");
assert_css(&doc, "video", 0);
assert_css(&doc, "iframe", 1);
}
#[ignore]
#[test]
fn video_macro_should_allow_hash_for_vimeo_video_to_be_specified_in_video_id() {
verifies!(
r#"
test 'video macro should allow hash for vimeo video to be specified in video ID' do
input = 'video::67480300/123456789[vimeo, 400, 300, options=loop]'
output = convert_string_to_embedded input
assert_css 'video', output, 0
assert_css 'iframe', output, 1
assert_css 'iframe[src="https://player.vimeo.com/video/67480300?h=123456789&loop=1"]', output, 1
assert_css 'iframe[width="400"]', output, 1
assert_css 'iframe[height="300"]', output, 1
end
"#
);
let doc =
Parser::default().parse("video::67480300/123456789[vimeo, 400, 300, options=loop]");
assert_css(&doc, "iframe", 1);
}
#[ignore]
#[test]
fn video_macro_should_allow_hash_for_vimeo_video_to_be_specified_using_hash_attribute() {
verifies!(
r#"
test 'video macro should allow hash for vimeo video to be specified using hash attribute' do
input = 'video::67480300[vimeo, 400, 300, options=loop, hash=123456789]'
output = convert_string_to_embedded input
assert_css 'video', output, 0
assert_css 'iframe', output, 1
assert_css 'iframe[src="https://player.vimeo.com/video/67480300?h=123456789&loop=1"]', output, 1
assert_css 'iframe[width="400"]', output, 1
assert_css 'iframe[height="300"]', output, 1
end
"#
);
let doc = Parser::default()
.parse("video::67480300[vimeo, 400, 300, options=loop, hash=123456789]");
assert_css(&doc, "iframe", 1);
}
#[ignore]
#[test]
fn video_macro_should_output_custom_html_with_iframe_for_youtube_service() {
verifies!(
r#"
test 'video macro should output custom HTML with iframe for youtube service' do
input = 'video::U8GBXvdmHT4/PLg7s6cbtAD15Das5LK9mXt_g59DLWxKUe[youtube, 640, 360, start=60, options="autoplay,muted,modest", theme=light]'
output = convert_string_to_embedded input
assert_css 'video', output, 0
assert_css 'iframe', output, 1
assert_css 'iframe[src="https://www.youtube.com/embed/U8GBXvdmHT4?rel=0&start=60&autoplay=1&mute=1&list=PLg7s6cbtAD15Das5LK9mXt_g59DLWxKUe&modestbranding=1&theme=light"]', output, 1
assert_css 'iframe[width="640"]', output, 1
assert_css 'iframe[height="360"]', output, 1
end
"#
);
let doc = Parser::default().parse(
"video::U8GBXvdmHT4/PLg7s6cbtAD15Das5LK9mXt_g59DLWxKUe[youtube, 640, 360, start=60, options=\"autoplay,muted,modest\", theme=light]",
);
assert_css(&doc, "iframe", 1);
}
#[ignore]
#[test]
fn video_macro_should_output_custom_html_with_iframe_for_youtube_service_with_dynamic_playlist()
{
verifies!(
r#"
test 'video macro should output custom HTML with iframe for youtube service with dynamic playlist' do
input = 'video::SCZF6I-Rc4I,AsKGOeonbIs,HwrPhOp6-aM[youtube, 640, 360, start=60, options=autoplay]'
output = convert_string_to_embedded input
assert_css 'video', output, 0
assert_css 'iframe', output, 1
assert_css 'iframe[src="https://www.youtube.com/embed/SCZF6I-Rc4I?rel=0&start=60&autoplay=1&playlist=SCZF6I-Rc4I,AsKGOeonbIs,HwrPhOp6-aM"]', output, 1
assert_css 'iframe[width="640"]', output, 1
assert_css 'iframe[height="360"]', output, 1
end
"#
);
let doc = Parser::default()
.parse("video::SCZF6I-Rc4I,AsKGOeonbIs,HwrPhOp6-aM[youtube, 640, 360, start=60, options=autoplay]");
assert_css(&doc, "iframe", 1);
}
#[test]
fn should_detect_and_convert_audio_macro() {
verifies!(
r#"
test 'should detect and convert audio macro' do
input = 'audio::podcast.mp3[]'
output = convert_string_to_embedded input
assert_css 'audio', output, 1
assert_css 'audio[src="podcast.mp3"]', output, 1
end
"#
);
let doc = Parser::default().parse("audio::podcast.mp3[]");
assert_css(&doc, "audio", 1);
assert_css(&doc, "audio[src=\"podcast.mp3\"]", 1);
}
// NOTE: divergence from Asciidoctor (see
// `video_macro_should_use_imagesdir_attribute_to_resolve_target_and_poster`):
// the audio target is not resolved relative to `imagesdir`.
// TODO: resolve audio target relative to `imagesdir`.
#[ignore]
#[test]
fn audio_macro_should_use_imagesdir_attribute_to_resolve_target() {
verifies!(
r#"
test 'audio macro should use imagesdir attribute to resolve target' do
input = <<~'EOS'
:imagesdir: assets
audio::podcast.mp3[]
EOS
output = convert_string_to_embedded input
assert_css 'audio', output, 1
assert_css 'audio[src="assets/podcast.mp3"]', output, 1
end
"#
);
let doc = Parser::default().parse(":imagesdir: assets\n\naudio::podcast.mp3[]\n");
assert_css(&doc, "audio[src=\"assets/podcast.mp3\"]", 1);
}
#[test]
fn audio_macro_should_not_use_imagesdir_attribute_to_resolve_target_if_target_is_a_url() {
verifies!(
r#"
test 'audio macro should not use imagesdir attribute to resolve target if target is a URL' do
input = <<~'EOS'
:imagesdir: assets
video::http://example.org/podcast.mp3[]
EOS
output = convert_string_to_embedded input
assert_css 'video', output, 1
assert_css 'video[src="http://example.org/podcast.mp3"]', output, 1
end
"#
);
let doc = Parser::default()
.parse(":imagesdir: assets\n\nvideo::http://example.org/podcast.mp3[]\n");
assert_css(&doc, "video", 1);
assert_xpath(&doc, "//video[@src=\"http://example.org/podcast.mp3\"]", 1);
}
#[test]
fn audio_macro_should_honor_all_options() {
verifies!(
r#"
test 'audio macro should honor all options' do
input = 'audio::podcast.mp3[options="autoplay,nocontrols,loop"]'
output = convert_string_to_embedded input
assert_css 'audio', output, 1
assert_css 'audio[autoplay]', output, 1
assert_css 'audio:not([controls])', output, 1
assert_css 'audio[loop]', output, 1
end
"#
);
let doc =
Parser::default().parse("audio::podcast.mp3[options=\"autoplay,nocontrols,loop\"]");
assert_css(&doc, "audio", 1);
assert_css(&doc, "audio[autoplay]", 1);
assert_css(&doc, "audio:not([controls])", 1);
assert_css(&doc, "audio[loop]", 1);
}
#[test]
fn audio_macro_should_support_start_and_end_time() {
verifies!(
r#"
test 'audio macro should support start and end time' do
input = 'audio::podcast.mp3[start=1,end=2]'
output = convert_string_to_embedded input
assert_css 'audio', output, 1
assert_css 'audio[controls]', output, 1
assert_css 'audio[src="podcast.mp3#t=1,2"]', output, 1
end
"#
);
let doc = Parser::default().parse("audio::podcast.mp3[start=1,end=2]");
assert_css(&doc, "audio", 1);
assert_css(&doc, "audio[controls]", 1);
assert_css(&doc, "audio[src=\"podcast.mp3#t=1,2\"]", 1);
}
non_normative!(
r#"
end
"#
);
}
mod admonition_icons {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Admonition icons' do
"#
);
// NOTE: divergence from Asciidoctor pervasive to the image-based icon tests
// below. This crate renders font-based admonition icons (`:icons: font` →
// `<i class="fa icon-...">`) but does not render image-based icons
// (`:icons:` / `:icons: image` → `<img src=".../tip.png">`); it emits the
// admonition label as a `.icon > .title` instead. The image-icon tests are
// kept `#[ignore]`d with the Ruby-intended assertions; the ones that also
// require reading icon files (data-uri) or emitting document-head asset
// links are reproduced as `non_normative`.
// TODO: render image-based admonition icons.
#[ignore]
#[test]
fn can_resolve_icon_relative_to_default_iconsdir() {
verifies!(
r#"
test 'can resolve icon relative to default iconsdir' do
input = <<~'EOS'
:icons:
[TIP]
You can use icons for admonitions by setting the 'icons' attribute.
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="./images/icons/tip.png"][@alt="Tip"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":icons:\n\n[TIP]\nYou can use icons for admonitions by setting the 'icons' attribute.\n",
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/img[@src=\"./images/icons/tip.png\"][@alt=\"Tip\"]",
1,
);
}
// TODO: render image-based admonition icons with a custom iconsdir.
#[ignore]
#[test]
fn can_resolve_icon_relative_to_custom_iconsdir() {
verifies!(
r#"
test 'can resolve icon relative to custom iconsdir' do
input = <<~'EOS'
:icons:
:iconsdir: icons
[TIP]
You can use icons for admonitions by setting the 'icons' attribute.
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="icons/tip.png"][@alt="Tip"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":icons:\n:iconsdir: icons\n\n[TIP]\nYou can use icons for admonitions by setting the 'icons' attribute.\n",
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/img[@src=\"icons/tip.png\"][@alt=\"Tip\"]",
1,
);
}
// TODO: render image-based admonition icons.
#[ignore]
#[test]
fn should_add_file_extension_to_custom_icon_if_not_specified() {
verifies!(
r#"
test 'should add file extension to custom icon if not specified' do
input = <<~'EOS'
:icons: font
:iconsdir: images/icons
[TIP,icon=a]
Override the icon of an admonition block using an attribute
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="images/icons/a.png"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":icons: font\n:iconsdir: images/icons\n\n[TIP,icon=a]\nOverride the icon of an admonition block using an attribute\n",
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/img[@src=\"images/icons/a.png\"]",
1,
);
}
// TODO: render image-based admonition icons (icontype variations).
#[ignore]
#[test]
fn should_allow_icontype_to_be_specified_when_using_built_in_admonition_icon() {
verifies!(
r##"
test 'should allow icontype to be specified when using built-in admonition icon' do
input = 'TIP: Set the icontype using either the icontype attribute on the icons attribute.'
[
{ 'icons' => '', 'ext' => 'png' },
{ 'icons' => '', 'icontype' => 'jpg', 'ext' => 'jpg' },
{ 'icons' => 'jpg', 'ext' => 'jpg' },
{ 'icons' => 'image', 'ext' => 'png' },
].each do |attributes|
expected_src = %(./images/icons/tip.#{attributes.delete 'ext'})
output = convert_string input, attributes: attributes
assert_xpath %(//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="#{expected_src}"]), output, 1
end
end
"##
);
let doc = Parser::default()
.with_intrinsic_attribute("icons", "", crate::parser::ModificationContext::ApiOnly)
.parse(
"TIP: Set the icontype using either the icontype attribute on the icons attribute.",
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/img[@src=\"./images/icons/tip.png\"]",
1,
);
}
// TODO: render image-based admonition icons (custom icon, icontype variations).
#[ignore]
#[test]
fn should_allow_icontype_to_be_specified_when_using_custom_admonition_icon() {
verifies!(
r##"
test 'should allow icontype to be specified when using custom admonition icon' do
input = <<~'EOS'
[TIP,icon=hint]
Set the icontype using either the icontype attribute on the icons attribute.
EOS
[
{ 'icons' => '', 'ext' => 'png' },
{ 'icons' => '', 'icontype' => 'jpg', 'ext' => 'jpg' },
{ 'icons' => 'jpg', 'ext' => 'jpg' },
{ 'icons' => 'image', 'ext' => 'png' },
].each do |attributes|
expected_src = %(./images/icons/hint.#{attributes.delete 'ext'})
output = convert_string input, attributes: attributes
assert_xpath %(//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="#{expected_src}"]), output, 1
end
end
"##
);
let doc = Parser::default().with_intrinsic_attribute("icons", "", crate::parser::ModificationContext::ApiOnly).parse(
"[TIP,icon=hint]\nSet the icontype using either the icontype attribute on the icons attribute.\n",
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/img[@src=\"./images/icons/hint.png\"]",
1,
);
}
// The data-uri icon tests require reading icon files from disk and
// base64-encoding them; out of scope for this crate.
non_normative!(
r#"
test 'embeds base64-encoded data uri of icon when data-uri attribute is set and safe mode level is less than SECURE' do
input = <<~'EOS'
:icons:
:iconsdir: fixtures
:icontype: gif
:data-uri:
[TIP]
You can use icons for admonitions by setting the 'icons' attribute.
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Tip"]', output, 1
end
test 'should embed base64-encoded data uri of custom icon when data-uri attribute is set' do
input = <<~'EOS'
:icons:
:iconsdir: fixtures
:icontype: gif
:data-uri:
[TIP,icon=tip]
You can set a custom icon using the icon attribute on the block.
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Tip"]', output, 1
end
"#
);
// TODO: render image-based admonition icons.
#[ignore]
#[test]
fn does_not_embed_base64_encoded_data_uri_of_icon_when_safe_mode_level_is_secure_or_greater() {
verifies!(
r#"
test 'does not embed base64-encoded data uri of icon when safe mode level is SECURE or greater' do
input = <<~'EOS'
:icons:
:iconsdir: fixtures
:icontype: gif
:data-uri:
[TIP]
You can use icons for admonitions by setting the 'icons' attribute.
EOS
output = convert_string input, attributes: { 'icons' => '' }
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="fixtures/tip.gif"][@alt="Tip"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":icons:\n:iconsdir: fixtures\n:icontype: gif\n:data-uri:\n\n[TIP]\nYou can use icons for admonitions by setting the 'icons' attribute.\n",
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/img[@src=\"fixtures/tip.gif\"][@alt=\"Tip\"]",
1,
);
}
// Safe-mode ancestor-directory cleaning during icon data-uri reads is out of
// scope.
non_normative!(
r#"
test 'cleans reference to ancestor directories before reading icon if safe mode level is at least SAFE' do
input = <<~'EOS'
:icons:
:iconsdir: ../fixtures
:icontype: gif
:data-uri:
[TIP]
You can use icons for admonitions by setting the 'icons' attribute.
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Tip"]', output, 1
assert_message @logger, :WARN, 'image has illegal reference to ancestor of jail; recovering automatically'
end
"#
);
#[test]
fn should_import_font_awesome_and_use_font_based_icons_when_value_of_icons_attribute_is_font() {
verifies!(
r##"
test 'should import Font Awesome and use font-based icons when value of icons attribute is font' do
input = <<~'EOS'
:icons: font
[TIP]
You can use icons for admonitions by setting the 'icons' attribute.
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SERVER
assert_css %(html > head > link[rel="stylesheet"][href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/#{Asciidoctor::FONT_AWESOME_VERSION}/css/font-awesome.min.css"]), output, 1
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/i[@class="fa icon-tip"]', output, 1
end
"##
);
// This crate renders the font-based icon as `<i class="fa icon-tip">`.
// The Font Awesome stylesheet `<link>` in the document head is a
// standalone-document concern this crate's DOM does not model.
let doc = Parser::default().parse(
":icons: font\n\n[TIP]\nYou can use icons for admonitions by setting the 'icons' attribute.\n",
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/i[@class=\"fa icon-tip\"]",
1,
);
}
// NOTE: divergence from Asciidoctor. When `:icons: font` is set, this crate
// always renders the font icon; it does not fall back to an `<img>` when a
// custom image icon is specified on the block. Kept `#[ignore]`d.
// TODO: honor a custom image icon over a font icon.
#[ignore]
#[test]
fn font_based_icon_should_not_override_icon_specified_on_admonition() {
verifies!(
r#"
test 'font-based icon should not override icon specified on admonition' do
input = <<~'EOS'
:icons: font
:iconsdir: images/icons
[TIP,icon=a.png]
Override the icon of an admonition block using an attribute
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SERVER
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/i[@class="fa icon-tip"]', output, 0
assert_xpath '//*[@class="admonitionblock tip"]//*[@class="icon"]/img[@src="images/icons/a.png"]', output, 1
end
"#
);
let doc = Parser::default().parse(
":icons: font\n:iconsdir: images/icons\n\n[TIP,icon=a.png]\nOverride the icon of an admonition block using an attribute\n",
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/i[@class=\"fa icon-tip\"]",
0,
);
assert_xpath(
&doc,
"//*[@class=\"admonitionblock tip\"]//*[@class=\"icon\"]/img[@src=\"images/icons/a.png\"]",
1,
);
}
// The asset-uri-scheme tests assert document-head `<link>`/`<script>` CDN
// asset URLs (Font Awesome, highlight.js) in standalone output, which this
// crate's DOM does not model.
non_normative!(
r##"
test 'should use http uri scheme for assets when asset-uri-scheme is http' do
input = <<~'EOS'
:asset-uri-scheme: http
:icons: font
:source-highlighter: highlightjs
TIP: You can control the URI scheme used for assets with the asset-uri-scheme attribute
[source,ruby]
puts "AsciiDoc, FTW!"
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css %(html > head > link[rel="stylesheet"][href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/#{Asciidoctor::FONT_AWESOME_VERSION}/css/font-awesome.min.css"]), output, 1
assert_css %(html > body > script[src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/#{Asciidoctor::HIGHLIGHT_JS_VERSION}/highlight.min.js"]), output, 1
end
test 'should use no uri scheme for assets when asset-uri-scheme is blank' do
input = <<~'EOS'
:asset-uri-scheme:
:icons: font
:source-highlighter: highlightjs
TIP: You can control the URI scheme used for assets with the asset-uri-scheme attribute
[source,ruby]
puts "AsciiDoc, FTW!"
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css %(html > head > link[rel="stylesheet"][href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/#{Asciidoctor::FONT_AWESOME_VERSION}/css/font-awesome.min.css"]), output, 1
assert_css %(html > body > script[src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/#{Asciidoctor::HIGHLIGHT_JS_VERSION}/highlight.min.js"]), output, 1
end
"##
);
non_normative!(
r#"
end
"#
);
}
mod image_paths {
use crate::tests::prelude::*;
// Both Image-paths tests exercise Ruby's `Block#normalize_asset_path`, a
// safe-mode filesystem path-jailing API with no equivalent in this crate.
non_normative!(
r##"
context 'Image paths' do
test 'restricts access to ancestor directories when safe mode level is at least SAFE' do
input = 'image::asciidoctor.png[Asciidoctor]'
basedir = testdir
block = block_from_string input, attributes: { 'docdir' => basedir }
doc = block.document
assert doc.safe >= Asciidoctor::SafeMode::SAFE
assert_equal File.join(basedir, 'images'), block.normalize_asset_path('images')
assert_equal File.join(basedir, 'etc/images'), block.normalize_asset_path("#{disk_root}etc/images")
assert_equal File.join(basedir, 'images'), block.normalize_asset_path('../../images')
end
test 'does not restrict access to ancestor directories when safe mode is disabled' do
input = 'image::asciidoctor.png[Asciidoctor]'
basedir = testdir
block = block_from_string input, safe: Asciidoctor::SafeMode::UNSAFE, attributes: { 'docdir' => basedir }
doc = block.document
assert doc.safe == Asciidoctor::SafeMode::UNSAFE
assert_equal File.join(basedir, 'images'), block.normalize_asset_path('images')
absolute_path = "#{disk_root}etc/images"
assert_equal absolute_path, block.normalize_asset_path(absolute_path)
assert_equal File.expand_path(File.join(basedir, '../../images')), block.normalize_asset_path('../../images')
end
end
"##
);
}
mod source_code {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Source code' do
"#
);
// NOTE: divergence from Asciidoctor. A fenced code block with no language is
// not given the `source` style by this crate (its `declared_style()` is
// `None`), so no `<code>` element is rendered. Kept `#[ignore]`d with the
// Ruby-intended assertions.
// TODO: assign the `source` style to a language-less fenced code block.
#[ignore]
#[test]
fn should_support_fenced_code_block_using_backticks() {
verifies!(
r##"
test 'should support fenced code block using backticks' do
input = <<~'EOS'
```
puts "Hello, World!"
```
EOS
block = block_from_string input
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :fenced_code, (block.attr 'cloaked-context')
assert_nil (block.attr 'language')
output = convert_string_to_embedded input
assert_css '.listingblock', output, 1
assert_css '.listingblock pre code', output, 1
assert_css '.listingblock pre code:not([class])', output, 1
end
"##
);
let doc = Parser::default().parse("```\nputs \"Hello, World!\"\n```\n");
assert_css(&doc, ".listingblock", 1);
assert_css(&doc, ".listingblock pre code", 1);
assert_css(&doc, ".listingblock pre code:not([class])", 1);
}
#[test]
fn should_not_recognize_fenced_code_blocks_with_more_than_three_delimiters() {
verifies!(
r##"
test 'should not recognize fenced code blocks with more than three delimiters' do
input = <<~'EOS'
````ruby
puts "Hello, World!"
````
~~~~ javascript
alert("Hello, World!")
~~~~
EOS
output = convert_string_to_embedded input
assert_css '.listingblock', output, 0
end
"##
);
let doc = Parser::default()
.parse("````ruby\nputs \"Hello, World!\"\n````\n\n~~~~ javascript\nalert(\"Hello, World!\")\n~~~~\n");
assert_css(&doc, ".listingblock", 0);
}
#[test]
fn should_support_fenced_code_blocks_with_languages() {
verifies!(
r##"
test 'should support fenced code blocks with languages' do
input = <<~'EOS'
```ruby
puts "Hello, World!"
```
``` javascript
alert("Hello, World!")
```
EOS
block = (document_from_string input).blocks[0]
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :fenced_code, (block.attr 'cloaked-context')
assert_equal 'ruby', (block.attr 'language')
output = convert_string_to_embedded input
assert_css '.listingblock', output, 2
assert_css '.listingblock pre code.language-ruby[data-lang=ruby]', output, 1
assert_css '.listingblock pre code.language-javascript[data-lang=javascript]', output, 1
end
"##
);
let doc = Parser::default()
.parse("```ruby\nputs \"Hello, World!\"\n```\n\n``` javascript\nalert(\"Hello, World!\")\n```\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.raw_context().as_ref(), "listing");
assert_eq!(block.declared_style(), Some("source"));
assert_css(&doc, ".listingblock", 2);
assert_css(
&doc,
".listingblock pre code.language-ruby[data-lang=ruby]",
1,
);
assert_css(
&doc,
".listingblock pre code.language-javascript[data-lang=javascript]",
1,
);
}
// NOTE: divergence from Asciidoctor. This crate does not split a fenced
// code block's info string on the comma, so `ruby,numbered` becomes the
// language rather than language `ruby` plus a line-numbering option. Kept
// `#[ignore]`d with the Ruby-intended assertions.
// TODO: parse the fenced code info string into language and options.
#[ignore]
#[test]
fn should_support_fenced_code_blocks_with_languages_and_numbering() {
verifies!(
r##"
test 'should support fenced code blocks with languages and numbering' do
input = <<~'EOS'
```ruby,numbered
puts "Hello, World!"
```
``` javascript, numbered
alert("Hello, World!")
```
EOS
output = convert_string_to_embedded input
assert_css '.listingblock', output, 2
assert_css '.listingblock pre code.language-ruby[data-lang=ruby]', output, 1
assert_css '.listingblock pre code.language-javascript[data-lang=javascript]', output, 1
end
"##
);
let doc = Parser::default()
.parse("```ruby,numbered\nputs \"Hello, World!\"\n```\n\n``` javascript, numbered\nalert(\"Hello, World!\")\n```\n");
assert_css(&doc, ".listingblock", 2);
assert_css(
&doc,
".listingblock pre code.language-ruby[data-lang=ruby]",
1,
);
assert_css(
&doc,
".listingblock pre code.language-javascript[data-lang=javascript]",
1,
);
}
#[test]
fn should_allow_source_style_to_be_specified_on_literal_block() {
verifies!(
r#"
test 'should allow source style to be specified on literal block' do
input = <<~'EOS'
[source]
....
console.log('Hello, World!')
....
EOS
block = block_from_string input
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :literal, (block.attr 'cloaked-context')
assert_nil (block.attr 'language')
output = convert_string_to_embedded input
assert_css '.listingblock', output, 1
assert_css '.listingblock pre', output, 1
assert_css '.listingblock pre code', output, 1
assert_css '.listingblock pre code[data-lang]', output, 0
end
"#
);
let doc = Parser::default().parse("[source]\n....\nconsole.log('Hello, World!')\n....\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.declared_style(), Some("source"));
assert_css(&doc, ".listingblock", 1);
assert_css(&doc, ".listingblock pre", 1);
assert_css(&doc, ".listingblock pre code", 1);
assert_css(&doc, ".listingblock pre code[data-lang]", 0);
}
#[test]
fn should_allow_source_style_and_language_to_be_specified_on_literal_block() {
verifies!(
r#"
test 'should allow source style and language to be specified on literal block' do
input = <<~'EOS'
[source,js]
....
console.log('Hello, World!')
....
EOS
block = block_from_string input
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :literal, (block.attr 'cloaked-context')
assert_equal 'js', (block.attr 'language')
output = convert_string_to_embedded input
assert_css '.listingblock', output, 1
assert_css '.listingblock pre', output, 1
assert_css '.listingblock pre code', output, 1
assert_css '.listingblock pre code[data-lang]', output, 1
end
"#
);
let doc =
Parser::default().parse("[source,js]\n....\nconsole.log('Hello, World!')\n....\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.declared_style(), Some("source"));
assert_css(&doc, ".listingblock", 1);
assert_css(&doc, ".listingblock pre", 1);
assert_css(&doc, ".listingblock pre code", 1);
assert_css(&doc, ".listingblock pre code[data-lang]", 1);
}
non_normative!(
r#"
end
"#
);
}
mod abstract_and_part_intro {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'Abstract and Part Intro' do
"#
);
// NOTE: the `abstract` block style (#783) is modeled: an `[abstract]` open
// block (or paragraph) resolves to the `open` context with the `abstract`
// declared style, renders as `quoteblock.abstract`, and an abstract used as
// a direct child of a doctitle-less book document is excluded with a
// warning. The `partintro` block style is out of scope for this crate: it
// is meaningful only inside a book part, and the `book` doctype's part
// structure (like the DocBook backend it is most often paired with) is not
// supported here (see #794 and #800). All `partintro` tests are therefore
// reproduced as `non_normative`.
#[test]
fn should_make_abstract_on_open_block_without_title_a_quote_block_for_article() {
verifies!(
r#"
test 'should make abstract on open block without title a quote block for article' do
input = <<~'EOS'
= Article
[abstract]
--
This article is about stuff.
And other stuff.
--
== Section One
content
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock.abstract', output, 1
assert_css '#preamble .quoteblock', output, 1
assert_css '.quoteblock > blockquote', output, 1
assert_css '.quoteblock > blockquote > .paragraph', output, 2
end
"#
);
let doc = Parser::default().parse(
"= Article\n\n[abstract]\n--\nThis article is about stuff.\n\nAnd other stuff.\n--\n\n== Section One\n\ncontent\n",
);
assert_css(&doc, ".quoteblock.abstract", 1);
assert_css(&doc, ".quoteblock > blockquote > .paragraph", 2);
}
#[test]
fn should_make_abstract_on_open_block_with_title_a_quote_block_with_title_for_article() {
verifies!(
r#"
test 'should make abstract on open block with title a quote block with title for article' do
input = <<~'EOS'
= Article
.My abstract
[abstract]
--
This article is about stuff.
--
== Section One
content
EOS
output = convert_string input
assert_css '.quoteblock', output, 1
assert_css '.quoteblock.abstract', output, 1
assert_css '#preamble .quoteblock', output, 1
assert_css '.quoteblock > .title', output, 1
assert_css '.quoteblock > .title + blockquote', output, 1
assert_css '.quoteblock > .title + blockquote > .paragraph', output, 1
end
"#
);
let doc = Parser::default().parse(
"= Article\n\n.My abstract\n[abstract]\n--\nThis article is about stuff.\n--\n\n== Section One\n\ncontent\n",
);
assert_css(&doc, ".quoteblock.abstract", 1);
assert_css(&doc, ".quoteblock > .title", 1);
}
#[test]
fn should_allow_abstract_in_document_with_title_if_doctype_is_book() {
verifies!(
r#"
test 'should allow abstract in document with title if doctype is book' do
input = <<~'EOS'
= Book
:doctype: book
[abstract]
Abstract for book with title is valid
EOS
output = convert_string input
assert_css '.abstract', output, 1
end
"#
);
let doc = Parser::default()
.parse("= Book\n:doctype: book\n\n[abstract]\nAbstract for book with title is valid\n");
assert_css(&doc, ".abstract", 1);
assert!(doc.warnings().next().is_none());
}
#[test]
fn should_not_allow_abstract_as_direct_child_of_document_if_doctype_is_book() {
verifies!(
r#"
test 'should not allow abstract as direct child of document if doctype is book' do
input = <<~'EOS'
:doctype: book
[abstract]
Abstract for book without title is invalid.
EOS
output = convert_string input
assert_css '.abstract', output, 0
assert_message @logger, :WARN, 'abstract block cannot be used in a document without a doctitle when doctype is book. Excluding block content.'
end
"#
);
let doc = Parser::default()
.parse(":doctype: book\n\n[abstract]\nAbstract for book without title is invalid.\n");
assert_css(&doc, ".abstract", 0);
let warnings: Vec<_> = doc.warnings().collect();
assert_eq!(warnings.len(), 1);
assert!(matches!(
warnings[0].warning,
WarningType::AbstractBlockInBookWithoutDoctitle
));
}
// The DocBook abstract variants are backend-specific and out of scope.
non_normative!(
r#"
test 'should make abstract on open block without title converted to DocBook' do
input = <<~'EOS'
= Article
[abstract]
--
This article is about stuff.
And other stuff.
--
EOS
output = convert_string input, backend: 'docbook'
assert_css 'info > abstract', output, 1
assert_css 'info > abstract > simpara', output, 2
end
test 'should make abstract on open block with title converted to DocBook' do
input = <<~'EOS'
= Article
.My abstract
[abstract]
--
This article is about stuff.
--
EOS
output = convert_string input, backend: 'docbook'
assert_css 'info > abstract', output, 1
assert_css 'info > abstract > title', output, 1
assert_css 'info > abstract > title + simpara', output, 1
end
test 'should allow abstract in document with title if doctype is book converted to DocBook' do
input = <<~'EOS'
= Book
:doctype: book
[abstract]
Abstract for book with title is valid
EOS
output = convert_string input, backend: 'docbook'
assert_css 'info > abstract', output, 1
assert_css 'preface', output, 0
end
test 'should not allow abstract as direct child of document if doctype is book converted to DocBook' do
input = <<~'EOS'
:doctype: book
[abstract]
Abstract for book is invalid.
EOS
output = convert_string input, backend: 'docbook'
assert_css 'abstract', output, 0
assert_message @logger, :WARN, 'abstract block cannot be used in a document without a doctitle when doctype is book. Excluding block content.'
end
"#
);
// The `partintro` block style is out of scope for this crate: it is only
// meaningful as the first child of a book part, and neither the `book`
// doctype's part structure nor the DocBook backend that the remaining
// variants target is supported here (see #794 and #800).
non_normative!(
r##"
# TODO partintro shouldn't be recognized if doctype is not book, should be in proper place
test 'should accept partintro on open block without title' do
input = <<~'EOS'
= Book
:doctype: book
= Part 1
[partintro]
--
This is a part intro.
It can have multiple paragraphs.
--
== Chapter 1
content
EOS
output = convert_string input
assert_css '.openblock', output, 1
assert_css '.openblock.partintro', output, 1
assert_css '.openblock .title', output, 0
assert_css '.openblock .content', output, 1
assert_xpath %(//h1[@id="_part_1"]/following-sibling::*[#{contains_class(:openblock)}]), output, 1
assert_xpath %(//*[#{contains_class(:openblock)}]/*[@class="content"]/*[@class="paragraph"]), output, 2
end
test 'should accept partintro on open block with title' do
input = <<~'EOS'
= Book
:doctype: book
= Part 1
.Intro title
[partintro]
--
This is a part intro with a title.
--
== Chapter 1
content
EOS
output = convert_string input
assert_css '.openblock', output, 1
assert_css '.openblock.partintro', output, 1
assert_css '.openblock .title', output, 1
assert_css '.openblock .content', output, 1
assert_xpath %(//h1[@id="_part_1"]/following-sibling::*[#{contains_class(:openblock)}]), output, 1
assert_xpath %(//*[#{contains_class(:openblock)}]/*[@class="title"][text()="Intro title"]), output, 1
assert_xpath %(//*[#{contains_class(:openblock)}]/*[@class="content"]/*[@class="paragraph"]), output, 1
end
test 'should exclude partintro if not a child of part' do
input = <<~'EOS'
= Book
:doctype: book
[partintro]
part intro paragraph
EOS
output = convert_string input
assert_css '.partintro', output, 0
assert_message @logger, :ERROR, 'partintro block can only be used when doctype is book and must be a child of a book part. Excluding block content.'
end
test 'should not allow partintro unless doctype is book' do
input = <<~'EOS'
[partintro]
part intro paragraph
EOS
output = convert_string input
assert_css '.partintro', output, 0
assert_message @logger, :ERROR, 'partintro block can only be used when doctype is book and must be a child of a book part. Excluding block content.'
end
test 'should accept partintro on open block without title converted to DocBook' do
input = <<~'EOS'
= Book
:doctype: book
= Part 1
[partintro]
--
This is a part intro.
It can have multiple paragraphs.
--
== Chapter 1
content
EOS
output = convert_string input, backend: 'docbook'
assert_css 'partintro', output, 1
assert_css 'part[xml|id="_part_1"] > partintro', output, 1
assert_css 'partintro > simpara', output, 2
end
test 'should accept partintro on open block with title converted to DocBook' do
input = <<~'EOS'
= Book
:doctype: book
= Part 1
.Intro title
[partintro]
--
This is a part intro with a title.
--
== Chapter 1
content
EOS
output = convert_string input, backend: 'docbook'
assert_css 'partintro', output, 1
assert_css 'part[xml|id="_part_1"] > partintro', output, 1
assert_css 'partintro > title', output, 1
assert_css 'partintro > title + simpara', output, 1
end
test 'should exclude partintro if not a child of part converted to DocBook' do
input = <<~'EOS'
= Book
:doctype: book
[partintro]
part intro paragraph
EOS
output = convert_string input, backend: 'docbook'
assert_css 'partintro', output, 0
assert_message @logger, :ERROR, 'partintro block can only be used when doctype is book and must be a child of a book part. Excluding block content.'
end
test 'should not allow partintro unless doctype is book converted to DocBook' do
input = <<~'EOS'
[partintro]
part intro paragraph
EOS
output = convert_string input, backend: 'docbook'
assert_css 'partintro', output, 0
assert_message @logger, :ERROR, 'partintro block can only be used when doctype is book and must be a child of a book part. Excluding block content.'
end
"##
);
non_normative!(
r#"
end
"#
);
}
mod substitutions {
use crate::{content::SubstitutionStep, tests::prelude::*};
non_normative!(
r#"
context 'Substitutions' do
"#
);
#[test]
fn processor_should_not_crash_if_subs_are_empty() {
verifies!(
r#"
test 'processor should not crash if subs are empty' do
input = <<~'EOS'
[subs=","]
....
content
....
EOS
doc = document_from_string input
block = doc.blocks.first
assert_equal [], block.subs
end
"#
);
let doc = Parser::default().parse("[subs=\",\"]\n....\ncontent\n....\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(
block.substitution_group(),
SubstitutionGroup::Custom(vec![])
);
}
// NOTE: divergence from Asciidoctor. This crate's default verbatim
// substitution list includes `Callouts`, so appending to it yields
// `[specialcharacters, callouts, attributes, macros]` rather than Ruby's
// `[specialcharacters, attributes, macros]`. Kept `#[ignore]`d.
// TODO: reconcile the default verbatim subs list with Asciidoctor.
#[ignore]
#[test]
fn should_be_able_to_append_subs_to_default_block_substitution_list() {
verifies!(
r#"
test 'should be able to append subs to default block substitution list' do
input = <<~'EOS'
:application: Asciidoctor
[subs="+attributes,+macros"]
....
{application}
....
EOS
doc = document_from_string input
block = doc.blocks.first
assert_equal [:specialcharacters, :attributes, :macros], block.subs
end
"#
);
let doc = Parser::default()
.parse(":application: Asciidoctor\n\n[subs=\"+attributes,+macros\"]\n....\n{application}\n....\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(
block.substitution_group(),
SubstitutionGroup::Custom(vec![
SubstitutionStep::SpecialCharacters,
SubstitutionStep::AttributeReferences,
SubstitutionStep::Macros,
])
);
}
// NOTE: divergence from Asciidoctor (default verbatim subs include
// `Callouts`; see
// `should_be_able_to_append_subs_to_default_block_substitution_list`).
// TODO: reconcile the default verbatim subs list with Asciidoctor.
#[ignore]
#[test]
fn should_be_able_to_prepend_subs_to_default_block_substitution_list() {
verifies!(
r#"
test 'should be able to prepend subs to default block substitution list' do
input = <<~'EOS'
:application: Asciidoctor
[subs="attributes+"]
....
{application}
....
EOS
doc = document_from_string input
block = doc.blocks.first
assert_equal [:attributes, :specialcharacters], block.subs
end
"#
);
let doc = Parser::default().parse(
":application: Asciidoctor\n\n[subs=\"attributes+\"]\n....\n{application}\n....\n",
);
let block = doc.child_blocks().next().unwrap();
assert_eq!(
block.substitution_group(),
SubstitutionGroup::Custom(vec![
SubstitutionStep::AttributeReferences,
SubstitutionStep::SpecialCharacters,
])
);
}
#[test]
fn should_be_able_to_remove_subs_to_default_block_substitution_list() {
verifies!(
r#"
test 'should be able to remove subs to default block substitution list' do
input = <<~'EOS'
[subs="-quotes,-replacements"]
content
EOS
doc = document_from_string input
block = doc.blocks.first
assert_equal [:specialcharacters, :attributes, :macros, :post_replacements], block.subs
end
"#
);
let doc = Parser::default().parse("[subs=\"-quotes,-replacements\"]\ncontent\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(
block.substitution_group(),
SubstitutionGroup::Custom(vec![
SubstitutionStep::SpecialCharacters,
SubstitutionStep::AttributeReferences,
SubstitutionStep::Macros,
SubstitutionStep::PostReplacement,
])
);
}
// NOTE: divergence from Asciidoctor. The combined prepend/append/remove
// modifiers do not yield Asciidoctor's exact list here, and the `+macros`
// sub does not produce the expected inline link in the rendered output.
// Kept `#[ignore]`d with the Ruby-intended assertions.
// TODO: reconcile combined subs modifiers and verbatim macro substitution.
#[ignore]
#[test]
fn should_be_able_to_prepend_append_and_remove_subs_from_default_block_substitution_list() {
verifies!(
r##"
test 'should be able to prepend, append and remove subs from default block substitution list' do
input = <<~'EOS'
:application: asciidoctor
[subs="attributes+,-verbatim,+specialcharacters,+macros"]
....
https://{application}.org[{gt}{gt}] <1>
....
EOS
doc = document_from_string input, standalone: false
block = doc.blocks.first
assert_equal [:attributes, :specialcharacters, :macros], block.subs
result = doc.convert
assert_includes result, '<pre><a href="https://asciidoctor.org">>></a> <1></pre>'
end
"##
);
let doc = Parser::default().parse(
":application: asciidoctor\n\n[subs=\"attributes+,-verbatim,+specialcharacters,+macros\"]\n....\nhttps://{application}.org[{gt}{gt}] <1>\n....\n",
);
let block = doc.child_blocks().next().unwrap();
assert_eq!(
block.substitution_group(),
SubstitutionGroup::Custom(vec![
SubstitutionStep::AttributeReferences,
SubstitutionStep::SpecialCharacters,
SubstitutionStep::Macros,
])
);
assert_xpath(&doc, "//pre/a[@href=\"https://asciidoctor.org\"]", 1);
}
#[test]
fn should_be_able_to_set_subs_then_modify_them() {
verifies!(
r#"
test 'should be able to set subs then modify them' do
input = <<~'EOS'
[subs="verbatim,-callouts"]
_hey now_ <1>
EOS
doc = document_from_string input, standalone: false
block = doc.blocks.first
assert_equal [:specialcharacters], block.subs
result = doc.convert
assert_includes result, '_hey now_ <1>'
end
"#
);
let doc = Parser::default().parse("[subs=\"verbatim,-callouts\"]\n_hey now_ <1>\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(
block.substitution_group(),
SubstitutionGroup::Custom(vec![SubstitutionStep::SpecialCharacters])
);
// Quotes are not applied (`_hey now_` stays literal); specialcharacters
// escapes `<1>` (shown decoded as `<1>` in the DOM text).
assert_rendered_contains(&doc, "_hey now_ <1>");
}
non_normative!(
r#"
end
"#
);
}
mod references {
use crate::tests::prelude::*;
non_normative!(
r#"
context 'References' do
"#
);
#[test]
fn should_not_recognize_block_anchor_with_illegal_id_characters() {
verifies!(
r#"
test 'should not recognize block anchor with illegal id characters' do
input = <<~'EOS'
[[illegal$id,Reference Text]]
----
content
----
EOS
doc = document_from_string input
block = doc.blocks.first
assert_nil block.id
assert_nil(block.attr 'reftext')
refute doc.catalog[:refs].key? 'illegal$id'
end
"#
);
let doc = Parser::default().parse("[[illegal$id,Reference Text]]\n----\ncontent\n----\n");
let block = doc.child_blocks().next().unwrap();
assert_eq!(block.id(), None);
assert!(!doc.catalog().contains_id("illegal$id"));
}
#[test]
fn should_not_recognize_block_anchor_that_starts_with_digit() {
verifies!(
r#"
test 'should not recognize block anchor that starts with digit' do
input = <<~'EOS'
[[3-blind-mice]]
--
see how they run
--
EOS
output = convert_string_to_embedded input
assert_includes output, '[[3-blind-mice]]'
assert_xpath '/*[@id=":3-blind-mice"]', output, 0
end
"#
);
let doc = Parser::default().parse("[[3-blind-mice]]\n--\nsee how they run\n--\n");
assert_rendered_contains(&doc, "[[3-blind-mice]]");
assert_xpath(&doc, "/*[@id=\":3-blind-mice\"]", 0);
}
#[test]
fn should_recognize_block_anchor_that_starts_with_colon() {
verifies!(
r#"
test 'should recognize block anchor that starts with colon' do
input = <<~'EOS'
[[:idname]]
--
content
--
EOS
output = convert_string_to_embedded input
assert_xpath '/*[@id=":idname"]', output, 1
end
"#
);
let doc = Parser::default().parse("[[:idname]]\n--\ncontent\n--\n");
assert_xpath(&doc, "/*[@id=\":idname\"]", 1);
}
#[test]
fn should_use_specified_id_and_reftext_when_registering_block_reference() {
verifies!(
r#"
test 'should use specified id and reftext when registering block reference' do
input = <<~'EOS'
[[debian,Debian Install]]
.Installation on Debian
----
$ apt-get install asciidoctor
----
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['debian']
refute_nil ref
assert_equal 'Debian Install', ref.reftext
assert_equal 'debian', (doc.resolve_id 'Debian Install')
end
"#
);
let doc = Parser::default().parse(
"[[debian,Debian Install]]\n.Installation on Debian\n----\n$ apt-get install asciidoctor\n----\n",
);
let entry = doc
.catalog()
.get_ref("debian")
.expect("ref should be registered");
assert_eq!(entry.reftext.as_deref(), Some("Debian Install"));
assert_eq!(
doc.catalog().resolve_id("Debian Install").as_deref(),
Some("debian")
);
}
#[test]
fn should_allow_square_brackets_in_block_reference_text() {
verifies!(
r#"
test 'should allow square brackets in block reference text' do
input = <<~'EOS'
[[debian,[Debian] Install]]
.Installation on Debian
----
$ apt-get install asciidoctor
----
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['debian']
refute_nil ref
assert_equal '[Debian] Install', ref.reftext
assert_equal 'debian', (doc.resolve_id '[Debian] Install')
end
"#
);
let doc = Parser::default().parse(
"[[debian,[Debian] Install]]\n.Installation on Debian\n----\n$ apt-get install asciidoctor\n----\n",
);
let entry = doc
.catalog()
.get_ref("debian")
.expect("ref should be registered");
assert_eq!(entry.reftext.as_deref(), Some("[Debian] Install"));
assert_eq!(
doc.catalog().resolve_id("[Debian] Install").as_deref(),
Some("debian")
);
}
// NOTE: divergence from Asciidoctor. This crate does not trim the leading
// space after the id when a block reference's reftext contains a comma, so
// the reftext is " Debian, Ubuntu" rather than "Debian, Ubuntu". Kept
// `#[ignore]`d with the Ruby-intended reftext.
// TODO: trim the leading space of a comma-containing block reftext.
#[ignore]
#[test]
fn should_allow_comma_in_block_reference_text() {
verifies!(
r#"
test 'should allow comma in block reference text' do
input = <<~'EOS'
[[debian, Debian, Ubuntu]]
.Installation on Debian
----
$ apt-get install asciidoctor
----
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['debian']
refute_nil ref
assert_equal 'Debian, Ubuntu', ref.reftext
assert_equal 'debian', (doc.resolve_id 'Debian, Ubuntu')
end
"#
);
let doc = Parser::default().parse(
"[[debian, Debian, Ubuntu]]\n.Installation on Debian\n----\n$ apt-get install asciidoctor\n----\n",
);
let entry = doc
.catalog()
.get_ref("debian")
.expect("ref should be registered");
assert_eq!(entry.reftext.as_deref(), Some("Debian, Ubuntu"));
}
// NOTE: divergence from Asciidoctor. This test exercises resolving an
// attribute reference in a block title against the attribute value in
// effect at the block's location, a discrete heading, and cross-reference
// rendering. This crate does not resolve the attribute reference in the
// registered title, so the assertions do not hold. Kept `#[ignore]`d.
// TODO: resolve attribute references in block titles at the block location.
#[ignore]
#[test]
fn should_resolve_attribute_reference_in_title_using_attribute_defined_at_location_of_block() {
verifies!(
r##"
test 'should resolve attribute reference in title using attribute defined at location of block' do
input = <<~'EOS'
= Document Title
:foo: baz
intro paragraph. see <<free-standing>>.
:foo: bar
.foo is {foo}
[#formal-para]
paragraph with title
[discrete#free-standing]
== foo is still {foo}
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['formal-para']
refute_nil ref
assert_equal 'foo is bar', ref.title
assert_equal 'formal-para', (doc.resolve_id 'foo is bar')
output = doc.convert standalone: false
assert_include '<a href="#free-standing">foo is still bar</a>', output
assert_include '<h2 id="free-standing" class="discrete">foo is still bar</h2>', output
end
"##
);
let doc = Parser::default().parse(
"= Document Title\n:foo: baz\n\nintro paragraph. see <<free-standing>>.\n\n:foo: bar\n\n.foo is {foo}\n[#formal-para]\nparagraph with title\n\n[discrete#free-standing]\n== foo is still {foo}\n",
);
let entry = doc
.catalog()
.get_ref("formal-para")
.expect("ref should be registered");
assert_eq!(entry.reftext.as_deref(), Some("foo is bar"));
}
// NOTE: divergence from Asciidoctor. This crate does not substitute
// attribute references in a block reference's reftext when registering it,
// so the reftext is stored verbatim ("Evolution of the {label-tiger}").
// Kept `#[ignore]`d with the Ruby-intended (substituted) reftext.
// TODO: substitute attribute references in a block reftext at registration.
#[ignore]
#[test]
fn should_substitute_attribute_references_in_reftext_when_registering_block_reference() {
verifies!(
r#"
test 'should substitute attribute references in reftext when registering block reference' do
input = <<~'EOS'
:label-tiger: Tiger
[[tiger-evolution,Evolution of the {label-tiger}]]
****
Information about the evolution of the tiger.
****
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['tiger-evolution']
refute_nil ref
assert_equal 'Evolution of the Tiger', ref.attributes['reftext']
assert_equal 'tiger-evolution', (doc.resolve_id 'Evolution of the Tiger')
end
"#
);
let doc = Parser::default().parse(
":label-tiger: Tiger\n\n[[tiger-evolution,Evolution of the {label-tiger}]]\n****\nInformation about the evolution of the tiger.\n****\n",
);
let entry = doc
.catalog()
.get_ref("tiger-evolution")
.expect("ref should be registered");
assert_eq!(entry.reftext.as_deref(), Some("Evolution of the Tiger"));
assert_eq!(
doc.catalog()
.resolve_id("Evolution of the Tiger")
.as_deref(),
Some("tiger-evolution")
);
}
#[test]
fn should_use_specified_reftext_when_registering_block_reference() {
verifies!(
r#"
test 'should use specified reftext when registering block reference' do
input = <<~'EOS'
[[debian]]
[reftext="Debian Install"]
.Installation on Debian
----
$ apt-get install asciidoctor
----
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['debian']
refute_nil ref
assert_equal 'Debian Install', ref.reftext
assert_equal 'debian', (doc.resolve_id 'Debian Install')
end
"#
);
let doc = Parser::default().parse(
"[[debian]]\n[reftext=\"Debian Install\"]\n.Installation on Debian\n----\n$ apt-get install asciidoctor\n----\n",
);
let entry = doc
.catalog()
.get_ref("debian")
.expect("ref should be registered");
assert_eq!(entry.reftext.as_deref(), Some("Debian Install"));
assert_eq!(
doc.catalog().resolve_id("Debian Install").as_deref(),
Some("debian")
);
}
non_normative!(
r#"
end
"#
);
}