Skip to content

Commit 364c29c

Browse files
committed
chore: pre-generate bindgen output, fix clippy warnings
1 parent 0ea93bb commit 364c29c

6 files changed

Lines changed: 175 additions & 78 deletions

File tree

litehtml/examples/browse.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use url::Url;
1414
use litehtml::pixbuf::PixbufContainer;
1515
use litehtml::selection::Selection;
1616
use litehtml::{
17-
BackgroundLayer, BorderRadiuses, Borders, Color, ConicGradient, DocumentContainer,
18-
DrawContext, FontDescription, FontHandle, FontMetrics, LinearGradient, ListMarker,
19-
MediaFeatures, Position, RadialGradient, Size, TextTransform,
17+
BackgroundLayer, BorderRadiuses, Borders, Color, ConicGradient, DocumentContainer, DrawContext,
18+
FontDescription, FontHandle, FontMetrics, LinearGradient, ListMarker, MediaFeatures, Position,
19+
RadialGradient, Size, TextTransform,
2020
};
2121

2222
const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0";
@@ -92,7 +92,14 @@ impl DocumentContainer for BrowseContainer {
9292
fn text_width(&self, text: &str, font: FontHandle) -> f32 {
9393
self.inner.text_width(text, font)
9494
}
95-
fn draw_text(&mut self, hdc: DrawContext, text: &str, font: FontHandle, color: Color, pos: Position) {
95+
fn draw_text(
96+
&mut self,
97+
hdc: DrawContext,
98+
text: &str,
99+
font: FontHandle,
100+
color: Color,
101+
pos: Position,
102+
) {
96103
self.inner.draw_text(hdc, text, font, color, pos);
97104
}
98105
fn draw_list_marker(&mut self, hdc: DrawContext, marker: &ListMarker) {
@@ -140,7 +147,13 @@ impl DocumentContainer for BrowseContainer {
140147
) {
141148
self.inner.draw_conic_gradient(hdc, layer, gradient);
142149
}
143-
fn draw_borders(&mut self, hdc: DrawContext, borders: &Borders, draw_pos: Position, root: bool) {
150+
fn draw_borders(
151+
&mut self,
152+
hdc: DrawContext,
153+
borders: &Borders,
154+
draw_pos: Position,
155+
root: bool,
156+
) {
144157
self.inner.draw_borders(hdc, borders, draw_pos, root);
145158
}
146159
fn set_base_url(&mut self, base_url: &str) {

litehtml/src/email.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Provides an email user-agent stylesheet and a convenience pipeline
44
//! that wraps [`crate::html::prepare_html`] with email-specific defaults.
55
6-
use crate::html;
6+
use crate::html::{self, ByteResolver};
77

88
// ---------------------------------------------------------------------------
99
// Email user-agent stylesheet
@@ -28,8 +28,8 @@ p, h1, h2, h3, h4, h5, h6 { margin: 0; padding: 0; }\n\
2828
/// in email-specific codebases.
2929
pub fn prepare_email_html(
3030
raw: &[u8],
31-
cid_resolver: Option<&dyn Fn(&str) -> Option<Vec<u8>>>,
32-
url_fetcher: Option<&dyn Fn(&str) -> Option<Vec<u8>>>,
31+
cid_resolver: ByteResolver<'_>,
32+
url_fetcher: ByteResolver<'_>,
3333
) -> PreparedEmail {
3434
let prepared = html::prepare_html(raw, cid_resolver, url_fetcher);
3535
PreparedEmail {

litehtml/src/html.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use base64::Engine;
88
use encoding_rs::Encoding;
99

10+
/// Callback type for resolving a URI string to raw bytes.
11+
pub type ByteResolver<'a> = Option<&'a dyn Fn(&str) -> Option<Vec<u8>>>;
12+
1013
// ---------------------------------------------------------------------------
1114
// Encoding detection & conversion
1215
// ---------------------------------------------------------------------------
@@ -73,12 +76,10 @@ fn detect_meta_charset(html: &str) -> Option<&'static Encoding> {
7376
let rest = rest.strip_prefix('=')?;
7477
let rest = rest.trim_start();
7578
// Strip optional quotes
76-
let (encoding_name, _) = if rest.starts_with('"') {
77-
let inner = &rest[1..];
79+
let (encoding_name, _) = if let Some(inner) = rest.strip_prefix('"') {
7880
let end = inner.find('"').unwrap_or(inner.len());
7981
(&inner[..end], &inner[end..])
80-
} else if rest.starts_with('\'') {
81-
let inner = &rest[1..];
82+
} else if let Some(inner) = rest.strip_prefix('\'') {
8283
let end = inner.find('\'').unwrap_or(inner.len());
8384
(&inner[..end], &inner[end..])
8485
} else {
@@ -236,12 +237,10 @@ fn is_stylesheet_link(tag_content: &str) -> bool {
236237
let rest = lower[pos + 3..].trim_start();
237238
if let Some(rest) = rest.strip_prefix('=') {
238239
let rest = rest.trim_start();
239-
let val = if rest.starts_with('"') {
240-
let inner = &rest[1..];
240+
let val = if let Some(inner) = rest.strip_prefix('"') {
241241
let end = inner.find('"').unwrap_or(inner.len());
242242
&inner[..end]
243-
} else if rest.starts_with('\'') {
244-
let inner = &rest[1..];
243+
} else if let Some(inner) = rest.strip_prefix('\'') {
245244
let end = inner.find('\'').unwrap_or(inner.len());
246245
&inner[..end]
247246
} else {
@@ -492,8 +491,8 @@ pub type CidResolver = Box<dyn Fn(&str) -> Option<Vec<u8>>>;
492491
/// - Remote URLs return `None` when no fetcher is given (no external fetching by default).
493492
pub fn resolve_image_uri(
494493
uri: &str,
495-
cid_resolver: Option<&dyn Fn(&str) -> Option<Vec<u8>>>,
496-
url_fetcher: Option<&dyn Fn(&str) -> Option<Vec<u8>>>,
494+
cid_resolver: ByteResolver<'_>,
495+
url_fetcher: ByteResolver<'_>,
497496
) -> Option<Vec<u8>> {
498497
if uri.starts_with("data:") {
499498
decode_data_uri(uri)
@@ -544,16 +543,14 @@ fn preprocess_body_bgcolor(html: &str) -> String {
544543
let rest = rest.trim_start();
545544

546545
// Extract the value (may be quoted or unquoted)
547-
let (value, attr_end_offset) = if rest.starts_with('"') {
548-
let inner = &rest[1..];
546+
let (value, attr_end_offset) = if let Some(inner) = rest.strip_prefix('"') {
549547
let end = inner.find('"').unwrap_or(inner.len());
550548
(
551549
&tag[bg_pos + 7 + (tag_lower.len() - bg_pos - 7 - rest.len()) + 1
552550
..bg_pos + 7 + (tag_lower.len() - bg_pos - 7 - rest.len()) + 1 + end],
553551
end + 2,
554552
)
555-
} else if rest.starts_with('\'') {
556-
let inner = &rest[1..];
553+
} else if let Some(inner) = rest.strip_prefix('\'') {
557554
let end = inner.find('\'').unwrap_or(inner.len());
558555
(
559556
&tag[bg_pos + 7 + (tag_lower.len() - bg_pos - 7 - rest.len()) + 1
@@ -659,16 +656,14 @@ fn preprocess_cellpadding(html: &str) -> String {
659656
}
660657
let rest = rest[1..].trim_start();
661658

662-
let (value, _val_len) = if rest.starts_with('"') {
663-
let inner = &rest[1..];
659+
let (value, _val_len) = if let Some(inner) = rest.strip_prefix('"') {
664660
let end = inner.find('"').unwrap_or(inner.len());
665661
(
666662
&html[abs_pos + 11 + (lower.len() - abs_pos - 11 - rest.len()) + 1
667663
..abs_pos + 11 + (lower.len() - abs_pos - 11 - rest.len()) + 1 + end],
668664
end + 2,
669665
)
670-
} else if rest.starts_with('\'') {
671-
let inner = &rest[1..];
666+
} else if let Some(inner) = rest.strip_prefix('\'') {
672667
let end = inner.find('\'').unwrap_or(inner.len());
673668
(
674669
&html[abs_pos + 11 + (lower.len() - abs_pos - 11 - rest.len()) + 1
@@ -773,8 +768,8 @@ pub struct PreparedHtml {
773768
/// Without a fetcher, remote URIs are skipped (no external fetching by default).
774769
pub fn prepare_html(
775770
raw: &[u8],
776-
cid_resolver: Option<&dyn Fn(&str) -> Option<Vec<u8>>>,
777-
url_fetcher: Option<&dyn Fn(&str) -> Option<Vec<u8>>>,
771+
cid_resolver: ByteResolver<'_>,
772+
url_fetcher: ByteResolver<'_>,
778773
) -> PreparedHtml {
779774
let decoded = decode_html(raw);
780775
let preprocessed = preprocess_attrs(&decoded);
@@ -794,12 +789,10 @@ pub fn prepare_html(
794789
}
795790

796791
let rest = &sanitized[abs_pos..];
797-
let (uri, _) = if rest.starts_with('"') {
798-
let inner = &rest[1..];
792+
let (uri, _) = if let Some(inner) = rest.strip_prefix('"') {
799793
let end = inner.find('"').unwrap_or(inner.len());
800794
(&inner[..end], end + 2)
801-
} else if rest.starts_with('\'') {
802-
let inner = &rest[1..];
795+
} else if let Some(inner) = rest.strip_prefix('\'') {
803796
let end = inner.find('\'').unwrap_or(inner.len());
804797
(&inner[..end], end + 2)
805798
} else {

litehtml/src/lib.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,14 @@ pub trait DocumentContainer {
12471247
fn text_width(&self, text: &str, font: FontHandle) -> f32;
12481248

12491249
/// Draw `text` at `pos` using `font` and `color`.
1250-
fn draw_text(&mut self, hdc: DrawContext, text: &str, font: FontHandle, color: Color, pos: Position);
1250+
fn draw_text(
1251+
&mut self,
1252+
hdc: DrawContext,
1253+
text: &str,
1254+
font: FontHandle,
1255+
color: Color,
1256+
pos: Position,
1257+
);
12511258

12521259
/// Convert typographic points to device pixels.
12531260
fn pt_to_px(&self, pt: f32) -> f32 {
@@ -1276,7 +1283,8 @@ pub trait DocumentContainer {
12761283
}
12771284

12781285
/// Draw a background image layer.
1279-
fn draw_image(&mut self, hdc: DrawContext, layer: &BackgroundLayer, url: &str, base_url: &str) {}
1286+
fn draw_image(&mut self, hdc: DrawContext, layer: &BackgroundLayer, url: &str, base_url: &str) {
1287+
}
12801288

12811289
/// Draw a solid color background layer.
12821290
fn draw_solid_fill(&mut self, hdc: DrawContext, layer: &BackgroundLayer, color: Color) {}
@@ -1309,7 +1317,14 @@ pub trait DocumentContainer {
13091317
}
13101318

13111319
/// Draw element borders.
1312-
fn draw_borders(&mut self, hdc: DrawContext, borders: &Borders, draw_pos: Position, root: bool) {}
1320+
fn draw_borders(
1321+
&mut self,
1322+
hdc: DrawContext,
1323+
borders: &Borders,
1324+
draw_pos: Position,
1325+
root: bool,
1326+
) {
1327+
}
13131328

13141329
/// Set the document title.
13151330
fn set_caption(&mut self, caption: &str) {}
@@ -1499,9 +1514,13 @@ unsafe extern "C" fn cb_draw_text(
14991514
let _ = catch_unwind(AssertUnwindSafe(|| {
15001515
let bridge = bridge_from_user_data(user_data);
15011516
let text = c_str_to_str(text);
1502-
bridge
1503-
.container
1504-
.draw_text(DrawContext(hdc), text, FontHandle(h_font), Color::from(color), Position::from(pos));
1517+
bridge.container.draw_text(
1518+
DrawContext(hdc),
1519+
text,
1520+
FontHandle(h_font),
1521+
Color::from(color),
1522+
Position::from(pos),
1523+
);
15051524
}));
15061525
}
15071526

@@ -1586,7 +1605,9 @@ unsafe extern "C" fn cb_draw_image(
15861605
let layer = BackgroundLayer::from_ptr(layer);
15871606
let url = c_str_to_str(url);
15881607
let base_url = c_str_to_str(base_url);
1589-
bridge.container.draw_image(DrawContext(hdc), &layer, url, base_url);
1608+
bridge
1609+
.container
1610+
.draw_image(DrawContext(hdc), &layer, url, base_url);
15901611
}));
15911612
}
15921613

@@ -1647,7 +1668,9 @@ unsafe extern "C" fn cb_draw_conic_gradient(
16471668
let bridge = bridge_from_user_data(user_data);
16481669
let layer = BackgroundLayer::from_ptr(layer);
16491670
let gradient = ConicGradient::from_ptr(gradient);
1650-
bridge.container.draw_conic_gradient(DrawContext(hdc), &layer, &gradient);
1671+
bridge
1672+
.container
1673+
.draw_conic_gradient(DrawContext(hdc), &layer, &gradient);
16511674
}));
16521675
}
16531676

@@ -1661,9 +1684,12 @@ unsafe extern "C" fn cb_draw_borders(
16611684
let _ = catch_unwind(AssertUnwindSafe(|| {
16621685
let bridge = bridge_from_user_data(user_data);
16631686
let borders = Borders::from(borders);
1664-
bridge
1665-
.container
1666-
.draw_borders(DrawContext(hdc), &borders, Position::from(draw_pos), root != 0);
1687+
bridge.container.draw_borders(
1688+
DrawContext(hdc),
1689+
&borders,
1690+
Position::from(draw_pos),
1691+
root != 0,
1692+
);
16671693
}));
16681694
}
16691695

0 commit comments

Comments
 (0)