Skip to content

Commit

Permalink
Merge pull request #135 from jugglerchris/hide_height_0
Browse files Browse the repository at this point in the history
Treat max-height: 0 as display: none.
  • Loading branch information
jugglerchris authored Mar 3, 2024
2 parents 4b04adb + 5ac5d08 commit e3d731f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Possible log types:
- `[fixed]` for any bug fixes.
- `[security]` to invite users to upgrade in case of vulnerabilities.

### 0.12.3

- [changed] Treat `max-height: 0` as if it's `display: none` when CSS is enabled.
This helps with a hack some e-mail senders use for e-mail previews. (thanks tkapias)

### 0.12.2

- [changed] Bump version of lightningcss dependency to fix build failures.
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.12.2"
version = "0.12.3"
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/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ fn styles_from_properties(decls: &DeclarationBlock<'_>) -> Vec<Style> {
}
styles.push(Style::BgColour(color.clone()));
}
Property::MaxHeight(height) => {
use lightningcss::properties::size::MaxSize::*;
use lightningcss::values::percentage::DimensionPercentage::*;
dbg!(&height);
match height {
LengthPercentage(Dimension(dim)) => {
// Treat max-height: 0 the same as display: none.
if Some(0.0) == dim.to_px() {
styles.push(Style::DisplayNone);
}
}
_ => (),
}
}
Property::Display(disp) => {
if let display::Display::Keyword(DisplayKeyword::None) = disp {
styles.push(Style::DisplayNone);
Expand Down
12 changes: 12 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2327,4 +2327,16 @@ text
"H\ne\nl\nl\no\n*<W></W>\n<W>t</W>\n<W>h</W>\n<W>e</W>\n<W>r</W>\n<W>e</W>\n*\nb\no\no\n",
1);
}

#[test]
fn test_height_0() {
test_html_css(
br#"
<div style="max-height: 0">This should be hidden</div>
<p>Hello</p>"#,
r#"Hello
"#,
20,
);
}
}

0 comments on commit e3d731f

Please sign in to comment.