Skip to content

Commit 59c3180

Browse files
committed
feat: Options to add dimension HTML attributes from CSS properties
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent 74e9718 commit 59c3180

File tree

30 files changed

+750
-23
lines changed

30 files changed

+750
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `InlineOptions::apply_width_attributes` and `InlineOptions::apply_height_attributes` options to add dimension HTML attributes from CSS properties on supported elements (`table`, `td`, `th`, `img`). [#652](https://github.com/Stranger6667/css-inline/issues/652)
8+
59
### Performance
610

711
- Skip selectors that reference non-existent classes, IDs, or tags.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ fn main() -> css_inline::Result<()> {
135135
- `extra_css`. Extra CSS to be inlined. Default: `None`
136136
- `preallocate_node_capacity`. **Advanced**. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: `32`
137137
- `remove_inlined_selectors`. Specifies whether to remove selectors that were successfully inlined from `<style>` blocks. Default: `false`
138+
- `apply_width_attributes`. Specifies whether to add `width` HTML attributes from CSS `width` properties on supported elements (`table`, `td`, `th`, `img`). Default: `false`
139+
- `apply_height_attributes`. Specifies whether to add `height` HTML attributes from CSS `height` properties on supported elements (`table`, `td`, `th`, `img`). Default: `false`
138140

139141
You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it:
140142

bindings/c/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `apply_width_attributes` and `apply_height_attributes` options to add dimension HTML attributes from CSS properties on supported elements (`table`, `td`, `th`, `img`). [#652](https://github.com/Stranger6667/css-inline/issues/652)
8+
59
### Performance
610

711
- Skip selectors that reference non-existent classes, IDs, or tags.

bindings/c/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ Possible configurations:
161161
- `extra_css`. Extra CSS to be inlined. Default: `NULL`
162162
- `preallocate_node_capacity`. **Advanced**. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: `32`
163163
- `remove_inlined_selectors`. Specifies whether to remove selectors that were successfully inlined from `<style>` blocks. Default: `false`
164+
- `apply_width_attributes`. Specifies whether to add `width` HTML attributes from CSS `width` properties on supported elements (`table`, `td`, `th`, `img`). Default: `false`
165+
- `apply_height_attributes`. Specifies whether to add `height` HTML attributes from CSS `height` properties on supported elements (`table`, `td`, `th`, `img`). Default: `false`
164166
165167
You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it:
166168

bindings/c/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ pub struct CssInlinerOptions {
9999
pub minify_css: bool,
100100
/// Remove selectors that were successfully inlined from inline `<style>` blocks.
101101
pub remove_inlined_selectors: bool,
102+
/// Apply `width` HTML attributes from CSS `width` properties on supported elements.
103+
pub apply_width_attributes: bool,
104+
/// Apply `height` HTML attributes from CSS `height` properties on supported elements.
105+
pub apply_height_attributes: bool,
102106
}
103107

104108
macro_rules! inliner {
@@ -201,6 +205,8 @@ pub extern "C" fn css_inliner_default_options() -> CssInlinerOptions {
201205
extra_css: ptr::null(),
202206
preallocate_node_capacity: 32,
203207
remove_inlined_selectors: false,
208+
apply_width_attributes: false,
209+
apply_height_attributes: false,
204210
}
205211
}
206212

@@ -258,6 +264,8 @@ impl TryFrom<&CssInlinerOptions> for InlineOptions<'_> {
258264
preallocate_node_capacity: value.preallocate_node_capacity,
259265
resolver: Arc::new(DefaultStylesheetResolver),
260266
remove_inlined_selectors: value.remove_inlined_selectors,
267+
apply_width_attributes: value.apply_width_attributes,
268+
apply_height_attributes: value.apply_height_attributes,
261269
})
262270
}
263271
}

