use crate::tests::prelude::*;
track_file!("ref/asciidoc-lang/docs/modules/tables/pages/striping.adoc");
fn parse_table(source: &str) -> crate::blocks::TableBlock<'_> {
let mut parser = Parser::default();
let mi = crate::blocks::Block::parse(crate::Span::new(source), &mut parser)
.unwrap_if_no_warnings()
.unwrap();
match mi.item {
crate::blocks::Block::Table(table) => table,
other => panic!("expected a table block, got {other:?}"),
}
}
fn first_table<'a>(doc: &'a crate::Document<'a>) -> &'a crate::blocks::TableBlock<'a> {
doc.nested_blocks()
.find_map(|block| match block {
crate::blocks::Block::Table(table) => Some(table),
_ => None,
})
.expect("expected a table block")
}
const BASE: &str = "|===\n|A1\n|B1\n|===";
non_normative!(
r#"
= Table Striping
"#
);
#[test]
fn intro() {
verifies!(
r#"
You can add zebra-striping to rows of a table.
When this feature is enabled, the specified rows are shaded using a background color to create a zebra striping effect.
"#
);
assert_eq!(parse_table(BASE).stripes(), Stripes::None);
non_normative!(
r#"
NOTE: In the HTML output, table striping is done using CSS and thus depends on the stylesheet to supply the necessary styles.
The default stylesheet for Asciidoctor includes the necessary styles for table striping.
"#
);
}
#[test]
fn striping_attributes() {
non_normative!(
r#"
== Striping attributes
"#
);
verifies!(
r#"
Which rows are striped is controlled using the `stripes` attribute on the table.
The stripes attribute defaults to the value `none` (implied value), which means rows are not striped by default.
This default can be changed by setting the `table-stripes` document attribute.
You can override the default value by setting the stripes attribute on the table.
"#
);
assert_eq!(parse_table(BASE).stripes(), Stripes::None);
let src = format!("[stripes=none]\n{BASE}");
assert_eq!(parse_table(&src).stripes(), Stripes::None);
let src = format!(":table-stripes: odd\n\n{BASE}");
let doc = Parser::default().parse(&src);
assert_eq!(first_table(&doc).stripes(), Stripes::Odd);
let src = format!(":table-stripes: odd\n\n[stripes=even]\n{BASE}");
let doc = Parser::default().parse(&src);
assert_eq!(first_table(&doc).stripes(), Stripes::Even);
verifies!(
r#"
The `stripes` attribute on a table and the `table-stripes` document attribute accept the following values:
* `none` - no rows are shaded (default)
* `even` - even rows are shaded
* `odd` - odd rows are shaded
* `all` - all rows are shaded
* `hover` - the row under the mouse cursor is shaded (HTML only)
"#
);
let src = format!("[stripes=none]\n{BASE}");
assert_eq!(parse_table(&src).stripes(), Stripes::None);
let src = format!("[stripes=even]\n{BASE}");
assert_eq!(parse_table(&src).stripes(), Stripes::Even);
let src = format!("[stripes=odd]\n{BASE}");
assert_eq!(parse_table(&src).stripes(), Stripes::Odd);
let src = format!("[stripes=all]\n{BASE}");
assert_eq!(parse_table(&src).stripes(), Stripes::All);
let src = format!("[stripes=hover]\n{BASE}");
assert_eq!(parse_table(&src).stripes(), Stripes::Hover);
let src = format!("[stripes=bogus]\n{BASE}");
assert_eq!(parse_table(&src).stripes(), Stripes::None);
}
#[test]
fn stripes_block_attribute() {
non_normative!(
r#"
== stripes block attribute
"#
);
verifies!(
r#"
In the following example, the stripes are enabled for even rows in the table body (the row that contains A2 and B2).
[source]
----
[cols=2*,stripes=even]
|===
|A1
|B1
|A2
|B2
|A3
|B3
|===
----
"#
);
let src = "[cols=2*,stripes=even]\n|===\n|A1\n|B1\n|A2\n|B2\n|A3\n|B3\n|===";
let table = parse_table(src);
assert_eq!(table.stripes(), Stripes::Even);
assert_eq!(table.columns().len(), 2);
assert!(table.header_row().is_none());
assert_eq!(table.body_rows().len(), 3);
verifies!(
r#"
Under the covers, the stripes attribute applies the CSS class `stripes-<value>` (e.g., `stripes-none`) to the table tag.
As a shorthand, you can simply apply the CSS class to the table directly using a role.
[source]
----
[.stripes-even,cols=2*]
|===
|A1
|B1
|A2
|B2
|A3
|B3
|===
----
"#
);
let src = "[.stripes-even,cols=2*]\n|===\n|A1\n|B1\n|A2\n|B2\n|A3\n|B3\n|===";
let table = parse_table(src);
assert_eq!(table.stripes(), Stripes::None);
assert!(table.attrlist().unwrap().roles().contains(&"stripes-even"));
}
#[test]
fn table_stripes_attribute() {
non_normative!(
r#"
== table-stripes attribute
"#
);
verifies!(
r#"
If you want to apply stripes to all tables in the document, set the `table-stripes` attribute in the document header.
You can still override this setting per table.
[source]
----
= Document Title
:table-stripes: even
[cols=2*]
|===
|A1
|B1
|A2
|B2
|A3
|B3
|===
----
"#
);
let src = "= Document Title\n:table-stripes: even\n\n[cols=2*]\n|===\n|A1\n|B1\n|A2\n|B2\n|A3\n|B3\n|===";
let doc = Parser::default().parse(src);
assert_eq!(first_table(&doc).stripes(), Stripes::Even);
let src =
"= Document Title\n:table-stripes: even\n\n[stripes=odd,cols=2*]\n|===\n|A1\n|B1\n|===";
let doc = Parser::default().parse(src);
assert_eq!(first_table(&doc).stripes(), Stripes::Odd);
}