flm_core/stdlib/features/cells/html.rs
1//! The `cells` feature's part of an HTML page: its style definition
2//! [`STYLE`] (`flm.cells.style`, [`stylesheet`]), the stylesheet that the
3//! HTML backend includes wherever a page holds a table of cells.
4//!
5//! A style definition ([`StyleDef`]) is a stylesheet that a feature's output
6//! depends on, recorded in the registry under an identifier; a lowering rule
7//! or an emitter requires it by identifier, and the backend that writes the
8//! output decides how it is delivered (see
9//! [style and script definitions](crate::guide::concepts_overview#style-and-script-definitions)).
10//!
11//! # The markup
12//!
13//! The HTML backend writes the `flm.pres.cells` node that the general rule
14//! stages as the [HTML contract](crate::guide::rendering#the-html-contract)
15//! states for it ([`CONVENTIONS`](crate::stdlib::backends::html::CONVENTIONS)):
16//! a `<table>` holding a `<tr>` per row of the grid and, in it, one element
17//! per position that no span covers. That element is a `<td>`, or a `<th>`
18//! carrying `scope` for a header cell, with `colspan` and `rowspan` where the
19//! cell spans several positions, and the cell's facts as its attributes; an
20//! empty `<td>` is written where no cell is placed:
21//!
22//! ```html
23//! <table class="flm-pres-cells">
24//! <tr>
25//! <th scope="col">
26//! <p class="flm-pres-par">Name</p>
27//! </th>
28//! <th scope="col">
29//! <p class="flm-pres-par">City</p>
30//! </th>
31//! </tr>
32//! <tr>
33//! <td data-flm-align="right" data-flm-rules="left" data-flm-style-green="">
34//! <p class="flm-pres-par">John</p>
35//! </td>
36//! <td></td>
37//! </tr>
38//! </table>
39//! ```
40//!
41//! The feature adds no tag and no class of its own: a stylesheet selects a
42//! table by the element's class and a cell by its attributes.
43//!
44//! # The stylesheet
45//!
46//! The style definition [`STYLE`] ([`stylesheet`]) holds one cascade layer
47//! with these declarations:
48//!
49//! - the table's outline, a line above it and a line below it, drawn on the
50//! `<table>` element;
51//! - a line under a column header;
52//! - a line at each edge listed in a cell's `data-flm-rules` (the cell's
53//! rules: lines drawn along its edges), independent of the other cells'
54//! lines;
55//! - a cell's alignment from `data-flm-align`, to the left where the cell
56//! has none;
57//! - the padding of a cell;
58//! - the backgrounds of the four custom styles the feature ships
59//! (`[data-flm-style-green]` and the three others);
60//! - a table that is a float's content is centered in the float.
61//!
62//! Every value the stylesheet uses is a custom property declared in it
63//! (`--flm-cells-spacing`, `--flm-cells-padding`, `--flm-cells-align`,
64//! `--flm-cells-outline`, `--flm-cells-header-rule`, `--flm-cells-rule`,
65//! `--flm-cells-green` and the other three colors), which a page can set to
66//! change these values without writing a selector of its own.
67//!
68//! The style definition reaches a page through the HTML contract: the
69//! contract's row for `flm.pres.cells` refers to it, and the backend
70//! requires it wherever it writes a table, so a page without a table does
71//! not include it. A custom cell style that another feature declares is
72//! styled by that feature's own style definition, which the declaration
73//! requires ([`CellStyle::requires`](super::CellStyle::requires)) and which
74//! the general rule requires in turn wherever a cell carries the style.
75
76use crate::core::registry::{BuildError, DefContent, FeatureBuilder, StyleDef};
77use crate::model::names::Name;
78
79use super::STYLE;
80
81/// The feature's style definition [`STYLE`] (`flm.cells.style`): a table's
82/// outline and edge lines, a cell's padding and alignment, and the backgrounds of
83/// the four custom styles the feature ships, in one cascade layer, every
84/// value a custom property it declares. The definition holds the
85/// stylesheet's text itself ([`DefContent::Inline`]). See the
86/// [module documentation](self).
87pub fn stylesheet() -> StyleDef {
88 StyleDef {
89 id: Name::from(STYLE),
90 content: vec![DefContent::Inline(String::from(include_str!("css/cells.css")))],
91 docs: String::from(
92 "a table's outline and edge lines, a cell's padding and alignment, and the backgrounds \
93 of the custom styles `green`, `blue`, `yellow` and `red`",
94 ),
95 }
96}
97
98/// Registers the style definition ([`stylesheet`]).
99///
100/// # Errors
101///
102/// The [`BuildError`] of the registry builder when a declaration is made in
103/// a registration round that does not accept it (see
104/// [`FeatureBuilder`](crate::core::registry::FeatureBuilder)).
105pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
106 b.style_def(stylesheet())
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn the_stylesheet_names_itself_and_is_one_layer() {
115 let style = stylesheet();
116 assert_eq!(style.id.as_str(), STYLE);
117 let [DefContent::Inline(css)] = style.content.as_slice() else { panic!("one inline part") };
118 assert_eq!(css.lines().next(), Some("/* flm.cells.style */"));
119 assert_eq!(css.matches("@layer flm {").count(), 1);
120 }
121}