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
use wasm_bindgen::prelude::*;

use crate::Response;

#[wasm_bindgen]
extern "C" {
    pub type HTMLRewriter;

    #[wasm_bindgen(constructor)]
    pub fn new() -> HTMLRewriter;

    #[wasm_bindgen(method)]
    pub fn on(this: &HTMLRewriter, selector: &str, handler: CfElementHandler) -> HTMLRewriter;

    #[wasm_bindgen(method, js_name=onDocument)]
    pub fn on_document(this: &HTMLRewriter, handler: CfDocumentHandler) -> HTMLRewriter;

    /// It's unclear what the content and return types are!
    #[wasm_bindgen(method)]
    pub fn transform(this: &HTMLRewriter, content: Response) -> Response;

}

pub trait ElementHandler {
    /// Can return a Promise, or undefined
    fn element(&self, element: Element) -> JsValue;

    /// Can return a Promise, or undefined
    fn comments(&self, comment: Comment) -> JsValue;

    /// Can return a Promise, or undefined
    fn text(&self, text: Text) -> JsValue;
}

#[wasm_bindgen]
pub struct CfElementHandler(Box<dyn ElementHandler>);

impl CfElementHandler {
    pub fn new<T>(handler: Box<T>) -> Self
    where
        T: ElementHandler + 'static,
    {
        Self(handler)
    }
}
#[wasm_bindgen]
impl CfElementHandler {
    /// Can return a Promise, or undefined
    #[wasm_bindgen]
    pub fn element(&self, element: Element) -> JsValue {
        self.0.element(element)
    }

    /// Can return a Promise, or undefined
    #[wasm_bindgen]
    pub fn comments(&self, comment: Comment) -> JsValue {
        self.0.comments(comment)
    }

    /// Can return a Promise, or undefined
    #[wasm_bindgen]
    pub fn text(&self, text: Text) -> JsValue {
        self.0.text(text)
    }
}

pub trait DocumentHandler {
    /// Can return a Promise, or undefined
    fn doctype(&self, doctype: Doctype) -> JsValue;

    /// Can return a Promise, or undefined
    fn comments(&self, comment: Comment) -> JsValue;

    /// Can return a Promise, or undefined
    fn text(&self, text: Text) -> JsValue;

    /// Can return a Promise, or undefined
    fn end(&self, end: End) -> JsValue;
}

#[wasm_bindgen]
pub struct CfDocumentHandler(Box<dyn DocumentHandler>);

impl CfDocumentHandler {
    pub fn new<T>(handler: T) -> Self
    where
        T: Into<Box<dyn DocumentHandler>>,
    {
        Self(handler.into())
    }
}

#[wasm_bindgen]
impl CfDocumentHandler {
    /// Can return a Promise, or undefined
    #[wasm_bindgen]
    pub fn doctype(&self, doctype: Doctype) -> JsValue {
        self.0.doctype(doctype)
    }

    /// Can return a Promise, or undefined
    #[wasm_bindgen]
    pub fn comments(&self, comment: Comment) -> JsValue {
        self.0.comments(comment)
    }

    /// Can return a Promise, or undefined
    #[wasm_bindgen]
    pub fn text(&self, text: Text) -> JsValue {
        self.0.text(text)
    }

    /// Can return a Promise, or undefined
    #[wasm_bindgen]
    pub fn end(&self, end: End) -> JsValue {
        self.0.end(end)
    }
}

