Skip to content

Commit 18ac7f9

Browse files
Add remove() and isRemoved in HTMLRewriterTypes.Doctype interface (#16031)
1 parent fe4176e commit 18ac7f9

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

packages/bun-types/html-rewriter.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ declare namespace HTMLRewriterTypes {
2626
readonly name: string | null;
2727
readonly publicId: string | null;
2828
readonly systemId: string | null;
29+
readonly removed: boolean;
30+
remove(): Doctype;
2931
}
3032

3133
interface DocumentEnd {

src/bun.js/api/html_rewriter.classes.ts

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ export default [
8181
getter: "publicId",
8282
cache: true,
8383
},
84+
remove: {
85+
fn: "remove",
86+
length: 0,
87+
},
88+
removed: {
89+
getter: "removed",
90+
},
8491
},
8592
}),
8693
define({

src/bun.js/api/html_rewriter.zig

+20
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,26 @@ pub const DocType = struct {
11571157
return JSValue.jsNull();
11581158
return ZigString.init(str).toJS(globalObject);
11591159
}
1160+
1161+
pub fn remove(
1162+
this: *DocType,
1163+
_: *JSGlobalObject,
1164+
callFrame: *JSC.CallFrame,
1165+
) bun.JSError!JSValue {
1166+
if (this.doctype == null)
1167+
return JSValue.jsUndefined();
1168+
this.doctype.?.remove();
1169+
return callFrame.this();
1170+
}
1171+
1172+
pub fn removed(
1173+
this: *DocType,
1174+
_: *JSGlobalObject,
1175+
) JSValue {
1176+
if (this.doctype == null)
1177+
return JSValue.jsUndefined();
1178+
return JSValue.jsBoolean(this.doctype.?.isRemoved());
1179+
}
11601180
};
11611181

11621182
pub const DocEnd = struct {

src/deps/lol-html.zig

+10
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,8 @@ pub const DocType = opaque {
796796
extern fn lol_html_doctype_system_id_get(doctype: *const DocType) HTMLString;
797797
extern fn lol_html_doctype_user_data_set(doctype: *const DocType, user_data: ?*anyopaque) void;
798798
extern fn lol_html_doctype_user_data_get(doctype: *const DocType) ?*anyopaque;
799+
extern fn lol_html_doctype_remove(doctype: *DocType) void;
800+
extern fn lol_html_doctype_is_removed(doctype: *const DocType) bool;
799801

800802
pub const Callback = *const fn (*DocType, ?*anyopaque) callconv(.C) Directive;
801803

@@ -811,6 +813,14 @@ pub const DocType = opaque {
811813
auto_disable();
812814
return this.lol_html_doctype_system_id_get();
813815
}
816+
pub fn remove(this: *DocType) void {
817+
auto_disable();
818+
return this.lol_html_doctype_remove();
819+
}
820+
pub fn isRemoved(this: *const DocType) bool {
821+
auto_disable();
822+
return this.lol_html_doctype_is_removed();
823+
}
814824
};
815825

816826
pub const Encoding = enum {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect, test, describe } from "bun:test";
2+
3+
describe("HTMLRewriter DOCTYPE handler", () => {
4+
test("remove and removed property work on DOCTYPE", () => {
5+
const html = "<!DOCTYPE html><html><head></head><body>Hello</body></html>";
6+
let sawDoctype = false;
7+
let wasRemoved = false;
8+
9+
const rewriter = new HTMLRewriter().onDocument({
10+
doctype(doctype) {
11+
sawDoctype = true;
12+
doctype.remove();
13+
wasRemoved = doctype.removed;
14+
},
15+
});
16+
17+
const result = rewriter.transform(html);
18+
19+
expect(sawDoctype).toBe(true);
20+
expect(wasRemoved).toBe(true);
21+
expect(result).not.toContain("<!DOCTYPE");
22+
expect(result).toContain("<html>");
23+
});
24+
});

0 commit comments

Comments
 (0)