Skip to content

Commit 3f8830d

Browse files
committed
feat: Option to remove "style" tags
Ref: #11
1 parent 9909226 commit 3f8830d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

CHANGELOG.md

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

77
- `CSSInliner` and customization options. [#9](https://github.com/Stranger6667/css-inline/issues/9)
8+
- Option to remove "style" tags. [#11](https://github.com/Stranger6667/css-inline/issues/11)
89

910
### Changed
1011

src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
//! </html>"#;
9696
//!
9797
//!fn main() -> Result<(), css_inline::InlineError> {
98-
//! let options = css_inline::InlineOptions {};
98+
//! let options = css_inline::InlineOptions { remove_style_tags: true };
9999
//! let inliner = css_inline::CSSInliner::new(options);
100100
//! let inlined = inliner.inline(HTML)?;
101101
//! // Do something with inlined HTML, e.g. send an email
@@ -154,12 +154,17 @@ impl Rule {
154154

155155
/// Configuration options for CSS inlining process.
156156
#[derive(Debug)]
157-
pub struct InlineOptions {}
157+
pub struct InlineOptions {
158+
/// Remove "style" tags after inlining
159+
pub remove_style_tags: bool,
160+
}
158161

159162
impl Default for InlineOptions {
160163
#[inline]
161164
fn default() -> Self {
162-
InlineOptions {}
165+
InlineOptions {
166+
remove_style_tags: false,
167+
}
163168
}
164169
}
165170

@@ -216,6 +221,9 @@ impl CSSInliner {
216221
}
217222
}
218223
}
224+
if self.options.remove_style_tags {
225+
style_tag.as_node().detach()
226+
}
219227
}
220228
let mut out = vec![];
221229
document.serialize(&mut out)?;

tests/test_inlining.rs

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

33
macro_rules! html {
44
($style: expr, $body: expr) => {
@@ -100,3 +100,14 @@ fn invalid_rule() {
100100
assert!(result.is_err());
101101
assert_eq!(result.unwrap_err().to_string(), "Invalid @ rule: wrong")
102102
}
103+
104+
#[test]
105+
fn remove_style_tag() {
106+
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);
111+
let result = inliner.inline(&html).unwrap();
112+
assert_eq!(result, "<html><head><title>Test</title></head><body><h1 style=\"background-color: blue;\">Hello world!</h1></body></html>")
113+
}

0 commit comments

Comments
 (0)