-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlib.rs
190 lines (163 loc) · 4.93 KB
/
lib.rs
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
use js_sys::TypeError;
use lol_html::html_content::ContentType as NativeContentType;
use std::cell::Cell;
use std::convert::Into;
use std::marker::PhantomData;
use std::mem;
use std::ops::Drop;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
type JsResult<T> = Result<T, JsValue>;
struct Anchor<'r> {
poisoned: Rc<Cell<bool>>,
lifetime: PhantomData<&'r mut ()>,
}
impl<'r> Anchor<'r> {
pub fn new(poisoned: Rc<Cell<bool>>) -> Self {
Anchor {
poisoned,
lifetime: PhantomData,
}
}
}
impl Drop for Anchor<'_> {
fn drop(&mut self) {
self.poisoned.replace(true);
}
}
// NOTE: wasm_bindgen doesn't allow structures with lifetimes. To workaround that
// we create a wrapper that erases all the lifetime information from the inner reference
// and provides an anchor object that keeps track of the lifetime in the runtime.
//
// When anchor goes out of scope, wrapper becomes poisoned and any attempt to get inner
// object results in exception.
#[derive(Clone)]
struct NativeRefWrap<R> {
inner_ptr: *mut R,
poisoned: Rc<Cell<bool>>,
stack_ptr: *mut u8,
}
impl<R> NativeRefWrap<R> {
pub fn wrap<I>(inner: &mut I, stack_ptr: *mut u8) -> (Self, Anchor) {
let wrap = NativeRefWrap {
inner_ptr: unsafe { mem::transmute(inner) },
poisoned: Rc::new(Cell::new(false)),
stack_ptr,
};
let anchor = Anchor::new(Rc::clone(&wrap.poisoned));
(wrap, anchor)
}
fn assert_not_poisoned(&self) -> JsResult<()> {
if self.poisoned.get() {
Err(TypeError::new(
"This content token is no longer valid. Content tokens are only valid during the execution of the relevant content handler.",
).into())
} else {
Ok(())
}
}
pub fn get(&self) -> JsResult<&R> {
self.assert_not_poisoned()?;
Ok(unsafe { self.inner_ptr.as_ref() }.unwrap())
}
pub fn get_mut(&mut self) -> JsResult<&mut R> {
self.assert_not_poisoned()?;
Ok(unsafe { self.inner_ptr.as_mut() }.unwrap())
}
}
trait IntoJsResult<T> {
fn into_js_result(self) -> JsResult<T>;
}
impl<T, E: ToString> IntoJsResult<T> for Result<T, E> {
#[inline]
fn into_js_result(self) -> JsResult<T> {
self.map_err(|e| {
let mut msg = String::from("Parser error: ");
msg.push_str(&e.to_string());
TypeError::new(&msg).into()
})
}
}
trait IntoNative<T> {
fn into_native(self) -> T;
}
#[wasm_bindgen]
extern "C" {
pub type ContentTypeOptions;
#[wasm_bindgen(method, getter)]
fn html(this: &ContentTypeOptions) -> Option<bool>;
}
impl IntoNative<NativeContentType> for Option<ContentTypeOptions> {
fn into_native(self) -> NativeContentType {
match self {
Some(opts) => match opts.html() {
Some(true) => NativeContentType::Html,
Some(false) => NativeContentType::Text,
None => NativeContentType::Text,
},
None => NativeContentType::Text,
}
}
}
macro_rules! impl_mutations {
($Ty:ident) => {
#[wasm_bindgen]
impl $Ty {
pub fn before(
&mut self,
content: &str,
content_type: Option<ContentTypeOptions>,
) -> Result<(), JsValue> {
self.0
.get_mut()
.map(|o| o.before(content, content_type.into_native()))
}
pub fn after(
&mut self,
content: &str,
content_type: Option<ContentTypeOptions>,
) -> Result<(), JsValue> {
self.0
.get_mut()
.map(|o| o.after(content, content_type.into_native()))
}
pub fn replace(
&mut self,
content: &str,
content_type: Option<ContentTypeOptions>,
) -> Result<(), JsValue> {
self.0
.get_mut()
.map(|o| o.replace(content, content_type.into_native()))
}
pub fn remove(&mut self) -> Result<(), JsValue> {
self.0.get_mut().map(|o| o.remove())
}
#[wasm_bindgen(method, getter)]
pub fn removed(&self) -> JsResult<bool> {
self.0.get().map(|o| o.removed())
}
}
};
}
macro_rules! impl_from_native {
($Ty:ident --> $JsTy:ident) => {
impl $JsTy {
pub(crate) fn from_native<'r>(
inner: &'r mut $Ty,
stack_ptr: *mut u8,
) -> (Self, Anchor<'r>) {
let (ref_wrap, anchor) = NativeRefWrap::wrap(inner, stack_ptr);
($JsTy(ref_wrap), anchor)
}
}
};
}
mod comment;
mod doctype;
mod document_end;
mod element;
mod end_tag;
mod handlers;
mod html_rewriter;
mod text_chunk;