bindings/java/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `applyWidthAttributes` and `applyHeightAttributes` options to add dimension HTML attributes from CSS properties on supported elements (`table`, `td`, `th`, `img`). [#652](https://github.com/Stranger6667/css-inline/issues/652)
8+
59
### Performance
610

711
- Skip selectors that reference non-existent classes, IDs, or tags.

bindings/java/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public class ConfigExample {
154154
- **`setCacheSize(int)`** - External stylesheet cache size, must be ≥ 0 (default: `0`)
155155
- **`setPreallocateNodeCapacity(int)`** - HTML node capacity, must be > 0 (default: `32`)
156156
- **`setRemoveInlinedSelectors(boolean)`** - Remove selectors that were successfully inlined from `<style>` blocks (default: `false`)
157+
- **`setApplyWidthAttributes(boolean)`** - Add `width` HTML attributes from CSS `width` properties on supported elements (`table`, `td`, `th`, `img`) (default: `false`)
158+
- **`setApplyHeightAttributes(boolean)`** - Add `height` HTML attributes from CSS `height` properties on supported elements (`table`, `td`, `th`, `img`) (default: `false`)
157159

158160

159161
### HTML Fragments

bindings/java/src/main/java/org/cssinline/CssInlineConfig.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@ public class CssInlineConfig {
3535
/** Remove selectors that were successfully inlined from inline style blocks. */
3636
public final boolean removeInlinedSelectors;
3737

38+
/** Apply width HTML attributes from CSS width properties on supported elements. */
39+
public final boolean applyWidthAttributes;
40+
41+
/** Apply height HTML attributes from CSS height properties on supported elements. */
42+
public final boolean applyHeightAttributes;
43+
3844
private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean keepLinkTags,
3945
boolean keepAtRules, boolean minifyCss, boolean loadRemoteStylesheets, String baseUrl, String extraCss,
40-
int cacheSize, int preallocateNodeCapacity, boolean removeInlinedSelectors) {
46+
int cacheSize, int preallocateNodeCapacity, boolean removeInlinedSelectors,
47+
boolean applyWidthAttributes, boolean applyHeightAttributes) {
4148
this.inlineStyleTags = inlineStyleTags;
4249
this.keepStyleTags = keepStyleTags;
4350
this.keepLinkTags = keepLinkTags;
@@ -49,6 +56,8 @@ private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean
4956
this.cacheSize = cacheSize;
5057
this.preallocateNodeCapacity = preallocateNodeCapacity;
5158
this.removeInlinedSelectors = removeInlinedSelectors;
59+
this.applyWidthAttributes = applyWidthAttributes;
60+
this.applyHeightAttributes = applyHeightAttributes;
5261
}
5362

5463
/**
@@ -66,6 +75,8 @@ public static class Builder {
6675
private int cacheSize = 0;
6776
private int preallocateNodeCapacity = 32;
6877
private boolean removeInlinedSelectors = false;
78+
private boolean applyWidthAttributes = false;
79+
private boolean applyHeightAttributes = false;
6980

7081
/**
7182
* Creates a new builder with default configuration values.
@@ -210,14 +221,40 @@ public Builder setRemoveInlinedSelectors(boolean b) {
210221
return this;
211222
}
212223

224+
/**
225+
* Apply width HTML attributes from CSS width properties on supported elements.
226+
* This is useful for email compatibility with clients like Outlook that ignore CSS width.
227+
* Supported elements: table, td, th, img.
228+
*
229+
* @param b true to apply width attributes, false to skip them
230+
* @return this builder instance for method chaining
231+
*/
232+
public Builder setApplyWidthAttributes(boolean b) {
233+
this.applyWidthAttributes = b;
234+
return this;
235+
}
236+
237+
/**
238+
* Apply height HTML attributes from CSS height properties on supported elements.
239+
* This is useful for email compatibility with clients like Outlook that ignore CSS height.
240+
* Supported elements: table, td, th, img.
241+
*
242+
* @param b true to apply height attributes, false to skip them
243+
* @return this builder instance for method chaining
244+
*/
245+
public Builder setApplyHeightAttributes(boolean b) {
246+
this.applyHeightAttributes = b;
247+
return this;
248+
}
249+
213250
/**
214251
* Creates a new {@link CssInlineConfig} instance with the current builder settings.
215252
*
216253
* @return a new immutable configuration instance
217254
*/
218255
public CssInlineConfig build() {
219256
return new CssInlineConfig(inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, minifyCss, loadRemoteStylesheets, baseUrl,
220-
extraCss, cacheSize, preallocateNodeCapacity, removeInlinedSelectors);
257+
extraCss, cacheSize, preallocateNodeCapacity, removeInlinedSelectors, applyWidthAttributes, applyHeightAttributes);
221258
}
222259
}
223260
}

bindings/java/src/main/rust/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ fn build_inliner(
8080
let cache_size = env.get_int_field(&cfg, "cacheSize")?;
8181
let preallocate_node_capacity = env.get_int_field(&cfg, "preallocateNodeCapacity")?;
8282
let remove_inlined_selectors = env.get_bool_field(&cfg, "removeInlinedSelectors")?;
83+
let apply_width_attributes = env.get_bool_field(&cfg, "applyWidthAttributes")?;
84+
let apply_height_attributes = env.get_bool_field(&cfg, "applyHeightAttributes")?;
8385

8486
let extra_css = env.get_string_field_opt(&cfg, "extraCss")?;
8587
let base_url = env.get_string_field_opt(&cfg, "baseUrl")?;
@@ -92,7 +94,9 @@ fn build_inliner(
9294
.load_remote_stylesheets(load_remote_stylesheets)
9395
.extra_css(extra_css.map(Cow::Owned))
9496
.preallocate_node_capacity(preallocate_node_capacity as usize)
95-
.remove_inlined_selectors(remove_inlined_selectors);
97+
.remove_inlined_selectors(remove_inlined_selectors)
98+
.apply_width_attributes(apply_width_attributes)
99+
.apply_height_attributes(apply_height_attributes);
96100

97101
if let Some(url) = base_url {
98102
match css_inline::Url::parse(&url) {

bindings/javascript/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `applyWidthAttributes` and `applyHeightAttributes` options to add dimension HTML attributes from CSS properties on supported elements (`table`, `td`, `th`, `img`). [#652](https://github.com/Stranger6667/css-inline/issues/652)
8+
59
### Performance
610

711
- Skip selectors that reference non-existent classes, IDs, or tags.

0 commit comments

Comments
 (0)