Skip to content

Commit b9ea2cf

Browse files
shadowfactsbitwalker
authored andcommitted
Add Document::print_node and C FFI for it
1 parent 392bdd2 commit b9ea2cf

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

crates/core/c_src/include/LiveViewNativeCore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ typedef struct __Node {
4242

4343
extern _RustString __liveview_native_core$Document$to_string(__Document doc);
4444

45+
extern _RustString __liveview_native_core$Document$node_to_string(__Document doc, NodeRef node);
46+
4547
extern __Document __liveview_native_core$Document$empty();
4648

4749
extern void __liveview_native_core$Document$drop(__Document doc);

crates/core/src/dom/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,17 @@ impl Document {
557557

558558
/// Prints this document using the given writer and options
559559
pub fn print(&self, writer: &mut dyn fmt::Write, options: PrintOptions) -> fmt::Result {
560-
let printer = Printer::new(self, self.root, options);
560+
self.print_node(self.root, writer, options)
561+
}
562+
563+
/// Prints a node in this document using the given writer and options
564+
pub fn print_node(
565+
&self,
566+
node: NodeRef,
567+
writer: &mut dyn fmt::Write,
568+
options: PrintOptions,
569+
) -> fmt::Result {
570+
let printer = Printer::new(self, node, options);
561571
printer.print(writer)
562572
}
563573
}

crates/core/src/ffi/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ pub extern "C" fn document_to_string(doc: *mut dom::Document) -> RustString {
130130
RustString::from_string(doc.to_string())
131131
}
132132

133+
#[export_name = "__liveview_native_core$Document$node_to_string"]
134+
pub extern "C" fn document_node_to_string(doc: *mut dom::Document, node: NodeRef) -> RustString {
135+
let doc = unsafe { &*doc };
136+
let mut buf = String::new();
137+
doc.print_node(node, &mut buf, dom::PrintOptions::Pretty)
138+
.expect("error printing node");
139+
RustString::from_string(buf)
140+
}
141+
133142
#[export_name = "__liveview_native_core$Document$merge"]
134143
pub extern "C" fn document_merge(doc: *mut dom::Document, other: *const dom::Document) -> bool {
135144
let doc = unsafe { &mut *doc };

0 commit comments

Comments
 (0)