forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLScanner.zig
More file actions
308 lines (278 loc) · 10.3 KB
/
Copy pathHTMLScanner.zig
File metadata and controls
308 lines (278 loc) · 10.3 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const HTMLScanner = @This();
allocator: std.mem.Allocator,
import_records: ImportRecord.List = .{},
log: *logger.Log,
source: *const logger.Source,
pub fn init(allocator: std.mem.Allocator, log: *logger.Log, source: *const logger.Source) HTMLScanner {
return .{
.allocator = allocator,
.import_records = .{},
.log = log,
.source = source,
};
}
pub fn deinit(this: *HTMLScanner) void {
for (this.import_records.slice()) |*record| {
this.allocator.free(record.path.text);
}
this.import_records.deinit(this.allocator);
}
fn createImportRecord(this: *HTMLScanner, input_path: []const u8, kind: ImportKind) !void {
// In HTML, sometimes people do /src/index.js
// In that case, we don't want to use the absolute filesystem path, we want to use the path relative to the project root
const path_to_use = if (input_path.len > 1 and input_path[0] == '/')
bun.path.joinAbsString(bun.fs.FileSystem.instance.top_level_dir, &[_][]const u8{input_path[1..]}, .auto)
// Check if imports to (e.g) "App.tsx" are actually relative imoprts w/o the "./"
else if (input_path.len > 2 and input_path[0] != '.' and input_path[1] != '/') blk: {
const index_of_dot = std.mem.lastIndexOfScalar(u8, input_path, '.') orelse break :blk input_path;
const ext = input_path[index_of_dot..];
if (ext.len > 4) break :blk input_path;
// /foo/bar/index.html -> /foo/bar
const dirname: []const u8 = std.fs.path.dirname(this.source.path.text) orelse break :blk input_path;
const resolved = bun.path.joinAbsString(dirname, &[_][]const u8{input_path}, .auto);
break :blk if (bun.sys.exists(resolved)) resolved else input_path;
} else input_path;
const record = ImportRecord{
.path = fs.Path.init(try this.allocator.dupeZ(u8, path_to_use)),
.kind = kind,
.range = logger.Range.None,
};
try this.import_records.append(this.allocator, record);
}
const debug = bun.Output.scoped(.HTMLScanner, .hidden);
pub fn onWriteHTML(_: *HTMLScanner, bytes: []const u8) void {
_ = bytes; // bytes are not written in scan phase
}
pub fn onHTMLParseError(this: *HTMLScanner, message: []const u8) void {
this.log.addError(
this.source,
logger.Loc.Empty,
message,
) catch |err| bun.handleOom(err);
}
pub fn onTag(this: *HTMLScanner, _: *lol.Element, path: []const u8, url_attribute: []const u8, kind: ImportKind) void {
_ = url_attribute;
this.createImportRecord(path, kind) catch {};
}
const processor = HTMLProcessor(HTMLScanner, false);
pub fn scan(this: *HTMLScanner, input: []const u8) !void {
try processor.run(this, input);
}
pub fn HTMLProcessor(
comptime T: type,
/// If the visitor should visit html, head, body
comptime visit_document_tags: bool,
) type {
return struct {
const TagHandler = struct {
/// CSS selector to match elements
selector: []const u8,
/// Whether this tag can have text content that needs to be processed
has_content: bool = false,
/// The attribute to extract the URL from
url_attribute: []const u8,
/// The kind of import to create
kind: ImportKind,
is_head_or_html: bool = false,
};
const tag_handlers = [_]TagHandler{
// Module scripts with src
.{
.selector = "script[src]",
.has_content = false,
.url_attribute = "src",
.kind = .stmt,
},
// CSS Stylesheets
.{
.selector = "link[rel='stylesheet'][href]",
.url_attribute = "href",
.kind = .at,
},
// CSS Assets
.{
.selector = "link[as='style'][href]",
.url_attribute = "href",
.kind = .at,
},
// Font files
.{
.selector = "link[as='font'][href], link[type^='font/'][href]",
.url_attribute = "href",
.kind = .url,
},
// Image assets
.{
.selector = "link[as='image'][href]",
.url_attribute = "href",
.kind = .url,
},
// Audio/Video assets
.{
.selector = "link[as='video'][href], link[as='audio'][href]",
.url_attribute = "href",
.kind = .url,
},
// Web Workers
.{
.selector = "link[as='worker'][href]",
.url_attribute = "href",
.kind = .stmt,
},
// Manifest files
.{
.selector = "link[rel='manifest'][href]",
.url_attribute = "href",
.kind = .url,
},
// Icons
.{
.selector = "link[rel='icon'][href], link[rel='apple-touch-icon'][href]",
.url_attribute = "href",
.kind = .url,
},
// Images with src
.{
.selector = "img[src]",
.url_attribute = "src",
.kind = .url,
},
// Images with srcset
.{
.selector = "img[srcset]",
.url_attribute = "srcset",
.kind = .url,
},
// Videos with src
.{
.selector = "video[src]",
.url_attribute = "src",
.kind = .url,
},
// Videos with poster
.{
.selector = "video[poster]",
.url_attribute = "poster",
.kind = .url,
},
// Audio with src
.{
.selector = "audio[src]",
.url_attribute = "src",
.kind = .url,
},
// Source elements with src
.{
.selector = "source[src]",
.url_attribute = "src",
.kind = .url,
},
// Source elements with srcset
.{
.selector = "source[srcset]",
.url_attribute = "srcset",
.kind = .url,
},
// // Iframes
// .{
// .selector = "iframe[src]",
// .url_attribute = "src",
// .kind = .url,
// },
};
fn generateHandlerForTag(comptime tag_info: TagHandler) fn (*T, *lol.Element) bool {
const Handler = struct {
pub fn handle(this: *T, element: *lol.Element) bool {
// Handle URL attribute if present
if (tag_info.url_attribute.len > 0) {
if (element.hasAttribute(tag_info.url_attribute) catch false) {
const value = element.getAttribute(tag_info.url_attribute);
defer value.deinit();
if (value.len > 0) {
debug("{s} {s}", .{ tag_info.selector, value.slice() });
T.onTag(this, element, value.slice(), tag_info.url_attribute, tag_info.kind);
}
}
}
return false;
}
};
return Handler.handle;
}
pub fn run(this: *T, input: []const u8) !void {
var builder = lol.HTMLRewriter.Builder.init();
defer builder.deinit();
var selectors: bun.BoundedArray(*lol.HTMLSelector, tag_handlers.len + if (visit_document_tags) 3 else 0) = .{};
defer for (selectors.slice()) |selector| {
selector.deinit();
};
// Add handlers for each tag type
inline for (tag_handlers) |tag_info| {
const selector = try lol.HTMLSelector.parse(tag_info.selector);
selectors.appendAssumeCapacity(selector);
try builder.addElementContentHandlers(
selector,
T,
comptime generateHandlerForTag(tag_info),
this,
void,
null,
null,
void,
null,
null,
);
}
if (visit_document_tags) {
inline for (.{ "body", "head", "html" }, &.{ T.onBodyTag, T.onHeadTag, T.onHtmlTag }) |tag, cb| {
const head_selector = try lol.HTMLSelector.parse(tag);
selectors.appendAssumeCapacity(head_selector);
try builder.addElementContentHandlers(
head_selector,
T,
cb,
this,
void,
null,
null,
void,
null,
null,
);
}
}
const memory_settings = lol.MemorySettings{
.preallocated_parsing_buffer_size = @max(input.len / 4, 1024),
.max_allowed_memory_usage = 1024 * 1024 * 10,
};
errdefer {
const last_error = lol.HTMLString.lastError();
defer last_error.deinit();
if (last_error.len > 0) {
this.onHTMLParseError(last_error.slice());
}
}
var rewriter = try builder.build(
.UTF8,
memory_settings,
false,
T,
this,
T.onWriteHTML,
struct {
fn done(_: *T) void {}
}.done,
);
defer rewriter.deinit();
try rewriter.write(input);
try rewriter.end();
}
};
}
const lol = @import("./deps/lol-html.zig");
const std = @import("std");
const ImportKind = @import("./import_record.zig").ImportKind;
const ImportRecord = @import("./import_record.zig").ImportRecord;
const bun = @import("bun");
const fs = bun.fs;
const logger = bun.logger;