#[wasm_bindgen]
extern "C" {
    pub type Element;

    #[wasm_bindgen(method, getter, js_name=tagName)]
    pub fn tag_name(this: &Element) -> String;

    #[wasm_bindgen(method, getter)]
    pub fn attributes(this: &Element) -> js_sys::Iterator;

    #[wasm_bindgen(method, getter)]
    pub fn removed(this: &Element) -> bool;

    #[wasm_bindgen(method, getter, js_name=namespaceURI)]
    pub fn namespace_uri(this: &Element) -> String;

    #[wasm_bindgen(method)]
    pub fn getAttribute(this: &Element, name: &str) -> String;
    #[wasm_bindgen(method)]
    pub fn hasAttribute(this: &Element, name: &str) -> bool;
    #[wasm_bindgen(method)]
    pub fn setAttribute(this: &Element, name: &str, value: &str) -> Element;
    #[wasm_bindgen(method)]
    pub fn removeAttribute(this: &Element, name: &str) -> Element;
    #[wasm_bindgen(method)]
    pub fn before(this: &Element, content: &str, escape: Escape) -> Element;
    #[wasm_bindgen(method)]
    pub fn after(this: &Element, content: &str, escape: Escape) -> Element;
    #[wasm_bindgen(method)]
    pub fn prepend(this: &Element, content: &str, escape: Escape) -> Element;
    #[wasm_bindgen(method)]
    pub fn append(this: &Element, content: &str, escape: Escape) -> Element;
    #[wasm_bindgen(method)]
    pub fn replace(this: &Element, content: &str, escape: Escape) -> Element;
    #[wasm_bindgen(method)]
    pub fn setInnerContent(this: &Element, content: &str, escape: Escape) -> Element;
    #[wasm_bindgen(method)]
    pub fn remove(this: &Element, content: &str, escape: Escape) -> Element;
    #[wasm_bindgen(method)]
    pub fn removeAndKeepContent(this: &Element, content: &str, escape: Escape) -> Element;
}

#[wasm_bindgen]
extern "C" {
    pub type Text;

    // Properties
    #[wasm_bindgen(method, getter)]
    pub fn removed(this: &Text) -> bool;
    #[wasm_bindgen(method, getter)]
    pub fn text(this: &Text) -> String;
    #[wasm_bindgen(method, getter, js_name=lastInTextNode)]
    pub fn last_in_text_node(this: &Text) -> bool;

    // Methods
    #[wasm_bindgen(method)]
    pub fn before(this: &Text, content: &str, escape: Escape);
    #[wasm_bindgen(method)]
    pub fn after(this: &Text, content: &str, escape: Escape);
    #[wasm_bindgen(method)]
    pub fn replace(this: &Text, content: &str, escape: Escape);
    #[wasm_bindgen(method)]
    pub fn remove(this: &Text);
}

#[wasm_bindgen]
extern "C" {
    pub type Doctype;

    #[wasm_bindgen(method, getter)]
    pub fn name(this: &Doctype) -> String;

    #[wasm_bindgen(method, getter)]
    pub fn publicId(this: &Doctype) -> String;

    #[wasm_bindgen(method, getter)]
    pub fn systemId(this: &Doctype) -> String;
}
#[wasm_bindgen]
extern "C" {
    pub type Comment;

    #[wasm_bindgen(method, getter)]
    pub fn removed(this: &Comment) -> bool;

    #[wasm_bindgen(method, getter)]
    pub fn text(this: &Comment) -> String;

    #[wasm_bindgen(method)]
    pub fn before(this: &Comment, content: &str, escape: Escape);
    #[wasm_bindgen(method)]
    pub fn after(this: &Comment, content: &str, escape: Escape);
    #[wasm_bindgen(method)]
    pub fn replace(this: &Comment, content: &str, escape: Escape);
    #[wasm_bindgen(method)]
    pub fn remove(this: &Comment);
}

#[wasm_bindgen]
extern "C" {
    pub type End;

    #[wasm_bindgen(method)]
    pub fn append(this: &End, content: &str, escape: Escape);
}

#[wasm_bindgen]
pub struct Escape(pub bool);

#[wasm_bindgen]
impl Escape {
    #[wasm_bindgen(getter, method)]
    pub fn html(&self) -> bool {
        // https://developers.cloudflare.com/workers/runtime-apis/html-rewriter#global-types
        // true = no escaping
        !self.0
    }
}