Skip to content

Commit c875e85

Browse files
committed
Update for lol-html version 0.3.0
1 parent 9f2286c commit c875e85

File tree

4 files changed

+43
-42
lines changed

4 files changed

+43
-42
lines changed

src/comment.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ impl_mutations!(Comment);
99

1010
#[wasm_bindgen]
1111
impl Comment {
12-
#[wasm_bindgen(method, getter)]
12+
#[wasm_bindgen(method, getter=text)]
1313
pub fn text(&self) -> JsResult<String> {
1414
self.0.get().map(|c| c.text().into())
1515
}
16+
17+
#[wasm_bindgen(method, setter=text)]
18+
pub fn set_text(&mut self, text: &str) -> JsResult<()> {
19+
self.0.get_mut()?.set_text(text).into_js_result()
20+
}
1621
}

src/doctype.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@ impl_from_native!(NativeDoctype --> Doctype);
99
#[wasm_bindgen]
1010
impl Doctype {
1111
#[wasm_bindgen(method, getter)]
12-
pub fn name(&self) -> JsResult<Option<String>> {
13-
self.0.get().map(|d| d.name())
12+
pub fn name(&self) -> JsResult<JsValue> {
13+
self.0
14+
.get()
15+
.map(|d| d.name().map(JsValue::from).unwrap_or(JsValue::null()))
1416
}
1517

1618
#[wasm_bindgen(method, getter=publicId)]
17-
pub fn public_id(&self) -> JsResult<Option<String>> {
18-
self.0.get().map(|d| d.public_id())
19+
pub fn public_id(&self) -> JsResult<JsValue> {
20+
self.0
21+
.get()
22+
.map(|d| d.public_id().map(JsValue::from).unwrap_or(JsValue::null()))
1923
}
2024

2125
#[wasm_bindgen(method, getter=systemId)]
22-
pub fn system_id(&self) -> JsResult<Option<String>> {
23-
self.0.get().map(|d| d.system_id())
26+
pub fn system_id(&self) -> JsResult<JsValue> {
27+
self.0
28+
.get()
29+
.map(|d| d.system_id().map(JsValue::from).unwrap_or(JsValue::null()))
2430
}
2531
}

src/element.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
use super::*;
2-
use lol_html::html_content::{Attribute as NativeAttribute, Element as NativeElement};
3-
use serde::Serialize;
2+
use lol_html::html_content::Element as NativeElement;
43
use serde_wasm_bindgen::to_value as to_js_value;
54

6-
#[derive(Serialize)]
7-
pub struct Attribute {
8-
pub name: String,
9-
pub value: String,
10-
}
11-
12-
impl From<&NativeAttribute<'_>> for Attribute {
13-
fn from(native: &NativeAttribute) -> Self {
14-
Attribute {
15-
name: native.name(),
16-
value: native.value(),
17-
}
18-
}
19-
}
20-
215
#[wasm_bindgen]
226
pub struct Element(NativeRefWrap<NativeElement<'static, 'static>>);
237

@@ -48,15 +32,19 @@ impl Element {
4832
.map(|e| {
4933
e.attributes()
5034
.iter()
51-
.map(Attribute::from)
35+
.map(|a| vec![a.name(), a.value()])
5236
.collect::<Vec<_>>()
5337
})
5438
.and_then(|a| to_js_value(&a).into_js_result())
5539
}
5640

5741
#[wasm_bindgen(method, js_name=getAttribute)]
58-
pub fn get_attribute(&self, name: &str) -> JsResult<Option<String>> {
59-
self.0.get().map(|e| e.get_attribute(name))
42+
pub fn get_attribute(&self, name: &str) -> JsResult<JsValue> {
43+
self.0.get().map(|e| {
44+
e.get_attribute(name)
45+
.map(JsValue::from)
46+
.unwrap_or(JsValue::null())
47+
})
6048
}
6149

6250
#[wasm_bindgen(method, js_name=hasAttribute)]

src/html_rewriter.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use lol_html::{
77
ElementContentHandlers as NativeElementContentHandlers, HtmlRewriter as NativeHTMLRewriter,
88
OutputSink, Selector, Settings,
99
};
10+
use std::borrow::Cow;
1011

1112
struct JsOutputSink(JsFunction);
1213

@@ -27,6 +28,7 @@ impl OutputSink for JsOutputSink {
2728
}
2829
}
2930

31+
//noinspection RsTypeCheck
3032
fn rewriting_error_to_js(err: RewritingError) -> JsValue {
3133
match err {
3234
RewritingError::ContentHandlerError(err) => err.downcast::<HandlerJsErrorWrap>().unwrap().0,
@@ -41,23 +43,22 @@ pub struct HTMLRewriter {
4143
element_content_handlers: Vec<NativeElementContentHandlers<'static>>,
4244
document_content_handlers: Vec<NativeDocumentContentHandlers<'static>>,
4345
output_sink: Option<JsOutputSink>,
44-
encoding: String,
4546
inner: Option<NativeHTMLRewriter<'static, JsOutputSink>>,
47+
inner_constructed: bool,
4648
}
4749

4850
#[wasm_bindgen]
4951
impl HTMLRewriter {
5052
#[wasm_bindgen(constructor)]
51-
pub fn new(encoding: String, output_sink: &JsFunction) -> Self {
53+
pub fn new(output_sink: &JsFunction) -> Self {
5254
HTMLRewriter {
5355
output_sink: Some(JsOutputSink::new(output_sink)),
54-
encoding,
5556
..Self::default()
5657
}
5758
}
5859

5960
fn assert_not_fully_constructed(&self) -> JsResult<()> {
60-
if self.inner.is_some() {
61+
if self.inner_constructed {
6162
Err("Handlers can't be added after write.".into())
6263
} else {
6364
Ok(())
@@ -69,28 +70,23 @@ impl HTMLRewriter {
6970
Some(ref mut inner) => inner,
7071
None => {
7172
let output_sink = self.output_sink.take().unwrap();
72-
// NOTE: selector are passed by reference to the rewriter ctor, though they
73-
// are not stored in the rewriter, so we need their references to be valid
74-
// only during the rewriter invocation.
75-
let selectors: Vec<_> = self.selectors.drain(..).collect();
7673

7774
let settings = Settings {
7875
element_content_handlers: self
79-
.element_content_handlers
76+
.selectors
8077
.drain(..)
81-
.enumerate()
82-
.map(|(i, h)| (&selectors[i], h))
78+
.zip(self.element_content_handlers.drain(..))
79+
.map(|(selector, h)| (Cow::Owned(selector), h))
8380
.collect(),
8481

8582
document_content_handlers: self.document_content_handlers.drain(..).collect(),
86-
encoding: &self.encoding,
8783
..Settings::default()
8884
};
8985

90-
let rewriter =
91-
NativeHTMLRewriter::try_new(settings, output_sink).into_js_result()?;
86+
let rewriter = NativeHTMLRewriter::new(settings, output_sink);
9287

9388
self.inner = Some(rewriter);
89+
self.inner_constructed = true;
9490

9591
self.inner.as_mut().unwrap()
9692
}
@@ -123,6 +119,12 @@ impl HTMLRewriter {
123119
}
124120

125121
pub fn end(&mut self) -> JsResult<()> {
126-
self.inner_mut()?.end().map_err(rewriting_error_to_js)
122+
self.inner_mut()?;
123+
// Rewriter must be constructed by self.inner_mut()
124+
self.inner
125+
.take()
126+
.unwrap()
127+
.end()
128+
.map_err(rewriting_error_to_js)
127129
}
128130
}

0 commit comments

Comments
 (0)