Skip to content

Commit a13fc37

Browse files
committed
feat: CSSInliner::compact() constructor
1 parent 3f8830d commit a13fc37

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `CSSInliner` and customization options. [#9](https://github.com/Stranger6667/css-inline/issues/9)
88
- Option to remove "style" tags. [#11](https://github.com/Stranger6667/css-inline/issues/11)
9+
- `CSSInliner::compact()` constructor for producing smaller HTML output.
910

1011
### Changed
1112

src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@
9595
//! </html>"#;
9696
//!
9797
//!fn main() -> Result<(), css_inline::InlineError> {
98-
//! let options = css_inline::InlineOptions { remove_style_tags: true };
99-
//! let inliner = css_inline::CSSInliner::new(options);
98+
//! let inliner = css_inline::CSSInliner::compact();
10099
//! let inlined = inliner.inline(HTML)?;
101100
//! // Do something with inlined HTML, e.g. send an email
102101
//! Ok(())
@@ -159,6 +158,16 @@ pub struct InlineOptions {
159158
pub remove_style_tags: bool,
160159
}
161160

161+
impl InlineOptions {
162+
/// Options for "compact" HTML output
163+
#[inline]
164+
pub fn compact() -> Self {
165+
InlineOptions {
166+
remove_style_tags: true,
167+
}
168+
}
169+
}
170+
162171
impl Default for InlineOptions {
163172
#[inline]
164173
fn default() -> Self {
@@ -181,6 +190,15 @@ impl CSSInliner {
181190
CSSInliner { options }
182191
}
183192

193+
/// Inliner, that will produce "compact" HTML.
194+
/// For example, "style" tags will be removed.
195+
#[inline]
196+
pub fn compact() -> Self {
197+
CSSInliner {
198+
options: InlineOptions::compact(),
199+
}
200+
}
201+
184202
/// Inline CSS styles from <style> tags to matching elements in the HTML tree.
185203
#[inline]
186204
pub fn inline(&self, html: &str) -> Result<String, InlineError> {

tests/test_inlining.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use css_inline::{inline, CSSInliner, InlineOptions};
1+
use css_inline::{inline, CSSInliner};
22

33
macro_rules! html {
44
($style: expr, $body: expr) => {
@@ -104,10 +104,7 @@ fn invalid_rule() {
104104
#[test]
105105
fn remove_style_tag() {
106106
let html = html!("h1 {background-color: blue;}", "<h1>Hello world!</h1>");
107-
let options = InlineOptions {
108-
remove_style_tags: true,
109-
};
110-
let inliner = CSSInliner::new(options);
107+
let inliner = CSSInliner::compact();
111108
let result = inliner.inline(&html).unwrap();
112109
assert_eq!(result, "<html><head><title>Test</title></head><body><h1 style=\"background-color: blue;\">Hello world!</h1></body></html>")
113110
}

0 commit comments

Comments
 (0)