Repository navigation
Expand file tree
/
Copy pathhtmlTransform.zig
More file actions
53 lines (42 loc) · 1.61 KB
/
Copy pathhtmlTransform.zig
File metadata and controls
53 lines (42 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Example: HTML DOM Mutation and Serialization
//!
//! Demonstrates:
//! 1. Parsing an HTML fragment
//! 2. Mutating attributes and text nodes safely
//! 3. Serializing the modified DOM tree back to canonical XSS-safe HTML
//!
//! Run with: `zig build run-html-transform`
const std = @import("std");
const httpx = @import("httpx");
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
std.debug.print("==> HTTPX HTML DOM Transformation & Serialization Demo\n\n", .{});
const initialHtml =
\\<div class="card" id="user-1">
\\ <h2 class="name">Old Name</h2>
\\ <p class="role">Guest</p>
\\</div>
;
var p = httpx.Parser.init(allocator, .{});
var doc = try p.parseHtml(initialHtml);
defer doc.deinit();
std.debug.print("Original HTML:\n{s}\n\n", .{initialHtml});
// 1. Modify attribute on container
if (try doc.getElementById("user-1")) |card| {
try card.setAttr("data-status", "verified");
}
// 2. Modify name text
if (try doc.selectFirst(".name")) |nameNode| {
try nameNode.replaceText("Jane Doe");
}
// 3. Modify role text with special characters needing escaping
if (try doc.selectFirst(".role")) |roleNode| {
try roleNode.replaceText("Senior Architect & Lead <Engineering>");
}
// 4. Serialize back to HTML
const serialized = try doc.serialize();
std.debug.print("Transformed & Serialized HTML (with XSS escaping):\n{s}\n", .{serialized});
std.debug.print("\nTransformation verification successful.\n", .{});
}