mirror of
https://github.com/typst/typst
synced 2025-08-10 05:07:55 +08:00
Implement show rules for HTML tables.
This commit is contained in:
parent
265df6c29f
commit
a94fd6e19e
@ -602,7 +602,7 @@ pub enum Entry<'a> {
|
|||||||
|
|
||||||
impl<'a> Entry<'a> {
|
impl<'a> Entry<'a> {
|
||||||
/// Obtains the cell inside this entry, if this is not a merged cell.
|
/// Obtains the cell inside this entry, if this is not a merged cell.
|
||||||
fn as_cell(&self) -> Option<&Cell<'a>> {
|
pub fn as_cell(&self) -> Option<&Cell<'a>> {
|
||||||
match self {
|
match self {
|
||||||
Self::Cell(cell) => Some(cell),
|
Self::Cell(cell) => Some(cell),
|
||||||
Self::Merged { .. } => None,
|
Self::Merged { .. } => None,
|
||||||
|
@ -7,7 +7,11 @@ use crate::diag::{bail, HintedStrResult, HintedString, SourceResult};
|
|||||||
use crate::engine::Engine;
|
use crate::engine::Engine;
|
||||||
use crate::foundations::{
|
use crate::foundations::{
|
||||||
cast, elem, scope, Content, NativeElement, Packed, Show, Smart, StyleChain,
|
cast, elem, scope, Content, NativeElement, Packed, Show, Smart, StyleChain,
|
||||||
|
TargetElem,
|
||||||
};
|
};
|
||||||
|
use crate::html::{tag, HtmlAttr, HtmlAttrs, HtmlElem};
|
||||||
|
use crate::introspection::Locator;
|
||||||
|
use crate::layout::grid::resolve::table_to_cellgrid;
|
||||||
use crate::layout::{
|
use crate::layout::{
|
||||||
show_grid_cell, Abs, Alignment, BlockElem, Celled, GridCell, GridFooter, GridHLine,
|
show_grid_cell, Abs, Alignment, BlockElem, Celled, GridCell, GridFooter, GridHLine,
|
||||||
GridHeader, GridVLine, Length, OuterHAlignment, OuterVAlignment, Rel, Sides,
|
GridHeader, GridVLine, Length, OuterHAlignment, OuterVAlignment, Rel, Sides,
|
||||||
@ -259,10 +263,23 @@ impl TableElem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Show for Packed<TableElem> {
|
impl Show for Packed<TableElem> {
|
||||||
fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||||
Ok(BlockElem::multi_layouter(self.clone(), engine.routines.layout_table)
|
Ok(if TargetElem::target_in(styles).is_html() {
|
||||||
.pack()
|
// TODO: This is a hack, it is not clear whether the locator is actually used by HTML.
|
||||||
.spanned(self.span()))
|
// How can we find out whether locator is actually used?
|
||||||
|
let locator = Locator::root();
|
||||||
|
let grid = table_to_cellgrid(self, engine, locator, styles)?;
|
||||||
|
let rows = grid.entries.chunks(grid.cols.len()).map(|row| {
|
||||||
|
let row =
|
||||||
|
row.iter().flat_map(|entry| Some(entry.as_cell()?.body.clone()));
|
||||||
|
HtmlElem::new(tag::tr).with_body(Some(Content::sequence(row))).pack()
|
||||||
|
});
|
||||||
|
let content = Content::sequence(rows);
|
||||||
|
HtmlElem::new(tag::table).with_body(Some(content)).pack()
|
||||||
|
} else {
|
||||||
|
BlockElem::multi_layouter(self.clone(), engine.routines.layout_table).pack()
|
||||||
|
}
|
||||||
|
.spanned(self.span()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -706,7 +723,20 @@ cast! {
|
|||||||
|
|
||||||
impl Show for Packed<TableCell> {
|
impl Show for Packed<TableCell> {
|
||||||
fn show(&self, _engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
fn show(&self, _engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||||
show_grid_cell(self.body().clone(), self.inset(styles), self.align(styles))
|
if TargetElem::target_in(styles).is_html() {
|
||||||
|
let mut attrs = HtmlAttrs::default();
|
||||||
|
let span = |n: NonZeroUsize| (n != NonZeroUsize::MIN).then(|| n.to_string());
|
||||||
|
if let Some(colspan) = span(self.colspan(styles)) {
|
||||||
|
attrs.push(HtmlAttr::constant("colspan"), colspan);
|
||||||
|
}
|
||||||
|
if let Some(rowspan) = span(self.rowspan(styles)) {
|
||||||
|
attrs.push(HtmlAttr::constant("rowspan"), rowspan);
|
||||||
|
}
|
||||||
|
let body = Some(self.body.clone());
|
||||||
|
Ok(HtmlElem::new(tag::td).with_body(body).with_attrs(attrs).pack())
|
||||||
|
} else {
|
||||||
|
show_grid_cell(self.body().clone(), self.inset(styles), self.align(styles))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user