Skip to content

Lightpanda: the headless browser designed for AI and automation

cargo fmt

Karl Seguin49d473d845c7
diff --git a/src/rust/ffi/lib.rs b/src/rust/ffi/lib.rs
index ebb8b9961..bdf5bff88 100644
--- a/src/rust/ffi/lib.rs
+++ b/src/rust/ffi/lib.rs
@@ -20,8 +20,8 @@
//! entry points; re-exporting them here keeps their symbols in the archive.
//! Process-wide concerns (the allocator) live here, not in a domain crate.
-pub use lightpanda_render::*;
pub use lightpanda_html5ever::*;
+pub use lightpanda_render::*;
#[cfg(feature = "memstats")]
#[global_allocator]
diff --git a/src/rust/html5ever/lib.rs b/src/rust/html5ever/lib.rs
index e5db80fd2..fea23d236 100644
--- a/src/rust/html5ever/lib.rs
+++ b/src/rust/html5ever/lib.rs
@@ -541,7 +541,10 @@ pub extern "C" fn html5ever_parse_fragment(
qname: context_qname.clone(),
mathml_annotation_xml_integration_point: false,
});
- let mut context_attrs = CAttributeIterator { vec: vec![], pos: 0 };
+ let mut context_attrs = CAttributeIterator {
+ vec: vec![],
+ pos: 0,
+ };
let context_elem = unsafe {
(create_context_element_callback)(
ctx,
diff --git a/src/rust/html5ever/sink.rs b/src/rust/html5ever/sink.rs
index b47d8d966..2d3b48bb1 100644
--- a/src/rust/html5ever/sink.rs
+++ b/src/rust/html5ever/sink.rs
@@ -16,16 +16,16 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use std::ptr;
-use std::cell::Cell;
use std::borrow::Cow;
-use std::os::raw::{c_void};
+use std::cell::Cell;
+use std::os::raw::c_void;
+use std::ptr;
use crate::types::*;
-use html5ever::tendril::{StrTendril};
-use html5ever::{Attribute, QualName};
use html5ever::interface::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
+use html5ever::tendril::StrTendril;
+use html5ever::{Attribute, QualName};
type Arena<'arena> = &'arena typed_arena::Arena<ElementData>;
@@ -142,15 +142,24 @@ impl<'arena> TreeSink for Sink<'arena> {
}
fn create_comment(&self, txt: StrTendril) -> Ref {
- let str = StringSlice{ ptr: txt.as_ptr(), len: txt.len()};
+ let str = StringSlice {
+ ptr: txt.as_ptr(),
+ len: txt.len(),
+ };
unsafe {
return (self.create_comment_callback)(self.ctx, str);
}
}
fn create_pi(&self, target: StrTendril, data: StrTendril) -> Ref {
- let str_target = StringSlice{ ptr: target.as_ptr(), len: target.len()};
- let str_data = StringSlice{ ptr: data.as_ptr(), len: data.len()};
+ let str_target = StringSlice {
+ ptr: target.as_ptr(),
+ len: target.len(),
+ };
+ let str_data = StringSlice {
+ ptr: data.as_ptr(),
+ len: data.len(),
+ };
unsafe {
return (self.create_processing_instruction)(self.ctx, str_target, str_data);
}
@@ -169,24 +178,33 @@ impl<'arena> TreeSink for Sink<'arena> {
// you'll occasionally see that zeroed memory. Makes no sense to
// me, but a far as I can tell, this version works.
let byte_slice = t.as_ref().as_bytes();
- let static_slice: &'static [u8] = unsafe {
- std::mem::transmute(byte_slice)
- };
+ let static_slice: &'static [u8] = unsafe { std::mem::transmute(byte_slice) };
unsafe {
- (self.append_callback)(self.ctx, *parent, CNodeOrText{
- tag: 1,
- node: ptr::null(),
- text: StringSlice { ptr: static_slice.as_ptr(), len: static_slice.len()},
- });
+ (self.append_callback)(
+ self.ctx,
+ *parent,
+ CNodeOrText {
+ tag: 1,
+ node: ptr::null(),
+ text: StringSlice {
+ ptr: static_slice.as_ptr(),
+ len: static_slice.len(),
+ },
+ },
+ );
};
- },
+ }
NodeOrText::AppendNode(node) => {
- unsafe {
- (self.append_callback)(self.ctx, *parent, CNodeOrText{
- tag: 0,
- node: node,
- text: StringSlice::default()
- });
+ unsafe {
+ (self.append_callback)(
+ self.ctx,
+ *parent,
+ CNodeOrText {
+ tag: 0,
+ node: node,
+ text: StringSlice::default(),
+ },
+ );
};
}
}
@@ -196,24 +214,33 @@ impl<'arena> TreeSink for Sink<'arena> {
match child {
NodeOrText::AppendText(ref t) => {
let byte_slice = t.as_ref().as_bytes();
- let static_slice: &'static [u8] = unsafe {
- std::mem::transmute(byte_slice)
- };
+ let static_slice: &'static [u8] = unsafe { std::mem::transmute(byte_slice) };
unsafe {
- (self.append_before_sibling_callback)(self.ctx, *sibling, CNodeOrText{
- tag: 1,
- node: ptr::null(),
- text: StringSlice { ptr: static_slice.as_ptr(), len: static_slice.len()},
- });
+ (self.append_before_sibling_callback)(
+ self.ctx,
+ *sibling,
+ CNodeOrText {
+ tag: 1,
+ node: ptr::null(),
+ text: StringSlice {
+ ptr: static_slice.as_ptr(),
+ len: static_slice.len(),
+ },
+ },
+ );
};
- },
+ }
NodeOrText::AppendNode(node) => {
- unsafe {
- (self.append_before_sibling_callback)(self.ctx, *sibling, CNodeOrText{
- tag: 0,
- node: node,
- text: StringSlice::default()
- });
+ unsafe {
+ (self.append_before_sibling_callback)(
+ self.ctx,
+ *sibling,
+ CNodeOrText {
+ tag: 0,
+ node: node,
+ text: StringSlice::default(),
+ },
+ );
};
}
}
@@ -228,24 +255,35 @@ impl<'arena> TreeSink for Sink<'arena> {
match child {
NodeOrText::AppendText(ref t) => {
let byte_slice = t.as_ref().as_bytes();
- let static_slice: &'static [u8] = unsafe {
- std::mem::transmute(byte_slice)
- };
+ let static_slice: &'static [u8] = unsafe { std::mem::transmute(byte_slice) };
unsafe {
- (self.append_based_on_parent_node_callback)(self.ctx, *element, *prev_element, CNodeOrText{
- tag: 1,
- node: ptr::null(),
- text: StringSlice { ptr: static_slice.as_ptr(), len: static_slice.len()},
- });
+ (self.append_based_on_parent_node_callback)(
+ self.ctx,
+ *element,
+ *prev_element,
+ CNodeOrText {
+ tag: 1,
+ node: ptr::null(),
+ text: StringSlice {
+ ptr: static_slice.as_ptr(),
+ len: static_slice.len(),
+ },
+ },
+ );
};
- },
+ }
NodeOrText::AppendNode(node) => {
- unsafe {
- (self.append_based_on_parent_node_callback)(self.ctx, *element, *prev_element, CNodeOrText{
- tag: 0,
- node: node,
- text: StringSlice::default()
- });
+ unsafe {
+ (self.append_based_on_parent_node_callback)(
+ self.ctx,
+ *element,
+ *prev_element,
+ CNodeOrText {
+ tag: 0,
+ node: node,
+ text: StringSlice::default(),
+ },
+ );
};
}
}
@@ -257,9 +295,18 @@ impl<'arena> TreeSink for Sink<'arena> {
public_id: StrTendril,
system_id: StrTendril,
) {
- let name_str = StringSlice{ ptr: name.as_ptr(), len: name.len()};
- let public_id_str = StringSlice{ ptr: public_id.as_ptr(), len: public_id.len()};
- let system_id_str = StringSlice{ ptr: system_id.as_ptr(), len: system_id.len()};
+ let name_str = StringSlice {
+ ptr: name.as_ptr(),
+ len: name.len(),
+ };
+ let public_id_str = StringSlice {
+ ptr: public_id.as_ptr(),
+ len: public_id.len(),
+ };
+ let system_id_str = StringSlice {
+ ptr: system_id.as_ptr(),
+ len: system_id.len(),
+ };
unsafe {
(self.append_doctype_to_document)(self.ctx, name_str, public_id_str, system_id_str);
}
@@ -293,7 +340,12 @@ impl<'arena> TreeSink for Sink<'arena> {
self.allow_declarative_shadow
}
- fn attach_declarative_shadow(&self, location: &Ref, template: &Ref, attrs: &[Attribute]) -> bool {
+ fn attach_declarative_shadow(
+ &self,
+ location: &Ref,
+ template: &Ref,
+ attrs: &[Attribute],
+ ) -> bool {
// html5ever only calls this when shadowrootmode is "open" or "closed",
// so anything other than "open" is treated as "closed".
let mode_is_open = attrs
diff --git a/src/rust/html5ever/types.rs b/src/rust/html5ever/types.rs
index 13eb76a4f..e18c08cf0 100644
--- a/src/rust/html5ever/types.rs
+++ b/src/rust/html5ever/types.rs
@@ -16,9 +16,9 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use std::ptr;
-use html5ever::{QualName, Attribute};
+use html5ever::{Attribute, QualName};
use std::os::raw::{c_uchar, c_void};
+use std::ptr;
pub type CreateElementCallback = unsafe extern "C" fn(
ctx: Ref,
@@ -27,10 +27,7 @@ pub type CreateElementCallback = unsafe extern "C" fn(
attributes: *mut c_void,
) -> Ref;
-pub type CreateCommentCallback = unsafe extern "C" fn(
- ctx: Ref,
- str: StringSlice,
-) -> Ref;
+pub type CreateCommentCallback = unsafe extern "C" fn(ctx: Ref, str: StringSlice) -> Ref;
pub type AppendDoctypeToDocumentCallback = unsafe extern "C" fn(
ctx: Ref,
@@ -39,54 +36,39 @@ pub type AppendDoctypeToDocumentCallback = unsafe extern "C" fn(
system_id: StringSlice,
) -> ();
-pub type CreateProcessingInstruction = unsafe extern "C" fn(
- ctx: Ref,
- target: StringSlice,
- data: StringSlice,
-) -> Ref;
+pub type CreateProcessingInstruction =
+ unsafe extern "C" fn(ctx: Ref, target: StringSlice, data: StringSlice) -> Ref;
pub type GetDataCallback = unsafe extern "C" fn(ctx: Ref) -> *mut c_void;
-pub type AppendCallback = unsafe extern "C" fn(
- ctx: Ref,
- parent: Ref,
- node_or_text: CNodeOrText
-) -> ();
+pub type AppendCallback =
+ unsafe extern "C" fn(ctx: Ref, parent: Ref, node_or_text: CNodeOrText) -> ();
pub type ParseErrorCallback = unsafe extern "C" fn(ctx: Ref, str: StringSlice) -> ();
pub type PopCallback = unsafe extern "C" fn(ctx: Ref, node: Ref) -> ();
-pub type AddAttrsIfMissingCallback = unsafe extern "C" fn(
- ctx: Ref,
- target: Ref,
- attributes: *mut c_void,
-) -> ();
+pub type AddAttrsIfMissingCallback =
+ unsafe extern "C" fn(ctx: Ref, target: Ref, attributes: *mut c_void) -> ();
pub type GetTemplateContentsCallback = unsafe extern "C" fn(ctx: Ref, target: Ref) -> Ref;
-pub type AttachDeclarativeShadowCallback = unsafe extern "C" fn(
- ctx: Ref,
- host: Ref,
- template: Ref,
- mode_is_open: u8,
-) -> u8;
+pub type AttachDeclarativeShadowCallback =
+ unsafe extern "C" fn(ctx: Ref, host: Ref, template: Ref, mode_is_open: u8) -> u8;
pub type RemoveFromParentCallback = unsafe extern "C" fn(ctx: Ref, target: Ref) -> ();
-pub type ReparentChildrenCallback = unsafe extern "C" fn(ctx: Ref, node: Ref, new_parent: Ref) -> ();
+pub type ReparentChildrenCallback =
+ unsafe extern "C" fn(ctx: Ref, node: Ref, new_parent: Ref) -> ();
-pub type AppendBeforeSiblingCallback = unsafe extern "C" fn(
- ctx: Ref,
- sibling: Ref,
- node_or_text: CNodeOrText
-) -> ();
+pub type AppendBeforeSiblingCallback =
+ unsafe extern "C" fn(ctx: Ref, sibling: Ref, node_or_text: CNodeOrText) -> ();
pub type AppendBasedOnParentNodeCallback = unsafe extern "C" fn(
ctx: Ref,
element: Ref,
prev_element: Ref,
- node_or_text: CNodeOrText
+ node_or_text: CNodeOrText,
) -> ();
pub type Ref = *const c_void;
@@ -98,11 +80,14 @@ pub struct CNullable<T> {
}
impl<T: Default> CNullable<T> {
pub fn none() -> CNullable<T> {
- return Self{tag: 0, value: T::default()};
+ return Self {
+ tag: 0,
+ value: T::default(),
+ };
}
pub fn some(v: T) -> CNullable<T> {
- return Self{tag: 1, value: v};
+ return Self { tag: 1, value: v };
}
}
@@ -113,7 +98,10 @@ pub struct Slice<T> {
}
impl<T> Default for Slice<T> {
fn default() -> Self {
- return Self{ptr: ptr::null(), len: 0};
+ return Self {
+ ptr: ptr::null(),
+ len: 0,
+ };
}
}
@@ -127,13 +115,22 @@ pub struct CQualName {
}
impl CQualName {
pub fn create(q: &QualName) -> Self {
- let ns = StringSlice { ptr: q.ns.as_ptr(), len: q.ns.len()};
- let local = StringSlice { ptr: q.local.as_ptr(), len: q.local.len()};
+ let ns = StringSlice {
+ ptr: q.ns.as_ptr(),
+ len: q.ns.len(),
+ };
+ let local = StringSlice {
+ ptr: q.local.as_ptr(),
+ len: q.local.len(),
+ };
let prefix = match &q.prefix {
None => CNullable::<StringSlice>::none(),
- Some(prefix) => CNullable::<StringSlice>::some(StringSlice { ptr: prefix.as_ptr(), len: prefix.len()}),
+ Some(prefix) => CNullable::<StringSlice>::some(StringSlice {
+ ptr: prefix.as_ptr(),
+ len: prefix.len(),
+ }),
};
- CQualName{
+ CQualName {
// inner: q as *const _ as *const c_void,
ns: ns,
local: local,
@@ -143,7 +140,7 @@ impl CQualName {
}
impl Default for CQualName {
fn default() -> Self {
- Self{
+ Self {
prefix: CNullable::<StringSlice>::none(),
ns: StringSlice::default(),
local: StringSlice::default(),
@@ -158,7 +155,10 @@ pub struct CAttribute {
}
impl Default for CAttribute {
fn default() -> Self {
- return Self{name: CQualName::default(), value: StringSlice::default()};
+ return Self {
+ name: CQualName::default(),
+ value: StringSlice::default(),
+ };
}
}
diff --git a/src/rust/render/lib.rs b/src/rust/render/lib.rs
index 89a8ef0c7..dbb398fda 100644
--- a/src/rust/render/lib.rs
+++ b/src/rust/render/lib.rs
@@ -30,9 +30,9 @@ use std::sync::{Mutex, OnceLock};
use fontique::{Blob, Collection, CollectionOptions, GenericFamily, SourceCache};
use parley::{
- Alignment, AlignmentOptions, FontContext, FontFamily, FontStyle, FontWeight, GlyphRun,
- Layout, LayoutContext, LineHeight, OverflowWrap, PositionedLayoutItem, StyleProperty,
- TextStyle, WhiteSpaceCollapse,
+ Alignment, AlignmentOptions, FontContext, FontFamily, FontStyle, FontWeight, GlyphRun, Layout,
+ LayoutContext, LineHeight, OverflowWrap, PositionedLayoutItem, StyleProperty, TextStyle,
+ WhiteSpaceCollapse,
};
use skrifa::instance::{LocationRef, Size};
use skrifa::outline::{DrawSettings, OutlinePen};
@@ -161,10 +161,14 @@ unsafe fn render_png(
let Ok(text) = std::str::from_utf8(bytes) else {
return RC_INVALID;
};
- out_spans.push(Span { text, flags: s.flags, color: s.color });
+ out_spans.push(Span {
+ text,
+ flags: s.flags,
+ color: s.color,
+ });
}
- let marker = std::str::from_utf8(std::slice::from_raw_parts(b.marker, b.marker_len))
- .unwrap_or("");
+ let marker =
+ std::str::from_utf8(std::slice::from_raw_parts(b.marker, b.marker_len)).unwrap_or("");
doc.push(Block {
kind: b.kind,
level: b.level,
@@ -182,7 +186,11 @@ unsafe fn render_png(
.lock()
.unwrap_or_else(|e| e.into_inner());
- let mut sink = Sink { ctx, write, failed: false };
+ let mut sink = Sink {
+ ctx,
+ write,
+ failed: false,
+ };
let (h, rc) = r.render(&doc, opts, &mut sink);
if !content_height.is_null() {
*content_height = h;
@@ -342,7 +350,8 @@ impl Renderer {
};
prev_after = Some(after);
- let indent = block.list_depth as f32 * LIST_INDENT + block.quote_depth as f32 * QUOTE_INDENT;
+ let indent =
+ block.list_depth as f32 * LIST_INDENT + block.quote_depth as f32 * QUOTE_INDENT;
let quote_x = PAGE_MARGIN + block.list_depth as f32 * LIST_INDENT;
if block.kind == BLOCK_RULE {
@@ -393,9 +402,9 @@ impl Renderer {
props.push(StyleProperty::Strikethrough(true));
}
if span.flags & SPAN_MONO != 0 && !is_pre {
- props.push(StyleProperty::FontFamily(FontFamily::Source(Cow::Borrowed(
- "monospace",
- ))));
+ props.push(StyleProperty::FontFamily(FontFamily::Source(
+ Cow::Borrowed("monospace"),
+ )));
props.push(StyleProperty::FontSize(size * 0.9));
}
if span.flags & SPAN_HAS_COLOR != 0 {
@@ -420,7 +429,9 @@ impl Renderer {
let marker = if block.marker.is_empty() {
None
} else {
- let mut b = self.lcx.ranged_builder(&mut self.fcx, block.marker, scale, true);
+ let mut b = self
+ .lcx
+ .ranged_builder(&mut self.fcx, block.marker, scale, true);
b.push_default(StyleProperty::FontSize(BASE_SIZE));
b.push_default(StyleProperty::Brush(TEXT_COLOR));
let mut ml = b.build(block.marker);
@@ -450,7 +461,11 @@ impl Renderer {
// Rasterize the requested strip (viewport or full page), then crop.
// A clip reaching past the strip extends it, but never past the
// content.
- let mut out_h = if opts.height == 0 { content_h } else { opts.height };
+ let mut out_h = if opts.height == 0 {
+ content_h
+ } else {
+ opts.height
+ };
if opts.clip_w > 0.0 {
let reach = (opts.clip_y as f64 + opts.clip_h as f64).ceil();
// A NaN reach clamps to NaN and casts to 0, leaving out_h alone.
@@ -542,7 +557,14 @@ impl Renderer {
w.write_image_data(view.data())?;
w.finish()
})();
- (content_h, if encoded.is_ok() { RC_OK } else { RC_ENCODE_FAILED })
+ (
+ content_h,
+ if encoded.is_ok() {
+ RC_OK
+ } else {
+ RC_ENCODE_FAILED
+ },
+ )
}
fn draw_layout(&mut self, pixmap: &mut Pixmap, layout: &Layout<Rgb>, ox: f32, oy: f32) {
@@ -628,14 +650,19 @@ impl Renderer {
let x0 = ox + gr.offset();
for (deco, default_off, default_size) in [
(&style.underline, m.underline_offset, m.underline_size),
- (&style.strikethrough, m.strikethrough_offset, m.strikethrough_size),
+ (
+ &style.strikethrough,
+ m.strikethrough_offset,
+ m.strikethrough_size,
+ ),
] {
let Some(d) = deco else { continue };
let off = d.offset.unwrap_or(default_off);
let sz = d.size.unwrap_or(default_size).max(1.0);
let mut p2 = Paint::default();
p2.set_color_rgba8(d.brush.0, d.brush.1, d.brush.2, 255);
- if let Some(r) = Rect::from_xywh(x0, (baseline - off).round(), gr.advance(), sz.round()) {
+ if let Some(r) = Rect::from_xywh(x0, (baseline - off).round(), gr.advance(), sz.round())
+ {
pixmap.fill_rect(r, &p2, Transform::identity(), None);
}
}