Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 155: table rows with styling going missing. #156

Merged
merged 3 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Possible log types:
- `[fixed]` for any bug fixes.
- `[security]` to invite users to upgrade in case of vulnerabilities.

### 0.13.0-alpha.1

- [fixed] Table rows with colours would disappear. (thanks tkapias)

### 0.13.0-alpha.0

- [changed] Replaced LightningCSS with a smaller CSS parser. There is a chance
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "html2text"
version = "0.13.0-alpha.0"
version = "0.13.0-alpha.1"
authors = ["Chris Emerson <[email protected]>"]
description = "Render HTML as plain text."
repository = "https://github.com/jugglerchris/rust-html2text/"
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,20 @@ impl<'a, C: 'a, N> TreeMapResult<'a, C, N, RenderNode> {
cellinfo.content = vec![wrapped];
RenderNode::new(RenderNodeInfo::TableCell(cellinfo))
}
RenderNodeInfo::TableRow(mut row, vert) => {
let cells = row.cells;
let cells = cells
.into_iter()
.map(|mut child| {
let children = child.content;
let wrapped = RenderNode::new(f(children));
child.content = vec![wrapped];
child
})
.collect();
row.cells = cells;
RenderNode::new(RenderNodeInfo::TableRow(row, vert))
}
ni => RenderNode::new(f(vec![RenderNode::new(ni)])),
}
}))
Expand Down
6 changes: 0 additions & 6 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ pub(crate) trait Renderer {
/// Return the current width in character cells
fn width(&self) -> usize;

/// Add a line to the current block without starting a new one.
fn add_block_line(&mut self, line: &str);

/// Add a new block from a sub renderer, and prefix every line by the
/// corresponding text from each iteration of prefixes.
fn append_subrender<'a, I>(&mut self, other: Self, prefixes: I) -> Result<(), Error>
Expand All @@ -86,9 +83,6 @@ pub(crate) trait Renderer {
/// Returns true if this renderer has no content.
fn empty(&self) -> bool;

/// Return the length of the contained text.
fn text_len(&self) -> usize;

/// Start a hyperlink
/// TODO: return sub-builder or similar to make misuse
/// of start/link harder?
Expand Down
34 changes: 0 additions & 34 deletions src/render/text_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,22 +993,6 @@ impl<D: TextDecorator> SubRenderer<D> {
}
}

/// Add a prerendered (multiline) string with the current annotations.
fn add_subblock(&mut self, s: &str) {
use self::TaggedLineElement::Str;

html_trace!("add_subblock({}, {})", self.width, s);
let tag = self.ann_stack.clone();
self.lines.extend(s.lines().map(|l| {
let mut line = TaggedLine::new();
line.push(Str(TaggedString {
s: l.into(),
tag: tag.clone(),
}));
RenderLine::Text(line)
}));
}

/// Flushes the current wrapped block into the lines.
fn flush_wrapping(&mut self) -> Result<(), Error> {
if let Some(w) = self.wrapping.take() {
Expand Down Expand Up @@ -1249,10 +1233,6 @@ impl<D: TextDecorator> Renderer for SubRenderer<D> {
self.width
}

fn add_block_line(&mut self, line: &str) {
self.add_subblock(line);
}

fn append_subrender<'a, I>(&mut self, other: Self, prefixes: I) -> Result<(), Error>
where
I: Iterator<Item = &'a str>,
Expand Down Expand Up @@ -1478,20 +1458,6 @@ impl<D: TextDecorator> Renderer for SubRenderer<D> {
}
}

fn text_len(&self) -> usize {
let mut result = 0;
for line in &self.lines {
result += match *line {
RenderLine::Text(ref tline) => tline.width(),
RenderLine::Line(_) => 0, // FIXME: should borders count?
};
}
if let Some(ref w) = self.wrapping {
result += w.text_len();
}
result
}

fn start_link(&mut self, target: &str) -> crate::Result<()> {
let (s, annotation) = self.decorator.decorate_link_start(target);
self.ann_stack.push(annotation);
Expand Down
32 changes: 32 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,38 @@ Baz
r#"X<R>CD</R>Y

<G>C</G>D
"#,
20,
);
}

#[test]
fn test_colour_row() {
test_html_coloured(
br#"<head><style>
tr.r {
color: #f00;
}
</style></head><body>
<table>
<tr>
<td>Row</td><td>One</td>
</tr>
<tr class="r">
<td>Row</td><td>Two</td>
</tr>
<tr>
<td>Row</td><td>Three</td>
</tr>
</table>
"#,
r#"───┬─────
Row│One
───┼─────
<R>Row</R>│<R>Two</R>
───┼─────
Row│Three
───┴─────
"#,
20,
);
Expand Down
Loading