File tree 5 files changed +63
-0
lines changed
5 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ declare namespace HTMLRewriterTypes {
26
26
readonly name : string | null ;
27
27
readonly publicId : string | null ;
28
28
readonly systemId : string | null ;
29
+ readonly removed : boolean ;
30
+ remove ( ) : Doctype ;
29
31
}
30
32
31
33
interface DocumentEnd {
Original file line number Diff line number Diff line change @@ -81,6 +81,13 @@ export default [
81
81
getter : "publicId" ,
82
82
cache : true ,
83
83
} ,
84
+ remove : {
85
+ fn : "remove" ,
86
+ length : 0 ,
87
+ } ,
88
+ removed : {
89
+ getter : "removed" ,
90
+ } ,
84
91
} ,
85
92
} ) ,
86
93
define ( {
Original file line number Diff line number Diff line change @@ -1157,6 +1157,26 @@ pub const DocType = struct {
1157
1157
return JSValue .jsNull ();
1158
1158
return ZigString .init (str ).toJS (globalObject );
1159
1159
}
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
+ }
1160
1180
};
1161
1181
1162
1182
pub const DocEnd = struct {
Original file line number Diff line number Diff line change @@ -796,6 +796,8 @@ pub const DocType = opaque {
796
796
extern fn lol_html_doctype_system_id_get (doctype : * const DocType ) HTMLString ;
797
797
extern fn lol_html_doctype_user_data_set (doctype : * const DocType , user_data : ? * anyopaque ) void ;
798
798
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 ;
799
801
800
802
pub const Callback = * const fn (* DocType , ? * anyopaque ) callconv (.C ) Directive ;
801
803
@@ -811,6 +813,14 @@ pub const DocType = opaque {
811
813
auto_disable ();
812
814
return this .lol_html_doctype_system_id_get ();
813
815
}
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
+ }
814
824
};
815
825
816
826
pub const Encoding = enum {
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments