Skip to content

Commit 5beb478

Browse files
committed
feat: CssInline.inline(String html, String css) convenience overload for applying a CSS string to a full HTML document
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent d37dce9 commit 5beb478

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

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+
- `CssInline.inline(String html, String css)` convenience overload for applying a CSS string to a full HTML document. [#693](https://github.com/Stranger6667/css-inline/issues/693)
8+
59
### Fixed
610

711
- `inlineFragment` silently returning only leading whitespace when the input starts with whitespace. [#692](https://github.com/Stranger6667/css-inline/issues/692)

bindings/java/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ public class Example {
122122
}
123123
```
124124

125+
To apply a CSS string directly to a document, use the `inline(html, css)` shortcut:
126+
127+
```java
128+
String inlined = CssInline.inline(html, "h1 { color: blue; }");
129+
```
130+
125131
You can also configure the inlining process:
126132

127133
```java

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ public static String inline(String html, CssInlineConfig cfg) {
3535
return nativeInline(html, cfg);
3636
}
3737

38+
/**
39+
* Inlines the provided CSS into an HTML document using default configuration.
40+
*
41+
* <p>This is a convenience shortcut for applying a CSS string to a full HTML document.
42+
* Equivalent to building a config with {@code setExtraCss(css)} and calling
43+
* {@link #inline(String, CssInlineConfig)}.
44+
*
45+
* @param html the HTML document to process
46+
* @param css the CSS rules to inline
47+
* @return the HTML document with CSS styles inlined
48+
* @throws CssInlineException if an error occurs during processing
49+
*/
50+
public static String inline(String html, String css) {
51+
return inline(html, new CssInlineConfig.Builder().setExtraCss(css).build());
52+
}
53+
3854
/**
3955
* Inlines CSS styles into HTML elements using default configuration.
4056
*

bindings/java/src/test/java/org/cssinline/CssInlineTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ void inlinesSimpleStyleTag() {
1515
assertTrue(out.contains("style=\"color: blue;\""), "Output should inline styles for h1, got: " + out);
1616
}
1717

18+
@Test
19+
void inlineWithCssShortcut() {
20+
String html = "<html><head></head><body><h1>Hello</h1></body></html>";
21+
22+
String out = CssInline.inline(html, "h1 { color: blue; }");
23+
24+
assertEquals("<html><head></head><body><h1 style=\"color: blue;\">Hello</h1></body></html>", out);
25+
}
26+
1827
@Test
1928
void extraCssAddsBackground() {
2029
String html = "<html><head></head><body><h1>Hello</h1></body></html>";

0 commit comments

Comments
 (0)